2 * soc-topology.c -- ALSA SoC Topology
4 * Copyright (C) 2012 Texas Instruments Inc.
5 * Copyright (C) 2015 Intel Corporation.
7 * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
8 * K, Mythri P <mythri.p.k@intel.com>
9 * Prusty, Subhransu S <subhransu.s.prusty@intel.com>
10 * B, Jayachandran <jayachandran.b@intel.com>
11 * Abdullah, Omair M <omair.m.abdullah@intel.com>
12 * Jin, Yao <yao.jin@intel.com>
13 * Lin, Mengdong <mengdong.lin@intel.com>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * Add support to read audio firmware topology alongside firmware text. The
21 * topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
22 * equalizers, firmware, coefficients etc.
24 * This file only manages the core ALSA and ASoC components, all other bespoke
25 * firmware topology data is passed to component drivers for bespoke handling.
28 #include <linux/kernel.h>
29 #include <linux/export.h>
30 #include <linux/list.h>
31 #include <linux/firmware.h>
32 #include <linux/slab.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/soc-topology.h>
38 * We make several passes over the data (since it wont necessarily be ordered)
39 * and process objects in the following order. This guarantees the component
40 * drivers will be ready with any vendor data before the mixers and DAPM objects
41 * are loaded (that may make use of the vendor data).
43 #define SOC_TPLG_PASS_MANIFEST 0
44 #define SOC_TPLG_PASS_VENDOR 1
45 #define SOC_TPLG_PASS_MIXER 2
46 #define SOC_TPLG_PASS_WIDGET 3
47 #define SOC_TPLG_PASS_GRAPH 4
48 #define SOC_TPLG_PASS_PINS 5
49 #define SOC_TPLG_PASS_PCM_DAI 6
51 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
52 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_PCM_DAI
55 const struct firmware
*fw
;
57 /* runtime FW parsing */
58 const u8
*pos
; /* read postion */
59 const u8
*hdr_pos
; /* header position */
60 unsigned int pass
; /* pass number */
62 /* component caller */
64 struct snd_soc_component
*comp
;
65 u32 index
; /* current block index */
66 u32 req_index
; /* required index, only loaded/free matching blocks */
68 /* kcontrol operations */
69 const struct snd_soc_tplg_kcontrol_ops
*io_ops
;
72 /* optional fw loading callbacks to component drivers */
73 struct snd_soc_tplg_ops
*ops
;
76 static int soc_tplg_process_headers(struct soc_tplg
*tplg
);
77 static void soc_tplg_complete(struct soc_tplg
*tplg
);
78 struct snd_soc_dapm_widget
*
79 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
80 const struct snd_soc_dapm_widget
*widget
);
81 struct snd_soc_dapm_widget
*
82 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
83 const struct snd_soc_dapm_widget
*widget
);
85 /* check we dont overflow the data for this control chunk */
86 static int soc_tplg_check_elem_count(struct soc_tplg
*tplg
, size_t elem_size
,
87 unsigned int count
, size_t bytes
, const char *elem_type
)
89 const u8
*end
= tplg
->pos
+ elem_size
* count
;
91 if (end
> tplg
->fw
->data
+ tplg
->fw
->size
) {
92 dev_err(tplg
->dev
, "ASoC: %s overflow end of data\n",
97 /* check there is enough room in chunk for control.
98 extra bytes at the end of control are for vendor data here */
99 if (elem_size
* count
> bytes
) {
101 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
102 elem_type
, count
, elem_size
, bytes
);
109 static inline int soc_tplg_is_eof(struct soc_tplg
*tplg
)
111 const u8
*end
= tplg
->hdr_pos
;
113 if (end
>= tplg
->fw
->data
+ tplg
->fw
->size
)
118 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg
*tplg
)
120 return (unsigned long)(tplg
->hdr_pos
- tplg
->fw
->data
);
123 static inline unsigned long soc_tplg_get_offset(struct soc_tplg
*tplg
)
125 return (unsigned long)(tplg
->pos
- tplg
->fw
->data
);
128 /* mapping of Kcontrol types and associated operations. */
129 static const struct snd_soc_tplg_kcontrol_ops io_ops
[] = {
130 {SND_SOC_TPLG_CTL_VOLSW
, snd_soc_get_volsw
,
131 snd_soc_put_volsw
, snd_soc_info_volsw
},
132 {SND_SOC_TPLG_CTL_VOLSW_SX
, snd_soc_get_volsw_sx
,
133 snd_soc_put_volsw_sx
, NULL
},
134 {SND_SOC_TPLG_CTL_ENUM
, snd_soc_get_enum_double
,
135 snd_soc_put_enum_double
, snd_soc_info_enum_double
},
136 {SND_SOC_TPLG_CTL_ENUM_VALUE
, snd_soc_get_enum_double
,
137 snd_soc_put_enum_double
, NULL
},
138 {SND_SOC_TPLG_CTL_BYTES
, snd_soc_bytes_get
,
139 snd_soc_bytes_put
, snd_soc_bytes_info
},
140 {SND_SOC_TPLG_CTL_RANGE
, snd_soc_get_volsw_range
,
141 snd_soc_put_volsw_range
, snd_soc_info_volsw_range
},
142 {SND_SOC_TPLG_CTL_VOLSW_XR_SX
, snd_soc_get_xr_sx
,
143 snd_soc_put_xr_sx
, snd_soc_info_xr_sx
},
144 {SND_SOC_TPLG_CTL_STROBE
, snd_soc_get_strobe
,
145 snd_soc_put_strobe
, NULL
},
146 {SND_SOC_TPLG_DAPM_CTL_VOLSW
, snd_soc_dapm_get_volsw
,
147 snd_soc_dapm_put_volsw
, snd_soc_info_volsw
},
148 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
, snd_soc_dapm_get_enum_double
,
149 snd_soc_dapm_put_enum_double
, snd_soc_info_enum_double
},
150 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
, snd_soc_dapm_get_enum_double
,
151 snd_soc_dapm_put_enum_double
, NULL
},
152 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
, snd_soc_dapm_get_enum_double
,
153 snd_soc_dapm_put_enum_double
, NULL
},
154 {SND_SOC_TPLG_DAPM_CTL_PIN
, snd_soc_dapm_get_pin_switch
,
155 snd_soc_dapm_put_pin_switch
, snd_soc_dapm_info_pin_switch
},
158 struct soc_tplg_map
{
163 /* mapping of widget types from UAPI IDs to kernel IDs */
164 static const struct soc_tplg_map dapm_map
[] = {
165 {SND_SOC_TPLG_DAPM_INPUT
, snd_soc_dapm_input
},
166 {SND_SOC_TPLG_DAPM_OUTPUT
, snd_soc_dapm_output
},
167 {SND_SOC_TPLG_DAPM_MUX
, snd_soc_dapm_mux
},
168 {SND_SOC_TPLG_DAPM_MIXER
, snd_soc_dapm_mixer
},
169 {SND_SOC_TPLG_DAPM_PGA
, snd_soc_dapm_pga
},
170 {SND_SOC_TPLG_DAPM_OUT_DRV
, snd_soc_dapm_out_drv
},
171 {SND_SOC_TPLG_DAPM_ADC
, snd_soc_dapm_adc
},
172 {SND_SOC_TPLG_DAPM_DAC
, snd_soc_dapm_dac
},
173 {SND_SOC_TPLG_DAPM_SWITCH
, snd_soc_dapm_switch
},
174 {SND_SOC_TPLG_DAPM_PRE
, snd_soc_dapm_pre
},
175 {SND_SOC_TPLG_DAPM_POST
, snd_soc_dapm_post
},
176 {SND_SOC_TPLG_DAPM_AIF_IN
, snd_soc_dapm_aif_in
},
177 {SND_SOC_TPLG_DAPM_AIF_OUT
, snd_soc_dapm_aif_out
},
178 {SND_SOC_TPLG_DAPM_DAI_IN
, snd_soc_dapm_dai_in
},
179 {SND_SOC_TPLG_DAPM_DAI_OUT
, snd_soc_dapm_dai_out
},
180 {SND_SOC_TPLG_DAPM_DAI_LINK
, snd_soc_dapm_dai_link
},
183 static int tplc_chan_get_reg(struct soc_tplg
*tplg
,
184 struct snd_soc_tplg_channel
*chan
, int map
)
188 for (i
= 0; i
< SND_SOC_TPLG_MAX_CHAN
; i
++) {
189 if (chan
[i
].id
== map
)
196 static int tplc_chan_get_shift(struct soc_tplg
*tplg
,
197 struct snd_soc_tplg_channel
*chan
, int map
)
201 for (i
= 0; i
< SND_SOC_TPLG_MAX_CHAN
; i
++) {
202 if (chan
[i
].id
== map
)
203 return chan
[i
].shift
;
209 static int get_widget_id(int tplg_type
)
213 for (i
= 0; i
< ARRAY_SIZE(dapm_map
); i
++) {
214 if (tplg_type
== dapm_map
[i
].uid
)
215 return dapm_map
[i
].kid
;
221 static enum snd_soc_dobj_type
get_dobj_mixer_type(
222 struct snd_soc_tplg_ctl_hdr
*control_hdr
)
224 if (control_hdr
== NULL
)
225 return SND_SOC_DOBJ_NONE
;
227 switch (control_hdr
->ops
.info
) {
228 case SND_SOC_TPLG_CTL_VOLSW
:
229 case SND_SOC_TPLG_CTL_VOLSW_SX
:
230 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
231 case SND_SOC_TPLG_CTL_RANGE
:
232 case SND_SOC_TPLG_CTL_STROBE
:
233 return SND_SOC_DOBJ_MIXER
;
234 case SND_SOC_TPLG_CTL_ENUM
:
235 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
236 return SND_SOC_DOBJ_ENUM
;
237 case SND_SOC_TPLG_CTL_BYTES
:
238 return SND_SOC_DOBJ_BYTES
;
240 return SND_SOC_DOBJ_NONE
;
244 static enum snd_soc_dobj_type
get_dobj_type(struct snd_soc_tplg_hdr
*hdr
,
245 struct snd_soc_tplg_ctl_hdr
*control_hdr
)
248 case SND_SOC_TPLG_TYPE_MIXER
:
249 return get_dobj_mixer_type(control_hdr
);
250 case SND_SOC_TPLG_TYPE_DAPM_GRAPH
:
251 case SND_SOC_TPLG_TYPE_MANIFEST
:
252 return SND_SOC_DOBJ_NONE
;
253 case SND_SOC_TPLG_TYPE_DAPM_WIDGET
:
254 return SND_SOC_DOBJ_WIDGET
;
255 case SND_SOC_TPLG_TYPE_DAI_LINK
:
256 return SND_SOC_DOBJ_DAI_LINK
;
257 case SND_SOC_TPLG_TYPE_PCM
:
258 return SND_SOC_DOBJ_PCM
;
259 case SND_SOC_TPLG_TYPE_CODEC_LINK
:
260 return SND_SOC_DOBJ_CODEC_LINK
;
262 return SND_SOC_DOBJ_NONE
;
266 static inline void soc_bind_err(struct soc_tplg
*tplg
,
267 struct snd_soc_tplg_ctl_hdr
*hdr
, int index
)
270 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
271 hdr
->ops
.get
, hdr
->ops
.put
, hdr
->ops
.info
, index
,
272 soc_tplg_get_offset(tplg
));
275 static inline void soc_control_err(struct soc_tplg
*tplg
,
276 struct snd_soc_tplg_ctl_hdr
*hdr
, const char *name
)
279 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
280 name
, hdr
->ops
.get
, hdr
->ops
.put
, hdr
->ops
.info
,
281 soc_tplg_get_offset(tplg
));
284 /* pass vendor data to component driver for processing */
285 static int soc_tplg_vendor_load_(struct soc_tplg
*tplg
,
286 struct snd_soc_tplg_hdr
*hdr
)
290 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->vendor_load
)
291 ret
= tplg
->ops
->vendor_load(tplg
->comp
, hdr
);
293 dev_err(tplg
->dev
, "ASoC: no vendor load callback for ID %d\n",
300 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
301 soc_tplg_get_hdr_offset(tplg
),
302 soc_tplg_get_hdr_offset(tplg
),
303 hdr
->type
, hdr
->vendor_type
);
307 /* pass vendor data to component driver for processing */
308 static int soc_tplg_vendor_load(struct soc_tplg
*tplg
,
309 struct snd_soc_tplg_hdr
*hdr
)
311 if (tplg
->pass
!= SOC_TPLG_PASS_VENDOR
)
314 return soc_tplg_vendor_load_(tplg
, hdr
);
317 /* optionally pass new dynamic widget to component driver. This is mainly for
318 * external widgets where we can assign private data/ops */
319 static int soc_tplg_widget_load(struct soc_tplg
*tplg
,
320 struct snd_soc_dapm_widget
*w
, struct snd_soc_tplg_dapm_widget
*tplg_w
)
322 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->widget_load
)
323 return tplg
->ops
->widget_load(tplg
->comp
, w
, tplg_w
);
328 /* pass dynamic FEs configurations to component driver */
329 static int soc_tplg_pcm_dai_load(struct soc_tplg
*tplg
,
330 struct snd_soc_tplg_pcm_dai
*pcm_dai
, int num_pcm_dai
)
332 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->pcm_dai_load
)
333 return tplg
->ops
->pcm_dai_load(tplg
->comp
, pcm_dai
, num_pcm_dai
);
338 /* tell the component driver that all firmware has been loaded in this request */
339 static void soc_tplg_complete(struct soc_tplg
*tplg
)
341 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->complete
)
342 tplg
->ops
->complete(tplg
->comp
);
345 /* add a dynamic kcontrol */
346 static int soc_tplg_add_dcontrol(struct snd_card
*card
, struct device
*dev
,
347 const struct snd_kcontrol_new
*control_new
, const char *prefix
,
348 void *data
, struct snd_kcontrol
**kcontrol
)
352 *kcontrol
= snd_soc_cnew(control_new
, data
, control_new
->name
, prefix
);
353 if (*kcontrol
== NULL
) {
354 dev_err(dev
, "ASoC: Failed to create new kcontrol %s\n",
359 err
= snd_ctl_add(card
, *kcontrol
);
361 dev_err(dev
, "ASoC: Failed to add %s: %d\n",
362 control_new
->name
, err
);
369 /* add a dynamic kcontrol for component driver */
370 static int soc_tplg_add_kcontrol(struct soc_tplg
*tplg
,
371 struct snd_kcontrol_new
*k
, struct snd_kcontrol
**kcontrol
)
373 struct snd_soc_component
*comp
= tplg
->comp
;
375 return soc_tplg_add_dcontrol(comp
->card
->snd_card
,
376 comp
->dev
, k
, NULL
, comp
, kcontrol
);
379 /* remove a mixer kcontrol */
380 static void remove_mixer(struct snd_soc_component
*comp
,
381 struct snd_soc_dobj
*dobj
, int pass
)
383 struct snd_card
*card
= comp
->card
->snd_card
;
384 struct soc_mixer_control
*sm
=
385 container_of(dobj
, struct soc_mixer_control
, dobj
);
386 const unsigned int *p
= NULL
;
388 if (pass
!= SOC_TPLG_PASS_MIXER
)
391 if (dobj
->ops
&& dobj
->ops
->control_unload
)
392 dobj
->ops
->control_unload(comp
, dobj
);
394 if (sm
->dobj
.control
.kcontrol
->tlv
.p
)
395 p
= sm
->dobj
.control
.kcontrol
->tlv
.p
;
396 snd_ctl_remove(card
, sm
->dobj
.control
.kcontrol
);
397 list_del(&sm
->dobj
.list
);
402 /* remove an enum kcontrol */
403 static void remove_enum(struct snd_soc_component
*comp
,
404 struct snd_soc_dobj
*dobj
, int pass
)
406 struct snd_card
*card
= comp
->card
->snd_card
;
407 struct soc_enum
*se
= container_of(dobj
, struct soc_enum
, dobj
);
410 if (pass
!= SOC_TPLG_PASS_MIXER
)
413 if (dobj
->ops
&& dobj
->ops
->control_unload
)
414 dobj
->ops
->control_unload(comp
, dobj
);
416 snd_ctl_remove(card
, se
->dobj
.control
.kcontrol
);
417 list_del(&se
->dobj
.list
);
419 kfree(se
->dobj
.control
.dvalues
);
420 for (i
= 0; i
< se
->items
; i
++)
421 kfree(se
->dobj
.control
.dtexts
[i
]);
425 /* remove a byte kcontrol */
426 static void remove_bytes(struct snd_soc_component
*comp
,
427 struct snd_soc_dobj
*dobj
, int pass
)
429 struct snd_card
*card
= comp
->card
->snd_card
;
430 struct soc_bytes_ext
*sb
=
431 container_of(dobj
, struct soc_bytes_ext
, dobj
);
433 if (pass
!= SOC_TPLG_PASS_MIXER
)
436 if (dobj
->ops
&& dobj
->ops
->control_unload
)
437 dobj
->ops
->control_unload(comp
, dobj
);
439 snd_ctl_remove(card
, sb
->dobj
.control
.kcontrol
);
440 list_del(&sb
->dobj
.list
);
444 /* remove a widget and it's kcontrols - routes must be removed first */
445 static void remove_widget(struct snd_soc_component
*comp
,
446 struct snd_soc_dobj
*dobj
, int pass
)
448 struct snd_card
*card
= comp
->card
->snd_card
;
449 struct snd_soc_dapm_widget
*w
=
450 container_of(dobj
, struct snd_soc_dapm_widget
, dobj
);
453 if (pass
!= SOC_TPLG_PASS_WIDGET
)
456 if (dobj
->ops
&& dobj
->ops
->widget_unload
)
457 dobj
->ops
->widget_unload(comp
, dobj
);
460 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
461 * The enum may either have an array of values or strings.
463 if (dobj
->widget
.kcontrol_enum
) {
464 /* enumerated widget mixer */
465 struct soc_enum
*se
=
466 (struct soc_enum
*)w
->kcontrols
[0]->private_value
;
468 snd_ctl_remove(card
, w
->kcontrols
[0]);
470 kfree(se
->dobj
.control
.dvalues
);
471 for (i
= 0; i
< se
->items
; i
++)
472 kfree(se
->dobj
.control
.dtexts
[i
]);
475 kfree(w
->kcontrol_news
);
477 /* non enumerated widget mixer */
478 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
479 struct snd_kcontrol
*kcontrol
= w
->kcontrols
[i
];
480 struct soc_mixer_control
*sm
=
481 (struct soc_mixer_control
*) kcontrol
->private_value
;
483 kfree(w
->kcontrols
[i
]->tlv
.p
);
485 snd_ctl_remove(card
, w
->kcontrols
[i
]);
488 kfree(w
->kcontrol_news
);
490 /* widget w is freed by soc-dapm.c */
493 /* remove PCM DAI configurations */
494 static void remove_pcm_dai(struct snd_soc_component
*comp
,
495 struct snd_soc_dobj
*dobj
, int pass
)
497 if (pass
!= SOC_TPLG_PASS_PCM_DAI
)
500 if (dobj
->ops
&& dobj
->ops
->pcm_dai_unload
)
501 dobj
->ops
->pcm_dai_unload(comp
, dobj
);
503 list_del(&dobj
->list
);
507 /* bind a kcontrol to it's IO handlers */
508 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr
*hdr
,
509 struct snd_kcontrol_new
*k
,
510 const struct snd_soc_tplg_kcontrol_ops
*ops
, int num_ops
,
511 const struct snd_soc_tplg_kcontrol_ops
*bops
, int num_bops
)
515 /* try and map standard kcontrols handler first */
516 for (i
= 0; i
< num_ops
; i
++) {
518 if (ops
[i
].id
== hdr
->ops
.put
)
520 if (ops
[i
].id
== hdr
->ops
.get
)
522 if (ops
[i
].id
== hdr
->ops
.info
)
523 k
->info
= ops
[i
].info
;
526 /* standard handlers found ? */
527 if (k
->put
&& k
->get
&& k
->info
)
530 /* none found so try bespoke handlers */
531 for (i
= 0; i
< num_bops
; i
++) {
533 if (k
->put
== NULL
&& bops
[i
].id
== hdr
->ops
.put
)
534 k
->put
= bops
[i
].put
;
535 if (k
->get
== NULL
&& bops
[i
].id
== hdr
->ops
.get
)
536 k
->get
= bops
[i
].get
;
537 if (k
->info
== NULL
&& ops
[i
].id
== hdr
->ops
.info
)
538 k
->info
= bops
[i
].info
;
541 /* bespoke handlers found ? */
542 if (k
->put
&& k
->get
&& k
->info
)
545 /* nothing to bind */
549 /* bind a widgets to it's evnt handlers */
550 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget
*w
,
551 const struct snd_soc_tplg_widget_events
*events
,
552 int num_events
, u16 event_type
)
558 for (i
= 0; i
< num_events
; i
++) {
559 if (event_type
== events
[i
].type
) {
561 /* found - so assign event */
562 w
->event
= events
[i
].event_handler
;
570 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event
);
572 /* optionally pass new dynamic kcontrol to component driver. */
573 static int soc_tplg_init_kcontrol(struct soc_tplg
*tplg
,
574 struct snd_kcontrol_new
*k
, struct snd_soc_tplg_ctl_hdr
*hdr
)
576 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->control_load
)
577 return tplg
->ops
->control_load(tplg
->comp
, k
, hdr
);
582 static int soc_tplg_create_tlv(struct soc_tplg
*tplg
,
583 struct snd_kcontrol_new
*kc
, struct snd_soc_tplg_ctl_tlv
*tplg_tlv
)
585 struct snd_ctl_tlv
*tlv
;
588 if (tplg_tlv
->count
== 0)
591 size
= ((tplg_tlv
->count
+ (sizeof(unsigned int) - 1)) &
592 ~(sizeof(unsigned int) - 1));
593 tlv
= kzalloc(sizeof(*tlv
) + size
, GFP_KERNEL
);
597 dev_dbg(tplg
->dev
, " created TLV type %d size %d bytes\n",
598 tplg_tlv
->numid
, size
);
600 tlv
->numid
= tplg_tlv
->numid
;
602 memcpy(&tlv
->tlv
[0], tplg_tlv
->data
, size
);
603 kc
->tlv
.p
= (void *)tlv
;
608 static inline void soc_tplg_free_tlv(struct soc_tplg
*tplg
,
609 struct snd_kcontrol_new
*kc
)
614 static int soc_tplg_dbytes_create(struct soc_tplg
*tplg
, unsigned int count
,
617 struct snd_soc_tplg_bytes_control
*be
;
618 struct soc_bytes_ext
*sbe
;
619 struct snd_kcontrol_new kc
;
622 if (soc_tplg_check_elem_count(tplg
,
623 sizeof(struct snd_soc_tplg_bytes_control
), count
,
624 size
, "mixer bytes")) {
625 dev_err(tplg
->dev
, "ASoC: Invalid count %d for byte control\n",
630 for (i
= 0; i
< count
; i
++) {
631 be
= (struct snd_soc_tplg_bytes_control
*)tplg
->pos
;
633 /* validate kcontrol */
634 if (strnlen(be
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
635 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
638 sbe
= kzalloc(sizeof(*sbe
), GFP_KERNEL
);
642 tplg
->pos
+= (sizeof(struct snd_soc_tplg_bytes_control
) +
646 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
647 be
->hdr
.name
, be
->hdr
.access
);
649 memset(&kc
, 0, sizeof(kc
));
650 kc
.name
= be
->hdr
.name
;
651 kc
.private_value
= (long)sbe
;
652 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
653 kc
.access
= be
->hdr
.access
;
656 sbe
->dobj
.type
= SND_SOC_DOBJ_BYTES
;
657 sbe
->dobj
.ops
= tplg
->ops
;
658 INIT_LIST_HEAD(&sbe
->dobj
.list
);
660 /* map io handlers */
661 err
= soc_tplg_kcontrol_bind_io(&be
->hdr
, &kc
, io_ops
,
662 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
664 soc_control_err(tplg
, &be
->hdr
, be
->hdr
.name
);
669 /* pass control to driver for optional further init */
670 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
671 (struct snd_soc_tplg_ctl_hdr
*)be
);
673 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
679 /* register control here */
680 err
= soc_tplg_add_kcontrol(tplg
, &kc
,
681 &sbe
->dobj
.control
.kcontrol
);
683 dev_err(tplg
->dev
, "ASoC: failed to add %s\n",
689 list_add(&sbe
->dobj
.list
, &tplg
->comp
->dobj_list
);
695 static int soc_tplg_dmixer_create(struct soc_tplg
*tplg
, unsigned int count
,
698 struct snd_soc_tplg_mixer_control
*mc
;
699 struct soc_mixer_control
*sm
;
700 struct snd_kcontrol_new kc
;
703 if (soc_tplg_check_elem_count(tplg
,
704 sizeof(struct snd_soc_tplg_mixer_control
),
705 count
, size
, "mixers")) {
707 dev_err(tplg
->dev
, "ASoC: invalid count %d for controls\n",
712 for (i
= 0; i
< count
; i
++) {
713 mc
= (struct snd_soc_tplg_mixer_control
*)tplg
->pos
;
715 /* validate kcontrol */
716 if (strnlen(mc
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
717 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
720 sm
= kzalloc(sizeof(*sm
), GFP_KERNEL
);
723 tplg
->pos
+= (sizeof(struct snd_soc_tplg_mixer_control
) +
727 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
728 mc
->hdr
.name
, mc
->hdr
.access
);
730 memset(&kc
, 0, sizeof(kc
));
731 kc
.name
= mc
->hdr
.name
;
732 kc
.private_value
= (long)sm
;
733 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
734 kc
.access
= mc
->hdr
.access
;
736 /* we only support FL/FR channel mapping atm */
737 sm
->reg
= tplc_chan_get_reg(tplg
, mc
->channel
,
739 sm
->rreg
= tplc_chan_get_reg(tplg
, mc
->channel
,
741 sm
->shift
= tplc_chan_get_shift(tplg
, mc
->channel
,
743 sm
->rshift
= tplc_chan_get_shift(tplg
, mc
->channel
,
748 sm
->invert
= mc
->invert
;
749 sm
->platform_max
= mc
->platform_max
;
750 sm
->dobj
.index
= tplg
->index
;
751 sm
->dobj
.ops
= tplg
->ops
;
752 sm
->dobj
.type
= SND_SOC_DOBJ_MIXER
;
753 INIT_LIST_HEAD(&sm
->dobj
.list
);
755 /* map io handlers */
756 err
= soc_tplg_kcontrol_bind_io(&mc
->hdr
, &kc
, io_ops
,
757 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
759 soc_control_err(tplg
, &mc
->hdr
, mc
->hdr
.name
);
764 /* pass control to driver for optional further init */
765 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
766 (struct snd_soc_tplg_ctl_hdr
*) mc
);
768 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
774 /* create any TLV data */
775 soc_tplg_create_tlv(tplg
, &kc
, &mc
->tlv
);
777 /* register control here */
778 err
= soc_tplg_add_kcontrol(tplg
, &kc
,
779 &sm
->dobj
.control
.kcontrol
);
781 dev_err(tplg
->dev
, "ASoC: failed to add %s\n",
783 soc_tplg_free_tlv(tplg
, &kc
);
788 list_add(&sm
->dobj
.list
, &tplg
->comp
->dobj_list
);
794 static int soc_tplg_denum_create_texts(struct soc_enum
*se
,
795 struct snd_soc_tplg_enum_control
*ec
)
799 se
->dobj
.control
.dtexts
=
800 kzalloc(sizeof(char *) * ec
->items
, GFP_KERNEL
);
801 if (se
->dobj
.control
.dtexts
== NULL
)
804 for (i
= 0; i
< ec
->items
; i
++) {
806 if (strnlen(ec
->texts
[i
], SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
807 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) {
812 se
->dobj
.control
.dtexts
[i
] = kstrdup(ec
->texts
[i
], GFP_KERNEL
);
813 if (!se
->dobj
.control
.dtexts
[i
]) {
822 for (--i
; i
>= 0; i
--)
823 kfree(se
->dobj
.control
.dtexts
[i
]);
824 kfree(se
->dobj
.control
.dtexts
);
828 static int soc_tplg_denum_create_values(struct soc_enum
*se
,
829 struct snd_soc_tplg_enum_control
*ec
)
831 if (ec
->items
> sizeof(*ec
->values
))
834 se
->dobj
.control
.dvalues
=
835 kmalloc(ec
->items
* sizeof(u32
), GFP_KERNEL
);
836 if (!se
->dobj
.control
.dvalues
)
839 memcpy(se
->dobj
.control
.dvalues
, ec
->values
, ec
->items
* sizeof(u32
));
843 static int soc_tplg_denum_create(struct soc_tplg
*tplg
, unsigned int count
,
846 struct snd_soc_tplg_enum_control
*ec
;
848 struct snd_kcontrol_new kc
;
851 if (soc_tplg_check_elem_count(tplg
,
852 sizeof(struct snd_soc_tplg_enum_control
),
853 count
, size
, "enums")) {
855 dev_err(tplg
->dev
, "ASoC: invalid count %d for enum controls\n",
860 for (i
= 0; i
< count
; i
++) {
861 ec
= (struct snd_soc_tplg_enum_control
*)tplg
->pos
;
862 tplg
->pos
+= (sizeof(struct snd_soc_tplg_enum_control
) +
865 /* validate kcontrol */
866 if (strnlen(ec
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
867 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
870 se
= kzalloc((sizeof(*se
)), GFP_KERNEL
);
874 dev_dbg(tplg
->dev
, "ASoC: adding enum kcontrol %s size %d\n",
875 ec
->hdr
.name
, ec
->items
);
877 memset(&kc
, 0, sizeof(kc
));
878 kc
.name
= ec
->hdr
.name
;
879 kc
.private_value
= (long)se
;
880 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
881 kc
.access
= ec
->hdr
.access
;
883 se
->reg
= tplc_chan_get_reg(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
884 se
->shift_l
= tplc_chan_get_shift(tplg
, ec
->channel
,
886 se
->shift_r
= tplc_chan_get_shift(tplg
, ec
->channel
,
889 se
->items
= ec
->items
;
891 se
->dobj
.index
= tplg
->index
;
892 se
->dobj
.type
= SND_SOC_DOBJ_ENUM
;
893 se
->dobj
.ops
= tplg
->ops
;
894 INIT_LIST_HEAD(&se
->dobj
.list
);
896 switch (ec
->hdr
.ops
.info
) {
897 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
898 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
899 err
= soc_tplg_denum_create_values(se
, ec
);
902 "ASoC: could not create values for %s\n",
907 /* fall through and create texts */
908 case SND_SOC_TPLG_CTL_ENUM
:
909 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
910 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
911 err
= soc_tplg_denum_create_texts(se
, ec
);
914 "ASoC: could not create texts for %s\n",
922 "ASoC: invalid enum control type %d for %s\n",
923 ec
->hdr
.ops
.info
, ec
->hdr
.name
);
928 /* map io handlers */
929 err
= soc_tplg_kcontrol_bind_io(&ec
->hdr
, &kc
, io_ops
,
930 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
932 soc_control_err(tplg
, &ec
->hdr
, ec
->hdr
.name
);
937 /* pass control to driver for optional further init */
938 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
939 (struct snd_soc_tplg_ctl_hdr
*) ec
);
941 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
947 /* register control here */
948 ret
= soc_tplg_add_kcontrol(tplg
,
949 &kc
, &se
->dobj
.control
.kcontrol
);
951 dev_err(tplg
->dev
, "ASoC: could not add kcontrol %s\n",
957 list_add(&se
->dobj
.list
, &tplg
->comp
->dobj_list
);
963 static int soc_tplg_kcontrol_elems_load(struct soc_tplg
*tplg
,
964 struct snd_soc_tplg_hdr
*hdr
)
966 struct snd_soc_tplg_ctl_hdr
*control_hdr
;
969 if (tplg
->pass
!= SOC_TPLG_PASS_MIXER
) {
970 tplg
->pos
+= hdr
->size
+ hdr
->payload_size
;
974 dev_dbg(tplg
->dev
, "ASoC: adding %d kcontrols at 0x%lx\n", hdr
->count
,
975 soc_tplg_get_offset(tplg
));
977 for (i
= 0; i
< hdr
->count
; i
++) {
979 control_hdr
= (struct snd_soc_tplg_ctl_hdr
*)tplg
->pos
;
981 switch (control_hdr
->ops
.info
) {
982 case SND_SOC_TPLG_CTL_VOLSW
:
983 case SND_SOC_TPLG_CTL_STROBE
:
984 case SND_SOC_TPLG_CTL_VOLSW_SX
:
985 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
986 case SND_SOC_TPLG_CTL_RANGE
:
987 case SND_SOC_TPLG_DAPM_CTL_VOLSW
:
988 case SND_SOC_TPLG_DAPM_CTL_PIN
:
989 soc_tplg_dmixer_create(tplg
, 1, hdr
->payload_size
);
991 case SND_SOC_TPLG_CTL_ENUM
:
992 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
993 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
994 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
995 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
996 soc_tplg_denum_create(tplg
, 1, hdr
->payload_size
);
998 case SND_SOC_TPLG_CTL_BYTES
:
999 soc_tplg_dbytes_create(tplg
, 1, hdr
->payload_size
);
1002 soc_bind_err(tplg
, control_hdr
, i
);
1010 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg
*tplg
,
1011 struct snd_soc_tplg_hdr
*hdr
)
1013 struct snd_soc_dapm_context
*dapm
= &tplg
->comp
->dapm
;
1014 struct snd_soc_dapm_route route
;
1015 struct snd_soc_tplg_dapm_graph_elem
*elem
;
1016 int count
= hdr
->count
, i
;
1018 if (tplg
->pass
!= SOC_TPLG_PASS_GRAPH
) {
1019 tplg
->pos
+= hdr
->size
+ hdr
->payload_size
;
1023 if (soc_tplg_check_elem_count(tplg
,
1024 sizeof(struct snd_soc_tplg_dapm_graph_elem
),
1025 count
, hdr
->payload_size
, "graph")) {
1027 dev_err(tplg
->dev
, "ASoC: invalid count %d for DAPM routes\n",
1032 dev_dbg(tplg
->dev
, "ASoC: adding %d DAPM routes\n", count
);
1034 for (i
= 0; i
< count
; i
++) {
1035 elem
= (struct snd_soc_tplg_dapm_graph_elem
*)tplg
->pos
;
1036 tplg
->pos
+= sizeof(struct snd_soc_tplg_dapm_graph_elem
);
1038 /* validate routes */
1039 if (strnlen(elem
->source
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1040 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1042 if (strnlen(elem
->sink
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1043 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1045 if (strnlen(elem
->control
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1046 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1049 route
.source
= elem
->source
;
1050 route
.sink
= elem
->sink
;
1051 route
.connected
= NULL
; /* set to NULL atm for tplg users */
1052 if (strnlen(elem
->control
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) == 0)
1053 route
.control
= NULL
;
1055 route
.control
= elem
->control
;
1057 /* add route, but keep going if some fail */
1058 snd_soc_dapm_add_routes(dapm
, &route
, 1);
1064 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_dmixer_create(
1065 struct soc_tplg
*tplg
, int num_kcontrols
)
1067 struct snd_kcontrol_new
*kc
;
1068 struct soc_mixer_control
*sm
;
1069 struct snd_soc_tplg_mixer_control
*mc
;
1072 kc
= kzalloc(sizeof(*kc
) * num_kcontrols
, GFP_KERNEL
);
1076 for (i
= 0; i
< num_kcontrols
; i
++) {
1077 mc
= (struct snd_soc_tplg_mixer_control
*)tplg
->pos
;
1078 sm
= kzalloc(sizeof(*sm
), GFP_KERNEL
);
1082 tplg
->pos
+= (sizeof(struct snd_soc_tplg_mixer_control
) +
1085 /* validate kcontrol */
1086 if (strnlen(mc
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1087 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1090 dev_dbg(tplg
->dev
, " adding DAPM widget mixer control %s at %d\n",
1093 kc
[i
].name
= mc
->hdr
.name
;
1094 kc
[i
].private_value
= (long)sm
;
1095 kc
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1096 kc
[i
].access
= mc
->hdr
.access
;
1098 /* we only support FL/FR channel mapping atm */
1099 sm
->reg
= tplc_chan_get_reg(tplg
, mc
->channel
,
1101 sm
->rreg
= tplc_chan_get_reg(tplg
, mc
->channel
,
1103 sm
->shift
= tplc_chan_get_shift(tplg
, mc
->channel
,
1105 sm
->rshift
= tplc_chan_get_shift(tplg
, mc
->channel
,
1110 sm
->invert
= mc
->invert
;
1111 sm
->platform_max
= mc
->platform_max
;
1112 sm
->dobj
.index
= tplg
->index
;
1113 INIT_LIST_HEAD(&sm
->dobj
.list
);
1115 /* map io handlers */
1116 err
= soc_tplg_kcontrol_bind_io(&mc
->hdr
, &kc
[i
], io_ops
,
1117 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
1119 soc_control_err(tplg
, &mc
->hdr
, mc
->hdr
.name
);
1124 /* pass control to driver for optional further init */
1125 err
= soc_tplg_init_kcontrol(tplg
, &kc
[i
],
1126 (struct snd_soc_tplg_ctl_hdr
*)mc
);
1128 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1139 for (--i
; i
>= 0; i
--)
1140 kfree((void *)kc
[i
].private_value
);
1145 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_denum_create(
1146 struct soc_tplg
*tplg
)
1148 struct snd_kcontrol_new
*kc
;
1149 struct snd_soc_tplg_enum_control
*ec
;
1150 struct soc_enum
*se
;
1153 ec
= (struct snd_soc_tplg_enum_control
*)tplg
->pos
;
1154 tplg
->pos
+= (sizeof(struct snd_soc_tplg_enum_control
) +
1157 /* validate kcontrol */
1158 if (strnlen(ec
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1159 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1162 kc
= kzalloc(sizeof(*kc
), GFP_KERNEL
);
1166 se
= kzalloc(sizeof(*se
), GFP_KERNEL
);
1170 dev_dbg(tplg
->dev
, " adding DAPM widget enum control %s\n",
1173 kc
->name
= ec
->hdr
.name
;
1174 kc
->private_value
= (long)se
;
1175 kc
->iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1176 kc
->access
= ec
->hdr
.access
;
1178 /* we only support FL/FR channel mapping atm */
1179 se
->reg
= tplc_chan_get_reg(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
1180 se
->shift_l
= tplc_chan_get_shift(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
1181 se
->shift_r
= tplc_chan_get_shift(tplg
, ec
->channel
, SNDRV_CHMAP_FR
);
1183 se
->items
= ec
->items
;
1184 se
->mask
= ec
->mask
;
1185 se
->dobj
.index
= tplg
->index
;
1187 switch (ec
->hdr
.ops
.info
) {
1188 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1189 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1190 err
= soc_tplg_denum_create_values(se
, ec
);
1192 dev_err(tplg
->dev
, "ASoC: could not create values for %s\n",
1196 /* fall through to create texts */
1197 case SND_SOC_TPLG_CTL_ENUM
:
1198 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1199 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1200 err
= soc_tplg_denum_create_texts(se
, ec
);
1202 dev_err(tplg
->dev
, "ASoC: could not create texts for %s\n",
1208 dev_err(tplg
->dev
, "ASoC: invalid enum control type %d for %s\n",
1209 ec
->hdr
.ops
.info
, ec
->hdr
.name
);
1213 /* map io handlers */
1214 err
= soc_tplg_kcontrol_bind_io(&ec
->hdr
, kc
, io_ops
,
1215 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
1217 soc_control_err(tplg
, &ec
->hdr
, ec
->hdr
.name
);
1221 /* pass control to driver for optional further init */
1222 err
= soc_tplg_init_kcontrol(tplg
, kc
,
1223 (struct snd_soc_tplg_ctl_hdr
*)ec
);
1225 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1233 /* free values and texts */
1234 kfree(se
->dobj
.control
.dvalues
);
1235 for (i
= 0; i
< ec
->items
; i
++)
1236 kfree(se
->dobj
.control
.dtexts
[i
]);
1245 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_dbytes_create(
1246 struct soc_tplg
*tplg
, int count
)
1248 struct snd_soc_tplg_bytes_control
*be
;
1249 struct soc_bytes_ext
*sbe
;
1250 struct snd_kcontrol_new
*kc
;
1253 kc
= kzalloc(sizeof(*kc
) * count
, GFP_KERNEL
);
1257 for (i
= 0; i
< count
; i
++) {
1258 be
= (struct snd_soc_tplg_bytes_control
*)tplg
->pos
;
1260 /* validate kcontrol */
1261 if (strnlen(be
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1262 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1265 sbe
= kzalloc(sizeof(*sbe
), GFP_KERNEL
);
1269 tplg
->pos
+= (sizeof(struct snd_soc_tplg_bytes_control
) +
1273 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1274 be
->hdr
.name
, be
->hdr
.access
);
1276 memset(kc
, 0, sizeof(*kc
));
1277 kc
[i
].name
= be
->hdr
.name
;
1278 kc
[i
].private_value
= (long)sbe
;
1279 kc
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1280 kc
[i
].access
= be
->hdr
.access
;
1283 INIT_LIST_HEAD(&sbe
->dobj
.list
);
1285 /* map standard io handlers and check for external handlers */
1286 err
= soc_tplg_kcontrol_bind_io(&be
->hdr
, &kc
[i
], io_ops
,
1287 ARRAY_SIZE(io_ops
), tplg
->io_ops
,
1288 tplg
->io_ops_count
);
1290 soc_control_err(tplg
, &be
->hdr
, be
->hdr
.name
);
1295 /* pass control to driver for optional further init */
1296 err
= soc_tplg_init_kcontrol(tplg
, &kc
[i
],
1297 (struct snd_soc_tplg_ctl_hdr
*)be
);
1299 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1309 for (--i
; i
>= 0; i
--)
1310 kfree((void *)kc
[i
].private_value
);
1316 static int soc_tplg_dapm_widget_create(struct soc_tplg
*tplg
,
1317 struct snd_soc_tplg_dapm_widget
*w
)
1319 struct snd_soc_dapm_context
*dapm
= &tplg
->comp
->dapm
;
1320 struct snd_soc_dapm_widget
template, *widget
;
1321 struct snd_soc_tplg_ctl_hdr
*control_hdr
;
1322 struct snd_soc_card
*card
= tplg
->comp
->card
;
1325 if (strnlen(w
->name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1326 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1328 if (strnlen(w
->sname
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1329 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1332 dev_dbg(tplg
->dev
, "ASoC: creating DAPM widget %s id %d\n",
1335 memset(&template, 0, sizeof(template));
1337 /* map user to kernel widget ID */
1338 template.id
= get_widget_id(w
->id
);
1339 if (template.id
< 0)
1342 template.name
= kstrdup(w
->name
, GFP_KERNEL
);
1345 template.sname
= kstrdup(w
->sname
, GFP_KERNEL
);
1346 if (!template.sname
) {
1350 template.reg
= w
->reg
;
1351 template.shift
= w
->shift
;
1352 template.mask
= w
->mask
;
1353 template.on_val
= w
->invert
? 0 : 1;
1354 template.off_val
= w
->invert
? 1 : 0;
1355 template.ignore_suspend
= w
->ignore_suspend
;
1356 template.event_flags
= w
->event_flags
;
1357 template.dobj
.index
= tplg
->index
;
1360 (sizeof(struct snd_soc_tplg_dapm_widget
) + w
->priv
.size
);
1361 if (w
->num_kcontrols
== 0) {
1362 template.num_kcontrols
= 0;
1366 control_hdr
= (struct snd_soc_tplg_ctl_hdr
*)tplg
->pos
;
1367 dev_dbg(tplg
->dev
, "ASoC: template %s has %d controls of type %x\n",
1368 w
->name
, w
->num_kcontrols
, control_hdr
->type
);
1370 switch (control_hdr
->ops
.info
) {
1371 case SND_SOC_TPLG_CTL_VOLSW
:
1372 case SND_SOC_TPLG_CTL_STROBE
:
1373 case SND_SOC_TPLG_CTL_VOLSW_SX
:
1374 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
1375 case SND_SOC_TPLG_CTL_RANGE
:
1376 case SND_SOC_TPLG_DAPM_CTL_VOLSW
:
1377 template.num_kcontrols
= w
->num_kcontrols
;
1378 template.kcontrol_news
=
1379 soc_tplg_dapm_widget_dmixer_create(tplg
,
1380 template.num_kcontrols
);
1381 if (!template.kcontrol_news
) {
1386 case SND_SOC_TPLG_CTL_ENUM
:
1387 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1388 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1389 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1390 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1391 template.dobj
.widget
.kcontrol_enum
= 1;
1392 template.num_kcontrols
= 1;
1393 template.kcontrol_news
=
1394 soc_tplg_dapm_widget_denum_create(tplg
);
1395 if (!template.kcontrol_news
) {
1400 case SND_SOC_TPLG_CTL_BYTES
:
1401 template.num_kcontrols
= w
->num_kcontrols
;
1402 template.kcontrol_news
=
1403 soc_tplg_dapm_widget_dbytes_create(tplg
,
1404 template.num_kcontrols
);
1405 if (!template.kcontrol_news
) {
1411 dev_err(tplg
->dev
, "ASoC: invalid widget control type %d:%d:%d\n",
1412 control_hdr
->ops
.get
, control_hdr
->ops
.put
,
1413 control_hdr
->ops
.info
);
1419 ret
= soc_tplg_widget_load(tplg
, &template, w
);
1423 /* card dapm mutex is held by the core if we are loading topology
1424 * data during sound card init. */
1425 if (card
->instantiated
)
1426 widget
= snd_soc_dapm_new_control(dapm
, &template);
1428 widget
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
1429 if (widget
== NULL
) {
1430 dev_err(tplg
->dev
, "ASoC: failed to create widget %s controls\n",
1435 widget
->dobj
.type
= SND_SOC_DOBJ_WIDGET
;
1436 widget
->dobj
.ops
= tplg
->ops
;
1437 widget
->dobj
.index
= tplg
->index
;
1438 list_add(&widget
->dobj
.list
, &tplg
->comp
->dobj_list
);
1442 kfree(template.sname
);
1444 kfree(template.name
);
1448 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg
*tplg
,
1449 struct snd_soc_tplg_hdr
*hdr
)
1451 struct snd_soc_tplg_dapm_widget
*widget
;
1452 int ret
, count
= hdr
->count
, i
;
1454 if (tplg
->pass
!= SOC_TPLG_PASS_WIDGET
)
1457 dev_dbg(tplg
->dev
, "ASoC: adding %d DAPM widgets\n", count
);
1459 for (i
= 0; i
< count
; i
++) {
1460 widget
= (struct snd_soc_tplg_dapm_widget
*) tplg
->pos
;
1461 ret
= soc_tplg_dapm_widget_create(tplg
, widget
);
1463 dev_err(tplg
->dev
, "ASoC: failed to load widget %s\n",
1470 static int soc_tplg_dapm_complete(struct soc_tplg
*tplg
)
1472 struct snd_soc_card
*card
= tplg
->comp
->card
;
1475 /* Card might not have been registered at this point.
1476 * If so, just return success.
1478 if (!card
|| !card
->instantiated
) {
1479 dev_warn(tplg
->dev
, "ASoC: Parent card not yet available,"
1480 "Do not add new widgets now\n");
1484 ret
= snd_soc_dapm_new_widgets(card
);
1486 dev_err(tplg
->dev
, "ASoC: failed to create new widgets %d\n",
1492 static int soc_tplg_pcm_dai_elems_load(struct soc_tplg
*tplg
,
1493 struct snd_soc_tplg_hdr
*hdr
)
1495 struct snd_soc_tplg_pcm_dai
*pcm_dai
;
1496 struct snd_soc_dobj
*dobj
;
1497 int count
= hdr
->count
;
1500 if (tplg
->pass
!= SOC_TPLG_PASS_PCM_DAI
)
1503 pcm_dai
= (struct snd_soc_tplg_pcm_dai
*)tplg
->pos
;
1505 if (soc_tplg_check_elem_count(tplg
,
1506 sizeof(struct snd_soc_tplg_pcm_dai
), count
,
1507 hdr
->payload_size
, "PCM DAI")) {
1508 dev_err(tplg
->dev
, "ASoC: invalid count %d for PCM DAI elems\n",
1513 dev_dbg(tplg
->dev
, "ASoC: adding %d PCM DAIs\n", count
);
1514 tplg
->pos
+= sizeof(struct snd_soc_tplg_pcm_dai
) * count
;
1516 dobj
= kzalloc(sizeof(struct snd_soc_dobj
), GFP_KERNEL
);
1520 /* Call the platform driver call back to register the dais */
1521 ret
= soc_tplg_pcm_dai_load(tplg
, pcm_dai
, count
);
1523 dev_err(tplg
->comp
->dev
, "ASoC: PCM DAI loading failed\n");
1527 dobj
->type
= get_dobj_type(hdr
, NULL
);
1528 dobj
->pcm_dai
.count
= count
;
1529 dobj
->pcm_dai
.pd
= pcm_dai
;
1530 dobj
->ops
= tplg
->ops
;
1531 dobj
->index
= tplg
->index
;
1532 list_add(&dobj
->list
, &tplg
->comp
->dobj_list
);
1540 static int soc_tplg_manifest_load(struct soc_tplg
*tplg
,
1541 struct snd_soc_tplg_hdr
*hdr
)
1543 struct snd_soc_tplg_manifest
*manifest
;
1545 if (tplg
->pass
!= SOC_TPLG_PASS_MANIFEST
)
1548 manifest
= (struct snd_soc_tplg_manifest
*)tplg
->pos
;
1549 tplg
->pos
+= sizeof(struct snd_soc_tplg_manifest
);
1551 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->manifest
)
1552 return tplg
->ops
->manifest(tplg
->comp
, manifest
);
1554 dev_err(tplg
->dev
, "ASoC: Firmware manifest not supported\n");
1558 /* validate header magic, size and type */
1559 static int soc_valid_header(struct soc_tplg
*tplg
,
1560 struct snd_soc_tplg_hdr
*hdr
)
1562 if (soc_tplg_get_hdr_offset(tplg
) >= tplg
->fw
->size
)
1565 /* big endian firmware objects not supported atm */
1566 if (hdr
->magic
== cpu_to_be32(SND_SOC_TPLG_MAGIC
)) {
1568 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1569 tplg
->pass
, hdr
->magic
,
1570 soc_tplg_get_hdr_offset(tplg
), tplg
->fw
->size
);
1574 if (hdr
->magic
!= SND_SOC_TPLG_MAGIC
) {
1576 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1577 tplg
->pass
, hdr
->magic
,
1578 soc_tplg_get_hdr_offset(tplg
), tplg
->fw
->size
);
1582 if (hdr
->abi
!= SND_SOC_TPLG_ABI_VERSION
) {
1584 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1585 tplg
->pass
, hdr
->abi
,
1586 SND_SOC_TPLG_ABI_VERSION
, soc_tplg_get_hdr_offset(tplg
),
1591 if (hdr
->payload_size
== 0) {
1592 dev_err(tplg
->dev
, "ASoC: header has 0 size at offset 0x%lx.\n",
1593 soc_tplg_get_hdr_offset(tplg
));
1597 if (tplg
->pass
== hdr
->type
)
1599 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
1600 hdr
->payload_size
, hdr
->type
, hdr
->version
,
1601 hdr
->vendor_type
, tplg
->pass
);
1606 /* check header type and call appropriate handler */
1607 static int soc_tplg_load_header(struct soc_tplg
*tplg
,
1608 struct snd_soc_tplg_hdr
*hdr
)
1610 tplg
->pos
= tplg
->hdr_pos
+ sizeof(struct snd_soc_tplg_hdr
);
1612 /* check for matching ID */
1613 if (hdr
->index
!= tplg
->req_index
&&
1614 hdr
->index
!= SND_SOC_TPLG_INDEX_ALL
)
1617 tplg
->index
= hdr
->index
;
1619 switch (hdr
->type
) {
1620 case SND_SOC_TPLG_TYPE_MIXER
:
1621 case SND_SOC_TPLG_TYPE_ENUM
:
1622 case SND_SOC_TPLG_TYPE_BYTES
:
1623 return soc_tplg_kcontrol_elems_load(tplg
, hdr
);
1624 case SND_SOC_TPLG_TYPE_DAPM_GRAPH
:
1625 return soc_tplg_dapm_graph_elems_load(tplg
, hdr
);
1626 case SND_SOC_TPLG_TYPE_DAPM_WIDGET
:
1627 return soc_tplg_dapm_widget_elems_load(tplg
, hdr
);
1628 case SND_SOC_TPLG_TYPE_PCM
:
1629 case SND_SOC_TPLG_TYPE_DAI_LINK
:
1630 case SND_SOC_TPLG_TYPE_CODEC_LINK
:
1631 return soc_tplg_pcm_dai_elems_load(tplg
, hdr
);
1632 case SND_SOC_TPLG_TYPE_MANIFEST
:
1633 return soc_tplg_manifest_load(tplg
, hdr
);
1635 /* bespoke vendor data object */
1636 return soc_tplg_vendor_load(tplg
, hdr
);
1642 /* process the topology file headers */
1643 static int soc_tplg_process_headers(struct soc_tplg
*tplg
)
1645 struct snd_soc_tplg_hdr
*hdr
;
1648 tplg
->pass
= SOC_TPLG_PASS_START
;
1650 /* process the header types from start to end */
1651 while (tplg
->pass
<= SOC_TPLG_PASS_END
) {
1653 tplg
->hdr_pos
= tplg
->fw
->data
;
1654 hdr
= (struct snd_soc_tplg_hdr
*)tplg
->hdr_pos
;
1656 while (!soc_tplg_is_eof(tplg
)) {
1658 /* make sure header is valid before loading */
1659 ret
= soc_valid_header(tplg
, hdr
);
1665 /* load the header object */
1666 ret
= soc_tplg_load_header(tplg
, hdr
);
1670 /* goto next header */
1671 tplg
->hdr_pos
+= hdr
->payload_size
+
1672 sizeof(struct snd_soc_tplg_hdr
);
1673 hdr
= (struct snd_soc_tplg_hdr
*)tplg
->hdr_pos
;
1676 /* next data type pass */
1680 /* signal DAPM we are complete */
1681 ret
= soc_tplg_dapm_complete(tplg
);
1684 "ASoC: failed to initialise DAPM from Firmware\n");
1689 static int soc_tplg_load(struct soc_tplg
*tplg
)
1693 ret
= soc_tplg_process_headers(tplg
);
1695 soc_tplg_complete(tplg
);
1700 /* load audio component topology from "firmware" file */
1701 int snd_soc_tplg_component_load(struct snd_soc_component
*comp
,
1702 struct snd_soc_tplg_ops
*ops
, const struct firmware
*fw
, u32 id
)
1704 struct soc_tplg tplg
;
1706 /* setup parsing context */
1707 memset(&tplg
, 0, sizeof(tplg
));
1709 tplg
.dev
= comp
->dev
;
1712 tplg
.req_index
= id
;
1713 tplg
.io_ops
= ops
->io_ops
;
1714 tplg
.io_ops_count
= ops
->io_ops_count
;
1716 return soc_tplg_load(&tplg
);
1718 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load
);
1720 /* remove this dynamic widget */
1721 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget
*w
)
1723 /* make sure we are a widget */
1724 if (w
->dobj
.type
!= SND_SOC_DOBJ_WIDGET
)
1727 remove_widget(w
->dapm
->component
, &w
->dobj
, SOC_TPLG_PASS_WIDGET
);
1729 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove
);
1731 /* remove all dynamic widgets from this DAPM context */
1732 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context
*dapm
,
1735 struct snd_soc_dapm_widget
*w
, *next_w
;
1736 struct snd_soc_dapm_path
*p
, *next_p
;
1738 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
1740 /* make sure we are a widget with correct context */
1741 if (w
->dobj
.type
!= SND_SOC_DOBJ_WIDGET
|| w
->dapm
!= dapm
)
1745 if (w
->dobj
.index
!= index
&&
1746 w
->dobj
.index
!= SND_SOC_TPLG_INDEX_ALL
)
1752 * remove source and sink paths associated to this widget.
1753 * While removing the path, remove reference to it from both
1754 * source and sink widgets so that path is removed only once.
1756 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
) {
1757 list_del(&p
->list_sink
);
1758 list_del(&p
->list_source
);
1762 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
) {
1763 list_del(&p
->list_sink
);
1764 list_del(&p
->list_source
);
1768 /* check and free and dynamic widget kcontrols */
1769 snd_soc_tplg_widget_remove(w
);
1770 kfree(w
->kcontrols
);
1775 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all
);
1777 /* remove dynamic controls from the component driver */
1778 int snd_soc_tplg_component_remove(struct snd_soc_component
*comp
, u32 index
)
1780 struct snd_soc_dobj
*dobj
, *next_dobj
;
1781 int pass
= SOC_TPLG_PASS_END
;
1783 /* process the header types from end to start */
1784 while (pass
>= SOC_TPLG_PASS_START
) {
1786 /* remove mixer controls */
1787 list_for_each_entry_safe(dobj
, next_dobj
, &comp
->dobj_list
,
1791 if (dobj
->index
!= index
&&
1792 dobj
->index
!= SND_SOC_TPLG_INDEX_ALL
)
1795 switch (dobj
->type
) {
1796 case SND_SOC_DOBJ_MIXER
:
1797 remove_mixer(comp
, dobj
, pass
);
1799 case SND_SOC_DOBJ_ENUM
:
1800 remove_enum(comp
, dobj
, pass
);
1802 case SND_SOC_DOBJ_BYTES
:
1803 remove_bytes(comp
, dobj
, pass
);
1805 case SND_SOC_DOBJ_WIDGET
:
1806 remove_widget(comp
, dobj
, pass
);
1808 case SND_SOC_DOBJ_PCM
:
1809 case SND_SOC_DOBJ_DAI_LINK
:
1810 case SND_SOC_DOBJ_CODEC_LINK
:
1811 remove_pcm_dai(comp
, dobj
, pass
);
1814 dev_err(comp
->dev
, "ASoC: invalid component type %d for removal\n",
1822 /* let caller know if FW can be freed when no objects are left */
1823 return !list_empty(&comp
->dobj_list
);
1825 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove
);