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
52 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
53 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_PINS
56 const struct firmware *fw;
58 /* runtime FW parsing */
59 const u8 *pos; /* read postion */
60 const u8 *hdr_pos; /* header position */
61 unsigned int pass; /* pass number */
63 /* component caller */
65 struct snd_soc_component *comp;
66 u32 index; /* current block index */
67 u32 req_index; /* required index, only loaded/free matching blocks */
69 /* vendor specific kcontrol operations */
70 const struct snd_soc_tplg_kcontrol_ops *io_ops;
73 /* vendor specific bytes ext handlers, for TLV bytes controls */
74 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
75 int bytes_ext_ops_count;
77 /* optional fw loading callbacks to component drivers */
78 struct snd_soc_tplg_ops *ops;
81 static int soc_tplg_process_headers(struct soc_tplg *tplg);
82 static void soc_tplg_complete(struct soc_tplg *tplg);
83 struct snd_soc_dapm_widget *
84 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
85 const struct snd_soc_dapm_widget *widget);
86 struct snd_soc_dapm_widget *
87 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
88 const struct snd_soc_dapm_widget *widget);
90 /* check we dont overflow the data for this control chunk */
91 static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
92 unsigned int count, size_t bytes, const char *elem_type)
94 const u8 *end = tplg->pos + elem_size * count;
96 if (end > tplg->fw->data + tplg->fw->size) {
97 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
102 /* check there is enough room in chunk for control.
103 extra bytes at the end of control are for vendor data here */
104 if (elem_size * count > bytes) {
106 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
107 elem_type, count, elem_size, bytes);
114 static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
116 const u8 *end = tplg->hdr_pos;
118 if (end >= tplg->fw->data + tplg->fw->size)
123 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
125 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
128 static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
130 return (unsigned long)(tplg->pos - tplg->fw->data);
133 /* mapping of Kcontrol types and associated operations. */
134 static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
135 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
136 snd_soc_put_volsw, snd_soc_info_volsw},
137 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
138 snd_soc_put_volsw_sx, NULL},
139 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
140 snd_soc_put_enum_double, snd_soc_info_enum_double},
141 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
142 snd_soc_put_enum_double, NULL},
143 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
144 snd_soc_bytes_put, snd_soc_bytes_info},
145 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
146 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
147 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
148 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
149 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
150 snd_soc_put_strobe, NULL},
151 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
152 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
153 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
154 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
155 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
156 snd_soc_dapm_put_enum_double, NULL},
157 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
158 snd_soc_dapm_put_enum_double, NULL},
159 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
160 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
163 struct soc_tplg_map {
168 /* mapping of widget types from UAPI IDs to kernel IDs */
169 static const struct soc_tplg_map dapm_map[] = {
170 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
171 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
172 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
173 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
174 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
175 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
176 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
177 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
178 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
179 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
180 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
181 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
182 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
183 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
184 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
185 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
188 static int tplc_chan_get_reg(struct soc_tplg *tplg,
189 struct snd_soc_tplg_channel *chan, int map)
193 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
194 if (chan[i].id == map)
201 static int tplc_chan_get_shift(struct soc_tplg *tplg,
202 struct snd_soc_tplg_channel *chan, int map)
206 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
207 if (chan[i].id == map)
208 return chan[i].shift;
214 static int get_widget_id(int tplg_type)
218 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
219 if (tplg_type == dapm_map[i].uid)
220 return dapm_map[i].kid;
226 static enum snd_soc_dobj_type get_dobj_mixer_type(
227 struct snd_soc_tplg_ctl_hdr *control_hdr)
229 if (control_hdr == NULL)
230 return SND_SOC_DOBJ_NONE;
232 switch (control_hdr->ops.info) {
233 case SND_SOC_TPLG_CTL_VOLSW:
234 case SND_SOC_TPLG_CTL_VOLSW_SX:
235 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
236 case SND_SOC_TPLG_CTL_RANGE:
237 case SND_SOC_TPLG_CTL_STROBE:
238 return SND_SOC_DOBJ_MIXER;
239 case SND_SOC_TPLG_CTL_ENUM:
240 case SND_SOC_TPLG_CTL_ENUM_VALUE:
241 return SND_SOC_DOBJ_ENUM;
242 case SND_SOC_TPLG_CTL_BYTES:
243 return SND_SOC_DOBJ_BYTES;
245 return SND_SOC_DOBJ_NONE;
249 static enum snd_soc_dobj_type get_dobj_type(struct snd_soc_tplg_hdr *hdr,
250 struct snd_soc_tplg_ctl_hdr *control_hdr)
253 case SND_SOC_TPLG_TYPE_MIXER:
254 return get_dobj_mixer_type(control_hdr);
255 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
256 case SND_SOC_TPLG_TYPE_MANIFEST:
257 return SND_SOC_DOBJ_NONE;
258 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
259 return SND_SOC_DOBJ_WIDGET;
260 case SND_SOC_TPLG_TYPE_DAI_LINK:
261 return SND_SOC_DOBJ_DAI_LINK;
262 case SND_SOC_TPLG_TYPE_PCM:
263 return SND_SOC_DOBJ_PCM;
264 case SND_SOC_TPLG_TYPE_CODEC_LINK:
265 return SND_SOC_DOBJ_CODEC_LINK;
267 return SND_SOC_DOBJ_NONE;
271 static inline void soc_bind_err(struct soc_tplg *tplg,
272 struct snd_soc_tplg_ctl_hdr *hdr, int index)
275 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
276 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
277 soc_tplg_get_offset(tplg));
280 static inline void soc_control_err(struct soc_tplg *tplg,
281 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
284 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
285 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
286 soc_tplg_get_offset(tplg));
289 /* pass vendor data to component driver for processing */
290 static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
291 struct snd_soc_tplg_hdr *hdr)
295 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
296 ret = tplg->ops->vendor_load(tplg->comp, hdr);
298 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
305 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
306 soc_tplg_get_hdr_offset(tplg),
307 soc_tplg_get_hdr_offset(tplg),
308 hdr->type, hdr->vendor_type);
312 /* pass vendor data to component driver for processing */
313 static int soc_tplg_vendor_load(struct soc_tplg *tplg,
314 struct snd_soc_tplg_hdr *hdr)
316 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
319 return soc_tplg_vendor_load_(tplg, hdr);
322 /* optionally pass new dynamic widget to component driver. This is mainly for
323 * external widgets where we can assign private data/ops */
324 static int soc_tplg_widget_load(struct soc_tplg *tplg,
325 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
327 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
328 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
333 /* pass DAI configurations to component driver for extra intialization */
334 static int soc_tplg_dai_load(struct soc_tplg *tplg,
335 struct snd_soc_dai_driver *dai_drv)
337 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
338 return tplg->ops->dai_load(tplg->comp, dai_drv);
343 /* pass link configurations to component driver for extra intialization */
344 static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
345 struct snd_soc_dai_link *link)
347 if (tplg->comp && tplg->ops && tplg->ops->link_load)
348 return tplg->ops->link_load(tplg->comp, link);
353 /* tell the component driver that all firmware has been loaded in this request */
354 static void soc_tplg_complete(struct soc_tplg *tplg)
356 if (tplg->comp && tplg->ops && tplg->ops->complete)
357 tplg->ops->complete(tplg->comp);
360 /* add a dynamic kcontrol */
361 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
362 const struct snd_kcontrol_new *control_new, const char *prefix,
363 void *data, struct snd_kcontrol **kcontrol)
367 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
368 if (*kcontrol == NULL) {
369 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
374 err = snd_ctl_add(card, *kcontrol);
376 dev_err(dev, "ASoC: Failed to add %s: %d\n",
377 control_new->name, err);
384 /* add a dynamic kcontrol for component driver */
385 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
386 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
388 struct snd_soc_component *comp = tplg->comp;
390 return soc_tplg_add_dcontrol(comp->card->snd_card,
391 comp->dev, k, NULL, comp, kcontrol);
394 /* remove a mixer kcontrol */
395 static void remove_mixer(struct snd_soc_component *comp,
396 struct snd_soc_dobj *dobj, int pass)
398 struct snd_card *card = comp->card->snd_card;
399 struct soc_mixer_control *sm =
400 container_of(dobj, struct soc_mixer_control, dobj);
401 const unsigned int *p = NULL;
403 if (pass != SOC_TPLG_PASS_MIXER)
406 if (dobj->ops && dobj->ops->control_unload)
407 dobj->ops->control_unload(comp, dobj);
409 if (sm->dobj.control.kcontrol->tlv.p)
410 p = sm->dobj.control.kcontrol->tlv.p;
411 snd_ctl_remove(card, sm->dobj.control.kcontrol);
412 list_del(&sm->dobj.list);
417 /* remove an enum kcontrol */
418 static void remove_enum(struct snd_soc_component *comp,
419 struct snd_soc_dobj *dobj, int pass)
421 struct snd_card *card = comp->card->snd_card;
422 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
425 if (pass != SOC_TPLG_PASS_MIXER)
428 if (dobj->ops && dobj->ops->control_unload)
429 dobj->ops->control_unload(comp, dobj);
431 snd_ctl_remove(card, se->dobj.control.kcontrol);
432 list_del(&se->dobj.list);
434 kfree(se->dobj.control.dvalues);
435 for (i = 0; i < se->items; i++)
436 kfree(se->dobj.control.dtexts[i]);
440 /* remove a byte kcontrol */
441 static void remove_bytes(struct snd_soc_component *comp,
442 struct snd_soc_dobj *dobj, int pass)
444 struct snd_card *card = comp->card->snd_card;
445 struct soc_bytes_ext *sb =
446 container_of(dobj, struct soc_bytes_ext, dobj);
448 if (pass != SOC_TPLG_PASS_MIXER)
451 if (dobj->ops && dobj->ops->control_unload)
452 dobj->ops->control_unload(comp, dobj);
454 snd_ctl_remove(card, sb->dobj.control.kcontrol);
455 list_del(&sb->dobj.list);
459 /* remove a widget and it's kcontrols - routes must be removed first */
460 static void remove_widget(struct snd_soc_component *comp,
461 struct snd_soc_dobj *dobj, int pass)
463 struct snd_card *card = comp->card->snd_card;
464 struct snd_soc_dapm_widget *w =
465 container_of(dobj, struct snd_soc_dapm_widget, dobj);
468 if (pass != SOC_TPLG_PASS_WIDGET)
471 if (dobj->ops && dobj->ops->widget_unload)
472 dobj->ops->widget_unload(comp, dobj);
475 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
476 * The enum may either have an array of values or strings.
478 if (dobj->widget.kcontrol_enum) {
479 /* enumerated widget mixer */
480 struct soc_enum *se =
481 (struct soc_enum *)w->kcontrols[0]->private_value;
483 snd_ctl_remove(card, w->kcontrols[0]);
485 kfree(se->dobj.control.dvalues);
486 for (i = 0; i < se->items; i++)
487 kfree(se->dobj.control.dtexts[i]);
490 kfree(w->kcontrol_news);
492 /* non enumerated widget mixer */
493 for (i = 0; i < w->num_kcontrols; i++) {
494 struct snd_kcontrol *kcontrol = w->kcontrols[i];
495 struct soc_mixer_control *sm =
496 (struct soc_mixer_control *) kcontrol->private_value;
498 kfree(w->kcontrols[i]->tlv.p);
500 snd_ctl_remove(card, w->kcontrols[i]);
503 kfree(w->kcontrol_news);
505 /* widget w is freed by soc-dapm.c */
508 /* remove DAI configurations */
509 static void remove_dai(struct snd_soc_component *comp,
510 struct snd_soc_dobj *dobj, int pass)
512 struct snd_soc_dai_driver *dai_drv =
513 container_of(dobj, struct snd_soc_dai_driver, dobj);
515 if (pass != SOC_TPLG_PASS_PCM_DAI)
518 if (dobj->ops && dobj->ops->dai_unload)
519 dobj->ops->dai_unload(comp, dobj);
521 list_del(&dobj->list);
525 /* remove link configurations */
526 static void remove_link(struct snd_soc_component *comp,
527 struct snd_soc_dobj *dobj, int pass)
529 struct snd_soc_dai_link *link =
530 container_of(dobj, struct snd_soc_dai_link, dobj);
532 if (pass != SOC_TPLG_PASS_PCM_DAI)
535 if (dobj->ops && dobj->ops->link_unload)
536 dobj->ops->link_unload(comp, dobj);
538 list_del(&dobj->list);
539 snd_soc_remove_dai_link(comp->card, link);
543 /* bind a kcontrol to it's IO handlers */
544 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
545 struct snd_kcontrol_new *k,
546 const struct soc_tplg *tplg)
548 const struct snd_soc_tplg_kcontrol_ops *ops;
549 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
552 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
553 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
554 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
555 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
556 struct soc_bytes_ext *sbe;
557 struct snd_soc_tplg_bytes_control *be;
559 sbe = (struct soc_bytes_ext *)k->private_value;
560 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
562 /* TLV bytes controls need standard kcontrol info handler,
563 * TLV callback and extended put/get handlers.
565 k->info = snd_soc_bytes_info_ext;
566 k->tlv.c = snd_soc_bytes_tlv_callback;
568 ext_ops = tplg->bytes_ext_ops;
569 num_ops = tplg->bytes_ext_ops_count;
570 for (i = 0; i < num_ops; i++) {
571 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
572 sbe->put = ext_ops[i].put;
573 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
574 sbe->get = ext_ops[i].get;
577 if (sbe->put && sbe->get)
583 /* try and map vendor specific kcontrol handlers first */
585 num_ops = tplg->io_ops_count;
586 for (i = 0; i < num_ops; i++) {
588 if (k->put == NULL && ops[i].id == hdr->ops.put)
590 if (k->get == NULL && ops[i].id == hdr->ops.get)
592 if (k->info == NULL && ops[i].id == hdr->ops.info)
593 k->info = ops[i].info;
596 /* vendor specific handlers found ? */
597 if (k->put && k->get && k->info)
600 /* none found so try standard kcontrol handlers */
602 num_ops = ARRAY_SIZE(io_ops);
603 for (i = 0; i < num_ops; i++) {
605 if (k->put == NULL && ops[i].id == hdr->ops.put)
607 if (k->get == NULL && ops[i].id == hdr->ops.get)
609 if (k->info == NULL && ops[i].id == hdr->ops.info)
610 k->info = ops[i].info;
613 /* standard handlers found ? */
614 if (k->put && k->get && k->info)
617 /* nothing to bind */
621 /* bind a widgets to it's evnt handlers */
622 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
623 const struct snd_soc_tplg_widget_events *events,
624 int num_events, u16 event_type)
630 for (i = 0; i < num_events; i++) {
631 if (event_type == events[i].type) {
633 /* found - so assign event */
634 w->event = events[i].event_handler;
642 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
644 /* optionally pass new dynamic kcontrol to component driver. */
645 static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
646 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
648 if (tplg->comp && tplg->ops && tplg->ops->control_load)
649 return tplg->ops->control_load(tplg->comp, k, hdr);
655 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
656 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
658 unsigned int item_len = 2 * sizeof(unsigned int);
661 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
665 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
668 p[3] = (scale->step & TLV_DB_SCALE_MASK)
669 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
671 kc->tlv.p = (void *)p;
675 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
676 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
678 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
680 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
683 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
685 switch (tplg_tlv->type) {
686 case SNDRV_CTL_TLVT_DB_SCALE:
687 return soc_tplg_create_tlv_db_scale(tplg, kc,
690 /* TODO: add support for other TLV types */
692 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
701 static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
702 struct snd_kcontrol_new *kc)
707 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
710 struct snd_soc_tplg_bytes_control *be;
711 struct soc_bytes_ext *sbe;
712 struct snd_kcontrol_new kc;
715 if (soc_tplg_check_elem_count(tplg,
716 sizeof(struct snd_soc_tplg_bytes_control), count,
717 size, "mixer bytes")) {
718 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
723 for (i = 0; i < count; i++) {
724 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
726 /* validate kcontrol */
727 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
728 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
731 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
735 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
739 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
740 be->hdr.name, be->hdr.access);
742 memset(&kc, 0, sizeof(kc));
743 kc.name = be->hdr.name;
744 kc.private_value = (long)sbe;
745 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
746 kc.access = be->hdr.access;
749 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
750 sbe->dobj.ops = tplg->ops;
751 INIT_LIST_HEAD(&sbe->dobj.list);
753 /* map io handlers */
754 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
756 soc_control_err(tplg, &be->hdr, be->hdr.name);
761 /* pass control to driver for optional further init */
762 err = soc_tplg_init_kcontrol(tplg, &kc,
763 (struct snd_soc_tplg_ctl_hdr *)be);
765 dev_err(tplg->dev, "ASoC: failed to init %s\n",
771 /* register control here */
772 err = soc_tplg_add_kcontrol(tplg, &kc,
773 &sbe->dobj.control.kcontrol);
775 dev_err(tplg->dev, "ASoC: failed to add %s\n",
781 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
787 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
790 struct snd_soc_tplg_mixer_control *mc;
791 struct soc_mixer_control *sm;
792 struct snd_kcontrol_new kc;
795 if (soc_tplg_check_elem_count(tplg,
796 sizeof(struct snd_soc_tplg_mixer_control),
797 count, size, "mixers")) {
799 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
804 for (i = 0; i < count; i++) {
805 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
807 /* validate kcontrol */
808 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
809 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
812 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
815 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
819 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
820 mc->hdr.name, mc->hdr.access);
822 memset(&kc, 0, sizeof(kc));
823 kc.name = mc->hdr.name;
824 kc.private_value = (long)sm;
825 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
826 kc.access = mc->hdr.access;
828 /* we only support FL/FR channel mapping atm */
829 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
831 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
833 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
835 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
840 sm->invert = mc->invert;
841 sm->platform_max = mc->platform_max;
842 sm->dobj.index = tplg->index;
843 sm->dobj.ops = tplg->ops;
844 sm->dobj.type = SND_SOC_DOBJ_MIXER;
845 INIT_LIST_HEAD(&sm->dobj.list);
847 /* map io handlers */
848 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
850 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
855 /* pass control to driver for optional further init */
856 err = soc_tplg_init_kcontrol(tplg, &kc,
857 (struct snd_soc_tplg_ctl_hdr *) mc);
859 dev_err(tplg->dev, "ASoC: failed to init %s\n",
865 /* create any TLV data */
866 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
868 /* register control here */
869 err = soc_tplg_add_kcontrol(tplg, &kc,
870 &sm->dobj.control.kcontrol);
872 dev_err(tplg->dev, "ASoC: failed to add %s\n",
874 soc_tplg_free_tlv(tplg, &kc);
879 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
885 static int soc_tplg_denum_create_texts(struct soc_enum *se,
886 struct snd_soc_tplg_enum_control *ec)
890 se->dobj.control.dtexts =
891 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
892 if (se->dobj.control.dtexts == NULL)
895 for (i = 0; i < ec->items; i++) {
897 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
898 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
903 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
904 if (!se->dobj.control.dtexts[i]) {
913 for (--i; i >= 0; i--)
914 kfree(se->dobj.control.dtexts[i]);
915 kfree(se->dobj.control.dtexts);
919 static int soc_tplg_denum_create_values(struct soc_enum *se,
920 struct snd_soc_tplg_enum_control *ec)
922 if (ec->items > sizeof(*ec->values))
925 se->dobj.control.dvalues = kmemdup(ec->values,
926 ec->items * sizeof(u32),
928 if (!se->dobj.control.dvalues)
934 static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
937 struct snd_soc_tplg_enum_control *ec;
939 struct snd_kcontrol_new kc;
942 if (soc_tplg_check_elem_count(tplg,
943 sizeof(struct snd_soc_tplg_enum_control),
944 count, size, "enums")) {
946 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
951 for (i = 0; i < count; i++) {
952 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
953 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
956 /* validate kcontrol */
957 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
958 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
961 se = kzalloc((sizeof(*se)), GFP_KERNEL);
965 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
966 ec->hdr.name, ec->items);
968 memset(&kc, 0, sizeof(kc));
969 kc.name = ec->hdr.name;
970 kc.private_value = (long)se;
971 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
972 kc.access = ec->hdr.access;
974 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
975 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
977 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
980 se->items = ec->items;
982 se->dobj.index = tplg->index;
983 se->dobj.type = SND_SOC_DOBJ_ENUM;
984 se->dobj.ops = tplg->ops;
985 INIT_LIST_HEAD(&se->dobj.list);
987 switch (ec->hdr.ops.info) {
988 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
989 case SND_SOC_TPLG_CTL_ENUM_VALUE:
990 err = soc_tplg_denum_create_values(se, ec);
993 "ASoC: could not create values for %s\n",
998 /* fall through and create texts */
999 case SND_SOC_TPLG_CTL_ENUM:
1000 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1001 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1002 err = soc_tplg_denum_create_texts(se, ec);
1005 "ASoC: could not create texts for %s\n",
1013 "ASoC: invalid enum control type %d for %s\n",
1014 ec->hdr.ops.info, ec->hdr.name);
1019 /* map io handlers */
1020 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
1022 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1027 /* pass control to driver for optional further init */
1028 err = soc_tplg_init_kcontrol(tplg, &kc,
1029 (struct snd_soc_tplg_ctl_hdr *) ec);
1031 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1037 /* register control here */
1038 ret = soc_tplg_add_kcontrol(tplg,
1039 &kc, &se->dobj.control.kcontrol);
1041 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1047 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1053 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1054 struct snd_soc_tplg_hdr *hdr)
1056 struct snd_soc_tplg_ctl_hdr *control_hdr;
1059 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1060 tplg->pos += hdr->size + hdr->payload_size;
1064 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1065 soc_tplg_get_offset(tplg));
1067 for (i = 0; i < hdr->count; i++) {
1069 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1071 switch (control_hdr->ops.info) {
1072 case SND_SOC_TPLG_CTL_VOLSW:
1073 case SND_SOC_TPLG_CTL_STROBE:
1074 case SND_SOC_TPLG_CTL_VOLSW_SX:
1075 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1076 case SND_SOC_TPLG_CTL_RANGE:
1077 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1078 case SND_SOC_TPLG_DAPM_CTL_PIN:
1079 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1081 case SND_SOC_TPLG_CTL_ENUM:
1082 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1083 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1084 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1085 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1086 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1088 case SND_SOC_TPLG_CTL_BYTES:
1089 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1092 soc_bind_err(tplg, control_hdr, i);
1100 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1101 struct snd_soc_tplg_hdr *hdr)
1103 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1104 struct snd_soc_dapm_route route;
1105 struct snd_soc_tplg_dapm_graph_elem *elem;
1106 int count = hdr->count, i;
1108 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1109 tplg->pos += hdr->size + hdr->payload_size;
1113 if (soc_tplg_check_elem_count(tplg,
1114 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1115 count, hdr->payload_size, "graph")) {
1117 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1122 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1124 for (i = 0; i < count; i++) {
1125 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1126 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1128 /* validate routes */
1129 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1130 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1132 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1133 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1135 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1136 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1139 route.source = elem->source;
1140 route.sink = elem->sink;
1141 route.connected = NULL; /* set to NULL atm for tplg users */
1142 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1143 route.control = NULL;
1145 route.control = elem->control;
1147 /* add route, but keep going if some fail */
1148 snd_soc_dapm_add_routes(dapm, &route, 1);
1154 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1155 struct soc_tplg *tplg, int num_kcontrols)
1157 struct snd_kcontrol_new *kc;
1158 struct soc_mixer_control *sm;
1159 struct snd_soc_tplg_mixer_control *mc;
1162 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1166 for (i = 0; i < num_kcontrols; i++) {
1167 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1168 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1172 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1175 /* validate kcontrol */
1176 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1177 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1180 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1183 kc[i].name = mc->hdr.name;
1184 kc[i].private_value = (long)sm;
1185 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1186 kc[i].access = mc->hdr.access;
1188 /* we only support FL/FR channel mapping atm */
1189 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1191 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1193 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1195 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1200 sm->invert = mc->invert;
1201 sm->platform_max = mc->platform_max;
1202 sm->dobj.index = tplg->index;
1203 INIT_LIST_HEAD(&sm->dobj.list);
1205 /* map io handlers */
1206 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1208 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1213 /* pass control to driver for optional further init */
1214 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1215 (struct snd_soc_tplg_ctl_hdr *)mc);
1217 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1228 for (--i; i >= 0; i--)
1229 kfree((void *)kc[i].private_value);
1234 static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1235 struct soc_tplg *tplg)
1237 struct snd_kcontrol_new *kc;
1238 struct snd_soc_tplg_enum_control *ec;
1239 struct soc_enum *se;
1242 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1243 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1246 /* validate kcontrol */
1247 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1248 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1251 kc = kzalloc(sizeof(*kc), GFP_KERNEL);
1255 se = kzalloc(sizeof(*se), GFP_KERNEL);
1259 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1262 kc->name = ec->hdr.name;
1263 kc->private_value = (long)se;
1264 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1265 kc->access = ec->hdr.access;
1267 /* we only support FL/FR channel mapping atm */
1268 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1269 se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
1270 se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
1272 se->items = ec->items;
1273 se->mask = ec->mask;
1274 se->dobj.index = tplg->index;
1276 switch (ec->hdr.ops.info) {
1277 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1278 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1279 err = soc_tplg_denum_create_values(se, ec);
1281 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1285 /* fall through to create texts */
1286 case SND_SOC_TPLG_CTL_ENUM:
1287 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1288 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1289 err = soc_tplg_denum_create_texts(se, ec);
1291 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1297 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1298 ec->hdr.ops.info, ec->hdr.name);
1302 /* map io handlers */
1303 err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
1305 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1309 /* pass control to driver for optional further init */
1310 err = soc_tplg_init_kcontrol(tplg, kc,
1311 (struct snd_soc_tplg_ctl_hdr *)ec);
1313 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1321 /* free values and texts */
1322 kfree(se->dobj.control.dvalues);
1323 for (i = 0; i < ec->items; i++)
1324 kfree(se->dobj.control.dtexts[i]);
1333 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1334 struct soc_tplg *tplg, int count)
1336 struct snd_soc_tplg_bytes_control *be;
1337 struct soc_bytes_ext *sbe;
1338 struct snd_kcontrol_new *kc;
1341 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
1345 for (i = 0; i < count; i++) {
1346 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1348 /* validate kcontrol */
1349 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1350 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1353 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1357 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1361 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1362 be->hdr.name, be->hdr.access);
1364 kc[i].name = be->hdr.name;
1365 kc[i].private_value = (long)sbe;
1366 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1367 kc[i].access = be->hdr.access;
1370 INIT_LIST_HEAD(&sbe->dobj.list);
1372 /* map standard io handlers and check for external handlers */
1373 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1375 soc_control_err(tplg, &be->hdr, be->hdr.name);
1380 /* pass control to driver for optional further init */
1381 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1382 (struct snd_soc_tplg_ctl_hdr *)be);
1384 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1394 for (--i; i >= 0; i--)
1395 kfree((void *)kc[i].private_value);
1401 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1402 struct snd_soc_tplg_dapm_widget *w)
1404 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1405 struct snd_soc_dapm_widget template, *widget;
1406 struct snd_soc_tplg_ctl_hdr *control_hdr;
1407 struct snd_soc_card *card = tplg->comp->card;
1410 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1411 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1413 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1414 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1417 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1420 memset(&template, 0, sizeof(template));
1422 /* map user to kernel widget ID */
1423 template.id = get_widget_id(w->id);
1424 if (template.id < 0)
1427 template.name = kstrdup(w->name, GFP_KERNEL);
1430 template.sname = kstrdup(w->sname, GFP_KERNEL);
1431 if (!template.sname) {
1435 template.reg = w->reg;
1436 template.shift = w->shift;
1437 template.mask = w->mask;
1438 template.subseq = w->subseq;
1439 template.on_val = w->invert ? 0 : 1;
1440 template.off_val = w->invert ? 1 : 0;
1441 template.ignore_suspend = w->ignore_suspend;
1442 template.event_flags = w->event_flags;
1443 template.dobj.index = tplg->index;
1446 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1447 if (w->num_kcontrols == 0) {
1448 template.num_kcontrols = 0;
1452 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1453 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1454 w->name, w->num_kcontrols, control_hdr->type);
1456 switch (control_hdr->ops.info) {
1457 case SND_SOC_TPLG_CTL_VOLSW:
1458 case SND_SOC_TPLG_CTL_STROBE:
1459 case SND_SOC_TPLG_CTL_VOLSW_SX:
1460 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1461 case SND_SOC_TPLG_CTL_RANGE:
1462 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1463 template.num_kcontrols = w->num_kcontrols;
1464 template.kcontrol_news =
1465 soc_tplg_dapm_widget_dmixer_create(tplg,
1466 template.num_kcontrols);
1467 if (!template.kcontrol_news) {
1472 case SND_SOC_TPLG_CTL_ENUM:
1473 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1474 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1475 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1476 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1477 template.dobj.widget.kcontrol_enum = 1;
1478 template.num_kcontrols = 1;
1479 template.kcontrol_news =
1480 soc_tplg_dapm_widget_denum_create(tplg);
1481 if (!template.kcontrol_news) {
1486 case SND_SOC_TPLG_CTL_BYTES:
1487 template.num_kcontrols = w->num_kcontrols;
1488 template.kcontrol_news =
1489 soc_tplg_dapm_widget_dbytes_create(tplg,
1490 template.num_kcontrols);
1491 if (!template.kcontrol_news) {
1497 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1498 control_hdr->ops.get, control_hdr->ops.put,
1499 control_hdr->ops.info);
1505 ret = soc_tplg_widget_load(tplg, &template, w);
1509 /* card dapm mutex is held by the core if we are loading topology
1510 * data during sound card init. */
1511 if (card->instantiated)
1512 widget = snd_soc_dapm_new_control(dapm, &template);
1514 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1515 if (widget == NULL) {
1516 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1521 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1522 widget->dobj.ops = tplg->ops;
1523 widget->dobj.index = tplg->index;
1524 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1528 kfree(template.sname);
1530 kfree(template.name);
1534 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1535 struct snd_soc_tplg_hdr *hdr)
1537 struct snd_soc_tplg_dapm_widget *widget;
1538 int ret, count = hdr->count, i;
1540 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1543 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1545 for (i = 0; i < count; i++) {
1546 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1547 ret = soc_tplg_dapm_widget_create(tplg, widget);
1549 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1556 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1558 struct snd_soc_card *card = tplg->comp->card;
1561 /* Card might not have been registered at this point.
1562 * If so, just return success.
1564 if (!card || !card->instantiated) {
1565 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1566 "Do not add new widgets now\n");
1570 ret = snd_soc_dapm_new_widgets(card);
1572 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1578 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1579 struct snd_soc_tplg_pcm *pcm)
1581 struct snd_soc_dai_driver *dai_drv;
1582 struct snd_soc_pcm_stream *stream;
1583 struct snd_soc_tplg_stream_caps *caps;
1586 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1587 if (dai_drv == NULL)
1590 dai_drv->name = pcm->dai_name;
1591 dai_drv->id = pcm->dai_id;
1593 if (pcm->playback) {
1594 stream = &dai_drv->playback;
1595 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1597 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1598 stream->channels_min = caps->channels_min;
1599 stream->channels_max = caps->channels_max;
1600 stream->rates = snd_pcm_rate_range_to_bits(caps->rate_min,
1602 stream->formats = caps->formats;
1606 stream = &dai_drv->capture;
1607 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1609 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1610 stream->channels_min = caps->channels_min;
1611 stream->channels_max = caps->channels_max;
1612 stream->rates = snd_pcm_rate_range_to_bits(caps->rate_min,
1614 stream->formats = caps->formats;
1617 /* pass control to component driver for optional further init */
1618 ret = soc_tplg_dai_load(tplg, dai_drv);
1620 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1625 dai_drv->dobj.index = tplg->index;
1626 dai_drv->dobj.ops = tplg->ops;
1627 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1628 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1630 /* register the DAI to the component */
1631 return snd_soc_register_dai(tplg->comp, dai_drv);
1634 static int soc_tplg_link_create(struct soc_tplg *tplg,
1635 struct snd_soc_tplg_pcm *pcm)
1637 struct snd_soc_dai_link *link;
1640 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1644 link->name = pcm->pcm_name;
1645 link->stream_name = pcm->pcm_name;
1647 /* pass control to component driver for optional further init */
1648 ret = soc_tplg_dai_link_load(tplg, link);
1650 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1655 link->dobj.index = tplg->index;
1656 link->dobj.ops = tplg->ops;
1657 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1658 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1660 snd_soc_add_dai_link(tplg->comp->card, link);
1664 /* create a FE DAI and DAI link from the PCM object */
1665 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1666 struct snd_soc_tplg_pcm *pcm)
1670 ret = soc_tplg_dai_create(tplg, pcm);
1674 return soc_tplg_link_create(tplg, pcm);
1677 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1678 struct snd_soc_tplg_hdr *hdr)
1680 struct snd_soc_tplg_pcm *pcm;
1681 int count = hdr->count;
1684 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1687 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1689 if (soc_tplg_check_elem_count(tplg,
1690 sizeof(struct snd_soc_tplg_pcm), count,
1691 hdr->payload_size, "PCM DAI")) {
1692 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1697 /* create the FE DAIs and DAI links */
1698 for (i = 0; i < count; i++) {
1699 soc_tplg_pcm_create(tplg, pcm);
1703 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1704 tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count;
1709 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
1710 struct snd_soc_tplg_hdr *hdr)
1712 struct snd_soc_tplg_manifest *manifest;
1714 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
1717 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
1718 tplg->pos += sizeof(struct snd_soc_tplg_manifest);
1720 if (tplg->comp && tplg->ops && tplg->ops->manifest)
1721 return tplg->ops->manifest(tplg->comp, manifest);
1723 dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n");
1727 /* validate header magic, size and type */
1728 static int soc_valid_header(struct soc_tplg *tplg,
1729 struct snd_soc_tplg_hdr *hdr)
1731 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
1734 /* big endian firmware objects not supported atm */
1735 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
1737 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1738 tplg->pass, hdr->magic,
1739 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1743 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
1745 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1746 tplg->pass, hdr->magic,
1747 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1751 if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
1753 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1754 tplg->pass, hdr->abi,
1755 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
1760 if (hdr->payload_size == 0) {
1761 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
1762 soc_tplg_get_hdr_offset(tplg));
1766 if (tplg->pass == hdr->type)
1768 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
1769 hdr->payload_size, hdr->type, hdr->version,
1770 hdr->vendor_type, tplg->pass);
1775 /* check header type and call appropriate handler */
1776 static int soc_tplg_load_header(struct soc_tplg *tplg,
1777 struct snd_soc_tplg_hdr *hdr)
1779 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
1781 /* check for matching ID */
1782 if (hdr->index != tplg->req_index &&
1783 hdr->index != SND_SOC_TPLG_INDEX_ALL)
1786 tplg->index = hdr->index;
1788 switch (hdr->type) {
1789 case SND_SOC_TPLG_TYPE_MIXER:
1790 case SND_SOC_TPLG_TYPE_ENUM:
1791 case SND_SOC_TPLG_TYPE_BYTES:
1792 return soc_tplg_kcontrol_elems_load(tplg, hdr);
1793 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
1794 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
1795 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
1796 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
1797 case SND_SOC_TPLG_TYPE_PCM:
1798 return soc_tplg_pcm_elems_load(tplg, hdr);
1799 case SND_SOC_TPLG_TYPE_MANIFEST:
1800 return soc_tplg_manifest_load(tplg, hdr);
1802 /* bespoke vendor data object */
1803 return soc_tplg_vendor_load(tplg, hdr);
1809 /* process the topology file headers */
1810 static int soc_tplg_process_headers(struct soc_tplg *tplg)
1812 struct snd_soc_tplg_hdr *hdr;
1815 tplg->pass = SOC_TPLG_PASS_START;
1817 /* process the header types from start to end */
1818 while (tplg->pass <= SOC_TPLG_PASS_END) {
1820 tplg->hdr_pos = tplg->fw->data;
1821 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1823 while (!soc_tplg_is_eof(tplg)) {
1825 /* make sure header is valid before loading */
1826 ret = soc_valid_header(tplg, hdr);
1832 /* load the header object */
1833 ret = soc_tplg_load_header(tplg, hdr);
1837 /* goto next header */
1838 tplg->hdr_pos += hdr->payload_size +
1839 sizeof(struct snd_soc_tplg_hdr);
1840 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1843 /* next data type pass */
1847 /* signal DAPM we are complete */
1848 ret = soc_tplg_dapm_complete(tplg);
1851 "ASoC: failed to initialise DAPM from Firmware\n");
1856 static int soc_tplg_load(struct soc_tplg *tplg)
1860 ret = soc_tplg_process_headers(tplg);
1862 soc_tplg_complete(tplg);
1867 /* load audio component topology from "firmware" file */
1868 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
1869 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
1871 struct soc_tplg tplg;
1873 /* setup parsing context */
1874 memset(&tplg, 0, sizeof(tplg));
1876 tplg.dev = comp->dev;
1879 tplg.req_index = id;
1880 tplg.io_ops = ops->io_ops;
1881 tplg.io_ops_count = ops->io_ops_count;
1882 tplg.bytes_ext_ops = ops->bytes_ext_ops;
1883 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
1885 return soc_tplg_load(&tplg);
1887 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
1889 /* remove this dynamic widget */
1890 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
1892 /* make sure we are a widget */
1893 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
1896 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
1898 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
1900 /* remove all dynamic widgets from this DAPM context */
1901 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
1904 struct snd_soc_dapm_widget *w, *next_w;
1906 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1908 /* make sure we are a widget with correct context */
1909 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
1913 if (w->dobj.index != index &&
1914 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
1916 /* check and free and dynamic widget kcontrols */
1917 snd_soc_tplg_widget_remove(w);
1918 snd_soc_dapm_free_widget(w);
1920 snd_soc_dapm_reset_cache(dapm);
1922 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
1924 /* remove dynamic controls from the component driver */
1925 int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
1927 struct snd_soc_dobj *dobj, *next_dobj;
1928 int pass = SOC_TPLG_PASS_END;
1930 /* process the header types from end to start */
1931 while (pass >= SOC_TPLG_PASS_START) {
1933 /* remove mixer controls */
1934 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
1938 if (dobj->index != index &&
1939 dobj->index != SND_SOC_TPLG_INDEX_ALL)
1942 switch (dobj->type) {
1943 case SND_SOC_DOBJ_MIXER:
1944 remove_mixer(comp, dobj, pass);
1946 case SND_SOC_DOBJ_ENUM:
1947 remove_enum(comp, dobj, pass);
1949 case SND_SOC_DOBJ_BYTES:
1950 remove_bytes(comp, dobj, pass);
1952 case SND_SOC_DOBJ_WIDGET:
1953 remove_widget(comp, dobj, pass);
1955 case SND_SOC_DOBJ_PCM:
1956 remove_dai(comp, dobj, pass);
1958 case SND_SOC_DOBJ_DAI_LINK:
1959 remove_link(comp, dobj, pass);
1962 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
1970 /* let caller know if FW can be freed when no objects are left */
1971 return !list_empty(&comp->dobj_list);
1973 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);