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
52 #define SOC_TPLG_PASS_LINK 8
54 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
55 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
58 * Old version of ABI structs, supported for backward compatibility.
62 struct snd_soc_tplg_manifest_v4 {
63 __le32 size; /* in bytes of this structure */
64 __le32 control_elems; /* number of control elements */
65 __le32 widget_elems; /* number of widget elements */
66 __le32 graph_elems; /* number of graph elements */
67 __le32 pcm_elems; /* number of PCM elements */
68 __le32 dai_link_elems; /* number of DAI link elements */
69 struct snd_soc_tplg_private priv;
72 /* Stream Capabilities v4 */
73 struct snd_soc_tplg_stream_caps_v4 {
74 __le32 size; /* in bytes of this structure */
75 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
76 __le64 formats; /* supported formats SNDRV_PCM_FMTBIT_* */
77 __le32 rates; /* supported rates SNDRV_PCM_RATE_* */
78 __le32 rate_min; /* min rate */
79 __le32 rate_max; /* max rate */
80 __le32 channels_min; /* min channels */
81 __le32 channels_max; /* max channels */
82 __le32 periods_min; /* min number of periods */
83 __le32 periods_max; /* max number of periods */
84 __le32 period_size_min; /* min period size bytes */
85 __le32 period_size_max; /* max period size bytes */
86 __le32 buffer_size_min; /* min buffer size bytes */
87 __le32 buffer_size_max; /* max buffer size bytes */
91 struct snd_soc_tplg_pcm_v4 {
92 __le32 size; /* in bytes of this structure */
93 char pcm_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
94 char dai_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
95 __le32 pcm_id; /* unique ID - used to match with DAI link */
96 __le32 dai_id; /* unique ID - used to match */
97 __le32 playback; /* supports playback mode */
98 __le32 capture; /* supports capture mode */
99 __le32 compress; /* 1 = compressed; 0 = PCM */
100 struct snd_soc_tplg_stream stream[SND_SOC_TPLG_STREAM_CONFIG_MAX]; /* for DAI link */
101 __le32 num_streams; /* number of streams */
102 struct snd_soc_tplg_stream_caps_v4 caps[2]; /* playback and capture for DAI */
105 /* Physical link config v4 */
106 struct snd_soc_tplg_link_config_v4 {
107 __le32 size; /* in bytes of this structure */
108 __le32 id; /* unique ID - used to match */
109 struct snd_soc_tplg_stream stream[SND_SOC_TPLG_STREAM_CONFIG_MAX]; /* supported configs playback and captrure */
110 __le32 num_streams; /* number of streams */
113 /* topology context */
115 const struct firmware *fw;
117 /* runtime FW parsing */
118 const u8 *pos; /* read postion */
119 const u8 *hdr_pos; /* header position */
120 unsigned int pass; /* pass number */
122 /* component caller */
124 struct snd_soc_component *comp;
125 u32 index; /* current block index */
126 u32 req_index; /* required index, only loaded/free matching blocks */
128 /* vendor specific kcontrol operations */
129 const struct snd_soc_tplg_kcontrol_ops *io_ops;
132 /* vendor specific bytes ext handlers, for TLV bytes controls */
133 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
134 int bytes_ext_ops_count;
136 /* optional fw loading callbacks to component drivers */
137 struct snd_soc_tplg_ops *ops;
140 static int soc_tplg_process_headers(struct soc_tplg *tplg);
141 static void soc_tplg_complete(struct soc_tplg *tplg);
142 struct snd_soc_dapm_widget *
143 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
144 const struct snd_soc_dapm_widget *widget);
145 struct snd_soc_dapm_widget *
146 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
147 const struct snd_soc_dapm_widget *widget);
149 /* check we dont overflow the data for this control chunk */
150 static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
151 unsigned int count, size_t bytes, const char *elem_type)
153 const u8 *end = tplg->pos + elem_size * count;
155 if (end > tplg->fw->data + tplg->fw->size) {
156 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
161 /* check there is enough room in chunk for control.
162 extra bytes at the end of control are for vendor data here */
163 if (elem_size * count > bytes) {
165 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
166 elem_type, count, elem_size, bytes);
173 static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
175 const u8 *end = tplg->hdr_pos;
177 if (end >= tplg->fw->data + tplg->fw->size)
182 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
184 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
187 static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
189 return (unsigned long)(tplg->pos - tplg->fw->data);
192 /* mapping of Kcontrol types and associated operations. */
193 static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
194 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
195 snd_soc_put_volsw, snd_soc_info_volsw},
196 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
197 snd_soc_put_volsw_sx, NULL},
198 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
199 snd_soc_put_enum_double, snd_soc_info_enum_double},
200 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
201 snd_soc_put_enum_double, NULL},
202 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
203 snd_soc_bytes_put, snd_soc_bytes_info},
204 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
205 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
206 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
207 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
208 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
209 snd_soc_put_strobe, NULL},
210 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
211 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
212 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
213 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
214 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
215 snd_soc_dapm_put_enum_double, NULL},
216 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
217 snd_soc_dapm_put_enum_double, NULL},
218 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
219 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
222 struct soc_tplg_map {
227 /* mapping of widget types from UAPI IDs to kernel IDs */
228 static const struct soc_tplg_map dapm_map[] = {
229 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
230 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
231 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
232 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
233 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
234 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
235 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
236 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
237 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
238 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
239 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
240 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
241 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
242 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
243 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
244 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
247 static int tplc_chan_get_reg(struct soc_tplg *tplg,
248 struct snd_soc_tplg_channel *chan, int map)
252 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
253 if (chan[i].id == map)
260 static int tplc_chan_get_shift(struct soc_tplg *tplg,
261 struct snd_soc_tplg_channel *chan, int map)
265 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
266 if (chan[i].id == map)
267 return chan[i].shift;
273 static int get_widget_id(int tplg_type)
277 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
278 if (tplg_type == dapm_map[i].uid)
279 return dapm_map[i].kid;
285 static inline void soc_bind_err(struct soc_tplg *tplg,
286 struct snd_soc_tplg_ctl_hdr *hdr, int index)
289 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
290 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
291 soc_tplg_get_offset(tplg));
294 static inline void soc_control_err(struct soc_tplg *tplg,
295 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
298 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
299 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
300 soc_tplg_get_offset(tplg));
303 /* pass vendor data to component driver for processing */
304 static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
305 struct snd_soc_tplg_hdr *hdr)
309 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
310 ret = tplg->ops->vendor_load(tplg->comp, hdr);
312 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
319 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
320 soc_tplg_get_hdr_offset(tplg),
321 soc_tplg_get_hdr_offset(tplg),
322 hdr->type, hdr->vendor_type);
326 /* pass vendor data to component driver for processing */
327 static int soc_tplg_vendor_load(struct soc_tplg *tplg,
328 struct snd_soc_tplg_hdr *hdr)
330 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
333 return soc_tplg_vendor_load_(tplg, hdr);
336 /* optionally pass new dynamic widget to component driver. This is mainly for
337 * external widgets where we can assign private data/ops */
338 static int soc_tplg_widget_load(struct soc_tplg *tplg,
339 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
341 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
342 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
347 /* optionally pass new dynamic widget to component driver. This is mainly for
348 * external widgets where we can assign private data/ops */
349 static int soc_tplg_widget_ready(struct soc_tplg *tplg,
350 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
352 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
353 return tplg->ops->widget_ready(tplg->comp, w, tplg_w);
358 /* pass DAI configurations to component driver for extra initialization */
359 static int soc_tplg_dai_load(struct soc_tplg *tplg,
360 struct snd_soc_dai_driver *dai_drv,
361 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
363 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
364 return tplg->ops->dai_load(tplg->comp, dai_drv, pcm, dai);
369 /* pass link configurations to component driver for extra initialization */
370 static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
371 struct snd_soc_dai_link *link)
373 if (tplg->comp && tplg->ops && tplg->ops->link_load)
374 return tplg->ops->link_load(tplg->comp, link);
379 /* tell the component driver that all firmware has been loaded in this request */
380 static void soc_tplg_complete(struct soc_tplg *tplg)
382 if (tplg->comp && tplg->ops && tplg->ops->complete)
383 tplg->ops->complete(tplg->comp);
386 /* add a dynamic kcontrol */
387 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
388 const struct snd_kcontrol_new *control_new, const char *prefix,
389 void *data, struct snd_kcontrol **kcontrol)
393 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
394 if (*kcontrol == NULL) {
395 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
400 err = snd_ctl_add(card, *kcontrol);
402 dev_err(dev, "ASoC: Failed to add %s: %d\n",
403 control_new->name, err);
410 /* add a dynamic kcontrol for component driver */
411 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
412 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
414 struct snd_soc_component *comp = tplg->comp;
416 return soc_tplg_add_dcontrol(comp->card->snd_card,
417 comp->dev, k, NULL, comp, kcontrol);
420 /* remove a mixer kcontrol */
421 static void remove_mixer(struct snd_soc_component *comp,
422 struct snd_soc_dobj *dobj, int pass)
424 struct snd_card *card = comp->card->snd_card;
425 struct soc_mixer_control *sm =
426 container_of(dobj, struct soc_mixer_control, dobj);
427 const unsigned int *p = NULL;
429 if (pass != SOC_TPLG_PASS_MIXER)
432 if (dobj->ops && dobj->ops->control_unload)
433 dobj->ops->control_unload(comp, dobj);
435 if (sm->dobj.control.kcontrol->tlv.p)
436 p = sm->dobj.control.kcontrol->tlv.p;
437 snd_ctl_remove(card, sm->dobj.control.kcontrol);
438 list_del(&sm->dobj.list);
443 /* remove an enum kcontrol */
444 static void remove_enum(struct snd_soc_component *comp,
445 struct snd_soc_dobj *dobj, int pass)
447 struct snd_card *card = comp->card->snd_card;
448 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
451 if (pass != SOC_TPLG_PASS_MIXER)
454 if (dobj->ops && dobj->ops->control_unload)
455 dobj->ops->control_unload(comp, dobj);
457 snd_ctl_remove(card, se->dobj.control.kcontrol);
458 list_del(&se->dobj.list);
460 kfree(se->dobj.control.dvalues);
461 for (i = 0; i < se->items; i++)
462 kfree(se->dobj.control.dtexts[i]);
466 /* remove a byte kcontrol */
467 static void remove_bytes(struct snd_soc_component *comp,
468 struct snd_soc_dobj *dobj, int pass)
470 struct snd_card *card = comp->card->snd_card;
471 struct soc_bytes_ext *sb =
472 container_of(dobj, struct soc_bytes_ext, dobj);
474 if (pass != SOC_TPLG_PASS_MIXER)
477 if (dobj->ops && dobj->ops->control_unload)
478 dobj->ops->control_unload(comp, dobj);
480 snd_ctl_remove(card, sb->dobj.control.kcontrol);
481 list_del(&sb->dobj.list);
485 /* remove a widget and it's kcontrols - routes must be removed first */
486 static void remove_widget(struct snd_soc_component *comp,
487 struct snd_soc_dobj *dobj, int pass)
489 struct snd_card *card = comp->card->snd_card;
490 struct snd_soc_dapm_widget *w =
491 container_of(dobj, struct snd_soc_dapm_widget, dobj);
494 if (pass != SOC_TPLG_PASS_WIDGET)
497 if (dobj->ops && dobj->ops->widget_unload)
498 dobj->ops->widget_unload(comp, dobj);
501 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
502 * The enum may either have an array of values or strings.
504 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
505 /* enumerated widget mixer */
506 for (i = 0; i < w->num_kcontrols; i++) {
507 struct snd_kcontrol *kcontrol = w->kcontrols[i];
508 struct soc_enum *se =
509 (struct soc_enum *)kcontrol->private_value;
512 snd_ctl_remove(card, kcontrol);
514 kfree(se->dobj.control.dvalues);
515 for (j = 0; j < se->items; j++)
516 kfree(se->dobj.control.dtexts[j]);
520 kfree(w->kcontrol_news);
522 /* volume mixer or bytes controls */
523 for (i = 0; i < w->num_kcontrols; i++) {
524 struct snd_kcontrol *kcontrol = w->kcontrols[i];
526 if (dobj->widget.kcontrol_type
527 == SND_SOC_TPLG_TYPE_MIXER)
528 kfree(kcontrol->tlv.p);
530 /* Private value is used as struct soc_mixer_control
531 * for volume mixers or soc_bytes_ext for bytes
534 kfree((void *)kcontrol->private_value);
535 snd_ctl_remove(card, kcontrol);
537 kfree(w->kcontrol_news);
539 /* widget w is freed by soc-dapm.c */
542 /* remove DAI configurations */
543 static void remove_dai(struct snd_soc_component *comp,
544 struct snd_soc_dobj *dobj, int pass)
546 struct snd_soc_dai_driver *dai_drv =
547 container_of(dobj, struct snd_soc_dai_driver, dobj);
549 if (pass != SOC_TPLG_PASS_PCM_DAI)
552 if (dobj->ops && dobj->ops->dai_unload)
553 dobj->ops->dai_unload(comp, dobj);
555 kfree(dai_drv->name);
556 list_del(&dobj->list);
560 /* remove link configurations */
561 static void remove_link(struct snd_soc_component *comp,
562 struct snd_soc_dobj *dobj, int pass)
564 struct snd_soc_dai_link *link =
565 container_of(dobj, struct snd_soc_dai_link, dobj);
567 if (pass != SOC_TPLG_PASS_PCM_DAI)
570 if (dobj->ops && dobj->ops->link_unload)
571 dobj->ops->link_unload(comp, dobj);
574 kfree(link->stream_name);
575 kfree(link->cpu_dai_name);
577 list_del(&dobj->list);
578 snd_soc_remove_dai_link(comp->card, link);
582 /* bind a kcontrol to it's IO handlers */
583 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
584 struct snd_kcontrol_new *k,
585 const struct soc_tplg *tplg)
587 const struct snd_soc_tplg_kcontrol_ops *ops;
588 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
591 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
592 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
593 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
594 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
595 struct soc_bytes_ext *sbe;
596 struct snd_soc_tplg_bytes_control *be;
598 sbe = (struct soc_bytes_ext *)k->private_value;
599 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
601 /* TLV bytes controls need standard kcontrol info handler,
602 * TLV callback and extended put/get handlers.
604 k->info = snd_soc_bytes_info_ext;
605 k->tlv.c = snd_soc_bytes_tlv_callback;
607 ext_ops = tplg->bytes_ext_ops;
608 num_ops = tplg->bytes_ext_ops_count;
609 for (i = 0; i < num_ops; i++) {
610 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
611 sbe->put = ext_ops[i].put;
612 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
613 sbe->get = ext_ops[i].get;
616 if (sbe->put && sbe->get)
622 /* try and map vendor specific kcontrol handlers first */
624 num_ops = tplg->io_ops_count;
625 for (i = 0; i < num_ops; i++) {
627 if (k->put == NULL && ops[i].id == hdr->ops.put)
629 if (k->get == NULL && ops[i].id == hdr->ops.get)
631 if (k->info == NULL && ops[i].id == hdr->ops.info)
632 k->info = ops[i].info;
635 /* vendor specific handlers found ? */
636 if (k->put && k->get && k->info)
639 /* none found so try standard kcontrol handlers */
641 num_ops = ARRAY_SIZE(io_ops);
642 for (i = 0; i < num_ops; i++) {
644 if (k->put == NULL && ops[i].id == hdr->ops.put)
646 if (k->get == NULL && ops[i].id == hdr->ops.get)
648 if (k->info == NULL && ops[i].id == hdr->ops.info)
649 k->info = ops[i].info;
652 /* standard handlers found ? */
653 if (k->put && k->get && k->info)
656 /* nothing to bind */
660 /* bind a widgets to it's evnt handlers */
661 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
662 const struct snd_soc_tplg_widget_events *events,
663 int num_events, u16 event_type)
669 for (i = 0; i < num_events; i++) {
670 if (event_type == events[i].type) {
672 /* found - so assign event */
673 w->event = events[i].event_handler;
681 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
683 /* optionally pass new dynamic kcontrol to component driver. */
684 static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
685 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
687 if (tplg->comp && tplg->ops && tplg->ops->control_load)
688 return tplg->ops->control_load(tplg->comp, k, hdr);
694 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
695 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
697 unsigned int item_len = 2 * sizeof(unsigned int);
700 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
704 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
707 p[3] = (scale->step & TLV_DB_SCALE_MASK)
708 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
710 kc->tlv.p = (void *)p;
714 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
715 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
717 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
719 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
722 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
724 switch (tplg_tlv->type) {
725 case SNDRV_CTL_TLVT_DB_SCALE:
726 return soc_tplg_create_tlv_db_scale(tplg, kc,
729 /* TODO: add support for other TLV types */
731 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
740 static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
741 struct snd_kcontrol_new *kc)
746 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
749 struct snd_soc_tplg_bytes_control *be;
750 struct soc_bytes_ext *sbe;
751 struct snd_kcontrol_new kc;
754 if (soc_tplg_check_elem_count(tplg,
755 sizeof(struct snd_soc_tplg_bytes_control), count,
756 size, "mixer bytes")) {
757 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
762 for (i = 0; i < count; i++) {
763 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
765 /* validate kcontrol */
766 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
767 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
770 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
774 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
778 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
779 be->hdr.name, be->hdr.access);
781 memset(&kc, 0, sizeof(kc));
782 kc.name = be->hdr.name;
783 kc.private_value = (long)sbe;
784 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
785 kc.access = be->hdr.access;
788 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
789 sbe->dobj.ops = tplg->ops;
790 INIT_LIST_HEAD(&sbe->dobj.list);
792 /* map io handlers */
793 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
795 soc_control_err(tplg, &be->hdr, be->hdr.name);
800 /* pass control to driver for optional further init */
801 err = soc_tplg_init_kcontrol(tplg, &kc,
802 (struct snd_soc_tplg_ctl_hdr *)be);
804 dev_err(tplg->dev, "ASoC: failed to init %s\n",
810 /* register control here */
811 err = soc_tplg_add_kcontrol(tplg, &kc,
812 &sbe->dobj.control.kcontrol);
814 dev_err(tplg->dev, "ASoC: failed to add %s\n",
820 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
826 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
829 struct snd_soc_tplg_mixer_control *mc;
830 struct soc_mixer_control *sm;
831 struct snd_kcontrol_new kc;
834 if (soc_tplg_check_elem_count(tplg,
835 sizeof(struct snd_soc_tplg_mixer_control),
836 count, size, "mixers")) {
838 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
843 for (i = 0; i < count; i++) {
844 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
846 /* validate kcontrol */
847 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
848 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
851 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
854 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
858 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
859 mc->hdr.name, mc->hdr.access);
861 memset(&kc, 0, sizeof(kc));
862 kc.name = mc->hdr.name;
863 kc.private_value = (long)sm;
864 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
865 kc.access = mc->hdr.access;
867 /* we only support FL/FR channel mapping atm */
868 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
870 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
872 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
874 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
879 sm->invert = mc->invert;
880 sm->platform_max = mc->platform_max;
881 sm->dobj.index = tplg->index;
882 sm->dobj.ops = tplg->ops;
883 sm->dobj.type = SND_SOC_DOBJ_MIXER;
884 INIT_LIST_HEAD(&sm->dobj.list);
886 /* map io handlers */
887 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
889 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
894 /* pass control to driver for optional further init */
895 err = soc_tplg_init_kcontrol(tplg, &kc,
896 (struct snd_soc_tplg_ctl_hdr *) mc);
898 dev_err(tplg->dev, "ASoC: failed to init %s\n",
904 /* create any TLV data */
905 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
907 /* register control here */
908 err = soc_tplg_add_kcontrol(tplg, &kc,
909 &sm->dobj.control.kcontrol);
911 dev_err(tplg->dev, "ASoC: failed to add %s\n",
913 soc_tplg_free_tlv(tplg, &kc);
918 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
924 static int soc_tplg_denum_create_texts(struct soc_enum *se,
925 struct snd_soc_tplg_enum_control *ec)
929 se->dobj.control.dtexts =
930 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
931 if (se->dobj.control.dtexts == NULL)
934 for (i = 0; i < ec->items; i++) {
936 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
937 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
942 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
943 if (!se->dobj.control.dtexts[i]) {
949 se->texts = (const char * const *)se->dobj.control.dtexts;
953 for (--i; i >= 0; i--)
954 kfree(se->dobj.control.dtexts[i]);
955 kfree(se->dobj.control.dtexts);
959 static int soc_tplg_denum_create_values(struct soc_enum *se,
960 struct snd_soc_tplg_enum_control *ec)
962 if (ec->items > sizeof(*ec->values))
965 se->dobj.control.dvalues = kmemdup(ec->values,
966 ec->items * sizeof(u32),
968 if (!se->dobj.control.dvalues)
974 static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
977 struct snd_soc_tplg_enum_control *ec;
979 struct snd_kcontrol_new kc;
982 if (soc_tplg_check_elem_count(tplg,
983 sizeof(struct snd_soc_tplg_enum_control),
984 count, size, "enums")) {
986 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
991 for (i = 0; i < count; i++) {
992 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
993 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
996 /* validate kcontrol */
997 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
998 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1001 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1005 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1006 ec->hdr.name, ec->items);
1008 memset(&kc, 0, sizeof(kc));
1009 kc.name = ec->hdr.name;
1010 kc.private_value = (long)se;
1011 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1012 kc.access = ec->hdr.access;
1014 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1015 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1017 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1020 se->items = ec->items;
1021 se->mask = ec->mask;
1022 se->dobj.index = tplg->index;
1023 se->dobj.type = SND_SOC_DOBJ_ENUM;
1024 se->dobj.ops = tplg->ops;
1025 INIT_LIST_HEAD(&se->dobj.list);
1027 switch (ec->hdr.ops.info) {
1028 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1029 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1030 err = soc_tplg_denum_create_values(se, ec);
1033 "ASoC: could not create values for %s\n",
1038 /* fall through and create texts */
1039 case SND_SOC_TPLG_CTL_ENUM:
1040 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1041 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1042 err = soc_tplg_denum_create_texts(se, ec);
1045 "ASoC: could not create texts for %s\n",
1053 "ASoC: invalid enum control type %d for %s\n",
1054 ec->hdr.ops.info, ec->hdr.name);
1059 /* map io handlers */
1060 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
1062 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1067 /* pass control to driver for optional further init */
1068 err = soc_tplg_init_kcontrol(tplg, &kc,
1069 (struct snd_soc_tplg_ctl_hdr *) ec);
1071 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1077 /* register control here */
1078 ret = soc_tplg_add_kcontrol(tplg,
1079 &kc, &se->dobj.control.kcontrol);
1081 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1087 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1093 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1094 struct snd_soc_tplg_hdr *hdr)
1096 struct snd_soc_tplg_ctl_hdr *control_hdr;
1099 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1100 tplg->pos += hdr->size + hdr->payload_size;
1104 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1105 soc_tplg_get_offset(tplg));
1107 for (i = 0; i < hdr->count; i++) {
1109 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1111 if (control_hdr->size != sizeof(*control_hdr)) {
1112 dev_err(tplg->dev, "ASoC: invalid control size\n");
1116 switch (control_hdr->ops.info) {
1117 case SND_SOC_TPLG_CTL_VOLSW:
1118 case SND_SOC_TPLG_CTL_STROBE:
1119 case SND_SOC_TPLG_CTL_VOLSW_SX:
1120 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1121 case SND_SOC_TPLG_CTL_RANGE:
1122 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1123 case SND_SOC_TPLG_DAPM_CTL_PIN:
1124 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1126 case SND_SOC_TPLG_CTL_ENUM:
1127 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1128 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1129 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1130 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1131 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1133 case SND_SOC_TPLG_CTL_BYTES:
1134 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1137 soc_bind_err(tplg, control_hdr, i);
1145 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1146 struct snd_soc_tplg_hdr *hdr)
1148 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1149 struct snd_soc_dapm_route route;
1150 struct snd_soc_tplg_dapm_graph_elem *elem;
1151 int count = hdr->count, i;
1153 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1154 tplg->pos += hdr->size + hdr->payload_size;
1158 if (soc_tplg_check_elem_count(tplg,
1159 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1160 count, hdr->payload_size, "graph")) {
1162 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1167 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1169 for (i = 0; i < count; i++) {
1170 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1171 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1173 /* validate routes */
1174 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1175 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1177 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1178 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1180 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1181 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1184 route.source = elem->source;
1185 route.sink = elem->sink;
1186 route.connected = NULL; /* set to NULL atm for tplg users */
1187 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1188 route.control = NULL;
1190 route.control = elem->control;
1192 /* add route, but keep going if some fail */
1193 snd_soc_dapm_add_routes(dapm, &route, 1);
1199 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1200 struct soc_tplg *tplg, int num_kcontrols)
1202 struct snd_kcontrol_new *kc;
1203 struct soc_mixer_control *sm;
1204 struct snd_soc_tplg_mixer_control *mc;
1207 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1211 for (i = 0; i < num_kcontrols; i++) {
1212 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1213 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1217 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1220 /* validate kcontrol */
1221 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1222 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1225 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1228 kc[i].name = mc->hdr.name;
1229 kc[i].private_value = (long)sm;
1230 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1231 kc[i].access = mc->hdr.access;
1233 /* we only support FL/FR channel mapping atm */
1234 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1236 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1238 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1240 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1245 sm->invert = mc->invert;
1246 sm->platform_max = mc->platform_max;
1247 sm->dobj.index = tplg->index;
1248 INIT_LIST_HEAD(&sm->dobj.list);
1250 /* map io handlers */
1251 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1253 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1258 /* pass control to driver for optional further init */
1259 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1260 (struct snd_soc_tplg_ctl_hdr *)mc);
1262 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1273 for (--i; i >= 0; i--)
1274 kfree((void *)kc[i].private_value);
1279 static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1280 struct soc_tplg *tplg, int num_kcontrols)
1282 struct snd_kcontrol_new *kc;
1283 struct snd_soc_tplg_enum_control *ec;
1284 struct soc_enum *se;
1287 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1291 for (i = 0; i < num_kcontrols; i++) {
1292 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1293 /* validate kcontrol */
1294 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1295 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1298 se = kzalloc(sizeof(*se), GFP_KERNEL);
1302 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1305 kc[i].name = ec->hdr.name;
1306 kc[i].private_value = (long)se;
1307 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1308 kc[i].access = ec->hdr.access;
1310 /* we only support FL/FR channel mapping atm */
1311 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1312 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1314 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1317 se->items = ec->items;
1318 se->mask = ec->mask;
1319 se->dobj.index = tplg->index;
1321 switch (ec->hdr.ops.info) {
1322 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1323 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1324 err = soc_tplg_denum_create_values(se, ec);
1326 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1330 /* fall through to create texts */
1331 case SND_SOC_TPLG_CTL_ENUM:
1332 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1333 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1334 err = soc_tplg_denum_create_texts(se, ec);
1336 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1342 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1343 ec->hdr.ops.info, ec->hdr.name);
1347 /* map io handlers */
1348 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1350 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1354 /* pass control to driver for optional further init */
1355 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1356 (struct snd_soc_tplg_ctl_hdr *)ec);
1358 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1363 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1370 for (; i >= 0; i--) {
1371 /* free values and texts */
1372 se = (struct soc_enum *)kc[i].private_value;
1373 kfree(se->dobj.control.dvalues);
1374 for (j = 0; j < ec->items; j++)
1375 kfree(se->dobj.control.dtexts[j]);
1385 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1386 struct soc_tplg *tplg, int count)
1388 struct snd_soc_tplg_bytes_control *be;
1389 struct soc_bytes_ext *sbe;
1390 struct snd_kcontrol_new *kc;
1393 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
1397 for (i = 0; i < count; i++) {
1398 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1400 /* validate kcontrol */
1401 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1402 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1405 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1409 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1413 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1414 be->hdr.name, be->hdr.access);
1416 kc[i].name = be->hdr.name;
1417 kc[i].private_value = (long)sbe;
1418 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1419 kc[i].access = be->hdr.access;
1422 INIT_LIST_HEAD(&sbe->dobj.list);
1424 /* map standard io handlers and check for external handlers */
1425 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1427 soc_control_err(tplg, &be->hdr, be->hdr.name);
1432 /* pass control to driver for optional further init */
1433 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1434 (struct snd_soc_tplg_ctl_hdr *)be);
1436 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1446 for (--i; i >= 0; i--)
1447 kfree((void *)kc[i].private_value);
1453 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1454 struct snd_soc_tplg_dapm_widget *w)
1456 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1457 struct snd_soc_dapm_widget template, *widget;
1458 struct snd_soc_tplg_ctl_hdr *control_hdr;
1459 struct snd_soc_card *card = tplg->comp->card;
1460 unsigned int kcontrol_type;
1463 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1464 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1466 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1467 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1470 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1473 memset(&template, 0, sizeof(template));
1475 /* map user to kernel widget ID */
1476 template.id = get_widget_id(w->id);
1477 if (template.id < 0)
1480 /* strings are allocated here, but used and freed by the widget */
1481 template.name = kstrdup(w->name, GFP_KERNEL);
1484 template.sname = kstrdup(w->sname, GFP_KERNEL);
1485 if (!template.sname) {
1489 template.reg = w->reg;
1490 template.shift = w->shift;
1491 template.mask = w->mask;
1492 template.subseq = w->subseq;
1493 template.on_val = w->invert ? 0 : 1;
1494 template.off_val = w->invert ? 1 : 0;
1495 template.ignore_suspend = w->ignore_suspend;
1496 template.event_flags = w->event_flags;
1497 template.dobj.index = tplg->index;
1500 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1501 if (w->num_kcontrols == 0) {
1503 template.num_kcontrols = 0;
1507 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1508 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1509 w->name, w->num_kcontrols, control_hdr->type);
1511 switch (control_hdr->ops.info) {
1512 case SND_SOC_TPLG_CTL_VOLSW:
1513 case SND_SOC_TPLG_CTL_STROBE:
1514 case SND_SOC_TPLG_CTL_VOLSW_SX:
1515 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1516 case SND_SOC_TPLG_CTL_RANGE:
1517 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1518 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
1519 template.num_kcontrols = w->num_kcontrols;
1520 template.kcontrol_news =
1521 soc_tplg_dapm_widget_dmixer_create(tplg,
1522 template.num_kcontrols);
1523 if (!template.kcontrol_news) {
1528 case SND_SOC_TPLG_CTL_ENUM:
1529 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1530 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1531 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1532 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1533 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
1534 template.num_kcontrols = w->num_kcontrols;
1535 template.kcontrol_news =
1536 soc_tplg_dapm_widget_denum_create(tplg,
1537 template.num_kcontrols);
1538 if (!template.kcontrol_news) {
1543 case SND_SOC_TPLG_CTL_BYTES:
1544 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
1545 template.num_kcontrols = w->num_kcontrols;
1546 template.kcontrol_news =
1547 soc_tplg_dapm_widget_dbytes_create(tplg,
1548 template.num_kcontrols);
1549 if (!template.kcontrol_news) {
1555 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1556 control_hdr->ops.get, control_hdr->ops.put,
1557 control_hdr->ops.info);
1563 ret = soc_tplg_widget_load(tplg, &template, w);
1567 /* card dapm mutex is held by the core if we are loading topology
1568 * data during sound card init. */
1569 if (card->instantiated)
1570 widget = snd_soc_dapm_new_control(dapm, &template);
1572 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1573 if (IS_ERR(widget)) {
1574 ret = PTR_ERR(widget);
1575 /* Do not nag about probe deferrals */
1576 if (ret != -EPROBE_DEFER)
1578 "ASoC: failed to create widget %s controls (%d)\n",
1582 if (widget == NULL) {
1583 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1589 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1590 widget->dobj.widget.kcontrol_type = kcontrol_type;
1591 widget->dobj.ops = tplg->ops;
1592 widget->dobj.index = tplg->index;
1593 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1595 ret = soc_tplg_widget_ready(tplg, widget, w);
1602 snd_soc_tplg_widget_remove(widget);
1603 snd_soc_dapm_free_widget(widget);
1605 kfree(template.sname);
1607 kfree(template.name);
1611 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1612 struct snd_soc_tplg_hdr *hdr)
1614 struct snd_soc_tplg_dapm_widget *widget;
1615 int ret, count = hdr->count, i;
1617 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1620 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1622 for (i = 0; i < count; i++) {
1623 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1624 if (widget->size != sizeof(*widget)) {
1625 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1629 ret = soc_tplg_dapm_widget_create(tplg, widget);
1631 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1640 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1642 struct snd_soc_card *card = tplg->comp->card;
1645 /* Card might not have been registered at this point.
1646 * If so, just return success.
1648 if (!card || !card->instantiated) {
1649 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1650 " widget card binding deferred\n");
1654 ret = snd_soc_dapm_new_widgets(card);
1656 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1662 static void set_stream_info(struct snd_soc_pcm_stream *stream,
1663 struct snd_soc_tplg_stream_caps *caps)
1665 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1666 stream->channels_min = caps->channels_min;
1667 stream->channels_max = caps->channels_max;
1668 stream->rates = caps->rates;
1669 stream->rate_min = caps->rate_min;
1670 stream->rate_max = caps->rate_max;
1671 stream->formats = caps->formats;
1672 stream->sig_bits = caps->sig_bits;
1675 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1676 unsigned int flag_mask, unsigned int flags)
1678 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1679 dai_drv->symmetric_rates =
1680 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1682 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1683 dai_drv->symmetric_channels =
1684 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1687 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1688 dai_drv->symmetric_samplebits =
1689 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1693 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1694 struct snd_soc_tplg_pcm *pcm)
1696 struct snd_soc_dai_driver *dai_drv;
1697 struct snd_soc_pcm_stream *stream;
1698 struct snd_soc_tplg_stream_caps *caps;
1701 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1702 if (dai_drv == NULL)
1705 if (strlen(pcm->dai_name))
1706 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
1707 dai_drv->id = pcm->dai_id;
1709 if (pcm->playback) {
1710 stream = &dai_drv->playback;
1711 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1712 set_stream_info(stream, caps);
1716 stream = &dai_drv->capture;
1717 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1718 set_stream_info(stream, caps);
1721 /* pass control to component driver for optional further init */
1722 ret = soc_tplg_dai_load(tplg, dai_drv);
1724 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1729 dai_drv->dobj.index = tplg->index;
1730 dai_drv->dobj.ops = tplg->ops;
1731 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1732 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1734 /* register the DAI to the component */
1735 return snd_soc_register_dai(tplg->comp, dai_drv);
1738 static void set_link_flags(struct snd_soc_dai_link *link,
1739 unsigned int flag_mask, unsigned int flags)
1741 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1742 link->symmetric_rates =
1743 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1745 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1746 link->symmetric_channels =
1747 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1750 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1751 link->symmetric_samplebits =
1752 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1755 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1756 link->ignore_suspend =
1757 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1761 /* create the FE DAI link */
1762 static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1763 struct snd_soc_tplg_pcm *pcm)
1765 struct snd_soc_dai_link *link;
1768 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1772 if (strlen(pcm->pcm_name)) {
1773 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1774 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1776 link->id = pcm->pcm_id;
1778 if (strlen(pcm->dai_name))
1779 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1781 link->codec_name = "snd-soc-dummy";
1782 link->codec_dai_name = "snd-soc-dummy-dai";
1786 link->dpcm_playback = pcm->playback;
1787 link->dpcm_capture = pcm->capture;
1789 set_link_flags(link, pcm->flag_mask, pcm->flags);
1791 /* pass control to component driver for optional further init */
1792 ret = soc_tplg_dai_link_load(tplg, link);
1794 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1799 link->dobj.index = tplg->index;
1800 link->dobj.ops = tplg->ops;
1801 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1802 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1804 snd_soc_add_dai_link(tplg->comp->card, link);
1808 /* create a FE DAI and DAI link from the PCM object */
1809 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1810 struct snd_soc_tplg_pcm *pcm)
1814 ret = soc_tplg_dai_create(tplg, pcm);
1818 return soc_tplg_fe_link_create(tplg, pcm);
1821 /* copy stream caps from the old version 4 of source */
1822 static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1823 struct snd_soc_tplg_stream_caps_v4 *src)
1825 dest->size = sizeof(*dest);
1826 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1827 dest->formats = src->formats;
1828 dest->rates = src->rates;
1829 dest->rate_min = src->rate_min;
1830 dest->rate_max = src->rate_max;
1831 dest->channels_min = src->channels_min;
1832 dest->channels_max = src->channels_max;
1833 dest->periods_min = src->periods_min;
1834 dest->periods_max = src->periods_max;
1835 dest->period_size_min = src->period_size_min;
1836 dest->period_size_max = src->period_size_max;
1837 dest->buffer_size_min = src->buffer_size_min;
1838 dest->buffer_size_max = src->buffer_size_max;
1842 * pcm_new_ver - Create the new version of PCM from the old version.
1843 * @tplg: topology context
1844 * @src: older version of pcm as a source
1845 * @pcm: latest version of pcm created from the source
1847 * Support from vesion 4. User should free the returned pcm manually.
1849 static int pcm_new_ver(struct soc_tplg *tplg,
1850 struct snd_soc_tplg_pcm *src,
1851 struct snd_soc_tplg_pcm **pcm)
1853 struct snd_soc_tplg_pcm *dest;
1854 struct snd_soc_tplg_pcm_v4 *src_v4;
1859 if (src->size != sizeof(*src_v4)) {
1860 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1864 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1865 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1866 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1870 dest->size = sizeof(*dest); /* size of latest abi version */
1871 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1872 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1873 dest->pcm_id = src_v4->pcm_id;
1874 dest->dai_id = src_v4->dai_id;
1875 dest->playback = src_v4->playback;
1876 dest->capture = src_v4->capture;
1877 dest->compress = src_v4->compress;
1878 dest->num_streams = src_v4->num_streams;
1879 for (i = 0; i < dest->num_streams; i++)
1880 memcpy(&dest->stream[i], &src_v4->stream[i],
1881 sizeof(struct snd_soc_tplg_stream));
1883 for (i = 0; i < 2; i++)
1884 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1890 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1891 struct snd_soc_tplg_hdr *hdr)
1893 struct snd_soc_tplg_pcm *pcm, *_pcm;
1894 int count = hdr->count;
1898 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1901 /* check the element size and count */
1902 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1903 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1904 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1905 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1910 if (soc_tplg_check_elem_count(tplg,
1912 hdr->payload_size, "PCM DAI")) {
1913 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1918 for (i = 0; i < count; i++) {
1919 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1921 /* check ABI version by size, create a new version of pcm
1924 if (pcm->size == sizeof(*pcm)) {
1929 pcm_new_ver(tplg, pcm, &_pcm);
1932 /* create the FE DAIs and DAI links */
1933 soc_tplg_pcm_create(tplg, _pcm);
1935 /* offset by version-specific struct size and
1936 * real priv data size
1938 tplg->pos += pcm->size + _pcm->priv.size;
1941 kfree(_pcm); /* free the duplicated one */
1944 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1950 * set_link_hw_format - Set the HW audio format of the physical DAI link.
1951 * @link: &snd_soc_dai_link which should be updated
1952 * @cfg: physical link configs.
1954 * Topology context contains a list of supported HW formats (configs) and
1955 * a default format ID for the physical link. This function will use this
1956 * default ID to choose the HW format to set the link's DAI format for init.
1958 static void set_link_hw_format(struct snd_soc_dai_link *link,
1959 struct snd_soc_tplg_link_config *cfg)
1961 struct snd_soc_tplg_hw_config *hw_config;
1962 unsigned char bclk_master, fsync_master;
1963 unsigned char invert_bclk, invert_fsync;
1966 for (i = 0; i < cfg->num_hw_configs; i++) {
1967 hw_config = &cfg->hw_config[i];
1968 if (hw_config->id != cfg->default_hw_config_id)
1971 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
1973 /* clock signal polarity */
1974 invert_bclk = hw_config->invert_bclk;
1975 invert_fsync = hw_config->invert_fsync;
1976 if (!invert_bclk && !invert_fsync)
1977 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1978 else if (!invert_bclk && invert_fsync)
1979 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1980 else if (invert_bclk && !invert_fsync)
1981 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1983 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1986 bclk_master = hw_config->bclk_master;
1987 fsync_master = hw_config->fsync_master;
1988 if (!bclk_master && !fsync_master)
1989 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1990 else if (bclk_master && !fsync_master)
1991 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1992 else if (!bclk_master && fsync_master)
1993 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1995 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2000 * link_new_ver - Create a new physical link config from the old
2001 * version of source.
2002 * @tplg: topology context
2003 * @src: old version of phyical link config as a source
2004 * @link: latest version of physical link config created from the source
2006 * Support from vesion 4. User need free the returned link config manually.
2008 static int link_new_ver(struct soc_tplg *tplg,
2009 struct snd_soc_tplg_link_config *src,
2010 struct snd_soc_tplg_link_config **link)
2012 struct snd_soc_tplg_link_config *dest;
2013 struct snd_soc_tplg_link_config_v4 *src_v4;
2018 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2019 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2023 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2025 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2026 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2030 dest->size = sizeof(*dest);
2031 dest->id = src_v4->id;
2032 dest->num_streams = src_v4->num_streams;
2033 for (i = 0; i < dest->num_streams; i++)
2034 memcpy(&dest->stream[i], &src_v4->stream[i],
2035 sizeof(struct snd_soc_tplg_stream));
2041 /* Find and configure an existing physical DAI link */
2042 static int soc_tplg_link_config(struct soc_tplg *tplg,
2043 struct snd_soc_tplg_link_config *cfg)
2045 struct snd_soc_dai_link *link;
2046 const char *name, *stream_name;
2050 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2051 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2058 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2059 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2062 stream_name = cfg->stream_name;
2066 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2069 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2075 if (cfg->num_hw_configs)
2076 set_link_hw_format(link, cfg);
2080 set_link_flags(link, cfg->flag_mask, cfg->flags);
2082 /* pass control to component driver for optional further init */
2083 ret = soc_tplg_dai_link_load(tplg, link);
2085 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2093 /* Load physical link config elements from the topology context */
2094 static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2095 struct snd_soc_tplg_hdr *hdr)
2097 struct snd_soc_tplg_link_config *link, *_link;
2098 int count = hdr->count;
2102 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2103 tplg->pos += hdr->size + hdr->payload_size;
2107 /* check the element size and count */
2108 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2109 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2110 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2111 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2116 if (soc_tplg_check_elem_count(tplg,
2118 hdr->payload_size, "physical link config")) {
2119 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2124 /* config physical DAI links */
2125 for (i = 0; i < count; i++) {
2126 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2127 if (link->size == sizeof(*link)) {
2132 ret = link_new_ver(tplg, link, &_link);
2137 ret = soc_tplg_link_config(tplg, _link);
2141 /* offset by version-specific struct size and
2142 * real priv data size
2144 tplg->pos += link->size + _link->priv.size;
2147 kfree(_link); /* free the duplicated one */
2154 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2155 * @tplg: topology context
2156 * @d: physical DAI configs.
2158 * The physical dai should already be registered by the platform driver.
2159 * The platform driver should specify the DAI name and ID for matching.
2161 static int soc_tplg_dai_config(struct soc_tplg *tplg,
2162 struct snd_soc_tplg_dai *d)
2164 struct snd_soc_dai_link_component dai_component = {0};
2165 struct snd_soc_dai *dai;
2166 struct snd_soc_dai_driver *dai_drv;
2167 struct snd_soc_pcm_stream *stream;
2168 struct snd_soc_tplg_stream_caps *caps;
2171 dai_component.dai_name = d->dai_name;
2172 dai = snd_soc_find_dai(&dai_component);
2174 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2179 if (d->dai_id != dai->id) {
2180 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2185 dai_drv = dai->driver;
2190 stream = &dai_drv->playback;
2191 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2192 set_stream_info(stream, caps);
2196 stream = &dai_drv->capture;
2197 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2198 set_stream_info(stream, caps);
2202 set_dai_flags(dai_drv, d->flag_mask, d->flags);
2204 /* pass control to component driver for optional further init */
2205 ret = soc_tplg_dai_load(tplg, dai_drv);
2207 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2214 /* load physical DAI elements */
2215 static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2216 struct snd_soc_tplg_hdr *hdr)
2218 struct snd_soc_tplg_dai *dai;
2219 int count = hdr->count;
2222 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2225 /* config the existing BE DAIs */
2226 for (i = 0; i < count; i++) {
2227 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2228 if (dai->size != sizeof(*dai)) {
2229 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2233 soc_tplg_dai_config(tplg, dai);
2234 tplg->pos += (sizeof(*dai) + dai->priv.size);
2237 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2242 * manifest_new_ver - Create a new version of manifest from the old version
2244 * @tplg: topology context
2245 * @src: old version of manifest as a source
2246 * @manifest: latest version of manifest created from the source
2248 * Support from vesion 4. Users need free the returned manifest manually.
2250 static int manifest_new_ver(struct soc_tplg *tplg,
2251 struct snd_soc_tplg_manifest *src,
2252 struct snd_soc_tplg_manifest **manifest)
2254 struct snd_soc_tplg_manifest *dest;
2255 struct snd_soc_tplg_manifest_v4 *src_v4;
2259 if (src->size != sizeof(*src_v4)) {
2260 dev_err(tplg->dev, "ASoC: invalid manifest size\n");
2264 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2266 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2267 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2271 dest->size = sizeof(*dest); /* size of latest abi version */
2272 dest->control_elems = src_v4->control_elems;
2273 dest->widget_elems = src_v4->widget_elems;
2274 dest->graph_elems = src_v4->graph_elems;
2275 dest->pcm_elems = src_v4->pcm_elems;
2276 dest->dai_link_elems = src_v4->dai_link_elems;
2277 dest->priv.size = src_v4->priv.size;
2278 if (dest->priv.size)
2279 memcpy(dest->priv.data, src_v4->priv.data,
2286 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2287 struct snd_soc_tplg_hdr *hdr)
2289 struct snd_soc_tplg_manifest *manifest, *_manifest;
2293 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2296 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2298 /* check ABI version by size, create a new manifest if abi not match */
2299 if (manifest->size == sizeof(*manifest)) {
2301 _manifest = manifest;
2304 err = manifest_new_ver(tplg, manifest, &_manifest);
2309 /* pass control to component driver for optional further init */
2310 if (tplg->comp && tplg->ops && tplg->ops->manifest)
2311 return tplg->ops->manifest(tplg->comp, _manifest);
2313 if (!abi_match) /* free the duplicated one */
2319 /* validate header magic, size and type */
2320 static int soc_valid_header(struct soc_tplg *tplg,
2321 struct snd_soc_tplg_hdr *hdr)
2323 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2326 if (hdr->size != sizeof(*hdr)) {
2328 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2329 hdr->type, soc_tplg_get_hdr_offset(tplg),
2334 /* big endian firmware objects not supported atm */
2335 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2337 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2338 tplg->pass, hdr->magic,
2339 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2343 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2345 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2346 tplg->pass, hdr->magic,
2347 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2351 /* Support ABI from version 4 */
2352 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2353 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
2355 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2356 tplg->pass, hdr->abi,
2357 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2362 if (hdr->payload_size == 0) {
2363 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2364 soc_tplg_get_hdr_offset(tplg));
2368 if (tplg->pass == hdr->type)
2370 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2371 hdr->payload_size, hdr->type, hdr->version,
2372 hdr->vendor_type, tplg->pass);
2377 /* check header type and call appropriate handler */
2378 static int soc_tplg_load_header(struct soc_tplg *tplg,
2379 struct snd_soc_tplg_hdr *hdr)
2381 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2383 /* check for matching ID */
2384 if (hdr->index != tplg->req_index &&
2385 hdr->index != SND_SOC_TPLG_INDEX_ALL)
2388 tplg->index = hdr->index;
2390 switch (hdr->type) {
2391 case SND_SOC_TPLG_TYPE_MIXER:
2392 case SND_SOC_TPLG_TYPE_ENUM:
2393 case SND_SOC_TPLG_TYPE_BYTES:
2394 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2395 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2396 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2397 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2398 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2399 case SND_SOC_TPLG_TYPE_PCM:
2400 return soc_tplg_pcm_elems_load(tplg, hdr);
2401 case SND_SOC_TPLG_TYPE_DAI:
2402 return soc_tplg_dai_elems_load(tplg, hdr);
2403 case SND_SOC_TPLG_TYPE_DAI_LINK:
2404 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2405 /* physical link configurations */
2406 return soc_tplg_link_elems_load(tplg, hdr);
2407 case SND_SOC_TPLG_TYPE_MANIFEST:
2408 return soc_tplg_manifest_load(tplg, hdr);
2410 /* bespoke vendor data object */
2411 return soc_tplg_vendor_load(tplg, hdr);
2417 /* process the topology file headers */
2418 static int soc_tplg_process_headers(struct soc_tplg *tplg)
2420 struct snd_soc_tplg_hdr *hdr;
2423 tplg->pass = SOC_TPLG_PASS_START;
2425 /* process the header types from start to end */
2426 while (tplg->pass <= SOC_TPLG_PASS_END) {
2428 tplg->hdr_pos = tplg->fw->data;
2429 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2431 while (!soc_tplg_is_eof(tplg)) {
2433 /* make sure header is valid before loading */
2434 ret = soc_valid_header(tplg, hdr);
2440 /* load the header object */
2441 ret = soc_tplg_load_header(tplg, hdr);
2445 /* goto next header */
2446 tplg->hdr_pos += hdr->payload_size +
2447 sizeof(struct snd_soc_tplg_hdr);
2448 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2451 /* next data type pass */
2455 /* signal DAPM we are complete */
2456 ret = soc_tplg_dapm_complete(tplg);
2459 "ASoC: failed to initialise DAPM from Firmware\n");
2464 static int soc_tplg_load(struct soc_tplg *tplg)
2468 ret = soc_tplg_process_headers(tplg);
2470 soc_tplg_complete(tplg);
2475 /* load audio component topology from "firmware" file */
2476 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2477 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2479 struct soc_tplg tplg;
2481 /* setup parsing context */
2482 memset(&tplg, 0, sizeof(tplg));
2484 tplg.dev = comp->dev;
2487 tplg.req_index = id;
2488 tplg.io_ops = ops->io_ops;
2489 tplg.io_ops_count = ops->io_ops_count;
2490 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2491 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2493 return soc_tplg_load(&tplg);
2495 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2497 /* remove this dynamic widget */
2498 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2500 /* make sure we are a widget */
2501 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2504 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2506 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2508 /* remove all dynamic widgets from this DAPM context */
2509 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2512 struct snd_soc_dapm_widget *w, *next_w;
2514 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2516 /* make sure we are a widget with correct context */
2517 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2521 if (w->dobj.index != index &&
2522 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2524 /* check and free and dynamic widget kcontrols */
2525 snd_soc_tplg_widget_remove(w);
2526 snd_soc_dapm_free_widget(w);
2528 snd_soc_dapm_reset_cache(dapm);
2530 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2532 /* remove dynamic controls from the component driver */
2533 int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2535 struct snd_soc_dobj *dobj, *next_dobj;
2536 int pass = SOC_TPLG_PASS_END;
2538 /* process the header types from end to start */
2539 while (pass >= SOC_TPLG_PASS_START) {
2541 /* remove mixer controls */
2542 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2546 if (dobj->index != index &&
2547 dobj->index != SND_SOC_TPLG_INDEX_ALL)
2550 switch (dobj->type) {
2551 case SND_SOC_DOBJ_MIXER:
2552 remove_mixer(comp, dobj, pass);
2554 case SND_SOC_DOBJ_ENUM:
2555 remove_enum(comp, dobj, pass);
2557 case SND_SOC_DOBJ_BYTES:
2558 remove_bytes(comp, dobj, pass);
2560 case SND_SOC_DOBJ_WIDGET:
2561 remove_widget(comp, dobj, pass);
2563 case SND_SOC_DOBJ_PCM:
2564 remove_dai(comp, dobj, pass);
2566 case SND_SOC_DOBJ_DAI_LINK:
2567 remove_link(comp, dobj, pass);
2570 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2578 /* let caller know if FW can be freed when no objects are left */
2579 return !list_empty(&comp->dobj_list);
2581 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);