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_GRAPH 4
49 #define SOC_TPLG_PASS_PINS 5
50 #define SOC_TPLG_PASS_PCM_DAI 6
52 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
53 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_PCM_DAI
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 /* kcontrol operations */
70 const struct snd_soc_tplg_kcontrol_ops
*io_ops
;
73 /* optional fw loading callbacks to component drivers */
74 struct snd_soc_tplg_ops
*ops
;
77 static int soc_tplg_process_headers(struct soc_tplg
*tplg
);
78 static void soc_tplg_complete(struct soc_tplg
*tplg
);
79 struct snd_soc_dapm_widget
*
80 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
81 const struct snd_soc_dapm_widget
*widget
);
82 struct snd_soc_dapm_widget
*
83 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
84 const struct snd_soc_dapm_widget
*widget
);
86 /* check we dont overflow the data for this control chunk */
87 static int soc_tplg_check_elem_count(struct soc_tplg
*tplg
, size_t elem_size
,
88 unsigned int count
, size_t bytes
, const char *elem_type
)
90 const u8
*end
= tplg
->pos
+ elem_size
* count
;
92 if (end
> tplg
->fw
->data
+ tplg
->fw
->size
) {
93 dev_err(tplg
->dev
, "ASoC: %s overflow end of data\n",
98 /* check there is enough room in chunk for control.
99 extra bytes at the end of control are for vendor data here */
100 if (elem_size
* count
> bytes
) {
102 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
103 elem_type
, count
, elem_size
, bytes
);
110 static inline int soc_tplg_is_eof(struct soc_tplg
*tplg
)
112 const u8
*end
= tplg
->hdr_pos
;
114 if (end
>= tplg
->fw
->data
+ tplg
->fw
->size
)
119 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg
*tplg
)
121 return (unsigned long)(tplg
->hdr_pos
- tplg
->fw
->data
);
124 static inline unsigned long soc_tplg_get_offset(struct soc_tplg
*tplg
)
126 return (unsigned long)(tplg
->pos
- tplg
->fw
->data
);
129 /* mapping of Kcontrol types and associated operations. */
130 static const struct snd_soc_tplg_kcontrol_ops io_ops
[] = {
131 {SND_SOC_TPLG_CTL_VOLSW
, snd_soc_get_volsw
,
132 snd_soc_put_volsw
, snd_soc_info_volsw
},
133 {SND_SOC_TPLG_CTL_VOLSW_SX
, snd_soc_get_volsw_sx
,
134 snd_soc_put_volsw_sx
, NULL
},
135 {SND_SOC_TPLG_CTL_ENUM
, snd_soc_get_enum_double
,
136 snd_soc_put_enum_double
, snd_soc_info_enum_double
},
137 {SND_SOC_TPLG_CTL_ENUM_VALUE
, snd_soc_get_enum_double
,
138 snd_soc_put_enum_double
, NULL
},
139 {SND_SOC_TPLG_CTL_BYTES
, snd_soc_bytes_get
,
140 snd_soc_bytes_put
, snd_soc_bytes_info
},
141 {SND_SOC_TPLG_CTL_RANGE
, snd_soc_get_volsw_range
,
142 snd_soc_put_volsw_range
, snd_soc_info_volsw_range
},
143 {SND_SOC_TPLG_CTL_VOLSW_XR_SX
, snd_soc_get_xr_sx
,
144 snd_soc_put_xr_sx
, snd_soc_info_xr_sx
},
145 {SND_SOC_TPLG_CTL_STROBE
, snd_soc_get_strobe
,
146 snd_soc_put_strobe
, NULL
},
147 {SND_SOC_TPLG_DAPM_CTL_VOLSW
, snd_soc_dapm_get_volsw
,
148 snd_soc_dapm_put_volsw
, snd_soc_info_volsw
},
149 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
, snd_soc_dapm_get_enum_double
,
150 snd_soc_dapm_put_enum_double
, snd_soc_info_enum_double
},
151 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
, snd_soc_dapm_get_enum_double
,
152 snd_soc_dapm_put_enum_double
, NULL
},
153 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
, snd_soc_dapm_get_enum_double
,
154 snd_soc_dapm_put_enum_double
, NULL
},
155 {SND_SOC_TPLG_DAPM_CTL_PIN
, snd_soc_dapm_get_pin_switch
,
156 snd_soc_dapm_put_pin_switch
, snd_soc_dapm_info_pin_switch
},
159 struct soc_tplg_map
{
164 /* mapping of widget types from UAPI IDs to kernel IDs */
165 static const struct soc_tplg_map dapm_map
[] = {
166 {SND_SOC_TPLG_DAPM_INPUT
, snd_soc_dapm_input
},
167 {SND_SOC_TPLG_DAPM_OUTPUT
, snd_soc_dapm_output
},
168 {SND_SOC_TPLG_DAPM_MUX
, snd_soc_dapm_mux
},
169 {SND_SOC_TPLG_DAPM_MIXER
, snd_soc_dapm_mixer
},
170 {SND_SOC_TPLG_DAPM_PGA
, snd_soc_dapm_pga
},
171 {SND_SOC_TPLG_DAPM_OUT_DRV
, snd_soc_dapm_out_drv
},
172 {SND_SOC_TPLG_DAPM_ADC
, snd_soc_dapm_adc
},
173 {SND_SOC_TPLG_DAPM_DAC
, snd_soc_dapm_dac
},
174 {SND_SOC_TPLG_DAPM_SWITCH
, snd_soc_dapm_switch
},
175 {SND_SOC_TPLG_DAPM_PRE
, snd_soc_dapm_pre
},
176 {SND_SOC_TPLG_DAPM_POST
, snd_soc_dapm_post
},
177 {SND_SOC_TPLG_DAPM_AIF_IN
, snd_soc_dapm_aif_in
},
178 {SND_SOC_TPLG_DAPM_AIF_OUT
, snd_soc_dapm_aif_out
},
179 {SND_SOC_TPLG_DAPM_DAI_IN
, snd_soc_dapm_dai_in
},
180 {SND_SOC_TPLG_DAPM_DAI_OUT
, snd_soc_dapm_dai_out
},
181 {SND_SOC_TPLG_DAPM_DAI_LINK
, snd_soc_dapm_dai_link
},
184 static int tplc_chan_get_reg(struct soc_tplg
*tplg
,
185 struct snd_soc_tplg_channel
*chan
, int map
)
189 for (i
= 0; i
< SND_SOC_TPLG_MAX_CHAN
; i
++) {
190 if (chan
[i
].id
== map
)
197 static int tplc_chan_get_shift(struct soc_tplg
*tplg
,
198 struct snd_soc_tplg_channel
*chan
, int map
)
202 for (i
= 0; i
< SND_SOC_TPLG_MAX_CHAN
; i
++) {
203 if (chan
[i
].id
== map
)
204 return chan
[i
].shift
;
210 static int get_widget_id(int tplg_type
)
214 for (i
= 0; i
< ARRAY_SIZE(dapm_map
); i
++) {
215 if (tplg_type
== dapm_map
[i
].uid
)
216 return dapm_map
[i
].kid
;
222 static enum snd_soc_dobj_type
get_dobj_mixer_type(
223 struct snd_soc_tplg_ctl_hdr
*control_hdr
)
225 if (control_hdr
== NULL
)
226 return SND_SOC_DOBJ_NONE
;
228 switch (control_hdr
->ops
.info
) {
229 case SND_SOC_TPLG_CTL_VOLSW
:
230 case SND_SOC_TPLG_CTL_VOLSW_SX
:
231 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
232 case SND_SOC_TPLG_CTL_RANGE
:
233 case SND_SOC_TPLG_CTL_STROBE
:
234 return SND_SOC_DOBJ_MIXER
;
235 case SND_SOC_TPLG_CTL_ENUM
:
236 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
237 return SND_SOC_DOBJ_ENUM
;
238 case SND_SOC_TPLG_CTL_BYTES
:
239 return SND_SOC_DOBJ_BYTES
;
241 return SND_SOC_DOBJ_NONE
;
245 static enum snd_soc_dobj_type
get_dobj_type(struct snd_soc_tplg_hdr
*hdr
,
246 struct snd_soc_tplg_ctl_hdr
*control_hdr
)
249 case SND_SOC_TPLG_TYPE_MIXER
:
250 return get_dobj_mixer_type(control_hdr
);
251 case SND_SOC_TPLG_TYPE_DAPM_GRAPH
:
252 case SND_SOC_TPLG_TYPE_MANIFEST
:
253 return SND_SOC_DOBJ_NONE
;
254 case SND_SOC_TPLG_TYPE_DAPM_WIDGET
:
255 return SND_SOC_DOBJ_WIDGET
;
256 case SND_SOC_TPLG_TYPE_DAI_LINK
:
257 return SND_SOC_DOBJ_DAI_LINK
;
258 case SND_SOC_TPLG_TYPE_PCM
:
259 return SND_SOC_DOBJ_PCM
;
260 case SND_SOC_TPLG_TYPE_CODEC_LINK
:
261 return SND_SOC_DOBJ_CODEC_LINK
;
263 return SND_SOC_DOBJ_NONE
;
267 static inline void soc_bind_err(struct soc_tplg
*tplg
,
268 struct snd_soc_tplg_ctl_hdr
*hdr
, int index
)
271 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
272 hdr
->ops
.get
, hdr
->ops
.put
, hdr
->ops
.info
, index
,
273 soc_tplg_get_offset(tplg
));
276 static inline void soc_control_err(struct soc_tplg
*tplg
,
277 struct snd_soc_tplg_ctl_hdr
*hdr
, const char *name
)
280 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
281 name
, hdr
->ops
.get
, hdr
->ops
.put
, hdr
->ops
.info
,
282 soc_tplg_get_offset(tplg
));
285 /* pass vendor data to component driver for processing */
286 static int soc_tplg_vendor_load_(struct soc_tplg
*tplg
,
287 struct snd_soc_tplg_hdr
*hdr
)
291 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->vendor_load
)
292 ret
= tplg
->ops
->vendor_load(tplg
->comp
, hdr
);
294 dev_err(tplg
->dev
, "ASoC: no vendor load callback for ID %d\n",
301 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
302 soc_tplg_get_hdr_offset(tplg
),
303 soc_tplg_get_hdr_offset(tplg
),
304 hdr
->type
, hdr
->vendor_type
);
308 /* pass vendor data to component driver for processing */
309 static int soc_tplg_vendor_load(struct soc_tplg
*tplg
,
310 struct snd_soc_tplg_hdr
*hdr
)
312 if (tplg
->pass
!= SOC_TPLG_PASS_VENDOR
)
315 return soc_tplg_vendor_load_(tplg
, hdr
);
318 /* optionally pass new dynamic widget to component driver. This is mainly for
319 * external widgets where we can assign private data/ops */
320 static int soc_tplg_widget_load(struct soc_tplg
*tplg
,
321 struct snd_soc_dapm_widget
*w
, struct snd_soc_tplg_dapm_widget
*tplg_w
)
323 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->widget_load
)
324 return tplg
->ops
->widget_load(tplg
->comp
, w
, tplg_w
);
329 /* pass dynamic FEs configurations to component driver */
330 static int soc_tplg_pcm_dai_load(struct soc_tplg
*tplg
,
331 struct snd_soc_tplg_pcm_dai
*pcm_dai
, int num_pcm_dai
)
333 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->pcm_dai_load
)
334 return tplg
->ops
->pcm_dai_load(tplg
->comp
, pcm_dai
, num_pcm_dai
);
339 /* tell the component driver that all firmware has been loaded in this request */
340 static void soc_tplg_complete(struct soc_tplg
*tplg
)
342 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->complete
)
343 tplg
->ops
->complete(tplg
->comp
);
346 /* add a dynamic kcontrol */
347 static int soc_tplg_add_dcontrol(struct snd_card
*card
, struct device
*dev
,
348 const struct snd_kcontrol_new
*control_new
, const char *prefix
,
349 void *data
, struct snd_kcontrol
**kcontrol
)
353 *kcontrol
= snd_soc_cnew(control_new
, data
, control_new
->name
, prefix
);
354 if (*kcontrol
== NULL
) {
355 dev_err(dev
, "ASoC: Failed to create new kcontrol %s\n",
360 err
= snd_ctl_add(card
, *kcontrol
);
362 dev_err(dev
, "ASoC: Failed to add %s: %d\n",
363 control_new
->name
, err
);
370 /* add a dynamic kcontrol for component driver */
371 static int soc_tplg_add_kcontrol(struct soc_tplg
*tplg
,
372 struct snd_kcontrol_new
*k
, struct snd_kcontrol
**kcontrol
)
374 struct snd_soc_component
*comp
= tplg
->comp
;
376 return soc_tplg_add_dcontrol(comp
->card
->snd_card
,
377 comp
->dev
, k
, NULL
, comp
, kcontrol
);
380 /* remove a mixer kcontrol */
381 static void remove_mixer(struct snd_soc_component
*comp
,
382 struct snd_soc_dobj
*dobj
, int pass
)
384 struct snd_card
*card
= comp
->card
->snd_card
;
385 struct soc_mixer_control
*sm
=
386 container_of(dobj
, struct soc_mixer_control
, dobj
);
387 const unsigned int *p
= NULL
;
389 if (pass
!= SOC_TPLG_PASS_MIXER
)
392 if (dobj
->ops
&& dobj
->ops
->control_unload
)
393 dobj
->ops
->control_unload(comp
, dobj
);
395 if (sm
->dobj
.control
.kcontrol
->tlv
.p
)
396 p
= sm
->dobj
.control
.kcontrol
->tlv
.p
;
397 snd_ctl_remove(card
, sm
->dobj
.control
.kcontrol
);
398 list_del(&sm
->dobj
.list
);
403 /* remove an enum kcontrol */
404 static void remove_enum(struct snd_soc_component
*comp
,
405 struct snd_soc_dobj
*dobj
, int pass
)
407 struct snd_card
*card
= comp
->card
->snd_card
;
408 struct soc_enum
*se
= container_of(dobj
, struct soc_enum
, dobj
);
411 if (pass
!= SOC_TPLG_PASS_MIXER
)
414 if (dobj
->ops
&& dobj
->ops
->control_unload
)
415 dobj
->ops
->control_unload(comp
, dobj
);
417 snd_ctl_remove(card
, se
->dobj
.control
.kcontrol
);
418 list_del(&se
->dobj
.list
);
420 kfree(se
->dobj
.control
.dvalues
);
421 for (i
= 0; i
< se
->items
; i
++)
422 kfree(se
->dobj
.control
.dtexts
[i
]);
426 /* remove a byte kcontrol */
427 static void remove_bytes(struct snd_soc_component
*comp
,
428 struct snd_soc_dobj
*dobj
, int pass
)
430 struct snd_card
*card
= comp
->card
->snd_card
;
431 struct soc_bytes_ext
*sb
=
432 container_of(dobj
, struct soc_bytes_ext
, dobj
);
434 if (pass
!= SOC_TPLG_PASS_MIXER
)
437 if (dobj
->ops
&& dobj
->ops
->control_unload
)
438 dobj
->ops
->control_unload(comp
, dobj
);
440 snd_ctl_remove(card
, sb
->dobj
.control
.kcontrol
);
441 list_del(&sb
->dobj
.list
);
445 /* remove a widget and it's kcontrols - routes must be removed first */
446 static void remove_widget(struct snd_soc_component
*comp
,
447 struct snd_soc_dobj
*dobj
, int pass
)
449 struct snd_card
*card
= comp
->card
->snd_card
;
450 struct snd_soc_dapm_widget
*w
=
451 container_of(dobj
, struct snd_soc_dapm_widget
, dobj
);
454 if (pass
!= SOC_TPLG_PASS_WIDGET
)
457 if (dobj
->ops
&& dobj
->ops
->widget_unload
)
458 dobj
->ops
->widget_unload(comp
, dobj
);
461 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
462 * The enum may either have an array of values or strings.
464 if (dobj
->widget
.kcontrol_enum
) {
465 /* enumerated widget mixer */
466 struct soc_enum
*se
=
467 (struct soc_enum
*)w
->kcontrols
[0]->private_value
;
469 snd_ctl_remove(card
, w
->kcontrols
[0]);
471 kfree(se
->dobj
.control
.dvalues
);
472 for (i
= 0; i
< se
->items
; i
++)
473 kfree(se
->dobj
.control
.dtexts
[i
]);
476 kfree(w
->kcontrol_news
);
478 /* non enumerated widget mixer */
479 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
480 struct snd_kcontrol
*kcontrol
= w
->kcontrols
[i
];
481 struct soc_mixer_control
*sm
=
482 (struct soc_mixer_control
*) kcontrol
->private_value
;
484 kfree(w
->kcontrols
[i
]->tlv
.p
);
486 snd_ctl_remove(card
, w
->kcontrols
[i
]);
489 kfree(w
->kcontrol_news
);
491 /* widget w is freed by soc-dapm.c */
494 /* remove PCM DAI configurations */
495 static void remove_pcm_dai(struct snd_soc_component
*comp
,
496 struct snd_soc_dobj
*dobj
, int pass
)
498 if (pass
!= SOC_TPLG_PASS_PCM_DAI
)
501 if (dobj
->ops
&& dobj
->ops
->pcm_dai_unload
)
502 dobj
->ops
->pcm_dai_unload(comp
, dobj
);
504 list_del(&dobj
->list
);
508 /* bind a kcontrol to it's IO handlers */
509 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr
*hdr
,
510 struct snd_kcontrol_new
*k
,
511 const struct snd_soc_tplg_kcontrol_ops
*ops
, int num_ops
,
512 const struct snd_soc_tplg_kcontrol_ops
*bops
, int num_bops
)
516 /* try and map standard kcontrols handler first */
517 for (i
= 0; i
< num_ops
; i
++) {
519 if (ops
[i
].id
== hdr
->ops
.put
)
521 if (ops
[i
].id
== hdr
->ops
.get
)
523 if (ops
[i
].id
== hdr
->ops
.info
)
524 k
->info
= ops
[i
].info
;
527 /* standard handlers found ? */
528 if (k
->put
&& k
->get
&& k
->info
)
531 /* none found so try bespoke handlers */
532 for (i
= 0; i
< num_bops
; i
++) {
534 if (k
->put
== NULL
&& bops
[i
].id
== hdr
->ops
.put
)
535 k
->put
= bops
[i
].put
;
536 if (k
->get
== NULL
&& bops
[i
].id
== hdr
->ops
.get
)
537 k
->get
= bops
[i
].get
;
538 if (k
->info
== NULL
&& bops
[i
].id
== hdr
->ops
.info
)
539 k
->info
= bops
[i
].info
;
542 /* bespoke handlers found ? */
543 if (k
->put
&& k
->get
&& k
->info
)
546 /* nothing to bind */
550 /* bind a widgets to it's evnt handlers */
551 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget
*w
,
552 const struct snd_soc_tplg_widget_events
*events
,
553 int num_events
, u16 event_type
)
559 for (i
= 0; i
< num_events
; i
++) {
560 if (event_type
== events
[i
].type
) {
562 /* found - so assign event */
563 w
->event
= events
[i
].event_handler
;
571 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event
);
573 /* optionally pass new dynamic kcontrol to component driver. */
574 static int soc_tplg_init_kcontrol(struct soc_tplg
*tplg
,
575 struct snd_kcontrol_new
*k
, struct snd_soc_tplg_ctl_hdr
*hdr
)
577 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->control_load
)
578 return tplg
->ops
->control_load(tplg
->comp
, k
, hdr
);
584 static int soc_tplg_create_tlv_db_scale(struct soc_tplg
*tplg
,
585 struct snd_kcontrol_new
*kc
, struct snd_soc_tplg_tlv_dbscale
*scale
)
587 unsigned int item_len
= 2 * sizeof(unsigned int);
590 p
= kzalloc(item_len
+ 2 * sizeof(unsigned int), GFP_KERNEL
);
594 p
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
597 p
[3] = (scale
->step
& TLV_DB_SCALE_MASK
)
598 | (scale
->mute
? TLV_DB_SCALE_MUTE
: 0);
600 kc
->tlv
.p
= (void *)p
;
604 static int soc_tplg_create_tlv(struct soc_tplg
*tplg
,
605 struct snd_kcontrol_new
*kc
, struct snd_soc_tplg_ctl_hdr
*tc
)
607 struct snd_soc_tplg_ctl_tlv
*tplg_tlv
;
609 if (!(tc
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
))
612 if (tc
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
613 kc
->tlv
.c
= snd_soc_bytes_tlv_callback
;
616 switch (tplg_tlv
->type
) {
617 case SNDRV_CTL_TLVT_DB_SCALE
:
618 return soc_tplg_create_tlv_db_scale(tplg
, kc
,
621 /* TODO: add support for other TLV types */
623 dev_dbg(tplg
->dev
, "Unsupported TLV type %d\n",
632 static inline void soc_tplg_free_tlv(struct soc_tplg
*tplg
,
633 struct snd_kcontrol_new
*kc
)
638 static int soc_tplg_dbytes_create(struct soc_tplg
*tplg
, unsigned int count
,
641 struct snd_soc_tplg_bytes_control
*be
;
642 struct soc_bytes_ext
*sbe
;
643 struct snd_kcontrol_new kc
;
646 if (soc_tplg_check_elem_count(tplg
,
647 sizeof(struct snd_soc_tplg_bytes_control
), count
,
648 size
, "mixer bytes")) {
649 dev_err(tplg
->dev
, "ASoC: Invalid count %d for byte control\n",
654 for (i
= 0; i
< count
; i
++) {
655 be
= (struct snd_soc_tplg_bytes_control
*)tplg
->pos
;
657 /* validate kcontrol */
658 if (strnlen(be
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
659 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
662 sbe
= kzalloc(sizeof(*sbe
), GFP_KERNEL
);
666 tplg
->pos
+= (sizeof(struct snd_soc_tplg_bytes_control
) +
670 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
671 be
->hdr
.name
, be
->hdr
.access
);
673 memset(&kc
, 0, sizeof(kc
));
674 kc
.name
= be
->hdr
.name
;
675 kc
.private_value
= (long)sbe
;
676 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
677 kc
.access
= be
->hdr
.access
;
680 sbe
->dobj
.type
= SND_SOC_DOBJ_BYTES
;
681 sbe
->dobj
.ops
= tplg
->ops
;
682 INIT_LIST_HEAD(&sbe
->dobj
.list
);
684 /* map io handlers */
685 err
= soc_tplg_kcontrol_bind_io(&be
->hdr
, &kc
, io_ops
,
686 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
688 soc_control_err(tplg
, &be
->hdr
, be
->hdr
.name
);
693 /* pass control to driver for optional further init */
694 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
695 (struct snd_soc_tplg_ctl_hdr
*)be
);
697 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
703 /* register control here */
704 err
= soc_tplg_add_kcontrol(tplg
, &kc
,
705 &sbe
->dobj
.control
.kcontrol
);
707 dev_err(tplg
->dev
, "ASoC: failed to add %s\n",
713 list_add(&sbe
->dobj
.list
, &tplg
->comp
->dobj_list
);
719 static int soc_tplg_dmixer_create(struct soc_tplg
*tplg
, unsigned int count
,
722 struct snd_soc_tplg_mixer_control
*mc
;
723 struct soc_mixer_control
*sm
;
724 struct snd_kcontrol_new kc
;
727 if (soc_tplg_check_elem_count(tplg
,
728 sizeof(struct snd_soc_tplg_mixer_control
),
729 count
, size
, "mixers")) {
731 dev_err(tplg
->dev
, "ASoC: invalid count %d for controls\n",
736 for (i
= 0; i
< count
; i
++) {
737 mc
= (struct snd_soc_tplg_mixer_control
*)tplg
->pos
;
739 /* validate kcontrol */
740 if (strnlen(mc
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
741 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
744 sm
= kzalloc(sizeof(*sm
), GFP_KERNEL
);
747 tplg
->pos
+= (sizeof(struct snd_soc_tplg_mixer_control
) +
751 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
752 mc
->hdr
.name
, mc
->hdr
.access
);
754 memset(&kc
, 0, sizeof(kc
));
755 kc
.name
= mc
->hdr
.name
;
756 kc
.private_value
= (long)sm
;
757 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
758 kc
.access
= mc
->hdr
.access
;
760 /* we only support FL/FR channel mapping atm */
761 sm
->reg
= tplc_chan_get_reg(tplg
, mc
->channel
,
763 sm
->rreg
= tplc_chan_get_reg(tplg
, mc
->channel
,
765 sm
->shift
= tplc_chan_get_shift(tplg
, mc
->channel
,
767 sm
->rshift
= tplc_chan_get_shift(tplg
, mc
->channel
,
772 sm
->invert
= mc
->invert
;
773 sm
->platform_max
= mc
->platform_max
;
774 sm
->dobj
.index
= tplg
->index
;
775 sm
->dobj
.ops
= tplg
->ops
;
776 sm
->dobj
.type
= SND_SOC_DOBJ_MIXER
;
777 INIT_LIST_HEAD(&sm
->dobj
.list
);
779 /* map io handlers */
780 err
= soc_tplg_kcontrol_bind_io(&mc
->hdr
, &kc
, io_ops
,
781 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
783 soc_control_err(tplg
, &mc
->hdr
, mc
->hdr
.name
);
788 /* pass control to driver for optional further init */
789 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
790 (struct snd_soc_tplg_ctl_hdr
*) mc
);
792 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
798 /* create any TLV data */
799 soc_tplg_create_tlv(tplg
, &kc
, &mc
->hdr
);
801 /* register control here */
802 err
= soc_tplg_add_kcontrol(tplg
, &kc
,
803 &sm
->dobj
.control
.kcontrol
);
805 dev_err(tplg
->dev
, "ASoC: failed to add %s\n",
807 soc_tplg_free_tlv(tplg
, &kc
);
812 list_add(&sm
->dobj
.list
, &tplg
->comp
->dobj_list
);
818 static int soc_tplg_denum_create_texts(struct soc_enum
*se
,
819 struct snd_soc_tplg_enum_control
*ec
)
823 se
->dobj
.control
.dtexts
=
824 kzalloc(sizeof(char *) * ec
->items
, GFP_KERNEL
);
825 if (se
->dobj
.control
.dtexts
== NULL
)
828 for (i
= 0; i
< ec
->items
; i
++) {
830 if (strnlen(ec
->texts
[i
], SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
831 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) {
836 se
->dobj
.control
.dtexts
[i
] = kstrdup(ec
->texts
[i
], GFP_KERNEL
);
837 if (!se
->dobj
.control
.dtexts
[i
]) {
846 for (--i
; i
>= 0; i
--)
847 kfree(se
->dobj
.control
.dtexts
[i
]);
848 kfree(se
->dobj
.control
.dtexts
);
852 static int soc_tplg_denum_create_values(struct soc_enum
*se
,
853 struct snd_soc_tplg_enum_control
*ec
)
855 if (ec
->items
> sizeof(*ec
->values
))
858 se
->dobj
.control
.dvalues
=
859 kmalloc(ec
->items
* sizeof(u32
), GFP_KERNEL
);
860 if (!se
->dobj
.control
.dvalues
)
863 memcpy(se
->dobj
.control
.dvalues
, ec
->values
, ec
->items
* sizeof(u32
));
867 static int soc_tplg_denum_create(struct soc_tplg
*tplg
, unsigned int count
,
870 struct snd_soc_tplg_enum_control
*ec
;
872 struct snd_kcontrol_new kc
;
875 if (soc_tplg_check_elem_count(tplg
,
876 sizeof(struct snd_soc_tplg_enum_control
),
877 count
, size
, "enums")) {
879 dev_err(tplg
->dev
, "ASoC: invalid count %d for enum controls\n",
884 for (i
= 0; i
< count
; i
++) {
885 ec
= (struct snd_soc_tplg_enum_control
*)tplg
->pos
;
886 tplg
->pos
+= (sizeof(struct snd_soc_tplg_enum_control
) +
889 /* validate kcontrol */
890 if (strnlen(ec
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
891 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
894 se
= kzalloc((sizeof(*se
)), GFP_KERNEL
);
898 dev_dbg(tplg
->dev
, "ASoC: adding enum kcontrol %s size %d\n",
899 ec
->hdr
.name
, ec
->items
);
901 memset(&kc
, 0, sizeof(kc
));
902 kc
.name
= ec
->hdr
.name
;
903 kc
.private_value
= (long)se
;
904 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
905 kc
.access
= ec
->hdr
.access
;
907 se
->reg
= tplc_chan_get_reg(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
908 se
->shift_l
= tplc_chan_get_shift(tplg
, ec
->channel
,
910 se
->shift_r
= tplc_chan_get_shift(tplg
, ec
->channel
,
913 se
->items
= ec
->items
;
915 se
->dobj
.index
= tplg
->index
;
916 se
->dobj
.type
= SND_SOC_DOBJ_ENUM
;
917 se
->dobj
.ops
= tplg
->ops
;
918 INIT_LIST_HEAD(&se
->dobj
.list
);
920 switch (ec
->hdr
.ops
.info
) {
921 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
922 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
923 err
= soc_tplg_denum_create_values(se
, ec
);
926 "ASoC: could not create values for %s\n",
931 /* fall through and create texts */
932 case SND_SOC_TPLG_CTL_ENUM
:
933 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
934 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
935 err
= soc_tplg_denum_create_texts(se
, ec
);
938 "ASoC: could not create texts for %s\n",
946 "ASoC: invalid enum control type %d for %s\n",
947 ec
->hdr
.ops
.info
, ec
->hdr
.name
);
952 /* map io handlers */
953 err
= soc_tplg_kcontrol_bind_io(&ec
->hdr
, &kc
, io_ops
,
954 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
956 soc_control_err(tplg
, &ec
->hdr
, ec
->hdr
.name
);
961 /* pass control to driver for optional further init */
962 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
963 (struct snd_soc_tplg_ctl_hdr
*) ec
);
965 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
971 /* register control here */
972 ret
= soc_tplg_add_kcontrol(tplg
,
973 &kc
, &se
->dobj
.control
.kcontrol
);
975 dev_err(tplg
->dev
, "ASoC: could not add kcontrol %s\n",
981 list_add(&se
->dobj
.list
, &tplg
->comp
->dobj_list
);
987 static int soc_tplg_kcontrol_elems_load(struct soc_tplg
*tplg
,
988 struct snd_soc_tplg_hdr
*hdr
)
990 struct snd_soc_tplg_ctl_hdr
*control_hdr
;
993 if (tplg
->pass
!= SOC_TPLG_PASS_MIXER
) {
994 tplg
->pos
+= hdr
->size
+ hdr
->payload_size
;
998 dev_dbg(tplg
->dev
, "ASoC: adding %d kcontrols at 0x%lx\n", hdr
->count
,
999 soc_tplg_get_offset(tplg
));
1001 for (i
= 0; i
< hdr
->count
; i
++) {
1003 control_hdr
= (struct snd_soc_tplg_ctl_hdr
*)tplg
->pos
;
1005 switch (control_hdr
->ops
.info
) {
1006 case SND_SOC_TPLG_CTL_VOLSW
:
1007 case SND_SOC_TPLG_CTL_STROBE
:
1008 case SND_SOC_TPLG_CTL_VOLSW_SX
:
1009 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
1010 case SND_SOC_TPLG_CTL_RANGE
:
1011 case SND_SOC_TPLG_DAPM_CTL_VOLSW
:
1012 case SND_SOC_TPLG_DAPM_CTL_PIN
:
1013 soc_tplg_dmixer_create(tplg
, 1, hdr
->payload_size
);
1015 case SND_SOC_TPLG_CTL_ENUM
:
1016 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1017 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1018 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1019 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1020 soc_tplg_denum_create(tplg
, 1, hdr
->payload_size
);
1022 case SND_SOC_TPLG_CTL_BYTES
:
1023 soc_tplg_dbytes_create(tplg
, 1, hdr
->payload_size
);
1026 soc_bind_err(tplg
, control_hdr
, i
);
1034 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg
*tplg
,
1035 struct snd_soc_tplg_hdr
*hdr
)
1037 struct snd_soc_dapm_context
*dapm
= &tplg
->comp
->dapm
;
1038 struct snd_soc_dapm_route route
;
1039 struct snd_soc_tplg_dapm_graph_elem
*elem
;
1040 int count
= hdr
->count
, i
;
1042 if (tplg
->pass
!= SOC_TPLG_PASS_GRAPH
) {
1043 tplg
->pos
+= hdr
->size
+ hdr
->payload_size
;
1047 if (soc_tplg_check_elem_count(tplg
,
1048 sizeof(struct snd_soc_tplg_dapm_graph_elem
),
1049 count
, hdr
->payload_size
, "graph")) {
1051 dev_err(tplg
->dev
, "ASoC: invalid count %d for DAPM routes\n",
1056 dev_dbg(tplg
->dev
, "ASoC: adding %d DAPM routes\n", count
);
1058 for (i
= 0; i
< count
; i
++) {
1059 elem
= (struct snd_soc_tplg_dapm_graph_elem
*)tplg
->pos
;
1060 tplg
->pos
+= sizeof(struct snd_soc_tplg_dapm_graph_elem
);
1062 /* validate routes */
1063 if (strnlen(elem
->source
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1064 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1066 if (strnlen(elem
->sink
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1067 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1069 if (strnlen(elem
->control
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1070 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1073 route
.source
= elem
->source
;
1074 route
.sink
= elem
->sink
;
1075 route
.connected
= NULL
; /* set to NULL atm for tplg users */
1076 if (strnlen(elem
->control
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) == 0)
1077 route
.control
= NULL
;
1079 route
.control
= elem
->control
;
1081 /* add route, but keep going if some fail */
1082 snd_soc_dapm_add_routes(dapm
, &route
, 1);
1088 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_dmixer_create(
1089 struct soc_tplg
*tplg
, int num_kcontrols
)
1091 struct snd_kcontrol_new
*kc
;
1092 struct soc_mixer_control
*sm
;
1093 struct snd_soc_tplg_mixer_control
*mc
;
1096 kc
= kzalloc(sizeof(*kc
) * num_kcontrols
, GFP_KERNEL
);
1100 for (i
= 0; i
< num_kcontrols
; i
++) {
1101 mc
= (struct snd_soc_tplg_mixer_control
*)tplg
->pos
;
1102 sm
= kzalloc(sizeof(*sm
), GFP_KERNEL
);
1106 tplg
->pos
+= (sizeof(struct snd_soc_tplg_mixer_control
) +
1109 /* validate kcontrol */
1110 if (strnlen(mc
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1111 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1114 dev_dbg(tplg
->dev
, " adding DAPM widget mixer control %s at %d\n",
1117 kc
[i
].name
= mc
->hdr
.name
;
1118 kc
[i
].private_value
= (long)sm
;
1119 kc
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1120 kc
[i
].access
= mc
->hdr
.access
;
1122 /* we only support FL/FR channel mapping atm */
1123 sm
->reg
= tplc_chan_get_reg(tplg
, mc
->channel
,
1125 sm
->rreg
= tplc_chan_get_reg(tplg
, mc
->channel
,
1127 sm
->shift
= tplc_chan_get_shift(tplg
, mc
->channel
,
1129 sm
->rshift
= tplc_chan_get_shift(tplg
, mc
->channel
,
1134 sm
->invert
= mc
->invert
;
1135 sm
->platform_max
= mc
->platform_max
;
1136 sm
->dobj
.index
= tplg
->index
;
1137 INIT_LIST_HEAD(&sm
->dobj
.list
);
1139 /* map io handlers */
1140 err
= soc_tplg_kcontrol_bind_io(&mc
->hdr
, &kc
[i
], io_ops
,
1141 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
1143 soc_control_err(tplg
, &mc
->hdr
, mc
->hdr
.name
);
1148 /* pass control to driver for optional further init */
1149 err
= soc_tplg_init_kcontrol(tplg
, &kc
[i
],
1150 (struct snd_soc_tplg_ctl_hdr
*)mc
);
1152 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1163 for (--i
; i
>= 0; i
--)
1164 kfree((void *)kc
[i
].private_value
);
1169 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_denum_create(
1170 struct soc_tplg
*tplg
)
1172 struct snd_kcontrol_new
*kc
;
1173 struct snd_soc_tplg_enum_control
*ec
;
1174 struct soc_enum
*se
;
1177 ec
= (struct snd_soc_tplg_enum_control
*)tplg
->pos
;
1178 tplg
->pos
+= (sizeof(struct snd_soc_tplg_enum_control
) +
1181 /* validate kcontrol */
1182 if (strnlen(ec
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1183 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1186 kc
= kzalloc(sizeof(*kc
), GFP_KERNEL
);
1190 se
= kzalloc(sizeof(*se
), GFP_KERNEL
);
1194 dev_dbg(tplg
->dev
, " adding DAPM widget enum control %s\n",
1197 kc
->name
= ec
->hdr
.name
;
1198 kc
->private_value
= (long)se
;
1199 kc
->iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1200 kc
->access
= ec
->hdr
.access
;
1202 /* we only support FL/FR channel mapping atm */
1203 se
->reg
= tplc_chan_get_reg(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
1204 se
->shift_l
= tplc_chan_get_shift(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
1205 se
->shift_r
= tplc_chan_get_shift(tplg
, ec
->channel
, SNDRV_CHMAP_FR
);
1207 se
->items
= ec
->items
;
1208 se
->mask
= ec
->mask
;
1209 se
->dobj
.index
= tplg
->index
;
1211 switch (ec
->hdr
.ops
.info
) {
1212 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1213 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1214 err
= soc_tplg_denum_create_values(se
, ec
);
1216 dev_err(tplg
->dev
, "ASoC: could not create values for %s\n",
1220 /* fall through to create texts */
1221 case SND_SOC_TPLG_CTL_ENUM
:
1222 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1223 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1224 err
= soc_tplg_denum_create_texts(se
, ec
);
1226 dev_err(tplg
->dev
, "ASoC: could not create texts for %s\n",
1232 dev_err(tplg
->dev
, "ASoC: invalid enum control type %d for %s\n",
1233 ec
->hdr
.ops
.info
, ec
->hdr
.name
);
1237 /* map io handlers */
1238 err
= soc_tplg_kcontrol_bind_io(&ec
->hdr
, kc
, io_ops
,
1239 ARRAY_SIZE(io_ops
), tplg
->io_ops
, tplg
->io_ops_count
);
1241 soc_control_err(tplg
, &ec
->hdr
, ec
->hdr
.name
);
1245 /* pass control to driver for optional further init */
1246 err
= soc_tplg_init_kcontrol(tplg
, kc
,
1247 (struct snd_soc_tplg_ctl_hdr
*)ec
);
1249 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1257 /* free values and texts */
1258 kfree(se
->dobj
.control
.dvalues
);
1259 for (i
= 0; i
< ec
->items
; i
++)
1260 kfree(se
->dobj
.control
.dtexts
[i
]);
1269 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_dbytes_create(
1270 struct soc_tplg
*tplg
, int count
)
1272 struct snd_soc_tplg_bytes_control
*be
;
1273 struct soc_bytes_ext
*sbe
;
1274 struct snd_kcontrol_new
*kc
;
1277 kc
= kzalloc(sizeof(*kc
) * count
, GFP_KERNEL
);
1281 for (i
= 0; i
< count
; i
++) {
1282 be
= (struct snd_soc_tplg_bytes_control
*)tplg
->pos
;
1284 /* validate kcontrol */
1285 if (strnlen(be
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1286 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1289 sbe
= kzalloc(sizeof(*sbe
), GFP_KERNEL
);
1293 tplg
->pos
+= (sizeof(struct snd_soc_tplg_bytes_control
) +
1297 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1298 be
->hdr
.name
, be
->hdr
.access
);
1300 memset(kc
, 0, sizeof(*kc
));
1301 kc
[i
].name
= be
->hdr
.name
;
1302 kc
[i
].private_value
= (long)sbe
;
1303 kc
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1304 kc
[i
].access
= be
->hdr
.access
;
1307 INIT_LIST_HEAD(&sbe
->dobj
.list
);
1309 /* map standard io handlers and check for external handlers */
1310 err
= soc_tplg_kcontrol_bind_io(&be
->hdr
, &kc
[i
], io_ops
,
1311 ARRAY_SIZE(io_ops
), tplg
->io_ops
,
1312 tplg
->io_ops_count
);
1314 soc_control_err(tplg
, &be
->hdr
, be
->hdr
.name
);
1319 /* pass control to driver for optional further init */
1320 err
= soc_tplg_init_kcontrol(tplg
, &kc
[i
],
1321 (struct snd_soc_tplg_ctl_hdr
*)be
);
1323 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1333 for (--i
; i
>= 0; i
--)
1334 kfree((void *)kc
[i
].private_value
);
1340 static int soc_tplg_dapm_widget_create(struct soc_tplg
*tplg
,
1341 struct snd_soc_tplg_dapm_widget
*w
)
1343 struct snd_soc_dapm_context
*dapm
= &tplg
->comp
->dapm
;
1344 struct snd_soc_dapm_widget
template, *widget
;
1345 struct snd_soc_tplg_ctl_hdr
*control_hdr
;
1346 struct snd_soc_card
*card
= tplg
->comp
->card
;
1349 if (strnlen(w
->name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1350 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1352 if (strnlen(w
->sname
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1353 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1356 dev_dbg(tplg
->dev
, "ASoC: creating DAPM widget %s id %d\n",
1359 memset(&template, 0, sizeof(template));
1361 /* map user to kernel widget ID */
1362 template.id
= get_widget_id(w
->id
);
1363 if (template.id
< 0)
1366 template.name
= kstrdup(w
->name
, GFP_KERNEL
);
1369 template.sname
= kstrdup(w
->sname
, GFP_KERNEL
);
1370 if (!template.sname
) {
1374 template.reg
= w
->reg
;
1375 template.shift
= w
->shift
;
1376 template.mask
= w
->mask
;
1377 template.subseq
= w
->subseq
;
1378 template.on_val
= w
->invert
? 0 : 1;
1379 template.off_val
= w
->invert
? 1 : 0;
1380 template.ignore_suspend
= w
->ignore_suspend
;
1381 template.event_flags
= w
->event_flags
;
1382 template.dobj
.index
= tplg
->index
;
1385 (sizeof(struct snd_soc_tplg_dapm_widget
) + w
->priv
.size
);
1386 if (w
->num_kcontrols
== 0) {
1387 template.num_kcontrols
= 0;
1391 control_hdr
= (struct snd_soc_tplg_ctl_hdr
*)tplg
->pos
;
1392 dev_dbg(tplg
->dev
, "ASoC: template %s has %d controls of type %x\n",
1393 w
->name
, w
->num_kcontrols
, control_hdr
->type
);
1395 switch (control_hdr
->ops
.info
) {
1396 case SND_SOC_TPLG_CTL_VOLSW
:
1397 case SND_SOC_TPLG_CTL_STROBE
:
1398 case SND_SOC_TPLG_CTL_VOLSW_SX
:
1399 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
1400 case SND_SOC_TPLG_CTL_RANGE
:
1401 case SND_SOC_TPLG_DAPM_CTL_VOLSW
:
1402 template.num_kcontrols
= w
->num_kcontrols
;
1403 template.kcontrol_news
=
1404 soc_tplg_dapm_widget_dmixer_create(tplg
,
1405 template.num_kcontrols
);
1406 if (!template.kcontrol_news
) {
1411 case SND_SOC_TPLG_CTL_ENUM
:
1412 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1413 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1414 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1415 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1416 template.dobj
.widget
.kcontrol_enum
= 1;
1417 template.num_kcontrols
= 1;
1418 template.kcontrol_news
=
1419 soc_tplg_dapm_widget_denum_create(tplg
);
1420 if (!template.kcontrol_news
) {
1425 case SND_SOC_TPLG_CTL_BYTES
:
1426 template.num_kcontrols
= w
->num_kcontrols
;
1427 template.kcontrol_news
=
1428 soc_tplg_dapm_widget_dbytes_create(tplg
,
1429 template.num_kcontrols
);
1430 if (!template.kcontrol_news
) {
1436 dev_err(tplg
->dev
, "ASoC: invalid widget control type %d:%d:%d\n",
1437 control_hdr
->ops
.get
, control_hdr
->ops
.put
,
1438 control_hdr
->ops
.info
);
1444 ret
= soc_tplg_widget_load(tplg
, &template, w
);
1448 /* card dapm mutex is held by the core if we are loading topology
1449 * data during sound card init. */
1450 if (card
->instantiated
)
1451 widget
= snd_soc_dapm_new_control(dapm
, &template);
1453 widget
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
1454 if (widget
== NULL
) {
1455 dev_err(tplg
->dev
, "ASoC: failed to create widget %s controls\n",
1460 widget
->dobj
.type
= SND_SOC_DOBJ_WIDGET
;
1461 widget
->dobj
.ops
= tplg
->ops
;
1462 widget
->dobj
.index
= tplg
->index
;
1463 list_add(&widget
->dobj
.list
, &tplg
->comp
->dobj_list
);
1467 kfree(template.sname
);
1469 kfree(template.name
);
1473 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg
*tplg
,
1474 struct snd_soc_tplg_hdr
*hdr
)
1476 struct snd_soc_tplg_dapm_widget
*widget
;
1477 int ret
, count
= hdr
->count
, i
;
1479 if (tplg
->pass
!= SOC_TPLG_PASS_WIDGET
)
1482 dev_dbg(tplg
->dev
, "ASoC: adding %d DAPM widgets\n", count
);
1484 for (i
= 0; i
< count
; i
++) {
1485 widget
= (struct snd_soc_tplg_dapm_widget
*) tplg
->pos
;
1486 ret
= soc_tplg_dapm_widget_create(tplg
, widget
);
1488 dev_err(tplg
->dev
, "ASoC: failed to load widget %s\n",
1495 static int soc_tplg_dapm_complete(struct soc_tplg
*tplg
)
1497 struct snd_soc_card
*card
= tplg
->comp
->card
;
1500 /* Card might not have been registered at this point.
1501 * If so, just return success.
1503 if (!card
|| !card
->instantiated
) {
1504 dev_warn(tplg
->dev
, "ASoC: Parent card not yet available,"
1505 "Do not add new widgets now\n");
1509 ret
= snd_soc_dapm_new_widgets(card
);
1511 dev_err(tplg
->dev
, "ASoC: failed to create new widgets %d\n",
1517 static int soc_tplg_pcm_dai_elems_load(struct soc_tplg
*tplg
,
1518 struct snd_soc_tplg_hdr
*hdr
)
1520 struct snd_soc_tplg_pcm_dai
*pcm_dai
;
1521 struct snd_soc_dobj
*dobj
;
1522 int count
= hdr
->count
;
1525 if (tplg
->pass
!= SOC_TPLG_PASS_PCM_DAI
)
1528 pcm_dai
= (struct snd_soc_tplg_pcm_dai
*)tplg
->pos
;
1530 if (soc_tplg_check_elem_count(tplg
,
1531 sizeof(struct snd_soc_tplg_pcm_dai
), count
,
1532 hdr
->payload_size
, "PCM DAI")) {
1533 dev_err(tplg
->dev
, "ASoC: invalid count %d for PCM DAI elems\n",
1538 dev_dbg(tplg
->dev
, "ASoC: adding %d PCM DAIs\n", count
);
1539 tplg
->pos
+= sizeof(struct snd_soc_tplg_pcm_dai
) * count
;
1541 dobj
= kzalloc(sizeof(struct snd_soc_dobj
), GFP_KERNEL
);
1545 /* Call the platform driver call back to register the dais */
1546 ret
= soc_tplg_pcm_dai_load(tplg
, pcm_dai
, count
);
1548 dev_err(tplg
->comp
->dev
, "ASoC: PCM DAI loading failed\n");
1552 dobj
->type
= get_dobj_type(hdr
, NULL
);
1553 dobj
->pcm_dai
.count
= count
;
1554 dobj
->pcm_dai
.pd
= pcm_dai
;
1555 dobj
->ops
= tplg
->ops
;
1556 dobj
->index
= tplg
->index
;
1557 list_add(&dobj
->list
, &tplg
->comp
->dobj_list
);
1565 static int soc_tplg_manifest_load(struct soc_tplg
*tplg
,
1566 struct snd_soc_tplg_hdr
*hdr
)
1568 struct snd_soc_tplg_manifest
*manifest
;
1570 if (tplg
->pass
!= SOC_TPLG_PASS_MANIFEST
)
1573 manifest
= (struct snd_soc_tplg_manifest
*)tplg
->pos
;
1574 tplg
->pos
+= sizeof(struct snd_soc_tplg_manifest
);
1576 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->manifest
)
1577 return tplg
->ops
->manifest(tplg
->comp
, manifest
);
1579 dev_err(tplg
->dev
, "ASoC: Firmware manifest not supported\n");
1583 /* validate header magic, size and type */
1584 static int soc_valid_header(struct soc_tplg
*tplg
,
1585 struct snd_soc_tplg_hdr
*hdr
)
1587 if (soc_tplg_get_hdr_offset(tplg
) >= tplg
->fw
->size
)
1590 /* big endian firmware objects not supported atm */
1591 if (hdr
->magic
== cpu_to_be32(SND_SOC_TPLG_MAGIC
)) {
1593 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1594 tplg
->pass
, hdr
->magic
,
1595 soc_tplg_get_hdr_offset(tplg
), tplg
->fw
->size
);
1599 if (hdr
->magic
!= SND_SOC_TPLG_MAGIC
) {
1601 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1602 tplg
->pass
, hdr
->magic
,
1603 soc_tplg_get_hdr_offset(tplg
), tplg
->fw
->size
);
1607 if (hdr
->abi
!= SND_SOC_TPLG_ABI_VERSION
) {
1609 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1610 tplg
->pass
, hdr
->abi
,
1611 SND_SOC_TPLG_ABI_VERSION
, soc_tplg_get_hdr_offset(tplg
),
1616 if (hdr
->payload_size
== 0) {
1617 dev_err(tplg
->dev
, "ASoC: header has 0 size at offset 0x%lx.\n",
1618 soc_tplg_get_hdr_offset(tplg
));
1622 if (tplg
->pass
== hdr
->type
)
1624 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
1625 hdr
->payload_size
, hdr
->type
, hdr
->version
,
1626 hdr
->vendor_type
, tplg
->pass
);
1631 /* check header type and call appropriate handler */
1632 static int soc_tplg_load_header(struct soc_tplg
*tplg
,
1633 struct snd_soc_tplg_hdr
*hdr
)
1635 tplg
->pos
= tplg
->hdr_pos
+ sizeof(struct snd_soc_tplg_hdr
);
1637 /* check for matching ID */
1638 if (hdr
->index
!= tplg
->req_index
&&
1639 hdr
->index
!= SND_SOC_TPLG_INDEX_ALL
)
1642 tplg
->index
= hdr
->index
;
1644 switch (hdr
->type
) {
1645 case SND_SOC_TPLG_TYPE_MIXER
:
1646 case SND_SOC_TPLG_TYPE_ENUM
:
1647 case SND_SOC_TPLG_TYPE_BYTES
:
1648 return soc_tplg_kcontrol_elems_load(tplg
, hdr
);
1649 case SND_SOC_TPLG_TYPE_DAPM_GRAPH
:
1650 return soc_tplg_dapm_graph_elems_load(tplg
, hdr
);
1651 case SND_SOC_TPLG_TYPE_DAPM_WIDGET
:
1652 return soc_tplg_dapm_widget_elems_load(tplg
, hdr
);
1653 case SND_SOC_TPLG_TYPE_PCM
:
1654 case SND_SOC_TPLG_TYPE_DAI_LINK
:
1655 case SND_SOC_TPLG_TYPE_CODEC_LINK
:
1656 return soc_tplg_pcm_dai_elems_load(tplg
, hdr
);
1657 case SND_SOC_TPLG_TYPE_MANIFEST
:
1658 return soc_tplg_manifest_load(tplg
, hdr
);
1660 /* bespoke vendor data object */
1661 return soc_tplg_vendor_load(tplg
, hdr
);
1667 /* process the topology file headers */
1668 static int soc_tplg_process_headers(struct soc_tplg
*tplg
)
1670 struct snd_soc_tplg_hdr
*hdr
;
1673 tplg
->pass
= SOC_TPLG_PASS_START
;
1675 /* process the header types from start to end */
1676 while (tplg
->pass
<= SOC_TPLG_PASS_END
) {
1678 tplg
->hdr_pos
= tplg
->fw
->data
;
1679 hdr
= (struct snd_soc_tplg_hdr
*)tplg
->hdr_pos
;
1681 while (!soc_tplg_is_eof(tplg
)) {
1683 /* make sure header is valid before loading */
1684 ret
= soc_valid_header(tplg
, hdr
);
1690 /* load the header object */
1691 ret
= soc_tplg_load_header(tplg
, hdr
);
1695 /* goto next header */
1696 tplg
->hdr_pos
+= hdr
->payload_size
+
1697 sizeof(struct snd_soc_tplg_hdr
);
1698 hdr
= (struct snd_soc_tplg_hdr
*)tplg
->hdr_pos
;
1701 /* next data type pass */
1705 /* signal DAPM we are complete */
1706 ret
= soc_tplg_dapm_complete(tplg
);
1709 "ASoC: failed to initialise DAPM from Firmware\n");
1714 static int soc_tplg_load(struct soc_tplg
*tplg
)
1718 ret
= soc_tplg_process_headers(tplg
);
1720 soc_tplg_complete(tplg
);
1725 /* load audio component topology from "firmware" file */
1726 int snd_soc_tplg_component_load(struct snd_soc_component
*comp
,
1727 struct snd_soc_tplg_ops
*ops
, const struct firmware
*fw
, u32 id
)
1729 struct soc_tplg tplg
;
1731 /* setup parsing context */
1732 memset(&tplg
, 0, sizeof(tplg
));
1734 tplg
.dev
= comp
->dev
;
1737 tplg
.req_index
= id
;
1738 tplg
.io_ops
= ops
->io_ops
;
1739 tplg
.io_ops_count
= ops
->io_ops_count
;
1741 return soc_tplg_load(&tplg
);
1743 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load
);
1745 /* remove this dynamic widget */
1746 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget
*w
)
1748 /* make sure we are a widget */
1749 if (w
->dobj
.type
!= SND_SOC_DOBJ_WIDGET
)
1752 remove_widget(w
->dapm
->component
, &w
->dobj
, SOC_TPLG_PASS_WIDGET
);
1754 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove
);
1756 /* remove all dynamic widgets from this DAPM context */
1757 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context
*dapm
,
1760 struct snd_soc_dapm_widget
*w
, *next_w
;
1761 struct snd_soc_dapm_path
*p
, *next_p
;
1763 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
1765 /* make sure we are a widget with correct context */
1766 if (w
->dobj
.type
!= SND_SOC_DOBJ_WIDGET
|| w
->dapm
!= dapm
)
1770 if (w
->dobj
.index
!= index
&&
1771 w
->dobj
.index
!= SND_SOC_TPLG_INDEX_ALL
)
1777 * remove source and sink paths associated to this widget.
1778 * While removing the path, remove reference to it from both
1779 * source and sink widgets so that path is removed only once.
1781 list_for_each_entry_safe(p
, next_p
, &w
->sources
, list_sink
) {
1782 list_del(&p
->list_sink
);
1783 list_del(&p
->list_source
);
1787 list_for_each_entry_safe(p
, next_p
, &w
->sinks
, list_source
) {
1788 list_del(&p
->list_sink
);
1789 list_del(&p
->list_source
);
1793 /* check and free and dynamic widget kcontrols */
1794 snd_soc_tplg_widget_remove(w
);
1795 kfree(w
->kcontrols
);
1800 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all
);
1802 /* remove dynamic controls from the component driver */
1803 int snd_soc_tplg_component_remove(struct snd_soc_component
*comp
, u32 index
)
1805 struct snd_soc_dobj
*dobj
, *next_dobj
;
1806 int pass
= SOC_TPLG_PASS_END
;
1808 /* process the header types from end to start */
1809 while (pass
>= SOC_TPLG_PASS_START
) {
1811 /* remove mixer controls */
1812 list_for_each_entry_safe(dobj
, next_dobj
, &comp
->dobj_list
,
1816 if (dobj
->index
!= index
&&
1817 dobj
->index
!= SND_SOC_TPLG_INDEX_ALL
)
1820 switch (dobj
->type
) {
1821 case SND_SOC_DOBJ_MIXER
:
1822 remove_mixer(comp
, dobj
, pass
);
1824 case SND_SOC_DOBJ_ENUM
:
1825 remove_enum(comp
, dobj
, pass
);
1827 case SND_SOC_DOBJ_BYTES
:
1828 remove_bytes(comp
, dobj
, pass
);
1830 case SND_SOC_DOBJ_WIDGET
:
1831 remove_widget(comp
, dobj
, pass
);
1833 case SND_SOC_DOBJ_PCM
:
1834 case SND_SOC_DOBJ_DAI_LINK
:
1835 case SND_SOC_DOBJ_CODEC_LINK
:
1836 remove_pcm_dai(comp
, dobj
, pass
);
1839 dev_err(comp
->dev
, "ASoC: invalid component type %d for removal\n",
1847 /* let caller know if FW can be freed when no objects are left */
1848 return !list_empty(&comp
->dobj_list
);
1850 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove
);