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>
36 #include <sound/tlv.h>
39 * We make several passes over the data (since it wont necessarily be ordered)
40 * and process objects in the following order. This guarantees the component
41 * drivers will be ready with any vendor data before the mixers and DAPM objects
42 * are loaded (that may make use of the vendor data).
44 #define SOC_TPLG_PASS_MANIFEST 0
45 #define SOC_TPLG_PASS_VENDOR 1
46 #define SOC_TPLG_PASS_MIXER 2
47 #define SOC_TPLG_PASS_WIDGET 3
48 #define SOC_TPLG_PASS_PCM_DAI 4
49 #define SOC_TPLG_PASS_GRAPH 5
50 #define SOC_TPLG_PASS_PINS 6
51 #define SOC_TPLG_PASS_BE_DAI 7
53 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
54 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_BE_DAI
57 const struct firmware *fw;
59 /* runtime FW parsing */
60 const u8 *pos; /* read postion */
61 const u8 *hdr_pos; /* header position */
62 unsigned int pass; /* pass number */
64 /* component caller */
66 struct snd_soc_component *comp;
67 u32 index; /* current block index */
68 u32 req_index; /* required index, only loaded/free matching blocks */
70 /* vendor specific kcontrol operations */
71 const struct snd_soc_tplg_kcontrol_ops *io_ops;
74 /* vendor specific bytes ext handlers, for TLV bytes controls */
75 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
76 int bytes_ext_ops_count;
78 /* optional fw loading callbacks to component drivers */
79 struct snd_soc_tplg_ops *ops;
82 static int soc_tplg_process_headers(struct soc_tplg *tplg);
83 static void soc_tplg_complete(struct soc_tplg *tplg);
84 struct snd_soc_dapm_widget *
85 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
86 const struct snd_soc_dapm_widget *widget);
87 struct snd_soc_dapm_widget *
88 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
89 const struct snd_soc_dapm_widget *widget);
91 /* check we dont overflow the data for this control chunk */
92 static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
93 unsigned int count, size_t bytes, const char *elem_type)
95 const u8 *end = tplg->pos + elem_size * count;
97 if (end > tplg->fw->data + tplg->fw->size) {
98 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
103 /* check there is enough room in chunk for control.
104 extra bytes at the end of control are for vendor data here */
105 if (elem_size * count > bytes) {
107 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
108 elem_type, count, elem_size, bytes);
115 static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
117 const u8 *end = tplg->hdr_pos;
119 if (end >= tplg->fw->data + tplg->fw->size)
124 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
126 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
129 static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
131 return (unsigned long)(tplg->pos - tplg->fw->data);
134 /* mapping of Kcontrol types and associated operations. */
135 static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
136 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
137 snd_soc_put_volsw, snd_soc_info_volsw},
138 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
139 snd_soc_put_volsw_sx, NULL},
140 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
141 snd_soc_put_enum_double, snd_soc_info_enum_double},
142 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
143 snd_soc_put_enum_double, NULL},
144 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
145 snd_soc_bytes_put, snd_soc_bytes_info},
146 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
147 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
148 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
149 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
150 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
151 snd_soc_put_strobe, NULL},
152 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
153 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
154 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
155 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
156 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
157 snd_soc_dapm_put_enum_double, NULL},
158 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
159 snd_soc_dapm_put_enum_double, NULL},
160 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
161 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
164 struct soc_tplg_map {
169 /* mapping of widget types from UAPI IDs to kernel IDs */
170 static const struct soc_tplg_map dapm_map[] = {
171 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
172 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
173 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
174 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
175 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
176 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
177 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
178 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
179 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
180 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
181 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
182 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
183 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
184 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
185 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
186 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
189 static int tplc_chan_get_reg(struct soc_tplg *tplg,
190 struct snd_soc_tplg_channel *chan, int map)
194 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
195 if (chan[i].id == map)
202 static int tplc_chan_get_shift(struct soc_tplg *tplg,
203 struct snd_soc_tplg_channel *chan, int map)
207 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
208 if (chan[i].id == map)
209 return chan[i].shift;
215 static int get_widget_id(int tplg_type)
219 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
220 if (tplg_type == dapm_map[i].uid)
221 return dapm_map[i].kid;
227 static inline void soc_bind_err(struct soc_tplg *tplg,
228 struct snd_soc_tplg_ctl_hdr *hdr, int index)
231 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
232 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
233 soc_tplg_get_offset(tplg));
236 static inline void soc_control_err(struct soc_tplg *tplg,
237 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
240 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
241 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
242 soc_tplg_get_offset(tplg));
245 /* pass vendor data to component driver for processing */
246 static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
247 struct snd_soc_tplg_hdr *hdr)
251 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
252 ret = tplg->ops->vendor_load(tplg->comp, hdr);
254 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
261 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
262 soc_tplg_get_hdr_offset(tplg),
263 soc_tplg_get_hdr_offset(tplg),
264 hdr->type, hdr->vendor_type);
268 /* pass vendor data to component driver for processing */
269 static int soc_tplg_vendor_load(struct soc_tplg *tplg,
270 struct snd_soc_tplg_hdr *hdr)
272 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
275 return soc_tplg_vendor_load_(tplg, hdr);
278 /* optionally pass new dynamic widget to component driver. This is mainly for
279 * external widgets where we can assign private data/ops */
280 static int soc_tplg_widget_load(struct soc_tplg *tplg,
281 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
283 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
284 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
289 /* pass DAI configurations to component driver for extra intialization */
290 static int soc_tplg_dai_load(struct soc_tplg *tplg,
291 struct snd_soc_dai_driver *dai_drv)
293 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
294 return tplg->ops->dai_load(tplg->comp, dai_drv);
299 /* pass link configurations to component driver for extra intialization */
300 static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
301 struct snd_soc_dai_link *link)
303 if (tplg->comp && tplg->ops && tplg->ops->link_load)
304 return tplg->ops->link_load(tplg->comp, link);
309 /* tell the component driver that all firmware has been loaded in this request */
310 static void soc_tplg_complete(struct soc_tplg *tplg)
312 if (tplg->comp && tplg->ops && tplg->ops->complete)
313 tplg->ops->complete(tplg->comp);
316 /* add a dynamic kcontrol */
317 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
318 const struct snd_kcontrol_new *control_new, const char *prefix,
319 void *data, struct snd_kcontrol **kcontrol)
323 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
324 if (*kcontrol == NULL) {
325 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
330 err = snd_ctl_add(card, *kcontrol);
332 dev_err(dev, "ASoC: Failed to add %s: %d\n",
333 control_new->name, err);
340 /* add a dynamic kcontrol for component driver */
341 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
342 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
344 struct snd_soc_component *comp = tplg->comp;
346 return soc_tplg_add_dcontrol(comp->card->snd_card,
347 comp->dev, k, comp->name_prefix, comp, kcontrol);
350 /* remove a mixer kcontrol */
351 static void remove_mixer(struct snd_soc_component *comp,
352 struct snd_soc_dobj *dobj, int pass)
354 struct snd_card *card = comp->card->snd_card;
355 struct soc_mixer_control *sm =
356 container_of(dobj, struct soc_mixer_control, dobj);
357 const unsigned int *p = NULL;
359 if (pass != SOC_TPLG_PASS_MIXER)
362 if (dobj->ops && dobj->ops->control_unload)
363 dobj->ops->control_unload(comp, dobj);
365 if (sm->dobj.control.kcontrol->tlv.p)
366 p = sm->dobj.control.kcontrol->tlv.p;
367 snd_ctl_remove(card, sm->dobj.control.kcontrol);
368 list_del(&sm->dobj.list);
373 /* remove an enum kcontrol */
374 static void remove_enum(struct snd_soc_component *comp,
375 struct snd_soc_dobj *dobj, int pass)
377 struct snd_card *card = comp->card->snd_card;
378 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
381 if (pass != SOC_TPLG_PASS_MIXER)
384 if (dobj->ops && dobj->ops->control_unload)
385 dobj->ops->control_unload(comp, dobj);
387 snd_ctl_remove(card, se->dobj.control.kcontrol);
388 list_del(&se->dobj.list);
390 kfree(se->dobj.control.dvalues);
391 for (i = 0; i < se->items; i++)
392 kfree(se->dobj.control.dtexts[i]);
396 /* remove a byte kcontrol */
397 static void remove_bytes(struct snd_soc_component *comp,
398 struct snd_soc_dobj *dobj, int pass)
400 struct snd_card *card = comp->card->snd_card;
401 struct soc_bytes_ext *sb =
402 container_of(dobj, struct soc_bytes_ext, 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, sb->dobj.control.kcontrol);
411 list_del(&sb->dobj.list);
415 /* remove a widget and it's kcontrols - routes must be removed first */
416 static void remove_widget(struct snd_soc_component *comp,
417 struct snd_soc_dobj *dobj, int pass)
419 struct snd_card *card = comp->card->snd_card;
420 struct snd_soc_dapm_widget *w =
421 container_of(dobj, struct snd_soc_dapm_widget, dobj);
424 if (pass != SOC_TPLG_PASS_WIDGET)
427 if (dobj->ops && dobj->ops->widget_unload)
428 dobj->ops->widget_unload(comp, dobj);
431 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
432 * The enum may either have an array of values or strings.
434 if (dobj->widget.kcontrol_enum) {
435 /* enumerated widget mixer */
436 struct soc_enum *se =
437 (struct soc_enum *)w->kcontrols[0]->private_value;
439 snd_ctl_remove(card, w->kcontrols[0]);
441 kfree(se->dobj.control.dvalues);
442 for (i = 0; i < se->items; i++)
443 kfree(se->dobj.control.dtexts[i]);
446 kfree(w->kcontrol_news);
448 /* non enumerated widget mixer */
449 for (i = 0; i < w->num_kcontrols; i++) {
450 struct snd_kcontrol *kcontrol = w->kcontrols[i];
451 struct soc_mixer_control *sm =
452 (struct soc_mixer_control *) kcontrol->private_value;
454 kfree(w->kcontrols[i]->tlv.p);
456 snd_ctl_remove(card, w->kcontrols[i]);
459 kfree(w->kcontrol_news);
461 /* widget w is freed by soc-dapm.c */
464 /* remove DAI configurations */
465 static void remove_dai(struct snd_soc_component *comp,
466 struct snd_soc_dobj *dobj, int pass)
468 struct snd_soc_dai_driver *dai_drv =
469 container_of(dobj, struct snd_soc_dai_driver, dobj);
471 if (pass != SOC_TPLG_PASS_PCM_DAI)
474 if (dobj->ops && dobj->ops->dai_unload)
475 dobj->ops->dai_unload(comp, dobj);
477 list_del(&dobj->list);
481 /* remove link configurations */
482 static void remove_link(struct snd_soc_component *comp,
483 struct snd_soc_dobj *dobj, int pass)
485 struct snd_soc_dai_link *link =
486 container_of(dobj, struct snd_soc_dai_link, dobj);
488 if (pass != SOC_TPLG_PASS_PCM_DAI)
491 if (dobj->ops && dobj->ops->link_unload)
492 dobj->ops->link_unload(comp, dobj);
494 list_del(&dobj->list);
495 snd_soc_remove_dai_link(comp->card, link);
499 /* bind a kcontrol to it's IO handlers */
500 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
501 struct snd_kcontrol_new *k,
502 const struct soc_tplg *tplg)
504 const struct snd_soc_tplg_kcontrol_ops *ops;
505 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
508 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
509 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
510 && (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ
511 || k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
512 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
513 struct soc_bytes_ext *sbe;
514 struct snd_soc_tplg_bytes_control *be;
516 sbe = (struct soc_bytes_ext *)k->private_value;
517 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
519 /* TLV bytes controls need standard kcontrol info handler,
520 * TLV callback and extended put/get handlers.
522 k->info = snd_soc_bytes_info_ext;
523 k->tlv.c = snd_soc_bytes_tlv_callback;
525 ext_ops = tplg->bytes_ext_ops;
526 num_ops = tplg->bytes_ext_ops_count;
527 for (i = 0; i < num_ops; i++) {
528 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
529 sbe->put = ext_ops[i].put;
530 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
531 sbe->get = ext_ops[i].get;
534 if (sbe->put && sbe->get)
540 /* try and map vendor specific kcontrol handlers first */
542 num_ops = tplg->io_ops_count;
543 for (i = 0; i < num_ops; i++) {
545 if (k->put == NULL && ops[i].id == hdr->ops.put)
547 if (k->get == NULL && ops[i].id == hdr->ops.get)
549 if (k->info == NULL && ops[i].id == hdr->ops.info)
550 k->info = ops[i].info;
553 /* vendor specific handlers found ? */
554 if (k->put && k->get && k->info)
557 /* none found so try standard kcontrol handlers */
559 num_ops = ARRAY_SIZE(io_ops);
560 for (i = 0; i < num_ops; i++) {
562 if (k->put == NULL && ops[i].id == hdr->ops.put)
564 if (k->get == NULL && ops[i].id == hdr->ops.get)
566 if (k->info == NULL && ops[i].id == hdr->ops.info)
567 k->info = ops[i].info;
570 /* standard handlers found ? */
571 if (k->put && k->get && k->info)
574 /* nothing to bind */
578 /* bind a widgets to it's evnt handlers */
579 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
580 const struct snd_soc_tplg_widget_events *events,
581 int num_events, u16 event_type)
587 for (i = 0; i < num_events; i++) {
588 if (event_type == events[i].type) {
590 /* found - so assign event */
591 w->event = events[i].event_handler;
599 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
601 /* optionally pass new dynamic kcontrol to component driver. */
602 static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
603 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
605 if (tplg->comp && tplg->ops && tplg->ops->control_load)
606 return tplg->ops->control_load(tplg->comp, k, hdr);
612 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
613 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
615 unsigned int item_len = 2 * sizeof(unsigned int);
618 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
622 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
625 p[3] = (scale->step & TLV_DB_SCALE_MASK)
626 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
628 kc->tlv.p = (void *)p;
632 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
633 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
635 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
637 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
640 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
642 switch (tplg_tlv->type) {
643 case SNDRV_CTL_TLVT_DB_SCALE:
644 return soc_tplg_create_tlv_db_scale(tplg, kc,
647 /* TODO: add support for other TLV types */
649 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
658 static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
659 struct snd_kcontrol_new *kc)
664 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
667 struct snd_soc_tplg_bytes_control *be;
668 struct soc_bytes_ext *sbe;
669 struct snd_kcontrol_new kc;
672 if (soc_tplg_check_elem_count(tplg,
673 sizeof(struct snd_soc_tplg_bytes_control), count,
674 size, "mixer bytes")) {
675 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
680 for (i = 0; i < count; i++) {
681 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
683 /* validate kcontrol */
684 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
685 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
688 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
692 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
696 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
697 be->hdr.name, be->hdr.access);
699 memset(&kc, 0, sizeof(kc));
700 kc.name = be->hdr.name;
701 kc.private_value = (long)sbe;
702 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
703 kc.access = be->hdr.access;
706 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
707 sbe->dobj.ops = tplg->ops;
708 INIT_LIST_HEAD(&sbe->dobj.list);
710 /* map io handlers */
711 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
713 soc_control_err(tplg, &be->hdr, be->hdr.name);
718 /* pass control to driver for optional further init */
719 err = soc_tplg_init_kcontrol(tplg, &kc,
720 (struct snd_soc_tplg_ctl_hdr *)be);
722 dev_err(tplg->dev, "ASoC: failed to init %s\n",
728 /* register control here */
729 err = soc_tplg_add_kcontrol(tplg, &kc,
730 &sbe->dobj.control.kcontrol);
732 dev_err(tplg->dev, "ASoC: failed to add %s\n",
738 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
744 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
747 struct snd_soc_tplg_mixer_control *mc;
748 struct soc_mixer_control *sm;
749 struct snd_kcontrol_new kc;
752 if (soc_tplg_check_elem_count(tplg,
753 sizeof(struct snd_soc_tplg_mixer_control),
754 count, size, "mixers")) {
756 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
761 for (i = 0; i < count; i++) {
762 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
764 /* validate kcontrol */
765 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
766 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
769 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
772 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
776 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
777 mc->hdr.name, mc->hdr.access);
779 memset(&kc, 0, sizeof(kc));
780 kc.name = mc->hdr.name;
781 kc.private_value = (long)sm;
782 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
783 kc.access = mc->hdr.access;
785 /* we only support FL/FR channel mapping atm */
786 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
788 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
790 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
792 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
797 sm->invert = mc->invert;
798 sm->platform_max = mc->platform_max;
799 sm->dobj.index = tplg->index;
800 sm->dobj.ops = tplg->ops;
801 sm->dobj.type = SND_SOC_DOBJ_MIXER;
802 INIT_LIST_HEAD(&sm->dobj.list);
804 /* map io handlers */
805 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
807 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
812 /* pass control to driver for optional further init */
813 err = soc_tplg_init_kcontrol(tplg, &kc,
814 (struct snd_soc_tplg_ctl_hdr *) mc);
816 dev_err(tplg->dev, "ASoC: failed to init %s\n",
822 /* create any TLV data */
823 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
825 /* register control here */
826 err = soc_tplg_add_kcontrol(tplg, &kc,
827 &sm->dobj.control.kcontrol);
829 dev_err(tplg->dev, "ASoC: failed to add %s\n",
831 soc_tplg_free_tlv(tplg, &kc);
836 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
842 static int soc_tplg_denum_create_texts(struct soc_enum *se,
843 struct snd_soc_tplg_enum_control *ec)
847 se->dobj.control.dtexts =
848 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
849 if (se->dobj.control.dtexts == NULL)
852 for (i = 0; i < ec->items; i++) {
854 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
855 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
860 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
861 if (!se->dobj.control.dtexts[i]) {
870 for (--i; i >= 0; i--)
871 kfree(se->dobj.control.dtexts[i]);
872 kfree(se->dobj.control.dtexts);
876 static int soc_tplg_denum_create_values(struct soc_enum *se,
877 struct snd_soc_tplg_enum_control *ec)
879 if (ec->items > sizeof(*ec->values))
882 se->dobj.control.dvalues = kmemdup(ec->values,
883 ec->items * sizeof(u32),
885 if (!se->dobj.control.dvalues)
891 static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
894 struct snd_soc_tplg_enum_control *ec;
896 struct snd_kcontrol_new kc;
899 if (soc_tplg_check_elem_count(tplg,
900 sizeof(struct snd_soc_tplg_enum_control),
901 count, size, "enums")) {
903 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
908 for (i = 0; i < count; i++) {
909 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
910 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
913 /* validate kcontrol */
914 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
915 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
918 se = kzalloc((sizeof(*se)), GFP_KERNEL);
922 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
923 ec->hdr.name, ec->items);
925 memset(&kc, 0, sizeof(kc));
926 kc.name = ec->hdr.name;
927 kc.private_value = (long)se;
928 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
929 kc.access = ec->hdr.access;
931 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
932 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
934 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
937 se->items = ec->items;
939 se->dobj.index = tplg->index;
940 se->dobj.type = SND_SOC_DOBJ_ENUM;
941 se->dobj.ops = tplg->ops;
942 INIT_LIST_HEAD(&se->dobj.list);
944 switch (ec->hdr.ops.info) {
945 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
946 case SND_SOC_TPLG_CTL_ENUM_VALUE:
947 err = soc_tplg_denum_create_values(se, ec);
950 "ASoC: could not create values for %s\n",
955 /* fall through and create texts */
956 case SND_SOC_TPLG_CTL_ENUM:
957 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
958 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
959 err = soc_tplg_denum_create_texts(se, ec);
962 "ASoC: could not create texts for %s\n",
970 "ASoC: invalid enum control type %d for %s\n",
971 ec->hdr.ops.info, ec->hdr.name);
976 /* map io handlers */
977 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
979 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
984 /* pass control to driver for optional further init */
985 err = soc_tplg_init_kcontrol(tplg, &kc,
986 (struct snd_soc_tplg_ctl_hdr *) ec);
988 dev_err(tplg->dev, "ASoC: failed to init %s\n",
994 /* register control here */
995 ret = soc_tplg_add_kcontrol(tplg,
996 &kc, &se->dobj.control.kcontrol);
998 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1004 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1010 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1011 struct snd_soc_tplg_hdr *hdr)
1013 struct snd_soc_tplg_ctl_hdr *control_hdr;
1016 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1017 tplg->pos += hdr->size + hdr->payload_size;
1021 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1022 soc_tplg_get_offset(tplg));
1024 for (i = 0; i < hdr->count; i++) {
1026 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1028 if (control_hdr->size != sizeof(*control_hdr)) {
1029 dev_err(tplg->dev, "ASoC: invalid control size\n");
1033 switch (control_hdr->ops.info) {
1034 case SND_SOC_TPLG_CTL_VOLSW:
1035 case SND_SOC_TPLG_CTL_STROBE:
1036 case SND_SOC_TPLG_CTL_VOLSW_SX:
1037 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1038 case SND_SOC_TPLG_CTL_RANGE:
1039 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1040 case SND_SOC_TPLG_DAPM_CTL_PIN:
1041 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1043 case SND_SOC_TPLG_CTL_ENUM:
1044 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1045 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1046 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1047 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1048 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1050 case SND_SOC_TPLG_CTL_BYTES:
1051 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1054 soc_bind_err(tplg, control_hdr, i);
1062 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1063 struct snd_soc_tplg_hdr *hdr)
1065 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1066 struct snd_soc_dapm_route route;
1067 struct snd_soc_tplg_dapm_graph_elem *elem;
1068 int count = hdr->count, i;
1070 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1071 tplg->pos += hdr->size + hdr->payload_size;
1075 if (soc_tplg_check_elem_count(tplg,
1076 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1077 count, hdr->payload_size, "graph")) {
1079 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1084 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1086 for (i = 0; i < count; i++) {
1087 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1088 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1090 /* validate routes */
1091 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1092 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1094 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1095 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1097 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1098 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1101 route.source = elem->source;
1102 route.sink = elem->sink;
1103 route.connected = NULL; /* set to NULL atm for tplg users */
1104 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1105 route.control = NULL;
1107 route.control = elem->control;
1109 /* add route, but keep going if some fail */
1110 snd_soc_dapm_add_routes(dapm, &route, 1);
1116 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1117 struct soc_tplg *tplg, int num_kcontrols)
1119 struct snd_kcontrol_new *kc;
1120 struct soc_mixer_control *sm;
1121 struct snd_soc_tplg_mixer_control *mc;
1124 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1128 for (i = 0; i < num_kcontrols; i++) {
1129 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1130 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1134 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1137 /* validate kcontrol */
1138 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1139 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1142 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1145 kc[i].name = mc->hdr.name;
1146 kc[i].private_value = (long)sm;
1147 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1148 kc[i].access = mc->hdr.access;
1150 /* we only support FL/FR channel mapping atm */
1151 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1153 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1155 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1157 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1162 sm->invert = mc->invert;
1163 sm->platform_max = mc->platform_max;
1164 sm->dobj.index = tplg->index;
1165 INIT_LIST_HEAD(&sm->dobj.list);
1167 /* map io handlers */
1168 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1170 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1175 /* pass control to driver for optional further init */
1176 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1177 (struct snd_soc_tplg_ctl_hdr *)mc);
1179 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1185 /* create any TLV data */
1186 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1193 for (--i; i >= 0; i--)
1194 kfree((void *)kc[i].private_value);
1199 static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1200 struct soc_tplg *tplg)
1202 struct snd_kcontrol_new *kc;
1203 struct snd_soc_tplg_enum_control *ec;
1204 struct soc_enum *se;
1207 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1208 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1211 /* validate kcontrol */
1212 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1213 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1216 kc = kzalloc(sizeof(*kc), GFP_KERNEL);
1220 se = kzalloc(sizeof(*se), GFP_KERNEL);
1224 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1227 kc->name = ec->hdr.name;
1228 kc->private_value = (long)se;
1229 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1230 kc->access = ec->hdr.access;
1232 /* we only support FL/FR channel mapping atm */
1233 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1234 se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
1235 se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
1237 se->items = ec->items;
1238 se->mask = ec->mask;
1239 se->dobj.index = tplg->index;
1241 switch (ec->hdr.ops.info) {
1242 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1243 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1244 err = soc_tplg_denum_create_values(se, ec);
1246 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1250 /* fall through to create texts */
1251 case SND_SOC_TPLG_CTL_ENUM:
1252 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1253 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1254 err = soc_tplg_denum_create_texts(se, ec);
1256 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1262 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1263 ec->hdr.ops.info, ec->hdr.name);
1267 /* map io handlers */
1268 err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
1270 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1274 /* pass control to driver for optional further init */
1275 err = soc_tplg_init_kcontrol(tplg, kc,
1276 (struct snd_soc_tplg_ctl_hdr *)ec);
1278 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1286 /* free values and texts */
1287 kfree(se->dobj.control.dvalues);
1288 for (i = 0; i < ec->items; i++)
1289 kfree(se->dobj.control.dtexts[i]);
1298 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1299 struct soc_tplg *tplg, int count)
1301 struct snd_soc_tplg_bytes_control *be;
1302 struct soc_bytes_ext *sbe;
1303 struct snd_kcontrol_new *kc;
1306 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
1310 for (i = 0; i < count; i++) {
1311 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1313 /* validate kcontrol */
1314 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1315 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1318 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1322 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1326 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1327 be->hdr.name, be->hdr.access);
1329 kc[i].name = be->hdr.name;
1330 kc[i].private_value = (long)sbe;
1331 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1332 kc[i].access = be->hdr.access;
1335 INIT_LIST_HEAD(&sbe->dobj.list);
1337 /* map standard io handlers and check for external handlers */
1338 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1340 soc_control_err(tplg, &be->hdr, be->hdr.name);
1345 /* pass control to driver for optional further init */
1346 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1347 (struct snd_soc_tplg_ctl_hdr *)be);
1349 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1359 for (--i; i >= 0; i--)
1360 kfree((void *)kc[i].private_value);
1366 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1367 struct snd_soc_tplg_dapm_widget *w)
1369 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1370 struct snd_soc_dapm_widget template, *widget;
1371 struct snd_soc_tplg_ctl_hdr *control_hdr;
1372 struct snd_soc_card *card = tplg->comp->card;
1375 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1376 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1378 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1379 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1382 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1385 memset(&template, 0, sizeof(template));
1387 /* map user to kernel widget ID */
1388 template.id = get_widget_id(w->id);
1389 if (template.id < 0)
1392 template.name = kstrdup(w->name, GFP_KERNEL);
1395 template.sname = kstrdup(w->sname, GFP_KERNEL);
1396 if (!template.sname) {
1400 template.reg = w->reg;
1401 template.shift = w->shift;
1402 template.mask = w->mask;
1403 template.subseq = w->subseq;
1404 template.on_val = w->invert ? 0 : 1;
1405 template.off_val = w->invert ? 1 : 0;
1406 template.ignore_suspend = w->ignore_suspend;
1407 template.event_flags = w->event_flags;
1408 template.dobj.index = tplg->index;
1411 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1412 if (w->num_kcontrols == 0) {
1413 template.num_kcontrols = 0;
1417 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1418 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1419 w->name, w->num_kcontrols, control_hdr->type);
1421 switch (control_hdr->ops.info) {
1422 case SND_SOC_TPLG_CTL_VOLSW:
1423 case SND_SOC_TPLG_CTL_STROBE:
1424 case SND_SOC_TPLG_CTL_VOLSW_SX:
1425 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1426 case SND_SOC_TPLG_CTL_RANGE:
1427 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1428 template.num_kcontrols = w->num_kcontrols;
1429 template.kcontrol_news =
1430 soc_tplg_dapm_widget_dmixer_create(tplg,
1431 template.num_kcontrols);
1432 if (!template.kcontrol_news) {
1437 case SND_SOC_TPLG_CTL_ENUM:
1438 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1439 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1440 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1441 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1442 template.dobj.widget.kcontrol_enum = 1;
1443 template.num_kcontrols = 1;
1444 template.kcontrol_news =
1445 soc_tplg_dapm_widget_denum_create(tplg);
1446 if (!template.kcontrol_news) {
1451 case SND_SOC_TPLG_CTL_BYTES:
1452 template.num_kcontrols = w->num_kcontrols;
1453 template.kcontrol_news =
1454 soc_tplg_dapm_widget_dbytes_create(tplg,
1455 template.num_kcontrols);
1456 if (!template.kcontrol_news) {
1462 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1463 control_hdr->ops.get, control_hdr->ops.put,
1464 control_hdr->ops.info);
1470 ret = soc_tplg_widget_load(tplg, &template, w);
1474 /* card dapm mutex is held by the core if we are loading topology
1475 * data during sound card init. */
1476 if (card->instantiated)
1477 widget = snd_soc_dapm_new_control(dapm, &template);
1479 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1480 if (IS_ERR(widget)) {
1481 ret = PTR_ERR(widget);
1482 /* Do not nag about probe deferrals */
1483 if (ret != -EPROBE_DEFER)
1485 "ASoC: failed to create widget %s controls (%d)\n",
1489 if (widget == NULL) {
1490 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1496 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1497 widget->dobj.ops = tplg->ops;
1498 widget->dobj.index = tplg->index;
1499 kfree(template.sname);
1500 kfree(template.name);
1501 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1505 kfree(template.sname);
1507 kfree(template.name);
1511 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1512 struct snd_soc_tplg_hdr *hdr)
1514 struct snd_soc_tplg_dapm_widget *widget;
1515 int ret, count = hdr->count, i;
1517 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1520 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1522 for (i = 0; i < count; i++) {
1523 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1524 if (widget->size != sizeof(*widget)) {
1525 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1529 ret = soc_tplg_dapm_widget_create(tplg, widget);
1531 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1540 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1542 struct snd_soc_card *card = tplg->comp->card;
1545 /* Card might not have been registered at this point.
1546 * If so, just return success.
1548 if (!card || !card->instantiated) {
1549 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1550 "Do not add new widgets now\n");
1554 ret = snd_soc_dapm_new_widgets(card);
1556 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1562 static void set_stream_info(struct snd_soc_pcm_stream *stream,
1563 struct snd_soc_tplg_stream_caps *caps)
1565 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1566 stream->channels_min = caps->channels_min;
1567 stream->channels_max = caps->channels_max;
1568 stream->rates = caps->rates;
1569 stream->rate_min = caps->rate_min;
1570 stream->rate_max = caps->rate_max;
1571 stream->formats = caps->formats;
1572 stream->sig_bits = caps->sig_bits;
1575 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1576 unsigned int flag_mask, unsigned int flags)
1578 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1579 dai_drv->symmetric_rates =
1580 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1582 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1583 dai_drv->symmetric_channels =
1584 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1587 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1588 dai_drv->symmetric_samplebits =
1589 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1593 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1594 struct snd_soc_tplg_pcm *pcm)
1596 struct snd_soc_dai_driver *dai_drv;
1597 struct snd_soc_pcm_stream *stream;
1598 struct snd_soc_tplg_stream_caps *caps;
1601 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1602 if (dai_drv == NULL)
1605 dai_drv->name = pcm->dai_name;
1606 dai_drv->id = pcm->dai_id;
1608 if (pcm->playback) {
1609 stream = &dai_drv->playback;
1610 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1611 set_stream_info(stream, caps);
1615 stream = &dai_drv->capture;
1616 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1617 set_stream_info(stream, caps);
1620 /* pass control to component driver for optional further init */
1621 ret = soc_tplg_dai_load(tplg, dai_drv);
1623 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1628 dai_drv->dobj.index = tplg->index;
1629 dai_drv->dobj.ops = tplg->ops;
1630 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1631 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1633 /* register the DAI to the component */
1634 return snd_soc_register_dai(tplg->comp, dai_drv);
1637 /* create the FE DAI link */
1638 static int soc_tplg_link_create(struct soc_tplg *tplg,
1639 struct snd_soc_tplg_pcm *pcm)
1641 struct snd_soc_dai_link *link;
1644 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1648 link->name = pcm->pcm_name;
1649 link->stream_name = pcm->pcm_name;
1650 link->id = pcm->pcm_id;
1652 link->cpu_dai_name = pcm->dai_name;
1653 link->codec_name = "snd-soc-dummy";
1654 link->codec_dai_name = "snd-soc-dummy-dai";
1658 link->dpcm_playback = pcm->playback;
1659 link->dpcm_capture = pcm->capture;
1661 /* pass control to component driver for optional further init */
1662 ret = soc_tplg_dai_link_load(tplg, link);
1664 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1669 link->dobj.index = tplg->index;
1670 link->dobj.ops = tplg->ops;
1671 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1672 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1674 snd_soc_add_dai_link(tplg->comp->card, link);
1678 /* create a FE DAI and DAI link from the PCM object */
1679 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1680 struct snd_soc_tplg_pcm *pcm)
1684 ret = soc_tplg_dai_create(tplg, pcm);
1688 return soc_tplg_link_create(tplg, pcm);
1691 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1692 struct snd_soc_tplg_hdr *hdr)
1694 struct snd_soc_tplg_pcm *pcm;
1695 int count = hdr->count;
1698 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1701 if (soc_tplg_check_elem_count(tplg,
1702 sizeof(struct snd_soc_tplg_pcm), count,
1703 hdr->payload_size, "PCM DAI")) {
1704 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1709 /* create the FE DAIs and DAI links */
1710 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1711 for (i = 0; i < count; i++) {
1712 if (pcm->size != sizeof(*pcm)) {
1713 dev_err(tplg->dev, "ASoC: invalid pcm size\n");
1717 soc_tplg_pcm_create(tplg, pcm);
1721 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1722 tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count;
1728 * soc_tplg_be_dai_config - Find and configure an existing BE DAI.
1729 * @tplg: topology context
1730 * @be: topology BE DAI configs.
1732 * The BE dai should already be registered by the platform driver. The
1733 * platform driver should specify the BE DAI name and ID for matching.
1735 static int soc_tplg_be_dai_config(struct soc_tplg *tplg,
1736 struct snd_soc_tplg_be_dai *be)
1738 struct snd_soc_dai_link_component dai_component = {0};
1739 struct snd_soc_dai *dai;
1740 struct snd_soc_dai_driver *dai_drv;
1741 struct snd_soc_pcm_stream *stream;
1742 struct snd_soc_tplg_stream_caps *caps;
1745 dai_component.dai_name = be->dai_name;
1746 dai = snd_soc_find_dai(&dai_component);
1748 dev_err(tplg->dev, "ASoC: BE DAI %s not registered\n",
1753 if (be->dai_id != dai->id) {
1754 dev_err(tplg->dev, "ASoC: BE DAI %s id mismatch\n",
1759 dai_drv = dai->driver;
1764 stream = &dai_drv->playback;
1765 caps = &be->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1766 set_stream_info(stream, caps);
1770 stream = &dai_drv->capture;
1771 caps = &be->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1772 set_stream_info(stream, caps);
1776 set_dai_flags(dai_drv, be->flag_mask, be->flags);
1778 /* pass control to component driver for optional further init */
1779 ret = soc_tplg_dai_load(tplg, dai_drv);
1781 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1788 static int soc_tplg_be_dai_elems_load(struct soc_tplg *tplg,
1789 struct snd_soc_tplg_hdr *hdr)
1791 struct snd_soc_tplg_be_dai *be;
1792 int count = hdr->count;
1795 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
1798 /* config the existing BE DAIs */
1799 for (i = 0; i < count; i++) {
1800 be = (struct snd_soc_tplg_be_dai *)tplg->pos;
1801 if (be->size != sizeof(*be)) {
1802 dev_err(tplg->dev, "ASoC: invalid BE DAI size\n");
1806 soc_tplg_be_dai_config(tplg, be);
1807 tplg->pos += (sizeof(*be) + be->priv.size);
1810 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
1815 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
1816 struct snd_soc_tplg_hdr *hdr)
1818 struct snd_soc_tplg_manifest *manifest;
1820 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
1823 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
1824 if (manifest->size != sizeof(*manifest)) {
1825 dev_err(tplg->dev, "ASoC: invalid manifest size\n");
1829 tplg->pos += sizeof(struct snd_soc_tplg_manifest);
1831 if (tplg->comp && tplg->ops && tplg->ops->manifest)
1832 return tplg->ops->manifest(tplg->comp, manifest);
1834 dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n");
1838 /* validate header magic, size and type */
1839 static int soc_valid_header(struct soc_tplg *tplg,
1840 struct snd_soc_tplg_hdr *hdr)
1842 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
1845 if (hdr->size != sizeof(*hdr)) {
1847 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
1848 hdr->type, soc_tplg_get_hdr_offset(tplg),
1853 /* big endian firmware objects not supported atm */
1854 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
1856 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1857 tplg->pass, hdr->magic,
1858 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1862 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
1864 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1865 tplg->pass, hdr->magic,
1866 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1870 if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
1872 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1873 tplg->pass, hdr->abi,
1874 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
1879 if (hdr->payload_size == 0) {
1880 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
1881 soc_tplg_get_hdr_offset(tplg));
1885 if (tplg->pass == hdr->type)
1887 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
1888 hdr->payload_size, hdr->type, hdr->version,
1889 hdr->vendor_type, tplg->pass);
1894 /* check header type and call appropriate handler */
1895 static int soc_tplg_load_header(struct soc_tplg *tplg,
1896 struct snd_soc_tplg_hdr *hdr)
1898 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
1900 /* check for matching ID */
1901 if (hdr->index != tplg->req_index &&
1902 hdr->index != SND_SOC_TPLG_INDEX_ALL)
1905 tplg->index = hdr->index;
1907 switch (hdr->type) {
1908 case SND_SOC_TPLG_TYPE_MIXER:
1909 case SND_SOC_TPLG_TYPE_ENUM:
1910 case SND_SOC_TPLG_TYPE_BYTES:
1911 return soc_tplg_kcontrol_elems_load(tplg, hdr);
1912 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
1913 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
1914 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
1915 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
1916 case SND_SOC_TPLG_TYPE_PCM:
1917 return soc_tplg_pcm_elems_load(tplg, hdr);
1918 case SND_SOC_TPLG_TYPE_BE_DAI:
1919 return soc_tplg_be_dai_elems_load(tplg, hdr);
1920 case SND_SOC_TPLG_TYPE_MANIFEST:
1921 return soc_tplg_manifest_load(tplg, hdr);
1923 /* bespoke vendor data object */
1924 return soc_tplg_vendor_load(tplg, hdr);
1930 /* process the topology file headers */
1931 static int soc_tplg_process_headers(struct soc_tplg *tplg)
1933 struct snd_soc_tplg_hdr *hdr;
1936 tplg->pass = SOC_TPLG_PASS_START;
1938 /* process the header types from start to end */
1939 while (tplg->pass <= SOC_TPLG_PASS_END) {
1941 tplg->hdr_pos = tplg->fw->data;
1942 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1944 while (!soc_tplg_is_eof(tplg)) {
1946 /* make sure header is valid before loading */
1947 ret = soc_valid_header(tplg, hdr);
1953 /* load the header object */
1954 ret = soc_tplg_load_header(tplg, hdr);
1958 /* goto next header */
1959 tplg->hdr_pos += hdr->payload_size +
1960 sizeof(struct snd_soc_tplg_hdr);
1961 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1964 /* next data type pass */
1968 /* signal DAPM we are complete */
1969 ret = soc_tplg_dapm_complete(tplg);
1972 "ASoC: failed to initialise DAPM from Firmware\n");
1977 static int soc_tplg_load(struct soc_tplg *tplg)
1981 ret = soc_tplg_process_headers(tplg);
1983 soc_tplg_complete(tplg);
1988 /* load audio component topology from "firmware" file */
1989 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
1990 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
1992 struct soc_tplg tplg;
1995 /* setup parsing context */
1996 memset(&tplg, 0, sizeof(tplg));
1998 tplg.dev = comp->dev;
2001 tplg.req_index = id;
2002 tplg.io_ops = ops->io_ops;
2003 tplg.io_ops_count = ops->io_ops_count;
2004 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2005 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2007 ret = soc_tplg_load(&tplg);
2008 /* free the created components if fail to load topology */
2010 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2014 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2016 /* remove this dynamic widget */
2017 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2019 /* make sure we are a widget */
2020 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2023 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2025 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2027 /* remove all dynamic widgets from this DAPM context */
2028 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2031 struct snd_soc_dapm_widget *w, *next_w;
2033 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2035 /* make sure we are a widget with correct context */
2036 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2040 if (w->dobj.index != index &&
2041 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2043 /* check and free and dynamic widget kcontrols */
2044 snd_soc_tplg_widget_remove(w);
2045 snd_soc_dapm_free_widget(w);
2047 snd_soc_dapm_reset_cache(dapm);
2049 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2051 /* remove dynamic controls from the component driver */
2052 int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2054 struct snd_card *card = comp->card->snd_card;
2055 struct snd_soc_dobj *dobj, *next_dobj;
2056 int pass = SOC_TPLG_PASS_END;
2058 /* process the header types from end to start */
2059 while (pass >= SOC_TPLG_PASS_START) {
2061 /* remove mixer controls */
2062 down_write(&card->controls_rwsem);
2063 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2067 if (dobj->index != index &&
2068 dobj->index != SND_SOC_TPLG_INDEX_ALL)
2071 switch (dobj->type) {
2072 case SND_SOC_DOBJ_MIXER:
2073 remove_mixer(comp, dobj, pass);
2075 case SND_SOC_DOBJ_ENUM:
2076 remove_enum(comp, dobj, pass);
2078 case SND_SOC_DOBJ_BYTES:
2079 remove_bytes(comp, dobj, pass);
2081 case SND_SOC_DOBJ_WIDGET:
2082 remove_widget(comp, dobj, pass);
2084 case SND_SOC_DOBJ_PCM:
2085 remove_dai(comp, dobj, pass);
2087 case SND_SOC_DOBJ_DAI_LINK:
2088 remove_link(comp, dobj, pass);
2091 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2096 up_write(&card->controls_rwsem);
2100 /* let caller know if FW can be freed when no objects are left */
2101 return !list_empty(&comp->dobj_list);
2103 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);