2 * soc-topology.c -- ALSA SoC Topology
4 * Copyright (C) 2012 Texas Instruments Inc.
5 * Copyright (C) 2015 Intel Corporation.
7 * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
8 * K, Mythri P <mythri.p.k@intel.com>
9 * Prusty, Subhransu S <subhransu.s.prusty@intel.com>
10 * B, Jayachandran <jayachandran.b@intel.com>
11 * Abdullah, Omair M <omair.m.abdullah@intel.com>
12 * Jin, Yao <yao.jin@intel.com>
13 * Lin, Mengdong <mengdong.lin@intel.com>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * Add support to read audio firmware topology alongside firmware text. The
21 * topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
22 * equalizers, firmware, coefficients etc.
24 * This file only manages the core ALSA and ASoC components, all other bespoke
25 * firmware topology data is passed to component drivers for bespoke handling.
28 #include <linux/kernel.h>
29 #include <linux/export.h>
30 #include <linux/list.h>
31 #include <linux/firmware.h>
32 #include <linux/slab.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/soc-topology.h>
38 * We make several passes over the data (since it wont necessarily be ordered)
39 * and process objects in the following order. This guarantees the component
40 * drivers will be ready with any vendor data before the mixers and DAPM objects
41 * are loaded (that may make use of the vendor data).
43 #define SOC_TPLG_PASS_MANIFEST 0
44 #define SOC_TPLG_PASS_VENDOR 1
45 #define SOC_TPLG_PASS_MIXER 2
46 #define SOC_TPLG_PASS_WIDGET 3
47 #define SOC_TPLG_PASS_GRAPH 4
48 #define SOC_TPLG_PASS_PINS 5
49 #define SOC_TPLG_PASS_PCM_DAI 6
51 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
52 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_PCM_DAI
55 const struct firmware *fw;
57 /* runtime FW parsing */
58 const u8 *pos; /* read postion */
59 const u8 *hdr_pos; /* header position */
60 unsigned int pass; /* pass number */
62 /* component caller */
64 struct snd_soc_component *comp;
65 u32 index; /* current block index */
66 u32 req_index; /* required index, only loaded/free matching blocks */
68 /* kcontrol operations */
69 const struct snd_soc_tplg_kcontrol_ops *io_ops;
72 /* optional fw loading callbacks to component drivers */
73 struct snd_soc_tplg_ops *ops;
76 static int soc_tplg_process_headers(struct soc_tplg *tplg);
77 static void soc_tplg_complete(struct soc_tplg *tplg);
78 struct snd_soc_dapm_widget *
79 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
80 const struct snd_soc_dapm_widget *widget);
81 struct snd_soc_dapm_widget *
82 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
83 const struct snd_soc_dapm_widget *widget);
85 /* check we dont overflow the data for this control chunk */
86 static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
87 unsigned int count, size_t bytes, const char *elem_type)
89 const u8 *end = tplg->pos + elem_size * count;
91 if (end > tplg->fw->data + tplg->fw->size) {
92 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
97 /* check there is enough room in chunk for control.
98 extra bytes at the end of control are for vendor data here */
99 if (elem_size * count > bytes) {
101 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
102 elem_type, count, elem_size, bytes);
109 static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
111 const u8 *end = tplg->hdr_pos;
113 if (end >= tplg->fw->data + tplg->fw->size)
118 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
120 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
123 static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
125 return (unsigned long)(tplg->pos - tplg->fw->data);
128 /* mapping of Kcontrol types and associated operations. */
129 static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
130 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
131 snd_soc_put_volsw, snd_soc_info_volsw},
132 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
133 snd_soc_put_volsw_sx, NULL},
134 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
135 snd_soc_put_enum_double, snd_soc_info_enum_double},
136 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
137 snd_soc_put_enum_double, NULL},
138 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
139 snd_soc_bytes_put, snd_soc_bytes_info},
140 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
141 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
142 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
143 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
144 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
145 snd_soc_put_strobe, NULL},
146 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
147 snd_soc_dapm_put_volsw, NULL},
148 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
149 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
150 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
151 snd_soc_dapm_put_enum_double, NULL},
152 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
153 snd_soc_dapm_put_enum_double, NULL},
154 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
155 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
158 struct soc_tplg_map {
163 /* mapping of widget types from UAPI IDs to kernel IDs */
164 static const struct soc_tplg_map dapm_map[] = {
165 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
166 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
167 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
168 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
169 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
170 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
171 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
172 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
173 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
174 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
175 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
176 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
177 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
178 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
179 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
180 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
183 static int tplc_chan_get_reg(struct soc_tplg *tplg,
184 struct snd_soc_tplg_channel *chan, int map)
188 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
189 if (chan[i].id == map)
196 static int tplc_chan_get_shift(struct soc_tplg *tplg,
197 struct snd_soc_tplg_channel *chan, int map)
201 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
202 if (chan[i].id == map)
203 return chan[i].shift;
209 static int get_widget_id(int tplg_type)
213 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
214 if (tplg_type == dapm_map[i].uid)
215 return dapm_map[i].kid;
221 static enum snd_soc_dobj_type get_dobj_mixer_type(
222 struct snd_soc_tplg_ctl_hdr *control_hdr)
224 if (control_hdr == NULL)
225 return SND_SOC_DOBJ_NONE;
227 switch (control_hdr->ops.info) {
228 case SND_SOC_TPLG_CTL_VOLSW:
229 case SND_SOC_TPLG_CTL_VOLSW_SX:
230 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
231 case SND_SOC_TPLG_CTL_RANGE:
232 case SND_SOC_TPLG_CTL_STROBE:
233 return SND_SOC_DOBJ_MIXER;
234 case SND_SOC_TPLG_CTL_ENUM:
235 case SND_SOC_TPLG_CTL_ENUM_VALUE:
236 return SND_SOC_DOBJ_ENUM;
237 case SND_SOC_TPLG_CTL_BYTES:
238 return SND_SOC_DOBJ_BYTES;
240 return SND_SOC_DOBJ_NONE;
244 static enum snd_soc_dobj_type get_dobj_type(struct snd_soc_tplg_hdr *hdr,
245 struct snd_soc_tplg_ctl_hdr *control_hdr)
248 case SND_SOC_TPLG_TYPE_MIXER:
249 return get_dobj_mixer_type(control_hdr);
250 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
251 case SND_SOC_TPLG_TYPE_MANIFEST:
252 return SND_SOC_DOBJ_NONE;
253 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
254 return SND_SOC_DOBJ_WIDGET;
255 case SND_SOC_TPLG_TYPE_DAI_LINK:
256 return SND_SOC_DOBJ_DAI_LINK;
257 case SND_SOC_TPLG_TYPE_PCM:
258 return SND_SOC_DOBJ_PCM;
259 case SND_SOC_TPLG_TYPE_CODEC_LINK:
260 return SND_SOC_DOBJ_CODEC_LINK;
262 return SND_SOC_DOBJ_NONE;
266 static inline void soc_bind_err(struct soc_tplg *tplg,
267 struct snd_soc_tplg_ctl_hdr *hdr, int index)
270 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
271 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
272 soc_tplg_get_offset(tplg));
275 static inline void soc_control_err(struct soc_tplg *tplg,
276 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
279 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
280 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
281 soc_tplg_get_offset(tplg));
284 /* pass vendor data to component driver for processing */
285 static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
286 struct snd_soc_tplg_hdr *hdr)
290 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
291 ret = tplg->ops->vendor_load(tplg->comp, hdr);
293 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
300 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
301 soc_tplg_get_hdr_offset(tplg),
302 soc_tplg_get_hdr_offset(tplg),
303 hdr->type, hdr->vendor_type);
307 /* pass vendor data to component driver for processing */
308 static int soc_tplg_vendor_load(struct soc_tplg *tplg,
309 struct snd_soc_tplg_hdr *hdr)
311 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
314 return soc_tplg_vendor_load_(tplg, hdr);
317 /* optionally pass new dynamic widget to component driver. This is mainly for
318 * external widgets where we can assign private data/ops */
319 static int soc_tplg_widget_load(struct soc_tplg *tplg,
320 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
322 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
323 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
328 /* pass dynamic FEs configurations to component driver */
329 static int soc_tplg_pcm_dai_load(struct soc_tplg *tplg,
330 struct snd_soc_tplg_pcm_dai *pcm_dai, int num_pcm_dai)
332 if (tplg->comp && tplg->ops && tplg->ops->pcm_dai_load)
333 return tplg->ops->pcm_dai_load(tplg->comp, pcm_dai, num_pcm_dai);
338 /* tell the component driver that all firmware has been loaded in this request */
339 static void soc_tplg_complete(struct soc_tplg *tplg)
341 if (tplg->comp && tplg->ops && tplg->ops->complete)
342 tplg->ops->complete(tplg->comp);
345 /* add a dynamic kcontrol */
346 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
347 const struct snd_kcontrol_new *control_new, const char *prefix,
348 void *data, struct snd_kcontrol **kcontrol)
352 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
353 if (*kcontrol == NULL) {
354 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
359 err = snd_ctl_add(card, *kcontrol);
361 dev_err(dev, "ASoC: Failed to add %s: %d\n",
362 control_new->name, err);
369 /* add a dynamic kcontrol for component driver */
370 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
371 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
373 struct snd_soc_component *comp = tplg->comp;
375 return soc_tplg_add_dcontrol(comp->card->snd_card,
376 comp->dev, k, NULL, comp, kcontrol);
379 /* remove a mixer kcontrol */
380 static void remove_mixer(struct snd_soc_component *comp,
381 struct snd_soc_dobj *dobj, int pass)
383 struct snd_card *card = comp->card->snd_card;
384 struct soc_mixer_control *sm =
385 container_of(dobj, struct soc_mixer_control, dobj);
386 const unsigned int *p = NULL;
388 if (pass != SOC_TPLG_PASS_MIXER)
391 if (dobj->ops && dobj->ops->control_unload)
392 dobj->ops->control_unload(comp, dobj);
394 if (sm->dobj.control.kcontrol->tlv.p)
395 p = sm->dobj.control.kcontrol->tlv.p;
396 snd_ctl_remove(card, sm->dobj.control.kcontrol);
397 list_del(&sm->dobj.list);
402 /* remove an enum kcontrol */
403 static void remove_enum(struct snd_soc_component *comp,
404 struct snd_soc_dobj *dobj, int pass)
406 struct snd_card *card = comp->card->snd_card;
407 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
410 if (pass != SOC_TPLG_PASS_MIXER)
413 if (dobj->ops && dobj->ops->control_unload)
414 dobj->ops->control_unload(comp, dobj);
416 snd_ctl_remove(card, se->dobj.control.kcontrol);
417 list_del(&se->dobj.list);
419 kfree(se->dobj.control.dvalues);
420 for (i = 0; i < se->items; i++)
421 kfree(se->dobj.control.dtexts[i]);
425 /* remove a byte kcontrol */
426 static void remove_bytes(struct snd_soc_component *comp,
427 struct snd_soc_dobj *dobj, int pass)
429 struct snd_card *card = comp->card->snd_card;
430 struct soc_bytes_ext *sb =
431 container_of(dobj, struct soc_bytes_ext, dobj);
433 if (pass != SOC_TPLG_PASS_MIXER)
436 if (dobj->ops && dobj->ops->control_unload)
437 dobj->ops->control_unload(comp, dobj);
439 snd_ctl_remove(card, sb->dobj.control.kcontrol);
440 list_del(&sb->dobj.list);
444 /* remove a widget and it's kcontrols - routes must be removed first */
445 static void remove_widget(struct snd_soc_component *comp,
446 struct snd_soc_dobj *dobj, int pass)
448 struct snd_card *card = comp->card->snd_card;
449 struct snd_soc_dapm_widget *w =
450 container_of(dobj, struct snd_soc_dapm_widget, dobj);
453 if (pass != SOC_TPLG_PASS_WIDGET)
456 if (dobj->ops && dobj->ops->widget_unload)
457 dobj->ops->widget_unload(comp, dobj);
460 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
461 * The enum may either have an array of values or strings.
463 if (dobj->widget.kcontrol_enum) {
464 /* enumerated widget mixer */
465 struct soc_enum *se =
466 (struct soc_enum *)w->kcontrols[0]->private_value;
468 snd_ctl_remove(card, w->kcontrols[0]);
470 kfree(se->dobj.control.dvalues);
471 for (i = 0; i < se->items; i++)
472 kfree(se->dobj.control.dtexts[i]);
475 kfree(w->kcontrol_news);
477 /* non enumerated widget mixer */
478 for (i = 0; i < w->num_kcontrols; i++) {
479 struct snd_kcontrol *kcontrol = w->kcontrols[i];
480 struct soc_mixer_control *sm =
481 (struct soc_mixer_control *) kcontrol->private_value;
483 kfree(w->kcontrols[i]->tlv.p);
485 snd_ctl_remove(card, w->kcontrols[i]);
488 kfree(w->kcontrol_news);
490 /* widget w is freed by soc-dapm.c */
493 /* remove PCM DAI configurations */
494 static void remove_pcm_dai(struct snd_soc_component *comp,
495 struct snd_soc_dobj *dobj, int pass)
497 if (pass != SOC_TPLG_PASS_PCM_DAI)
500 if (dobj->ops && dobj->ops->pcm_dai_unload)
501 dobj->ops->pcm_dai_unload(comp, dobj);
503 list_del(&dobj->list);
507 /* bind a kcontrol to it's IO handlers */
508 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
509 struct snd_kcontrol_new *k,
510 const struct snd_soc_tplg_kcontrol_ops *ops, int num_ops,
511 const struct snd_soc_tplg_kcontrol_ops *bops, int num_bops)
515 /* try and map standard kcontrols handler first */
516 for (i = 0; i < num_ops; i++) {
518 if (ops[i].id == hdr->ops.put)
520 if (ops[i].id == hdr->ops.get)
522 if (ops[i].id == hdr->ops.info)
523 k->info = ops[i].info;
526 /* standard handlers found ? */
527 if (k->put && k->get && k->info)
530 /* none found so try bespoke handlers */
531 for (i = 0; i < num_bops; i++) {
533 if (k->put == NULL && bops[i].id == hdr->ops.put)
534 k->put = bops[i].put;
535 if (k->get == NULL && bops[i].id == hdr->ops.get)
536 k->get = bops[i].get;
537 if (k->info == NULL && ops[i].id == hdr->ops.info)
538 k->info = bops[i].info;
541 /* bespoke handlers found ? */
542 if (k->put && k->get && k->info)
545 /* nothing to bind */
549 /* bind a widgets to it's evnt handlers */
550 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
551 const struct snd_soc_tplg_widget_events *events,
552 int num_events, u16 event_type)
558 for (i = 0; i < num_events; i++) {
559 if (event_type == events[i].type) {
561 /* found - so assign event */
562 w->event = events[i].event_handler;
570 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
572 /* optionally pass new dynamic kcontrol to component driver. */
573 static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
574 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
576 if (tplg->comp && tplg->ops && tplg->ops->control_load)
577 return tplg->ops->control_load(tplg->comp, k, hdr);
582 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
583 struct snd_kcontrol_new *kc, u32 tlv_size)
585 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
586 struct snd_ctl_tlv *tlv;
591 tplg_tlv = (struct snd_soc_tplg_ctl_tlv *) tplg->pos;
592 tplg->pos += tlv_size;
594 tlv = kzalloc(sizeof(*tlv) + tlv_size, GFP_KERNEL);
598 dev_dbg(tplg->dev, " created TLV type %d size %d bytes\n",
599 tplg_tlv->numid, tplg_tlv->size);
601 tlv->numid = tplg_tlv->numid;
602 tlv->length = tplg_tlv->size;
603 memcpy(tlv->tlv, tplg_tlv + 1, tplg_tlv->size);
604 kc->tlv.p = (void *)tlv;
609 static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
610 struct snd_kcontrol_new *kc)
615 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
618 struct snd_soc_tplg_bytes_control *be;
619 struct soc_bytes_ext *sbe;
620 struct snd_kcontrol_new kc;
623 if (soc_tplg_check_elem_count(tplg,
624 sizeof(struct snd_soc_tplg_bytes_control), count,
625 size, "mixer bytes")) {
626 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
631 for (i = 0; i < count; i++) {
632 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
634 /* validate kcontrol */
635 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
636 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
639 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
643 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
647 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
648 be->hdr.name, be->hdr.access);
650 memset(&kc, 0, sizeof(kc));
651 kc.name = be->hdr.name;
652 kc.private_value = (long)sbe;
653 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
654 kc.access = be->hdr.access;
657 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
658 sbe->dobj.ops = tplg->ops;
659 INIT_LIST_HEAD(&sbe->dobj.list);
661 /* map io handlers */
662 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, io_ops,
663 ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
665 soc_control_err(tplg, &be->hdr, be->hdr.name);
670 /* pass control to driver for optional further init */
671 err = soc_tplg_init_kcontrol(tplg, &kc,
672 (struct snd_soc_tplg_ctl_hdr *)be);
674 dev_err(tplg->dev, "ASoC: failed to init %s\n",
680 /* register control here */
681 err = soc_tplg_add_kcontrol(tplg, &kc,
682 &sbe->dobj.control.kcontrol);
684 dev_err(tplg->dev, "ASoC: failed to add %s\n",
690 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
696 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
699 struct snd_soc_tplg_mixer_control *mc;
700 struct soc_mixer_control *sm;
701 struct snd_kcontrol_new kc;
704 if (soc_tplg_check_elem_count(tplg,
705 sizeof(struct snd_soc_tplg_mixer_control),
706 count, size, "mixers")) {
708 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
713 for (i = 0; i < count; i++) {
714 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
716 /* validate kcontrol */
717 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
718 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
721 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
724 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
728 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
729 mc->hdr.name, mc->hdr.access);
731 memset(&kc, 0, sizeof(kc));
732 kc.name = mc->hdr.name;
733 kc.private_value = (long)sm;
734 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
735 kc.access = mc->hdr.access;
737 /* we only support FL/FR channel mapping atm */
738 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
740 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
742 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
744 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
749 sm->invert = mc->invert;
750 sm->platform_max = mc->platform_max;
751 sm->dobj.index = tplg->index;
752 sm->dobj.ops = tplg->ops;
753 sm->dobj.type = SND_SOC_DOBJ_MIXER;
754 INIT_LIST_HEAD(&sm->dobj.list);
756 /* map io handlers */
757 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, io_ops,
758 ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
760 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
765 /* pass control to driver for optional further init */
766 err = soc_tplg_init_kcontrol(tplg, &kc,
767 (struct snd_soc_tplg_ctl_hdr *) mc);
769 dev_err(tplg->dev, "ASoC: failed to init %s\n",
775 /* create any TLV data */
776 soc_tplg_create_tlv(tplg, &kc, mc->hdr.tlv_size);
778 /* register control here */
779 err = soc_tplg_add_kcontrol(tplg, &kc,
780 &sm->dobj.control.kcontrol);
782 dev_err(tplg->dev, "ASoC: failed to add %s\n",
784 soc_tplg_free_tlv(tplg, &kc);
789 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
795 static int soc_tplg_denum_create_texts(struct soc_enum *se,
796 struct snd_soc_tplg_enum_control *ec)
800 se->dobj.control.dtexts =
801 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
802 if (se->dobj.control.dtexts == NULL)
805 for (i = 0; i < ec->items; i++) {
807 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
808 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
813 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
814 if (!se->dobj.control.dtexts[i]) {
823 for (--i; i >= 0; i--)
824 kfree(se->dobj.control.dtexts[i]);
825 kfree(se->dobj.control.dtexts);
829 static int soc_tplg_denum_create_values(struct soc_enum *se,
830 struct snd_soc_tplg_enum_control *ec)
832 if (ec->items > sizeof(*ec->values))
835 se->dobj.control.dvalues =
836 kmalloc(ec->items * sizeof(u32), GFP_KERNEL);
837 if (!se->dobj.control.dvalues)
840 memcpy(se->dobj.control.dvalues, ec->values, ec->items * sizeof(u32));
844 static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
847 struct snd_soc_tplg_enum_control *ec;
849 struct snd_kcontrol_new kc;
852 if (soc_tplg_check_elem_count(tplg,
853 sizeof(struct snd_soc_tplg_enum_control),
854 count, size, "enums")) {
856 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
861 for (i = 0; i < count; i++) {
862 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
863 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
866 /* validate kcontrol */
867 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
868 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
871 se = kzalloc((sizeof(*se)), GFP_KERNEL);
875 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
876 ec->hdr.name, ec->items);
878 memset(&kc, 0, sizeof(kc));
879 kc.name = ec->hdr.name;
880 kc.private_value = (long)se;
881 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
882 kc.access = ec->hdr.access;
884 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
885 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
887 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
890 se->items = ec->items;
892 se->dobj.index = tplg->index;
893 se->dobj.type = SND_SOC_DOBJ_ENUM;
894 se->dobj.ops = tplg->ops;
895 INIT_LIST_HEAD(&se->dobj.list);
897 switch (ec->hdr.ops.info) {
898 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
899 case SND_SOC_TPLG_CTL_ENUM_VALUE:
900 err = soc_tplg_denum_create_values(se, ec);
903 "ASoC: could not create values for %s\n",
908 /* fall through and create texts */
909 case SND_SOC_TPLG_CTL_ENUM:
910 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
911 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
912 err = soc_tplg_denum_create_texts(se, ec);
915 "ASoC: could not create texts for %s\n",
923 "ASoC: invalid enum control type %d for %s\n",
924 ec->hdr.ops.info, ec->hdr.name);
929 /* map io handlers */
930 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, io_ops,
931 ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
933 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
938 /* pass control to driver for optional further init */
939 err = soc_tplg_init_kcontrol(tplg, &kc,
940 (struct snd_soc_tplg_ctl_hdr *) ec);
942 dev_err(tplg->dev, "ASoC: failed to init %s\n",
948 /* register control here */
949 ret = soc_tplg_add_kcontrol(tplg,
950 &kc, &se->dobj.control.kcontrol);
952 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
958 list_add(&se->dobj.list, &tplg->comp->dobj_list);
964 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
965 struct snd_soc_tplg_hdr *hdr)
967 struct snd_soc_tplg_ctl_hdr *control_hdr;
970 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
971 tplg->pos += hdr->size + hdr->payload_size;
975 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
976 soc_tplg_get_offset(tplg));
978 for (i = 0; i < hdr->count; i++) {
980 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
982 switch (control_hdr->ops.info) {
983 case SND_SOC_TPLG_CTL_VOLSW:
984 case SND_SOC_TPLG_CTL_STROBE:
985 case SND_SOC_TPLG_CTL_VOLSW_SX:
986 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
987 case SND_SOC_TPLG_CTL_RANGE:
988 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
989 case SND_SOC_TPLG_DAPM_CTL_PIN:
990 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
992 case SND_SOC_TPLG_CTL_ENUM:
993 case SND_SOC_TPLG_CTL_ENUM_VALUE:
994 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
995 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
996 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
997 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
999 case SND_SOC_TPLG_CTL_BYTES:
1000 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1003 soc_bind_err(tplg, control_hdr, i);
1011 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1012 struct snd_soc_tplg_hdr *hdr)
1014 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1015 struct snd_soc_dapm_route route;
1016 struct snd_soc_tplg_dapm_graph_elem *elem;
1017 int count = hdr->count, i;
1019 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1020 tplg->pos += hdr->size + hdr->payload_size;
1024 if (soc_tplg_check_elem_count(tplg,
1025 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1026 count, hdr->payload_size, "graph")) {
1028 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1033 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1035 for (i = 0; i < count; i++) {
1036 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1037 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1039 /* validate routes */
1040 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1041 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1043 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1044 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1046 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1047 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1050 route.source = elem->source;
1051 route.sink = elem->sink;
1052 route.connected = NULL; /* set to NULL atm for tplg users */
1053 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1054 route.control = NULL;
1056 route.control = elem->control;
1058 /* add route, but keep going if some fail */
1059 snd_soc_dapm_add_routes(dapm, &route, 1);
1065 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1066 struct soc_tplg *tplg, int num_kcontrols)
1068 struct snd_kcontrol_new *kc;
1069 struct soc_mixer_control *sm;
1070 struct snd_soc_tplg_mixer_control *mc;
1073 kc = kzalloc(sizeof(*kc) * num_kcontrols, GFP_KERNEL);
1077 for (i = 0; i < num_kcontrols; i++) {
1078 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1079 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1083 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1086 /* validate kcontrol */
1087 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1088 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1091 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1094 kc[i].name = mc->hdr.name;
1095 kc[i].private_value = (long)sm;
1096 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1097 kc[i].access = mc->hdr.access;
1099 /* we only support FL/FR channel mapping atm */
1100 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1102 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1104 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1106 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1111 sm->invert = mc->invert;
1112 sm->platform_max = mc->platform_max;
1113 sm->dobj.index = tplg->index;
1114 INIT_LIST_HEAD(&sm->dobj.list);
1116 /* map io handlers */
1117 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], io_ops,
1118 ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
1120 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1125 /* pass control to driver for optional further init */
1126 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1127 (struct snd_soc_tplg_ctl_hdr *)mc);
1129 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1140 for (--i; i >= 0; i--)
1141 kfree((void *)kc[i].private_value);
1146 static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1147 struct soc_tplg *tplg)
1149 struct snd_kcontrol_new *kc;
1150 struct snd_soc_tplg_enum_control *ec;
1151 struct soc_enum *se;
1154 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1155 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1158 /* validate kcontrol */
1159 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1160 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1163 kc = kzalloc(sizeof(*kc), GFP_KERNEL);
1167 se = kzalloc(sizeof(*se), GFP_KERNEL);
1171 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1174 kc->name = ec->hdr.name;
1175 kc->private_value = (long)se;
1176 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1177 kc->access = ec->hdr.access;
1179 /* we only support FL/FR channel mapping atm */
1180 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1181 se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
1182 se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
1184 se->items = ec->items;
1185 se->mask = ec->mask;
1186 se->dobj.index = tplg->index;
1188 switch (ec->hdr.ops.info) {
1189 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1190 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1191 err = soc_tplg_denum_create_values(se, ec);
1193 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1197 /* fall through to create texts */
1198 case SND_SOC_TPLG_CTL_ENUM:
1199 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1200 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1201 err = soc_tplg_denum_create_texts(se, ec);
1203 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1209 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1210 ec->hdr.ops.info, ec->hdr.name);
1214 /* map io handlers */
1215 err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, io_ops,
1216 ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
1218 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1222 /* pass control to driver for optional further init */
1223 err = soc_tplg_init_kcontrol(tplg, kc,
1224 (struct snd_soc_tplg_ctl_hdr *)ec);
1226 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1234 /* free values and texts */
1235 kfree(se->dobj.control.dvalues);
1236 for (i = 0; i < ec->items; i++)
1237 kfree(se->dobj.control.dtexts[i]);
1246 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1247 struct soc_tplg *tplg, int count)
1249 struct snd_soc_tplg_bytes_control *be;
1250 struct soc_bytes_ext *sbe;
1251 struct snd_kcontrol_new *kc;
1254 kc = kzalloc(sizeof(*kc) * count, GFP_KERNEL);
1258 for (i = 0; i < count; i++) {
1259 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1261 /* validate kcontrol */
1262 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1263 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1266 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1270 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1274 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1275 be->hdr.name, be->hdr.access);
1277 memset(kc, 0, sizeof(*kc));
1278 kc[i].name = be->hdr.name;
1279 kc[i].private_value = (long)sbe;
1280 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1281 kc[i].access = be->hdr.access;
1284 INIT_LIST_HEAD(&sbe->dobj.list);
1286 /* map standard io handlers and check for external handlers */
1287 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], io_ops,
1288 ARRAY_SIZE(io_ops), tplg->io_ops,
1289 tplg->io_ops_count);
1291 soc_control_err(tplg, &be->hdr, be->hdr.name);
1296 /* pass control to driver for optional further init */
1297 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1298 (struct snd_soc_tplg_ctl_hdr *)be);
1300 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1310 for (--i; i >= 0; i--)
1311 kfree((void *)kc[i].private_value);
1317 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1318 struct snd_soc_tplg_dapm_widget *w)
1320 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1321 struct snd_soc_dapm_widget template, *widget;
1322 struct snd_soc_tplg_ctl_hdr *control_hdr;
1323 struct snd_soc_card *card = tplg->comp->card;
1326 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1327 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1329 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1330 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1333 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1336 memset(&template, 0, sizeof(template));
1338 /* map user to kernel widget ID */
1339 template.id = get_widget_id(w->id);
1340 if (template.id < 0)
1343 template.name = kstrdup(w->name, GFP_KERNEL);
1346 template.sname = kstrdup(w->sname, GFP_KERNEL);
1347 if (!template.sname) {
1351 template.reg = w->reg;
1352 template.shift = w->shift;
1353 template.mask = w->mask;
1354 template.on_val = w->invert ? 0 : 1;
1355 template.off_val = w->invert ? 1 : 0;
1356 template.ignore_suspend = w->ignore_suspend;
1357 template.event_flags = w->event_flags;
1358 template.dobj.index = tplg->index;
1361 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1362 if (w->num_kcontrols == 0) {
1363 template.num_kcontrols = 0;
1367 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1368 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1369 w->name, w->num_kcontrols, control_hdr->type);
1371 switch (control_hdr->ops.info) {
1372 case SND_SOC_TPLG_CTL_VOLSW:
1373 case SND_SOC_TPLG_CTL_STROBE:
1374 case SND_SOC_TPLG_CTL_VOLSW_SX:
1375 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1376 case SND_SOC_TPLG_CTL_RANGE:
1377 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1378 template.num_kcontrols = w->num_kcontrols;
1379 template.kcontrol_news =
1380 soc_tplg_dapm_widget_dmixer_create(tplg,
1381 template.num_kcontrols);
1382 if (!template.kcontrol_news) {
1387 case SND_SOC_TPLG_CTL_ENUM:
1388 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1389 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1390 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1391 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1392 template.dobj.widget.kcontrol_enum = 1;
1393 template.num_kcontrols = 1;
1394 template.kcontrol_news =
1395 soc_tplg_dapm_widget_denum_create(tplg);
1396 if (!template.kcontrol_news) {
1401 case SND_SOC_TPLG_CTL_BYTES:
1402 template.num_kcontrols = w->num_kcontrols;
1403 template.kcontrol_news =
1404 soc_tplg_dapm_widget_dbytes_create(tplg,
1405 template.num_kcontrols);
1406 if (!template.kcontrol_news) {
1412 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1413 control_hdr->ops.get, control_hdr->ops.put,
1414 control_hdr->ops.info);
1420 ret = soc_tplg_widget_load(tplg, &template, w);
1424 /* card dapm mutex is held by the core if we are loading topology
1425 * data during sound card init. */
1426 if (card->instantiated)
1427 widget = snd_soc_dapm_new_control(dapm, &template);
1429 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1430 if (widget == NULL) {
1431 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1436 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1437 widget->dobj.ops = tplg->ops;
1438 widget->dobj.index = tplg->index;
1439 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1443 kfree(template.sname);
1445 kfree(template.name);
1449 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1450 struct snd_soc_tplg_hdr *hdr)
1452 struct snd_soc_tplg_dapm_widget *widget;
1453 int ret, count = hdr->count, i;
1455 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1458 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1460 for (i = 0; i < count; i++) {
1461 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1462 ret = soc_tplg_dapm_widget_create(tplg, widget);
1464 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1471 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1473 struct snd_soc_card *card = tplg->comp->card;
1476 /* Card might not have been registered at this point.
1477 * If so, just return success.
1479 if (!card || !card->instantiated) {
1480 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1481 "Do not add new widgets now\n");
1485 ret = snd_soc_dapm_new_widgets(card);
1487 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1493 static int soc_tplg_pcm_dai_elems_load(struct soc_tplg *tplg,
1494 struct snd_soc_tplg_hdr *hdr)
1496 struct snd_soc_tplg_pcm_dai *pcm_dai;
1497 struct snd_soc_dobj *dobj;
1498 int count = hdr->count;
1501 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1504 pcm_dai = (struct snd_soc_tplg_pcm_dai *)tplg->pos;
1506 if (soc_tplg_check_elem_count(tplg,
1507 sizeof(struct snd_soc_tplg_pcm_dai), count,
1508 hdr->payload_size, "PCM DAI")) {
1509 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1514 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1515 tplg->pos += sizeof(struct snd_soc_tplg_pcm_dai) * count;
1517 dobj = kzalloc(sizeof(struct snd_soc_dobj), GFP_KERNEL);
1521 /* Call the platform driver call back to register the dais */
1522 ret = soc_tplg_pcm_dai_load(tplg, pcm_dai, count);
1524 dev_err(tplg->comp->dev, "ASoC: PCM DAI loading failed\n");
1528 dobj->type = get_dobj_type(hdr, NULL);
1529 dobj->pcm_dai.count = count;
1530 dobj->pcm_dai.pd = pcm_dai;
1531 dobj->ops = tplg->ops;
1532 dobj->index = tplg->index;
1533 list_add(&dobj->list, &tplg->comp->dobj_list);
1541 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
1542 struct snd_soc_tplg_hdr *hdr)
1544 struct snd_soc_tplg_manifest *manifest;
1546 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
1549 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
1550 tplg->pos += sizeof(struct snd_soc_tplg_manifest);
1552 if (tplg->comp && tplg->ops && tplg->ops->manifest)
1553 return tplg->ops->manifest(tplg->comp, manifest);
1555 dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n");
1559 /* validate header magic, size and type */
1560 static int soc_valid_header(struct soc_tplg *tplg,
1561 struct snd_soc_tplg_hdr *hdr)
1563 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
1566 /* big endian firmware objects not supported atm */
1567 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
1569 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1570 tplg->pass, hdr->magic,
1571 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1575 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
1577 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1578 tplg->pass, hdr->magic,
1579 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1583 if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
1585 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1586 tplg->pass, hdr->abi,
1587 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
1592 if (hdr->payload_size == 0) {
1593 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
1594 soc_tplg_get_hdr_offset(tplg));
1598 if (tplg->pass == hdr->type)
1600 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
1601 hdr->payload_size, hdr->type, hdr->version,
1602 hdr->vendor_type, tplg->pass);
1607 /* check header type and call appropriate handler */
1608 static int soc_tplg_load_header(struct soc_tplg *tplg,
1609 struct snd_soc_tplg_hdr *hdr)
1611 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
1613 /* check for matching ID */
1614 if (hdr->index != tplg->req_index &&
1615 hdr->index != SND_SOC_TPLG_INDEX_ALL)
1618 tplg->index = hdr->index;
1620 switch (hdr->type) {
1621 case SND_SOC_TPLG_TYPE_MIXER:
1622 case SND_SOC_TPLG_TYPE_ENUM:
1623 case SND_SOC_TPLG_TYPE_BYTES:
1624 return soc_tplg_kcontrol_elems_load(tplg, hdr);
1625 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
1626 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
1627 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
1628 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
1629 case SND_SOC_TPLG_TYPE_PCM:
1630 case SND_SOC_TPLG_TYPE_DAI_LINK:
1631 case SND_SOC_TPLG_TYPE_CODEC_LINK:
1632 return soc_tplg_pcm_dai_elems_load(tplg, hdr);
1633 case SND_SOC_TPLG_TYPE_MANIFEST:
1634 return soc_tplg_manifest_load(tplg, hdr);
1636 /* bespoke vendor data object */
1637 return soc_tplg_vendor_load(tplg, hdr);
1643 /* process the topology file headers */
1644 static int soc_tplg_process_headers(struct soc_tplg *tplg)
1646 struct snd_soc_tplg_hdr *hdr;
1649 tplg->pass = SOC_TPLG_PASS_START;
1651 /* process the header types from start to end */
1652 while (tplg->pass <= SOC_TPLG_PASS_END) {
1654 tplg->hdr_pos = tplg->fw->data;
1655 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1657 while (!soc_tplg_is_eof(tplg)) {
1659 /* make sure header is valid before loading */
1660 ret = soc_valid_header(tplg, hdr);
1666 /* load the header object */
1667 ret = soc_tplg_load_header(tplg, hdr);
1671 /* goto next header */
1672 tplg->hdr_pos += hdr->payload_size +
1673 sizeof(struct snd_soc_tplg_hdr);
1674 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1677 /* next data type pass */
1681 /* signal DAPM we are complete */
1682 ret = soc_tplg_dapm_complete(tplg);
1685 "ASoC: failed to initialise DAPM from Firmware\n");
1690 static int soc_tplg_load(struct soc_tplg *tplg)
1694 ret = soc_tplg_process_headers(tplg);
1696 soc_tplg_complete(tplg);
1701 /* load audio component topology from "firmware" file */
1702 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
1703 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
1705 struct soc_tplg tplg;
1707 /* setup parsing context */
1708 memset(&tplg, 0, sizeof(tplg));
1710 tplg.dev = comp->dev;
1713 tplg.req_index = id;
1714 tplg.io_ops = ops->io_ops;
1715 tplg.io_ops_count = ops->io_ops_count;
1717 return soc_tplg_load(&tplg);
1719 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
1721 /* remove this dynamic widget */
1722 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
1724 /* make sure we are a widget */
1725 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
1728 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
1730 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
1732 /* remove all dynamic widgets from this DAPM context */
1733 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
1736 struct snd_soc_dapm_widget *w, *next_w;
1737 struct snd_soc_dapm_path *p, *next_p;
1739 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1741 /* make sure we are a widget with correct context */
1742 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
1746 if (w->dobj.index != index &&
1747 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
1753 * remove source and sink paths associated to this widget.
1754 * While removing the path, remove reference to it from both
1755 * source and sink widgets so that path is removed only once.
1757 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1758 list_del(&p->list_sink);
1759 list_del(&p->list_source);
1763 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1764 list_del(&p->list_sink);
1765 list_del(&p->list_source);
1769 /* check and free and dynamic widget kcontrols */
1770 snd_soc_tplg_widget_remove(w);
1771 kfree(w->kcontrols);
1776 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
1778 /* remove dynamic controls from the component driver */
1779 int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
1781 struct snd_soc_dobj *dobj, *next_dobj;
1782 int pass = SOC_TPLG_PASS_END;
1784 /* process the header types from end to start */
1785 while (pass >= SOC_TPLG_PASS_START) {
1787 /* remove mixer controls */
1788 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
1792 if (dobj->index != index &&
1793 dobj->index != SND_SOC_TPLG_INDEX_ALL)
1796 switch (dobj->type) {
1797 case SND_SOC_DOBJ_MIXER:
1798 remove_mixer(comp, dobj, pass);
1800 case SND_SOC_DOBJ_ENUM:
1801 remove_enum(comp, dobj, pass);
1803 case SND_SOC_DOBJ_BYTES:
1804 remove_bytes(comp, dobj, pass);
1806 case SND_SOC_DOBJ_WIDGET:
1807 remove_widget(comp, dobj, pass);
1809 case SND_SOC_DOBJ_PCM:
1810 case SND_SOC_DOBJ_DAI_LINK:
1811 case SND_SOC_DOBJ_CODEC_LINK:
1812 remove_pcm_dai(comp, dobj, pass);
1815 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
1823 /* let caller know if FW can be freed when no objects are left */
1824 return !list_empty(&comp->dobj_list);
1826 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);