1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-topology.c -- ALSA SoC Topology
5 // Copyright (C) 2012 Texas Instruments Inc.
6 // Copyright (C) 2015 Intel Corporation.
8 // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 // K, Mythri P <mythri.p.k@intel.com>
10 // Prusty, Subhransu S <subhransu.s.prusty@intel.com>
11 // B, Jayachandran <jayachandran.b@intel.com>
12 // Abdullah, Omair M <omair.m.abdullah@intel.com>
13 // Jin, Yao <yao.jin@intel.com>
14 // Lin, Mengdong <mengdong.lin@intel.com>
16 // Add support to read audio firmware topology alongside firmware text. The
17 // topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
18 // equalizers, firmware, coefficients etc.
20 // This file only manages the core ALSA and ASoC components, all other bespoke
21 // firmware topology data is passed to component drivers for bespoke handling.
23 #include <linux/kernel.h>
24 #include <linux/export.h>
25 #include <linux/list.h>
26 #include <linux/firmware.h>
27 #include <linux/slab.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/soc-topology.h>
31 #include <sound/tlv.h>
33 #define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */
36 * We make several passes over the data (since it wont necessarily be ordered)
37 * and process objects in the following order. This guarantees the component
38 * drivers will be ready with any vendor data before the mixers and DAPM objects
39 * are loaded (that may make use of the vendor data).
41 #define SOC_TPLG_PASS_MANIFEST 0
42 #define SOC_TPLG_PASS_VENDOR 1
43 #define SOC_TPLG_PASS_MIXER 2
44 #define SOC_TPLG_PASS_WIDGET 3
45 #define SOC_TPLG_PASS_PCM_DAI 4
46 #define SOC_TPLG_PASS_GRAPH 5
47 #define SOC_TPLG_PASS_PINS 6
48 #define SOC_TPLG_PASS_BE_DAI 7
49 #define SOC_TPLG_PASS_LINK 8
51 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
52 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
54 /* topology context */
56 const struct firmware *fw;
58 /* runtime FW parsing */
59 const u8 *pos; /* read postion */
60 const u8 *hdr_pos; /* header position */
61 unsigned int pass; /* pass number */
63 /* component caller */
65 struct snd_soc_component *comp;
66 u32 index; /* current block index */
67 u32 req_index; /* required index, only loaded/free matching blocks */
69 /* vendor specific kcontrol operations */
70 const struct snd_soc_tplg_kcontrol_ops *io_ops;
73 /* vendor specific bytes ext handlers, for TLV bytes controls */
74 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
75 int bytes_ext_ops_count;
77 /* optional fw loading callbacks to component drivers */
78 struct snd_soc_tplg_ops *ops;
81 static int soc_tplg_process_headers(struct soc_tplg *tplg);
82 static void soc_tplg_complete(struct soc_tplg *tplg);
83 struct snd_soc_dapm_widget *
84 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
85 const struct snd_soc_dapm_widget *widget);
86 struct snd_soc_dapm_widget *
87 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
88 const struct snd_soc_dapm_widget *widget);
89 static void soc_tplg_denum_remove_texts(struct soc_enum *se);
90 static void soc_tplg_denum_remove_values(struct soc_enum *se);
92 /* check we dont overflow the data for this control chunk */
93 static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
94 unsigned int count, size_t bytes, const char *elem_type)
96 const u8 *end = tplg->pos + elem_size * count;
98 if (end > tplg->fw->data + tplg->fw->size) {
99 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
104 /* check there is enough room in chunk for control.
105 extra bytes at the end of control are for vendor data here */
106 if (elem_size * count > bytes) {
108 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
109 elem_type, count, elem_size, bytes);
116 static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
118 const u8 *end = tplg->hdr_pos;
120 if (end >= tplg->fw->data + tplg->fw->size)
125 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
127 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
130 static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
132 return (unsigned long)(tplg->pos - tplg->fw->data);
135 /* mapping of Kcontrol types and associated operations. */
136 static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
137 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
138 snd_soc_put_volsw, snd_soc_info_volsw},
139 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
140 snd_soc_put_volsw_sx, NULL},
141 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
142 snd_soc_put_enum_double, snd_soc_info_enum_double},
143 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
144 snd_soc_put_enum_double, NULL},
145 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
146 snd_soc_bytes_put, snd_soc_bytes_info},
147 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
148 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
149 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
150 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
151 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
152 snd_soc_put_strobe, NULL},
153 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
154 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
155 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
156 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
157 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
158 snd_soc_dapm_put_enum_double, NULL},
159 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
160 snd_soc_dapm_put_enum_double, NULL},
161 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
162 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
165 struct soc_tplg_map {
170 /* mapping of widget types from UAPI IDs to kernel IDs */
171 static const struct soc_tplg_map dapm_map[] = {
172 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
173 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
174 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
175 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
176 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
177 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
178 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
179 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
180 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
181 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
182 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
183 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
184 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
185 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
186 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
187 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
188 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
189 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
190 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
191 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
192 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
193 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
194 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
195 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
198 static int tplc_chan_get_reg(struct soc_tplg *tplg,
199 struct snd_soc_tplg_channel *chan, int map)
203 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
204 if (le32_to_cpu(chan[i].id) == map)
205 return le32_to_cpu(chan[i].reg);
211 static int tplc_chan_get_shift(struct soc_tplg *tplg,
212 struct snd_soc_tplg_channel *chan, int map)
216 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
217 if (le32_to_cpu(chan[i].id) == map)
218 return le32_to_cpu(chan[i].shift);
224 static int get_widget_id(int tplg_type)
228 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
229 if (tplg_type == dapm_map[i].uid)
230 return dapm_map[i].kid;
236 static inline void soc_bind_err(struct soc_tplg *tplg,
237 struct snd_soc_tplg_ctl_hdr *hdr, int index)
240 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
241 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
242 soc_tplg_get_offset(tplg));
245 static inline void soc_control_err(struct soc_tplg *tplg,
246 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
249 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
250 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
251 soc_tplg_get_offset(tplg));
254 /* pass vendor data to component driver for processing */
255 static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
256 struct snd_soc_tplg_hdr *hdr)
260 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
261 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
263 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
270 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
271 soc_tplg_get_hdr_offset(tplg),
272 soc_tplg_get_hdr_offset(tplg),
273 hdr->type, hdr->vendor_type);
277 /* pass vendor data to component driver for processing */
278 static int soc_tplg_vendor_load(struct soc_tplg *tplg,
279 struct snd_soc_tplg_hdr *hdr)
281 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
284 return soc_tplg_vendor_load_(tplg, hdr);
287 /* optionally pass new dynamic widget to component driver. This is mainly for
288 * external widgets where we can assign private data/ops */
289 static int soc_tplg_widget_load(struct soc_tplg *tplg,
290 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
292 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
293 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
299 /* optionally pass new dynamic widget to component driver. This is mainly for
300 * external widgets where we can assign private data/ops */
301 static int soc_tplg_widget_ready(struct soc_tplg *tplg,
302 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
304 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
305 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
311 /* pass DAI configurations to component driver for extra initialization */
312 static int soc_tplg_dai_load(struct soc_tplg *tplg,
313 struct snd_soc_dai_driver *dai_drv,
314 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
316 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
317 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
323 /* pass link configurations to component driver for extra initialization */
324 static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
325 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
327 if (tplg->comp && tplg->ops && tplg->ops->link_load)
328 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
333 /* tell the component driver that all firmware has been loaded in this request */
334 static void soc_tplg_complete(struct soc_tplg *tplg)
336 if (tplg->comp && tplg->ops && tplg->ops->complete)
337 tplg->ops->complete(tplg->comp);
340 /* add a dynamic kcontrol */
341 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
342 const struct snd_kcontrol_new *control_new, const char *prefix,
343 void *data, struct snd_kcontrol **kcontrol)
347 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
348 if (*kcontrol == NULL) {
349 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
354 err = snd_ctl_add(card, *kcontrol);
356 dev_err(dev, "ASoC: Failed to add %s: %d\n",
357 control_new->name, err);
364 /* add a dynamic kcontrol for component driver */
365 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
366 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
368 struct snd_soc_component *comp = tplg->comp;
370 return soc_tplg_add_dcontrol(comp->card->snd_card,
371 comp->dev, k, NULL, comp, kcontrol);
374 /* remove a mixer kcontrol */
375 static void remove_mixer(struct snd_soc_component *comp,
376 struct snd_soc_dobj *dobj, int pass)
378 struct snd_card *card = comp->card->snd_card;
379 struct soc_mixer_control *sm =
380 container_of(dobj, struct soc_mixer_control, dobj);
381 const unsigned int *p = NULL;
383 if (pass != SOC_TPLG_PASS_MIXER)
386 if (dobj->ops && dobj->ops->control_unload)
387 dobj->ops->control_unload(comp, dobj);
389 if (dobj->control.kcontrol->tlv.p)
390 p = dobj->control.kcontrol->tlv.p;
391 snd_ctl_remove(card, dobj->control.kcontrol);
392 list_del(&dobj->list);
397 /* remove an enum kcontrol */
398 static void remove_enum(struct snd_soc_component *comp,
399 struct snd_soc_dobj *dobj, int pass)
401 struct snd_card *card = comp->card->snd_card;
402 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
404 if (pass != SOC_TPLG_PASS_MIXER)
407 if (dobj->ops && dobj->ops->control_unload)
408 dobj->ops->control_unload(comp, dobj);
410 snd_ctl_remove(card, dobj->control.kcontrol);
411 list_del(&dobj->list);
413 soc_tplg_denum_remove_values(se);
414 soc_tplg_denum_remove_texts(se);
418 /* remove a byte kcontrol */
419 static void remove_bytes(struct snd_soc_component *comp,
420 struct snd_soc_dobj *dobj, int pass)
422 struct snd_card *card = comp->card->snd_card;
423 struct soc_bytes_ext *sb =
424 container_of(dobj, struct soc_bytes_ext, dobj);
426 if (pass != SOC_TPLG_PASS_MIXER)
429 if (dobj->ops && dobj->ops->control_unload)
430 dobj->ops->control_unload(comp, dobj);
432 snd_ctl_remove(card, dobj->control.kcontrol);
433 list_del(&dobj->list);
438 static void remove_route(struct snd_soc_component *comp,
439 struct snd_soc_dobj *dobj, int pass)
441 struct snd_soc_dapm_route *route =
442 container_of(dobj, struct snd_soc_dapm_route, dobj);
444 if (pass != SOC_TPLG_PASS_GRAPH)
447 if (dobj->ops && dobj->ops->dapm_route_unload)
448 dobj->ops->dapm_route_unload(comp, dobj);
450 list_del(&dobj->list);
454 /* remove a widget and it's kcontrols - routes must be removed first */
455 static void remove_widget(struct snd_soc_component *comp,
456 struct snd_soc_dobj *dobj, int pass)
458 struct snd_card *card = comp->card->snd_card;
459 struct snd_soc_dapm_widget *w =
460 container_of(dobj, struct snd_soc_dapm_widget, dobj);
463 if (pass != SOC_TPLG_PASS_WIDGET)
466 if (dobj->ops && dobj->ops->widget_unload)
467 dobj->ops->widget_unload(comp, dobj);
473 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
474 * The enum may either have an array of values or strings.
476 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
477 /* enumerated widget mixer */
478 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
479 struct snd_kcontrol *kcontrol = w->kcontrols[i];
480 struct soc_enum *se =
481 (struct soc_enum *)kcontrol->private_value;
483 snd_ctl_remove(card, kcontrol);
485 /* free enum kcontrol's dvalues and dtexts */
486 soc_tplg_denum_remove_values(se);
487 soc_tplg_denum_remove_texts(se);
490 kfree(w->kcontrol_news[i].name);
493 /* volume mixer or bytes controls */
494 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
495 struct snd_kcontrol *kcontrol = w->kcontrols[i];
497 if (dobj->widget.kcontrol_type
498 == SND_SOC_TPLG_TYPE_MIXER)
499 kfree(kcontrol->tlv.p);
501 /* Private value is used as struct soc_mixer_control
502 * for volume mixers or soc_bytes_ext for bytes
505 kfree((void *)kcontrol->private_value);
506 snd_ctl_remove(card, kcontrol);
507 kfree(w->kcontrol_news[i].name);
512 kfree(w->kcontrol_news);
514 list_del(&dobj->list);
516 /* widget w is freed by soc-dapm.c */
519 /* remove DAI configurations */
520 static void remove_dai(struct snd_soc_component *comp,
521 struct snd_soc_dobj *dobj, int pass)
523 struct snd_soc_dai_driver *dai_drv =
524 container_of(dobj, struct snd_soc_dai_driver, dobj);
525 struct snd_soc_dai *dai;
527 if (pass != SOC_TPLG_PASS_PCM_DAI)
530 if (dobj->ops && dobj->ops->dai_unload)
531 dobj->ops->dai_unload(comp, dobj);
533 list_for_each_entry(dai, &comp->dai_list, list)
534 if (dai->driver == dai_drv)
537 kfree(dai_drv->playback.stream_name);
538 kfree(dai_drv->capture.stream_name);
539 kfree(dai_drv->name);
540 list_del(&dobj->list);
544 /* remove link configurations */
545 static void remove_link(struct snd_soc_component *comp,
546 struct snd_soc_dobj *dobj, int pass)
548 struct snd_soc_dai_link *link =
549 container_of(dobj, struct snd_soc_dai_link, dobj);
551 if (pass != SOC_TPLG_PASS_PCM_DAI)
554 if (dobj->ops && dobj->ops->link_unload)
555 dobj->ops->link_unload(comp, dobj);
558 kfree(link->stream_name);
559 kfree(link->cpus->dai_name);
561 list_del(&dobj->list);
562 snd_soc_remove_dai_link(comp->card, link);
566 /* unload dai link */
567 static void remove_backend_link(struct snd_soc_component *comp,
568 struct snd_soc_dobj *dobj, int pass)
570 if (pass != SOC_TPLG_PASS_LINK)
573 if (dobj->ops && dobj->ops->link_unload)
574 dobj->ops->link_unload(comp, dobj);
577 * We don't free the link here as what remove_link() do since BE
578 * links are not allocated by topology.
579 * We however need to reset the dobj type to its initial values
581 dobj->type = SND_SOC_DOBJ_NONE;
582 list_del(&dobj->list);
585 /* bind a kcontrol to it's IO handlers */
586 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
587 struct snd_kcontrol_new *k,
588 const struct soc_tplg *tplg)
590 const struct snd_soc_tplg_kcontrol_ops *ops;
591 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
594 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
595 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
596 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
597 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
598 struct soc_bytes_ext *sbe;
599 struct snd_soc_tplg_bytes_control *be;
601 sbe = (struct soc_bytes_ext *)k->private_value;
602 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
604 /* TLV bytes controls need standard kcontrol info handler,
605 * TLV callback and extended put/get handlers.
607 k->info = snd_soc_bytes_info_ext;
608 k->tlv.c = snd_soc_bytes_tlv_callback;
610 ext_ops = tplg->bytes_ext_ops;
611 num_ops = tplg->bytes_ext_ops_count;
612 for (i = 0; i < num_ops; i++) {
613 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
614 sbe->put = ext_ops[i].put;
615 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
616 sbe->get = ext_ops[i].get;
619 if (sbe->put && sbe->get)
625 /* try and map vendor specific kcontrol handlers first */
627 num_ops = tplg->io_ops_count;
628 for (i = 0; i < num_ops; i++) {
630 if (k->put == NULL && ops[i].id == hdr->ops.put)
632 if (k->get == NULL && ops[i].id == hdr->ops.get)
634 if (k->info == NULL && ops[i].id == hdr->ops.info)
635 k->info = ops[i].info;
638 /* vendor specific handlers found ? */
639 if (k->put && k->get && k->info)
642 /* none found so try standard kcontrol handlers */
644 num_ops = ARRAY_SIZE(io_ops);
645 for (i = 0; i < num_ops; i++) {
647 if (k->put == NULL && ops[i].id == hdr->ops.put)
649 if (k->get == NULL && ops[i].id == hdr->ops.get)
651 if (k->info == NULL && ops[i].id == hdr->ops.info)
652 k->info = ops[i].info;
655 /* standard handlers found ? */
656 if (k->put && k->get && k->info)
659 /* nothing to bind */
663 /* bind a widgets to it's evnt handlers */
664 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
665 const struct snd_soc_tplg_widget_events *events,
666 int num_events, u16 event_type)
672 for (i = 0; i < num_events; i++) {
673 if (event_type == events[i].type) {
675 /* found - so assign event */
676 w->event = events[i].event_handler;
684 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
686 /* optionally pass new dynamic kcontrol to component driver. */
687 static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
688 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
690 if (tplg->comp && tplg->ops && tplg->ops->control_load)
691 return tplg->ops->control_load(tplg->comp, tplg->index, k,
698 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
699 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
701 unsigned int item_len = 2 * sizeof(unsigned int);
704 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
708 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
710 p[2] = le32_to_cpu(scale->min);
711 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
712 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
714 kc->tlv.p = (void *)p;
718 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
719 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
721 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
722 u32 access = le32_to_cpu(tc->access);
724 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
727 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
729 switch (le32_to_cpu(tplg_tlv->type)) {
730 case SNDRV_CTL_TLVT_DB_SCALE:
731 return soc_tplg_create_tlv_db_scale(tplg, kc,
734 /* TODO: add support for other TLV types */
736 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
745 static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
746 struct snd_kcontrol_new *kc)
751 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
754 struct snd_soc_tplg_bytes_control *be;
755 struct soc_bytes_ext *sbe;
756 struct snd_kcontrol_new kc;
759 if (soc_tplg_check_elem_count(tplg,
760 sizeof(struct snd_soc_tplg_bytes_control), count,
761 size, "mixer bytes")) {
762 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
767 for (i = 0; i < count; i++) {
768 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
770 /* validate kcontrol */
771 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
772 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
775 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
779 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
780 le32_to_cpu(be->priv.size));
783 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
784 be->hdr.name, be->hdr.access);
786 memset(&kc, 0, sizeof(kc));
787 kc.name = be->hdr.name;
788 kc.private_value = (long)sbe;
789 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
790 kc.access = le32_to_cpu(be->hdr.access);
792 sbe->max = le32_to_cpu(be->max);
793 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
794 sbe->dobj.ops = tplg->ops;
795 INIT_LIST_HEAD(&sbe->dobj.list);
797 /* map io handlers */
798 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
800 soc_control_err(tplg, &be->hdr, be->hdr.name);
805 /* pass control to driver for optional further init */
806 err = soc_tplg_init_kcontrol(tplg, &kc,
807 (struct snd_soc_tplg_ctl_hdr *)be);
809 dev_err(tplg->dev, "ASoC: failed to init %s\n",
815 /* register control here */
816 err = soc_tplg_add_kcontrol(tplg, &kc,
817 &sbe->dobj.control.kcontrol);
819 dev_err(tplg->dev, "ASoC: failed to add %s\n",
825 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
831 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
834 struct snd_soc_tplg_mixer_control *mc;
835 struct soc_mixer_control *sm;
836 struct snd_kcontrol_new kc;
839 if (soc_tplg_check_elem_count(tplg,
840 sizeof(struct snd_soc_tplg_mixer_control),
841 count, size, "mixers")) {
843 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
848 for (i = 0; i < count; i++) {
849 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
851 /* validate kcontrol */
852 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
853 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
856 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
859 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
860 le32_to_cpu(mc->priv.size));
863 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
864 mc->hdr.name, mc->hdr.access);
866 memset(&kc, 0, sizeof(kc));
867 kc.name = mc->hdr.name;
868 kc.private_value = (long)sm;
869 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
870 kc.access = le32_to_cpu(mc->hdr.access);
872 /* we only support FL/FR channel mapping atm */
873 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
875 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
877 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
879 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
882 sm->max = le32_to_cpu(mc->max);
883 sm->min = le32_to_cpu(mc->min);
884 sm->invert = le32_to_cpu(mc->invert);
885 sm->platform_max = le32_to_cpu(mc->platform_max);
886 sm->dobj.index = tplg->index;
887 sm->dobj.ops = tplg->ops;
888 sm->dobj.type = SND_SOC_DOBJ_MIXER;
889 INIT_LIST_HEAD(&sm->dobj.list);
891 /* map io handlers */
892 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
894 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
899 /* create any TLV data */
900 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
902 /* pass control to driver for optional further init */
903 err = soc_tplg_init_kcontrol(tplg, &kc,
904 (struct snd_soc_tplg_ctl_hdr *) mc);
906 dev_err(tplg->dev, "ASoC: failed to init %s\n",
908 soc_tplg_free_tlv(tplg, &kc);
913 /* register control here */
914 err = soc_tplg_add_kcontrol(tplg, &kc,
915 &sm->dobj.control.kcontrol);
917 dev_err(tplg->dev, "ASoC: failed to add %s\n",
919 soc_tplg_free_tlv(tplg, &kc);
924 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
930 static int soc_tplg_denum_create_texts(struct soc_enum *se,
931 struct snd_soc_tplg_enum_control *ec)
935 se->dobj.control.dtexts =
936 kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
937 if (se->dobj.control.dtexts == NULL)
940 for (i = 0; i < ec->items; i++) {
942 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
943 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
948 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
949 if (!se->dobj.control.dtexts[i]) {
955 se->items = le32_to_cpu(ec->items);
956 se->texts = (const char * const *)se->dobj.control.dtexts;
961 soc_tplg_denum_remove_texts(se);
965 static inline void soc_tplg_denum_remove_texts(struct soc_enum *se)
969 for (--i; i >= 0; i--)
970 kfree(se->dobj.control.dtexts[i]);
971 kfree(se->dobj.control.dtexts);
974 static int soc_tplg_denum_create_values(struct soc_enum *se,
975 struct snd_soc_tplg_enum_control *ec)
979 if (le32_to_cpu(ec->items) > sizeof(*ec->values))
982 se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
985 if (!se->dobj.control.dvalues)
988 /* convert from little-endian */
989 for (i = 0; i < le32_to_cpu(ec->items); i++) {
990 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
996 static inline void soc_tplg_denum_remove_values(struct soc_enum *se)
998 kfree(se->dobj.control.dvalues);
1001 static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
1004 struct snd_soc_tplg_enum_control *ec;
1005 struct soc_enum *se;
1006 struct snd_kcontrol_new kc;
1009 if (soc_tplg_check_elem_count(tplg,
1010 sizeof(struct snd_soc_tplg_enum_control),
1011 count, size, "enums")) {
1013 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1018 for (i = 0; i < count; i++) {
1019 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1021 /* validate kcontrol */
1022 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1023 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1026 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1030 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1031 le32_to_cpu(ec->priv.size));
1033 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1034 ec->hdr.name, ec->items);
1036 memset(&kc, 0, sizeof(kc));
1037 kc.name = ec->hdr.name;
1038 kc.private_value = (long)se;
1039 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1040 kc.access = le32_to_cpu(ec->hdr.access);
1042 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1043 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1045 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1048 se->mask = le32_to_cpu(ec->mask);
1049 se->dobj.index = tplg->index;
1050 se->dobj.type = SND_SOC_DOBJ_ENUM;
1051 se->dobj.ops = tplg->ops;
1052 INIT_LIST_HEAD(&se->dobj.list);
1054 switch (le32_to_cpu(ec->hdr.ops.info)) {
1055 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1056 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1057 err = soc_tplg_denum_create_values(se, ec);
1060 "ASoC: could not create values for %s\n",
1066 case SND_SOC_TPLG_CTL_ENUM:
1067 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1068 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1069 err = soc_tplg_denum_create_texts(se, ec);
1072 "ASoC: could not create texts for %s\n",
1080 "ASoC: invalid enum control type %d for %s\n",
1081 ec->hdr.ops.info, ec->hdr.name);
1086 /* map io handlers */
1087 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
1089 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1094 /* pass control to driver for optional further init */
1095 err = soc_tplg_init_kcontrol(tplg, &kc,
1096 (struct snd_soc_tplg_ctl_hdr *) ec);
1098 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1104 /* register control here */
1105 ret = soc_tplg_add_kcontrol(tplg,
1106 &kc, &se->dobj.control.kcontrol);
1108 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1114 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1120 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1121 struct snd_soc_tplg_hdr *hdr)
1123 struct snd_soc_tplg_ctl_hdr *control_hdr;
1126 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1127 tplg->pos += le32_to_cpu(hdr->size) +
1128 le32_to_cpu(hdr->payload_size);
1132 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1133 soc_tplg_get_offset(tplg));
1135 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
1137 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1139 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
1140 dev_err(tplg->dev, "ASoC: invalid control size\n");
1144 switch (le32_to_cpu(control_hdr->ops.info)) {
1145 case SND_SOC_TPLG_CTL_VOLSW:
1146 case SND_SOC_TPLG_CTL_STROBE:
1147 case SND_SOC_TPLG_CTL_VOLSW_SX:
1148 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1149 case SND_SOC_TPLG_CTL_RANGE:
1150 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1151 case SND_SOC_TPLG_DAPM_CTL_PIN:
1152 soc_tplg_dmixer_create(tplg, 1,
1153 le32_to_cpu(hdr->payload_size));
1155 case SND_SOC_TPLG_CTL_ENUM:
1156 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1157 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1158 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1159 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1160 soc_tplg_denum_create(tplg, 1,
1161 le32_to_cpu(hdr->payload_size));
1163 case SND_SOC_TPLG_CTL_BYTES:
1164 soc_tplg_dbytes_create(tplg, 1,
1165 le32_to_cpu(hdr->payload_size));
1168 soc_bind_err(tplg, control_hdr, i);
1176 /* optionally pass new dynamic kcontrol to component driver. */
1177 static int soc_tplg_add_route(struct soc_tplg *tplg,
1178 struct snd_soc_dapm_route *route)
1180 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1181 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1187 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1188 struct snd_soc_tplg_hdr *hdr)
1190 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1191 struct snd_soc_tplg_dapm_graph_elem *elem;
1192 struct snd_soc_dapm_route **routes;
1196 count = le32_to_cpu(hdr->count);
1198 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1200 le32_to_cpu(hdr->size) +
1201 le32_to_cpu(hdr->payload_size);
1206 if (soc_tplg_check_elem_count(tplg,
1207 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1208 count, le32_to_cpu(hdr->payload_size), "graph")) {
1210 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1215 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1218 /* allocate memory for pointer to array of dapm routes */
1219 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1225 * allocate memory for each dapm route in the array.
1226 * This needs to be done individually so that
1227 * each route can be freed when it is removed in remove_route().
1229 for (i = 0; i < count; i++) {
1230 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1232 /* free previously allocated memory */
1233 for (j = 0; j < i; j++)
1241 for (i = 0; i < count; i++) {
1242 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1243 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1245 /* validate routes */
1246 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1247 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1251 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1252 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1256 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1257 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1262 routes[i]->source = elem->source;
1263 routes[i]->sink = elem->sink;
1265 /* set to NULL atm for tplg users */
1266 routes[i]->connected = NULL;
1267 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1268 routes[i]->control = NULL;
1270 routes[i]->control = elem->control;
1272 /* add route dobj to dobj_list */
1273 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1274 routes[i]->dobj.ops = tplg->ops;
1275 routes[i]->dobj.index = tplg->index;
1276 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1278 soc_tplg_add_route(tplg, routes[i]);
1280 /* add route, but keep going if some fail */
1281 snd_soc_dapm_add_routes(dapm, routes[i], 1);
1284 /* free memory allocated for all dapm routes in case of error */
1286 for (i = 0; i < count ; i++)
1290 * free pointer to array of dapm routes as this is no longer needed.
1291 * The memory allocated for each dapm route will be freed
1292 * when it is removed in remove_route().
1299 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1300 struct soc_tplg *tplg, int num_kcontrols)
1302 struct snd_kcontrol_new *kc;
1303 struct soc_mixer_control *sm;
1304 struct snd_soc_tplg_mixer_control *mc;
1307 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1311 for (i = 0; i < num_kcontrols; i++) {
1312 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1314 /* validate kcontrol */
1315 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1316 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1319 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1323 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1324 le32_to_cpu(mc->priv.size));
1326 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1329 kc[i].private_value = (long)sm;
1330 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1331 if (kc[i].name == NULL)
1333 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1334 kc[i].access = mc->hdr.access;
1336 /* we only support FL/FR channel mapping atm */
1337 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1339 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1341 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1343 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1348 sm->invert = mc->invert;
1349 sm->platform_max = mc->platform_max;
1350 sm->dobj.index = tplg->index;
1351 INIT_LIST_HEAD(&sm->dobj.list);
1353 /* map io handlers */
1354 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1356 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1360 /* create any TLV data */
1361 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1363 /* pass control to driver for optional further init */
1364 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1365 (struct snd_soc_tplg_ctl_hdr *)mc);
1367 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1369 soc_tplg_free_tlv(tplg, &kc[i]);
1376 for (; i >= 0; i--) {
1377 sm = (struct soc_mixer_control *)kc[i].private_value;
1386 static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1387 struct soc_tplg *tplg, int num_kcontrols)
1389 struct snd_kcontrol_new *kc;
1390 struct snd_soc_tplg_enum_control *ec;
1391 struct soc_enum *se;
1394 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1398 for (i = 0; i < num_kcontrols; i++) {
1399 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1400 /* validate kcontrol */
1401 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1402 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1405 se = kzalloc(sizeof(*se), GFP_KERNEL);
1409 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1412 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1415 kc[i].private_value = (long)se;
1416 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
1417 if (kc[i].name == NULL)
1419 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1420 kc[i].access = ec->hdr.access;
1422 /* we only support FL/FR channel mapping atm */
1423 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1424 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1426 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1429 se->items = ec->items;
1430 se->mask = ec->mask;
1431 se->dobj.index = tplg->index;
1433 switch (le32_to_cpu(ec->hdr.ops.info)) {
1434 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1435 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1436 err = soc_tplg_denum_create_values(se, ec);
1438 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1443 case SND_SOC_TPLG_CTL_ENUM:
1444 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1445 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1446 err = soc_tplg_denum_create_texts(se, ec);
1448 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1454 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1455 ec->hdr.ops.info, ec->hdr.name);
1459 /* map io handlers */
1460 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1462 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1466 /* pass control to driver for optional further init */
1467 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1468 (struct snd_soc_tplg_ctl_hdr *)ec);
1470 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1479 for (; i >= 0; i--) {
1480 /* free values and texts */
1481 se = (struct soc_enum *)kc[i].private_value;
1484 soc_tplg_denum_remove_values(se);
1485 soc_tplg_denum_remove_texts(se);
1496 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1497 struct soc_tplg *tplg, int num_kcontrols)
1499 struct snd_soc_tplg_bytes_control *be;
1500 struct soc_bytes_ext *sbe;
1501 struct snd_kcontrol_new *kc;
1504 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1508 for (i = 0; i < num_kcontrols; i++) {
1509 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1511 /* validate kcontrol */
1512 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1513 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1516 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1520 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1521 le32_to_cpu(be->priv.size));
1524 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1525 be->hdr.name, be->hdr.access);
1527 kc[i].private_value = (long)sbe;
1528 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
1529 if (kc[i].name == NULL)
1531 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1532 kc[i].access = be->hdr.access;
1535 INIT_LIST_HEAD(&sbe->dobj.list);
1537 /* map standard io handlers and check for external handlers */
1538 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1540 soc_control_err(tplg, &be->hdr, be->hdr.name);
1544 /* pass control to driver for optional further init */
1545 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1546 (struct snd_soc_tplg_ctl_hdr *)be);
1548 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1557 for (; i >= 0; i--) {
1558 sbe = (struct soc_bytes_ext *)kc[i].private_value;
1567 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1568 struct snd_soc_tplg_dapm_widget *w)
1570 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1571 struct snd_soc_dapm_widget template, *widget;
1572 struct snd_soc_tplg_ctl_hdr *control_hdr;
1573 struct snd_soc_card *card = tplg->comp->card;
1574 unsigned int kcontrol_type;
1577 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1578 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1580 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1581 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1584 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1587 memset(&template, 0, sizeof(template));
1589 /* map user to kernel widget ID */
1590 template.id = get_widget_id(le32_to_cpu(w->id));
1591 if (template.id < 0)
1594 /* strings are allocated here, but used and freed by the widget */
1595 template.name = kstrdup(w->name, GFP_KERNEL);
1598 template.sname = kstrdup(w->sname, GFP_KERNEL);
1599 if (!template.sname) {
1603 template.reg = le32_to_cpu(w->reg);
1604 template.shift = le32_to_cpu(w->shift);
1605 template.mask = le32_to_cpu(w->mask);
1606 template.subseq = le32_to_cpu(w->subseq);
1607 template.on_val = w->invert ? 0 : 1;
1608 template.off_val = w->invert ? 1 : 0;
1609 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1610 template.event_flags = le16_to_cpu(w->event_flags);
1611 template.dobj.index = tplg->index;
1614 (sizeof(struct snd_soc_tplg_dapm_widget) +
1615 le32_to_cpu(w->priv.size));
1617 if (w->num_kcontrols == 0) {
1619 template.num_kcontrols = 0;
1623 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1624 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1625 w->name, w->num_kcontrols, control_hdr->type);
1627 switch (le32_to_cpu(control_hdr->ops.info)) {
1628 case SND_SOC_TPLG_CTL_VOLSW:
1629 case SND_SOC_TPLG_CTL_STROBE:
1630 case SND_SOC_TPLG_CTL_VOLSW_SX:
1631 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1632 case SND_SOC_TPLG_CTL_RANGE:
1633 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1634 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
1635 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1636 template.kcontrol_news =
1637 soc_tplg_dapm_widget_dmixer_create(tplg,
1638 template.num_kcontrols);
1639 if (!template.kcontrol_news) {
1644 case SND_SOC_TPLG_CTL_ENUM:
1645 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1646 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1647 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1648 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1649 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
1650 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1651 template.kcontrol_news =
1652 soc_tplg_dapm_widget_denum_create(tplg,
1653 template.num_kcontrols);
1654 if (!template.kcontrol_news) {
1659 case SND_SOC_TPLG_CTL_BYTES:
1660 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
1661 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1662 template.kcontrol_news =
1663 soc_tplg_dapm_widget_dbytes_create(tplg,
1664 template.num_kcontrols);
1665 if (!template.kcontrol_news) {
1671 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1672 control_hdr->ops.get, control_hdr->ops.put,
1673 le32_to_cpu(control_hdr->ops.info));
1679 ret = soc_tplg_widget_load(tplg, &template, w);
1683 /* card dapm mutex is held by the core if we are loading topology
1684 * data during sound card init. */
1685 if (card->instantiated)
1686 widget = snd_soc_dapm_new_control(dapm, &template);
1688 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1689 if (IS_ERR(widget)) {
1690 ret = PTR_ERR(widget);
1694 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1695 widget->dobj.widget.kcontrol_type = kcontrol_type;
1696 widget->dobj.ops = tplg->ops;
1697 widget->dobj.index = tplg->index;
1698 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1700 ret = soc_tplg_widget_ready(tplg, widget, w);
1704 kfree(template.sname);
1705 kfree(template.name);
1710 snd_soc_tplg_widget_remove(widget);
1711 snd_soc_dapm_free_widget(widget);
1713 kfree(template.sname);
1715 kfree(template.name);
1719 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1720 struct snd_soc_tplg_hdr *hdr)
1722 struct snd_soc_tplg_dapm_widget *widget;
1725 count = le32_to_cpu(hdr->count);
1727 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1730 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1732 for (i = 0; i < count; i++) {
1733 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1734 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
1735 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1739 ret = soc_tplg_dapm_widget_create(tplg, widget);
1741 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1750 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1752 struct snd_soc_card *card = tplg->comp->card;
1755 /* Card might not have been registered at this point.
1756 * If so, just return success.
1758 if (!card || !card->instantiated) {
1759 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1760 " widget card binding deferred\n");
1764 ret = snd_soc_dapm_new_widgets(card);
1766 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1772 static void set_stream_info(struct snd_soc_pcm_stream *stream,
1773 struct snd_soc_tplg_stream_caps *caps)
1775 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1776 stream->channels_min = le32_to_cpu(caps->channels_min);
1777 stream->channels_max = le32_to_cpu(caps->channels_max);
1778 stream->rates = le32_to_cpu(caps->rates);
1779 stream->rate_min = le32_to_cpu(caps->rate_min);
1780 stream->rate_max = le32_to_cpu(caps->rate_max);
1781 stream->formats = le64_to_cpu(caps->formats);
1782 stream->sig_bits = le32_to_cpu(caps->sig_bits);
1785 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1786 unsigned int flag_mask, unsigned int flags)
1788 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1789 dai_drv->symmetric_rates =
1790 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1792 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1793 dai_drv->symmetric_channels =
1794 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1797 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1798 dai_drv->symmetric_samplebits =
1799 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1803 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1804 struct snd_soc_tplg_pcm *pcm)
1806 struct snd_soc_dai_driver *dai_drv;
1807 struct snd_soc_pcm_stream *stream;
1808 struct snd_soc_tplg_stream_caps *caps;
1811 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1812 if (dai_drv == NULL)
1815 if (strlen(pcm->dai_name))
1816 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
1817 dai_drv->id = le32_to_cpu(pcm->dai_id);
1819 if (pcm->playback) {
1820 stream = &dai_drv->playback;
1821 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1822 set_stream_info(stream, caps);
1826 stream = &dai_drv->capture;
1827 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1828 set_stream_info(stream, caps);
1832 dai_drv->compress_new = snd_soc_new_compress;
1834 /* pass control to component driver for optional further init */
1835 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1837 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1838 kfree(dai_drv->playback.stream_name);
1839 kfree(dai_drv->capture.stream_name);
1840 kfree(dai_drv->name);
1845 dai_drv->dobj.index = tplg->index;
1846 dai_drv->dobj.ops = tplg->ops;
1847 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1848 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1850 /* register the DAI to the component */
1851 return snd_soc_register_dai(tplg->comp, dai_drv);
1854 static void set_link_flags(struct snd_soc_dai_link *link,
1855 unsigned int flag_mask, unsigned int flags)
1857 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1858 link->symmetric_rates =
1859 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1861 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1862 link->symmetric_channels =
1863 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1866 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1867 link->symmetric_samplebits =
1868 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1871 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1872 link->ignore_suspend =
1873 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1877 /* create the FE DAI link */
1878 static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1879 struct snd_soc_tplg_pcm *pcm)
1881 struct snd_soc_dai_link *link;
1882 struct snd_soc_dai_link_component *dlc;
1885 /* link + cpu + codec + platform */
1886 link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1890 dlc = (struct snd_soc_dai_link_component *)(link + 1);
1892 link->cpus = &dlc[0];
1893 link->codecs = &dlc[1];
1894 link->platforms = &dlc[2];
1897 link->num_codecs = 1;
1898 link->num_platforms = 1;
1900 if (strlen(pcm->pcm_name)) {
1901 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1902 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1904 link->id = le32_to_cpu(pcm->pcm_id);
1906 if (strlen(pcm->dai_name))
1907 link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1909 link->codecs->name = "snd-soc-dummy";
1910 link->codecs->dai_name = "snd-soc-dummy-dai";
1912 link->platforms->name = "snd-soc-dummy";
1916 link->dpcm_playback = le32_to_cpu(pcm->playback);
1917 link->dpcm_capture = le32_to_cpu(pcm->capture);
1919 set_link_flags(link,
1920 le32_to_cpu(pcm->flag_mask),
1921 le32_to_cpu(pcm->flags));
1923 /* pass control to component driver for optional further init */
1924 ret = soc_tplg_dai_link_load(tplg, link, NULL);
1926 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1928 kfree(link->stream_name);
1929 kfree(link->cpus->dai_name);
1934 link->dobj.index = tplg->index;
1935 link->dobj.ops = tplg->ops;
1936 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1937 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1939 snd_soc_add_dai_link(tplg->comp->card, link);
1943 /* create a FE DAI and DAI link from the PCM object */
1944 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1945 struct snd_soc_tplg_pcm *pcm)
1949 ret = soc_tplg_dai_create(tplg, pcm);
1953 return soc_tplg_fe_link_create(tplg, pcm);
1956 /* copy stream caps from the old version 4 of source */
1957 static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1958 struct snd_soc_tplg_stream_caps_v4 *src)
1960 dest->size = cpu_to_le32(sizeof(*dest));
1961 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1962 dest->formats = src->formats;
1963 dest->rates = src->rates;
1964 dest->rate_min = src->rate_min;
1965 dest->rate_max = src->rate_max;
1966 dest->channels_min = src->channels_min;
1967 dest->channels_max = src->channels_max;
1968 dest->periods_min = src->periods_min;
1969 dest->periods_max = src->periods_max;
1970 dest->period_size_min = src->period_size_min;
1971 dest->period_size_max = src->period_size_max;
1972 dest->buffer_size_min = src->buffer_size_min;
1973 dest->buffer_size_max = src->buffer_size_max;
1977 * pcm_new_ver - Create the new version of PCM from the old version.
1978 * @tplg: topology context
1979 * @src: older version of pcm as a source
1980 * @pcm: latest version of pcm created from the source
1982 * Support from vesion 4. User should free the returned pcm manually.
1984 static int pcm_new_ver(struct soc_tplg *tplg,
1985 struct snd_soc_tplg_pcm *src,
1986 struct snd_soc_tplg_pcm **pcm)
1988 struct snd_soc_tplg_pcm *dest;
1989 struct snd_soc_tplg_pcm_v4 *src_v4;
1994 if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
1995 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1999 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
2000 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
2001 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2005 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2006 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2007 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2008 dest->pcm_id = src_v4->pcm_id;
2009 dest->dai_id = src_v4->dai_id;
2010 dest->playback = src_v4->playback;
2011 dest->capture = src_v4->capture;
2012 dest->compress = src_v4->compress;
2013 dest->num_streams = src_v4->num_streams;
2014 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2015 memcpy(&dest->stream[i], &src_v4->stream[i],
2016 sizeof(struct snd_soc_tplg_stream));
2018 for (i = 0; i < 2; i++)
2019 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
2025 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
2026 struct snd_soc_tplg_hdr *hdr)
2028 struct snd_soc_tplg_pcm *pcm, *_pcm;
2034 count = le32_to_cpu(hdr->count);
2036 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
2039 /* check the element size and count */
2040 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
2041 size = le32_to_cpu(pcm->size);
2042 if (size > sizeof(struct snd_soc_tplg_pcm)
2043 || size < sizeof(struct snd_soc_tplg_pcm_v4)) {
2044 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
2049 if (soc_tplg_check_elem_count(tplg,
2051 le32_to_cpu(hdr->payload_size),
2053 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2058 for (i = 0; i < count; i++) {
2059 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
2060 size = le32_to_cpu(pcm->size);
2062 /* check ABI version by size, create a new version of pcm
2065 if (size == sizeof(*pcm)) {
2070 pcm_new_ver(tplg, pcm, &_pcm);
2073 /* create the FE DAIs and DAI links */
2074 soc_tplg_pcm_create(tplg, _pcm);
2076 /* offset by version-specific struct size and
2077 * real priv data size
2079 tplg->pos += size + le32_to_cpu(_pcm->priv.size);
2082 kfree(_pcm); /* free the duplicated one */
2085 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
2091 * set_link_hw_format - Set the HW audio format of the physical DAI link.
2092 * @link: &snd_soc_dai_link which should be updated
2093 * @cfg: physical link configs.
2095 * Topology context contains a list of supported HW formats (configs) and
2096 * a default format ID for the physical link. This function will use this
2097 * default ID to choose the HW format to set the link's DAI format for init.
2099 static void set_link_hw_format(struct snd_soc_dai_link *link,
2100 struct snd_soc_tplg_link_config *cfg)
2102 struct snd_soc_tplg_hw_config *hw_config;
2103 unsigned char bclk_master, fsync_master;
2104 unsigned char invert_bclk, invert_fsync;
2107 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
2108 hw_config = &cfg->hw_config[i];
2109 if (hw_config->id != cfg->default_hw_config_id)
2112 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
2113 SND_SOC_DAIFMT_FORMAT_MASK;
2116 switch (hw_config->clock_gated) {
2117 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2118 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2121 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2122 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2126 /* ignore the value */
2130 /* clock signal polarity */
2131 invert_bclk = hw_config->invert_bclk;
2132 invert_fsync = hw_config->invert_fsync;
2133 if (!invert_bclk && !invert_fsync)
2134 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2135 else if (!invert_bclk && invert_fsync)
2136 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2137 else if (invert_bclk && !invert_fsync)
2138 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2140 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2143 bclk_master = (hw_config->bclk_master ==
2144 SND_SOC_TPLG_BCLK_CM);
2145 fsync_master = (hw_config->fsync_master ==
2146 SND_SOC_TPLG_FSYNC_CM);
2147 if (bclk_master && fsync_master)
2148 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
2149 else if (!bclk_master && fsync_master)
2150 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2151 else if (bclk_master && !fsync_master)
2152 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2154 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2159 * link_new_ver - Create a new physical link config from the old
2160 * version of source.
2161 * @tplg: topology context
2162 * @src: old version of phyical link config as a source
2163 * @link: latest version of physical link config created from the source
2165 * Support from vesion 4. User need free the returned link config manually.
2167 static int link_new_ver(struct soc_tplg *tplg,
2168 struct snd_soc_tplg_link_config *src,
2169 struct snd_soc_tplg_link_config **link)
2171 struct snd_soc_tplg_link_config *dest;
2172 struct snd_soc_tplg_link_config_v4 *src_v4;
2177 if (le32_to_cpu(src->size) !=
2178 sizeof(struct snd_soc_tplg_link_config_v4)) {
2179 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2183 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2185 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2186 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2190 dest->size = cpu_to_le32(sizeof(*dest));
2191 dest->id = src_v4->id;
2192 dest->num_streams = src_v4->num_streams;
2193 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2194 memcpy(&dest->stream[i], &src_v4->stream[i],
2195 sizeof(struct snd_soc_tplg_stream));
2201 /* Find and configure an existing physical DAI link */
2202 static int soc_tplg_link_config(struct soc_tplg *tplg,
2203 struct snd_soc_tplg_link_config *cfg)
2205 struct snd_soc_dai_link *link;
2206 const char *name, *stream_name;
2210 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2211 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2218 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2219 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2222 stream_name = cfg->stream_name;
2226 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2229 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2235 if (cfg->num_hw_configs)
2236 set_link_hw_format(link, cfg);
2240 set_link_flags(link,
2241 le32_to_cpu(cfg->flag_mask),
2242 le32_to_cpu(cfg->flags));
2244 /* pass control to component driver for optional further init */
2245 ret = soc_tplg_dai_link_load(tplg, link, cfg);
2247 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2251 /* for unloading it in snd_soc_tplg_component_remove */
2252 link->dobj.index = tplg->index;
2253 link->dobj.ops = tplg->ops;
2254 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2255 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2261 /* Load physical link config elements from the topology context */
2262 static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2263 struct snd_soc_tplg_hdr *hdr)
2265 struct snd_soc_tplg_link_config *link, *_link;
2271 count = le32_to_cpu(hdr->count);
2273 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2274 tplg->pos += le32_to_cpu(hdr->size) +
2275 le32_to_cpu(hdr->payload_size);
2279 /* check the element size and count */
2280 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2281 size = le32_to_cpu(link->size);
2282 if (size > sizeof(struct snd_soc_tplg_link_config)
2283 || size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2284 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2289 if (soc_tplg_check_elem_count(tplg,
2291 le32_to_cpu(hdr->payload_size),
2292 "physical link config")) {
2293 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2298 /* config physical DAI links */
2299 for (i = 0; i < count; i++) {
2300 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2301 size = le32_to_cpu(link->size);
2302 if (size == sizeof(*link)) {
2307 ret = link_new_ver(tplg, link, &_link);
2312 ret = soc_tplg_link_config(tplg, _link);
2316 /* offset by version-specific struct size and
2317 * real priv data size
2319 tplg->pos += size + le32_to_cpu(_link->priv.size);
2322 kfree(_link); /* free the duplicated one */
2329 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2330 * @tplg: topology context
2331 * @d: physical DAI configs.
2333 * The physical dai should already be registered by the platform driver.
2334 * The platform driver should specify the DAI name and ID for matching.
2336 static int soc_tplg_dai_config(struct soc_tplg *tplg,
2337 struct snd_soc_tplg_dai *d)
2339 struct snd_soc_dai_link_component dai_component;
2340 struct snd_soc_dai *dai;
2341 struct snd_soc_dai_driver *dai_drv;
2342 struct snd_soc_pcm_stream *stream;
2343 struct snd_soc_tplg_stream_caps *caps;
2346 memset(&dai_component, 0, sizeof(dai_component));
2348 dai_component.dai_name = d->dai_name;
2349 dai = snd_soc_find_dai(&dai_component);
2351 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2356 if (le32_to_cpu(d->dai_id) != dai->id) {
2357 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2362 dai_drv = dai->driver;
2367 stream = &dai_drv->playback;
2368 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2369 set_stream_info(stream, caps);
2373 stream = &dai_drv->capture;
2374 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2375 set_stream_info(stream, caps);
2379 set_dai_flags(dai_drv,
2380 le32_to_cpu(d->flag_mask),
2381 le32_to_cpu(d->flags));
2383 /* pass control to component driver for optional further init */
2384 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2386 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2393 /* load physical DAI elements */
2394 static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2395 struct snd_soc_tplg_hdr *hdr)
2397 struct snd_soc_tplg_dai *dai;
2401 count = le32_to_cpu(hdr->count);
2403 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2406 /* config the existing BE DAIs */
2407 for (i = 0; i < count; i++) {
2408 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2409 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
2410 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2414 soc_tplg_dai_config(tplg, dai);
2415 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
2418 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2423 * manifest_new_ver - Create a new version of manifest from the old version
2425 * @tplg: topology context
2426 * @src: old version of manifest as a source
2427 * @manifest: latest version of manifest created from the source
2429 * Support from vesion 4. Users need free the returned manifest manually.
2431 static int manifest_new_ver(struct soc_tplg *tplg,
2432 struct snd_soc_tplg_manifest *src,
2433 struct snd_soc_tplg_manifest **manifest)
2435 struct snd_soc_tplg_manifest *dest;
2436 struct snd_soc_tplg_manifest_v4 *src_v4;
2441 size = le32_to_cpu(src->size);
2442 if (size != sizeof(*src_v4)) {
2443 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2447 src->size = cpu_to_le32(sizeof(*src_v4));
2450 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2452 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2453 dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2458 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2459 dest->control_elems = src_v4->control_elems;
2460 dest->widget_elems = src_v4->widget_elems;
2461 dest->graph_elems = src_v4->graph_elems;
2462 dest->pcm_elems = src_v4->pcm_elems;
2463 dest->dai_link_elems = src_v4->dai_link_elems;
2464 dest->priv.size = src_v4->priv.size;
2465 if (dest->priv.size)
2466 memcpy(dest->priv.data, src_v4->priv.data,
2467 le32_to_cpu(src_v4->priv.size));
2473 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2474 struct snd_soc_tplg_hdr *hdr)
2476 struct snd_soc_tplg_manifest *manifest, *_manifest;
2480 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2483 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2485 /* check ABI version by size, create a new manifest if abi not match */
2486 if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2488 _manifest = manifest;
2491 err = manifest_new_ver(tplg, manifest, &_manifest);
2496 /* pass control to component driver for optional further init */
2497 if (tplg->comp && tplg->ops && tplg->ops->manifest)
2498 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
2500 if (!abi_match) /* free the duplicated one */
2506 /* validate header magic, size and type */
2507 static int soc_valid_header(struct soc_tplg *tplg,
2508 struct snd_soc_tplg_hdr *hdr)
2510 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2513 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
2515 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2516 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
2521 /* big endian firmware objects not supported atm */
2522 if (hdr->magic == SOC_TPLG_MAGIC_BIG_ENDIAN) {
2524 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2525 tplg->pass, hdr->magic,
2526 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2530 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
2532 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2533 tplg->pass, hdr->magic,
2534 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2538 /* Support ABI from version 4 */
2539 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2540 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
2542 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2543 tplg->pass, hdr->abi,
2544 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2549 if (hdr->payload_size == 0) {
2550 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2551 soc_tplg_get_hdr_offset(tplg));
2555 if (tplg->pass == le32_to_cpu(hdr->type))
2557 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2558 hdr->payload_size, hdr->type, hdr->version,
2559 hdr->vendor_type, tplg->pass);
2564 /* check header type and call appropriate handler */
2565 static int soc_tplg_load_header(struct soc_tplg *tplg,
2566 struct snd_soc_tplg_hdr *hdr)
2568 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2570 /* check for matching ID */
2571 if (le32_to_cpu(hdr->index) != tplg->req_index &&
2572 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
2575 tplg->index = le32_to_cpu(hdr->index);
2577 switch (le32_to_cpu(hdr->type)) {
2578 case SND_SOC_TPLG_TYPE_MIXER:
2579 case SND_SOC_TPLG_TYPE_ENUM:
2580 case SND_SOC_TPLG_TYPE_BYTES:
2581 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2582 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2583 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2584 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2585 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2586 case SND_SOC_TPLG_TYPE_PCM:
2587 return soc_tplg_pcm_elems_load(tplg, hdr);
2588 case SND_SOC_TPLG_TYPE_DAI:
2589 return soc_tplg_dai_elems_load(tplg, hdr);
2590 case SND_SOC_TPLG_TYPE_DAI_LINK:
2591 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2592 /* physical link configurations */
2593 return soc_tplg_link_elems_load(tplg, hdr);
2594 case SND_SOC_TPLG_TYPE_MANIFEST:
2595 return soc_tplg_manifest_load(tplg, hdr);
2597 /* bespoke vendor data object */
2598 return soc_tplg_vendor_load(tplg, hdr);
2604 /* process the topology file headers */
2605 static int soc_tplg_process_headers(struct soc_tplg *tplg)
2607 struct snd_soc_tplg_hdr *hdr;
2610 tplg->pass = SOC_TPLG_PASS_START;
2612 /* process the header types from start to end */
2613 while (tplg->pass <= SOC_TPLG_PASS_END) {
2615 tplg->hdr_pos = tplg->fw->data;
2616 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2618 while (!soc_tplg_is_eof(tplg)) {
2620 /* make sure header is valid before loading */
2621 ret = soc_valid_header(tplg, hdr);
2627 /* load the header object */
2628 ret = soc_tplg_load_header(tplg, hdr);
2632 /* goto next header */
2633 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
2634 sizeof(struct snd_soc_tplg_hdr);
2635 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2638 /* next data type pass */
2642 /* signal DAPM we are complete */
2643 ret = soc_tplg_dapm_complete(tplg);
2646 "ASoC: failed to initialise DAPM from Firmware\n");
2651 static int soc_tplg_load(struct soc_tplg *tplg)
2655 ret = soc_tplg_process_headers(tplg);
2657 soc_tplg_complete(tplg);
2662 /* load audio component topology from "firmware" file */
2663 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2664 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2666 struct soc_tplg tplg;
2669 /* setup parsing context */
2670 memset(&tplg, 0, sizeof(tplg));
2672 tplg.dev = comp->dev;
2675 tplg.req_index = id;
2676 tplg.io_ops = ops->io_ops;
2677 tplg.io_ops_count = ops->io_ops_count;
2678 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2679 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2681 ret = soc_tplg_load(&tplg);
2682 /* free the created components if fail to load topology */
2684 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2688 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2690 /* remove this dynamic widget */
2691 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2693 /* make sure we are a widget */
2694 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2697 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2699 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2701 /* remove all dynamic widgets from this DAPM context */
2702 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2705 struct snd_soc_dapm_widget *w, *next_w;
2707 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2709 /* make sure we are a widget with correct context */
2710 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2714 if (w->dobj.index != index &&
2715 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2717 /* check and free and dynamic widget kcontrols */
2718 snd_soc_tplg_widget_remove(w);
2719 snd_soc_dapm_free_widget(w);
2721 snd_soc_dapm_reset_cache(dapm);
2723 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2725 /* remove dynamic controls from the component driver */
2726 int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2728 struct snd_soc_dobj *dobj, *next_dobj;
2729 int pass = SOC_TPLG_PASS_END;
2731 /* process the header types from end to start */
2732 while (pass >= SOC_TPLG_PASS_START) {
2734 /* remove mixer controls */
2735 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2739 if (dobj->index != index &&
2740 index != SND_SOC_TPLG_INDEX_ALL)
2743 switch (dobj->type) {
2744 case SND_SOC_DOBJ_MIXER:
2745 remove_mixer(comp, dobj, pass);
2747 case SND_SOC_DOBJ_ENUM:
2748 remove_enum(comp, dobj, pass);
2750 case SND_SOC_DOBJ_BYTES:
2751 remove_bytes(comp, dobj, pass);
2753 case SND_SOC_DOBJ_GRAPH:
2754 remove_route(comp, dobj, pass);
2756 case SND_SOC_DOBJ_WIDGET:
2757 remove_widget(comp, dobj, pass);
2759 case SND_SOC_DOBJ_PCM:
2760 remove_dai(comp, dobj, pass);
2762 case SND_SOC_DOBJ_DAI_LINK:
2763 remove_link(comp, dobj, pass);
2765 case SND_SOC_DOBJ_BACKEND_LINK:
2767 * call link_unload ops if extra
2768 * deinitialization is needed.
2770 remove_backend_link(comp, dobj, pass);
2773 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2781 /* let caller know if FW can be freed when no objects are left */
2782 return !list_empty(&comp->dobj_list);
2784 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);