2 * soc-topology.c -- ALSA SoC Topology
4 * Copyright (C) 2012 Texas Instruments Inc.
5 * Copyright (C) 2015 Intel Corporation.
7 * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
8 * K, Mythri P <mythri.p.k@intel.com>
9 * Prusty, Subhransu S <subhransu.s.prusty@intel.com>
10 * B, Jayachandran <jayachandran.b@intel.com>
11 * Abdullah, Omair M <omair.m.abdullah@intel.com>
12 * Jin, Yao <yao.jin@intel.com>
13 * Lin, Mengdong <mengdong.lin@intel.com>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * Add support to read audio firmware topology alongside firmware text. The
21 * topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
22 * equalizers, firmware, coefficients etc.
24 * This file only manages the core ALSA and ASoC components, all other bespoke
25 * firmware topology data is passed to component drivers for bespoke handling.
28 #include <linux/kernel.h>
29 #include <linux/export.h>
30 #include <linux/list.h>
31 #include <linux/firmware.h>
32 #include <linux/slab.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/soc-topology.h>
36 #include <sound/tlv.h>
39 * We make several passes over the data (since it wont necessarily be ordered)
40 * and process objects in the following order. This guarantees the component
41 * drivers will be ready with any vendor data before the mixers and DAPM objects
42 * are loaded (that may make use of the vendor data).
44 #define SOC_TPLG_PASS_MANIFEST 0
45 #define SOC_TPLG_PASS_VENDOR 1
46 #define SOC_TPLG_PASS_MIXER 2
47 #define SOC_TPLG_PASS_WIDGET 3
48 #define SOC_TPLG_PASS_PCM_DAI 4
49 #define SOC_TPLG_PASS_GRAPH 5
50 #define SOC_TPLG_PASS_PINS 6
51 #define SOC_TPLG_PASS_BE_DAI 7
52 #define SOC_TPLG_PASS_LINK 8
54 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
55 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
57 /* topology context */
59 const struct firmware
*fw
;
61 /* runtime FW parsing */
62 const u8
*pos
; /* read postion */
63 const u8
*hdr_pos
; /* header position */
64 unsigned int pass
; /* pass number */
66 /* component caller */
68 struct snd_soc_component
*comp
;
69 u32 index
; /* current block index */
70 u32 req_index
; /* required index, only loaded/free matching blocks */
72 /* vendor specific kcontrol operations */
73 const struct snd_soc_tplg_kcontrol_ops
*io_ops
;
76 /* vendor specific bytes ext handlers, for TLV bytes controls */
77 const struct snd_soc_tplg_bytes_ext_ops
*bytes_ext_ops
;
78 int bytes_ext_ops_count
;
80 /* optional fw loading callbacks to component drivers */
81 struct snd_soc_tplg_ops
*ops
;
84 static int soc_tplg_process_headers(struct soc_tplg
*tplg
);
85 static void soc_tplg_complete(struct soc_tplg
*tplg
);
86 struct snd_soc_dapm_widget
*
87 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
88 const struct snd_soc_dapm_widget
*widget
);
89 struct snd_soc_dapm_widget
*
90 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
91 const struct snd_soc_dapm_widget
*widget
);
93 /* check we dont overflow the data for this control chunk */
94 static int soc_tplg_check_elem_count(struct soc_tplg
*tplg
, size_t elem_size
,
95 unsigned int count
, size_t bytes
, const char *elem_type
)
97 const u8
*end
= tplg
->pos
+ elem_size
* count
;
99 if (end
> tplg
->fw
->data
+ tplg
->fw
->size
) {
100 dev_err(tplg
->dev
, "ASoC: %s overflow end of data\n",
105 /* check there is enough room in chunk for control.
106 extra bytes at the end of control are for vendor data here */
107 if (elem_size
* count
> bytes
) {
109 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
110 elem_type
, count
, elem_size
, bytes
);
117 static inline int soc_tplg_is_eof(struct soc_tplg
*tplg
)
119 const u8
*end
= tplg
->hdr_pos
;
121 if (end
>= tplg
->fw
->data
+ tplg
->fw
->size
)
126 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg
*tplg
)
128 return (unsigned long)(tplg
->hdr_pos
- tplg
->fw
->data
);
131 static inline unsigned long soc_tplg_get_offset(struct soc_tplg
*tplg
)
133 return (unsigned long)(tplg
->pos
- tplg
->fw
->data
);
136 /* mapping of Kcontrol types and associated operations. */
137 static const struct snd_soc_tplg_kcontrol_ops io_ops
[] = {
138 {SND_SOC_TPLG_CTL_VOLSW
, snd_soc_get_volsw
,
139 snd_soc_put_volsw
, snd_soc_info_volsw
},
140 {SND_SOC_TPLG_CTL_VOLSW_SX
, snd_soc_get_volsw_sx
,
141 snd_soc_put_volsw_sx
, NULL
},
142 {SND_SOC_TPLG_CTL_ENUM
, snd_soc_get_enum_double
,
143 snd_soc_put_enum_double
, snd_soc_info_enum_double
},
144 {SND_SOC_TPLG_CTL_ENUM_VALUE
, snd_soc_get_enum_double
,
145 snd_soc_put_enum_double
, NULL
},
146 {SND_SOC_TPLG_CTL_BYTES
, snd_soc_bytes_get
,
147 snd_soc_bytes_put
, snd_soc_bytes_info
},
148 {SND_SOC_TPLG_CTL_RANGE
, snd_soc_get_volsw_range
,
149 snd_soc_put_volsw_range
, snd_soc_info_volsw_range
},
150 {SND_SOC_TPLG_CTL_VOLSW_XR_SX
, snd_soc_get_xr_sx
,
151 snd_soc_put_xr_sx
, snd_soc_info_xr_sx
},
152 {SND_SOC_TPLG_CTL_STROBE
, snd_soc_get_strobe
,
153 snd_soc_put_strobe
, NULL
},
154 {SND_SOC_TPLG_DAPM_CTL_VOLSW
, snd_soc_dapm_get_volsw
,
155 snd_soc_dapm_put_volsw
, snd_soc_info_volsw
},
156 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
, snd_soc_dapm_get_enum_double
,
157 snd_soc_dapm_put_enum_double
, snd_soc_info_enum_double
},
158 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
, snd_soc_dapm_get_enum_double
,
159 snd_soc_dapm_put_enum_double
, NULL
},
160 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
, snd_soc_dapm_get_enum_double
,
161 snd_soc_dapm_put_enum_double
, NULL
},
162 {SND_SOC_TPLG_DAPM_CTL_PIN
, snd_soc_dapm_get_pin_switch
,
163 snd_soc_dapm_put_pin_switch
, snd_soc_dapm_info_pin_switch
},
166 struct soc_tplg_map
{
171 /* mapping of widget types from UAPI IDs to kernel IDs */
172 static const struct soc_tplg_map dapm_map
[] = {
173 {SND_SOC_TPLG_DAPM_INPUT
, snd_soc_dapm_input
},
174 {SND_SOC_TPLG_DAPM_OUTPUT
, snd_soc_dapm_output
},
175 {SND_SOC_TPLG_DAPM_MUX
, snd_soc_dapm_mux
},
176 {SND_SOC_TPLG_DAPM_MIXER
, snd_soc_dapm_mixer
},
177 {SND_SOC_TPLG_DAPM_PGA
, snd_soc_dapm_pga
},
178 {SND_SOC_TPLG_DAPM_OUT_DRV
, snd_soc_dapm_out_drv
},
179 {SND_SOC_TPLG_DAPM_ADC
, snd_soc_dapm_adc
},
180 {SND_SOC_TPLG_DAPM_DAC
, snd_soc_dapm_dac
},
181 {SND_SOC_TPLG_DAPM_SWITCH
, snd_soc_dapm_switch
},
182 {SND_SOC_TPLG_DAPM_PRE
, snd_soc_dapm_pre
},
183 {SND_SOC_TPLG_DAPM_POST
, snd_soc_dapm_post
},
184 {SND_SOC_TPLG_DAPM_AIF_IN
, snd_soc_dapm_aif_in
},
185 {SND_SOC_TPLG_DAPM_AIF_OUT
, snd_soc_dapm_aif_out
},
186 {SND_SOC_TPLG_DAPM_DAI_IN
, snd_soc_dapm_dai_in
},
187 {SND_SOC_TPLG_DAPM_DAI_OUT
, snd_soc_dapm_dai_out
},
188 {SND_SOC_TPLG_DAPM_DAI_LINK
, snd_soc_dapm_dai_link
},
189 {SND_SOC_TPLG_DAPM_BUFFER
, snd_soc_dapm_buffer
},
190 {SND_SOC_TPLG_DAPM_SCHEDULER
, snd_soc_dapm_scheduler
},
191 {SND_SOC_TPLG_DAPM_EFFECT
, snd_soc_dapm_effect
},
192 {SND_SOC_TPLG_DAPM_SIGGEN
, snd_soc_dapm_siggen
},
193 {SND_SOC_TPLG_DAPM_SRC
, snd_soc_dapm_src
},
194 {SND_SOC_TPLG_DAPM_ASRC
, snd_soc_dapm_asrc
},
195 {SND_SOC_TPLG_DAPM_ENCODER
, snd_soc_dapm_encoder
},
196 {SND_SOC_TPLG_DAPM_DECODER
, snd_soc_dapm_decoder
},
199 static int tplc_chan_get_reg(struct soc_tplg
*tplg
,
200 struct snd_soc_tplg_channel
*chan
, int map
)
204 for (i
= 0; i
< SND_SOC_TPLG_MAX_CHAN
; i
++) {
205 if (chan
[i
].id
== map
)
212 static int tplc_chan_get_shift(struct soc_tplg
*tplg
,
213 struct snd_soc_tplg_channel
*chan
, int map
)
217 for (i
= 0; i
< SND_SOC_TPLG_MAX_CHAN
; i
++) {
218 if (chan
[i
].id
== map
)
219 return chan
[i
].shift
;
225 static int get_widget_id(int tplg_type
)
229 for (i
= 0; i
< ARRAY_SIZE(dapm_map
); i
++) {
230 if (tplg_type
== dapm_map
[i
].uid
)
231 return dapm_map
[i
].kid
;
237 static inline void soc_bind_err(struct soc_tplg
*tplg
,
238 struct snd_soc_tplg_ctl_hdr
*hdr
, int index
)
241 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
242 hdr
->ops
.get
, hdr
->ops
.put
, hdr
->ops
.info
, index
,
243 soc_tplg_get_offset(tplg
));
246 static inline void soc_control_err(struct soc_tplg
*tplg
,
247 struct snd_soc_tplg_ctl_hdr
*hdr
, const char *name
)
250 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
251 name
, hdr
->ops
.get
, hdr
->ops
.put
, hdr
->ops
.info
,
252 soc_tplg_get_offset(tplg
));
255 /* pass vendor data to component driver for processing */
256 static int soc_tplg_vendor_load_(struct soc_tplg
*tplg
,
257 struct snd_soc_tplg_hdr
*hdr
)
261 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->vendor_load
)
262 ret
= tplg
->ops
->vendor_load(tplg
->comp
, hdr
);
264 dev_err(tplg
->dev
, "ASoC: no vendor load callback for ID %d\n",
271 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
272 soc_tplg_get_hdr_offset(tplg
),
273 soc_tplg_get_hdr_offset(tplg
),
274 hdr
->type
, hdr
->vendor_type
);
278 /* pass vendor data to component driver for processing */
279 static int soc_tplg_vendor_load(struct soc_tplg
*tplg
,
280 struct snd_soc_tplg_hdr
*hdr
)
282 if (tplg
->pass
!= SOC_TPLG_PASS_VENDOR
)
285 return soc_tplg_vendor_load_(tplg
, hdr
);
288 /* optionally pass new dynamic widget to component driver. This is mainly for
289 * external widgets where we can assign private data/ops */
290 static int soc_tplg_widget_load(struct soc_tplg
*tplg
,
291 struct snd_soc_dapm_widget
*w
, struct snd_soc_tplg_dapm_widget
*tplg_w
)
293 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->widget_load
)
294 return tplg
->ops
->widget_load(tplg
->comp
, w
, tplg_w
);
299 /* optionally pass new dynamic widget to component driver. This is mainly for
300 * external widgets where we can assign private data/ops */
301 static int soc_tplg_widget_ready(struct soc_tplg
*tplg
,
302 struct snd_soc_dapm_widget
*w
, struct snd_soc_tplg_dapm_widget
*tplg_w
)
304 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->widget_ready
)
305 return tplg
->ops
->widget_ready(tplg
->comp
, w
, tplg_w
);
310 /* pass DAI configurations to component driver for extra initialization */
311 static int soc_tplg_dai_load(struct soc_tplg
*tplg
,
312 struct snd_soc_dai_driver
*dai_drv
)
314 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->dai_load
)
315 return tplg
->ops
->dai_load(tplg
->comp
, dai_drv
);
320 /* pass link configurations to component driver for extra initialization */
321 static int soc_tplg_dai_link_load(struct soc_tplg
*tplg
,
322 struct snd_soc_dai_link
*link
)
324 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->link_load
)
325 return tplg
->ops
->link_load(tplg
->comp
, link
);
330 /* tell the component driver that all firmware has been loaded in this request */
331 static void soc_tplg_complete(struct soc_tplg
*tplg
)
333 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->complete
)
334 tplg
->ops
->complete(tplg
->comp
);
337 /* add a dynamic kcontrol */
338 static int soc_tplg_add_dcontrol(struct snd_card
*card
, struct device
*dev
,
339 const struct snd_kcontrol_new
*control_new
, const char *prefix
,
340 void *data
, struct snd_kcontrol
**kcontrol
)
344 *kcontrol
= snd_soc_cnew(control_new
, data
, control_new
->name
, prefix
);
345 if (*kcontrol
== NULL
) {
346 dev_err(dev
, "ASoC: Failed to create new kcontrol %s\n",
351 err
= snd_ctl_add(card
, *kcontrol
);
353 dev_err(dev
, "ASoC: Failed to add %s: %d\n",
354 control_new
->name
, err
);
361 /* add a dynamic kcontrol for component driver */
362 static int soc_tplg_add_kcontrol(struct soc_tplg
*tplg
,
363 struct snd_kcontrol_new
*k
, struct snd_kcontrol
**kcontrol
)
365 struct snd_soc_component
*comp
= tplg
->comp
;
367 return soc_tplg_add_dcontrol(comp
->card
->snd_card
,
368 comp
->dev
, k
, NULL
, comp
, kcontrol
);
371 /* remove a mixer kcontrol */
372 static void remove_mixer(struct snd_soc_component
*comp
,
373 struct snd_soc_dobj
*dobj
, int pass
)
375 struct snd_card
*card
= comp
->card
->snd_card
;
376 struct soc_mixer_control
*sm
=
377 container_of(dobj
, struct soc_mixer_control
, dobj
);
378 const unsigned int *p
= NULL
;
380 if (pass
!= SOC_TPLG_PASS_MIXER
)
383 if (dobj
->ops
&& dobj
->ops
->control_unload
)
384 dobj
->ops
->control_unload(comp
, dobj
);
386 if (sm
->dobj
.control
.kcontrol
->tlv
.p
)
387 p
= sm
->dobj
.control
.kcontrol
->tlv
.p
;
388 snd_ctl_remove(card
, sm
->dobj
.control
.kcontrol
);
389 list_del(&sm
->dobj
.list
);
394 /* remove an enum kcontrol */
395 static void remove_enum(struct snd_soc_component
*comp
,
396 struct snd_soc_dobj
*dobj
, int pass
)
398 struct snd_card
*card
= comp
->card
->snd_card
;
399 struct soc_enum
*se
= container_of(dobj
, struct soc_enum
, dobj
);
402 if (pass
!= SOC_TPLG_PASS_MIXER
)
405 if (dobj
->ops
&& dobj
->ops
->control_unload
)
406 dobj
->ops
->control_unload(comp
, dobj
);
408 snd_ctl_remove(card
, se
->dobj
.control
.kcontrol
);
409 list_del(&se
->dobj
.list
);
411 kfree(se
->dobj
.control
.dvalues
);
412 for (i
= 0; i
< se
->items
; i
++)
413 kfree(se
->dobj
.control
.dtexts
[i
]);
417 /* remove a byte kcontrol */
418 static void remove_bytes(struct snd_soc_component
*comp
,
419 struct snd_soc_dobj
*dobj
, int pass
)
421 struct snd_card
*card
= comp
->card
->snd_card
;
422 struct soc_bytes_ext
*sb
=
423 container_of(dobj
, struct soc_bytes_ext
, dobj
);
425 if (pass
!= SOC_TPLG_PASS_MIXER
)
428 if (dobj
->ops
&& dobj
->ops
->control_unload
)
429 dobj
->ops
->control_unload(comp
, dobj
);
431 snd_ctl_remove(card
, sb
->dobj
.control
.kcontrol
);
432 list_del(&sb
->dobj
.list
);
436 /* remove a widget and it's kcontrols - routes must be removed first */
437 static void remove_widget(struct snd_soc_component
*comp
,
438 struct snd_soc_dobj
*dobj
, int pass
)
440 struct snd_card
*card
= comp
->card
->snd_card
;
441 struct snd_soc_dapm_widget
*w
=
442 container_of(dobj
, struct snd_soc_dapm_widget
, dobj
);
445 if (pass
!= SOC_TPLG_PASS_WIDGET
)
448 if (dobj
->ops
&& dobj
->ops
->widget_unload
)
449 dobj
->ops
->widget_unload(comp
, dobj
);
455 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
456 * The enum may either have an array of values or strings.
458 if (dobj
->widget
.kcontrol_type
== SND_SOC_TPLG_TYPE_ENUM
) {
459 /* enumerated widget mixer */
460 for (i
= 0; w
->kcontrols
!= NULL
&& i
< w
->num_kcontrols
; i
++) {
461 struct snd_kcontrol
*kcontrol
= w
->kcontrols
[i
];
462 struct soc_enum
*se
=
463 (struct soc_enum
*)kcontrol
->private_value
;
466 snd_ctl_remove(card
, kcontrol
);
468 kfree(se
->dobj
.control
.dvalues
);
469 for (j
= 0; j
< se
->items
; j
++)
470 kfree(se
->dobj
.control
.dtexts
[j
]);
473 kfree(w
->kcontrol_news
[i
].name
);
476 /* volume mixer or bytes controls */
477 for (i
= 0; w
->kcontrols
!= NULL
&& i
< w
->num_kcontrols
; i
++) {
478 struct snd_kcontrol
*kcontrol
= w
->kcontrols
[i
];
480 if (dobj
->widget
.kcontrol_type
481 == SND_SOC_TPLG_TYPE_MIXER
)
482 kfree(kcontrol
->tlv
.p
);
484 /* Private value is used as struct soc_mixer_control
485 * for volume mixers or soc_bytes_ext for bytes
488 kfree((void *)kcontrol
->private_value
);
489 snd_ctl_remove(card
, kcontrol
);
490 kfree(w
->kcontrol_news
[i
].name
);
495 kfree(w
->kcontrol_news
);
497 /* widget w is freed by soc-dapm.c */
500 /* remove DAI configurations */
501 static void remove_dai(struct snd_soc_component
*comp
,
502 struct snd_soc_dobj
*dobj
, int pass
)
504 struct snd_soc_dai_driver
*dai_drv
=
505 container_of(dobj
, struct snd_soc_dai_driver
, dobj
);
507 if (pass
!= SOC_TPLG_PASS_PCM_DAI
)
510 if (dobj
->ops
&& dobj
->ops
->dai_unload
)
511 dobj
->ops
->dai_unload(comp
, dobj
);
513 kfree(dai_drv
->name
);
514 list_del(&dobj
->list
);
518 /* remove link configurations */
519 static void remove_link(struct snd_soc_component
*comp
,
520 struct snd_soc_dobj
*dobj
, int pass
)
522 struct snd_soc_dai_link
*link
=
523 container_of(dobj
, struct snd_soc_dai_link
, dobj
);
525 if (pass
!= SOC_TPLG_PASS_PCM_DAI
)
528 if (dobj
->ops
&& dobj
->ops
->link_unload
)
529 dobj
->ops
->link_unload(comp
, dobj
);
532 kfree(link
->stream_name
);
533 kfree(link
->cpu_dai_name
);
535 list_del(&dobj
->list
);
536 snd_soc_remove_dai_link(comp
->card
, link
);
540 /* bind a kcontrol to it's IO handlers */
541 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr
*hdr
,
542 struct snd_kcontrol_new
*k
,
543 const struct soc_tplg
*tplg
)
545 const struct snd_soc_tplg_kcontrol_ops
*ops
;
546 const struct snd_soc_tplg_bytes_ext_ops
*ext_ops
;
549 if (hdr
->ops
.info
== SND_SOC_TPLG_CTL_BYTES
550 && k
->iface
& SNDRV_CTL_ELEM_IFACE_MIXER
551 && k
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
552 && k
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
553 struct soc_bytes_ext
*sbe
;
554 struct snd_soc_tplg_bytes_control
*be
;
556 sbe
= (struct soc_bytes_ext
*)k
->private_value
;
557 be
= container_of(hdr
, struct snd_soc_tplg_bytes_control
, hdr
);
559 /* TLV bytes controls need standard kcontrol info handler,
560 * TLV callback and extended put/get handlers.
562 k
->info
= snd_soc_bytes_info_ext
;
563 k
->tlv
.c
= snd_soc_bytes_tlv_callback
;
565 ext_ops
= tplg
->bytes_ext_ops
;
566 num_ops
= tplg
->bytes_ext_ops_count
;
567 for (i
= 0; i
< num_ops
; i
++) {
568 if (!sbe
->put
&& ext_ops
[i
].id
== be
->ext_ops
.put
)
569 sbe
->put
= ext_ops
[i
].put
;
570 if (!sbe
->get
&& ext_ops
[i
].id
== be
->ext_ops
.get
)
571 sbe
->get
= ext_ops
[i
].get
;
574 if (sbe
->put
&& sbe
->get
)
580 /* try and map vendor specific kcontrol handlers first */
582 num_ops
= tplg
->io_ops_count
;
583 for (i
= 0; i
< num_ops
; i
++) {
585 if (k
->put
== NULL
&& ops
[i
].id
== hdr
->ops
.put
)
587 if (k
->get
== NULL
&& ops
[i
].id
== hdr
->ops
.get
)
589 if (k
->info
== NULL
&& ops
[i
].id
== hdr
->ops
.info
)
590 k
->info
= ops
[i
].info
;
593 /* vendor specific handlers found ? */
594 if (k
->put
&& k
->get
&& k
->info
)
597 /* none found so try standard kcontrol handlers */
599 num_ops
= ARRAY_SIZE(io_ops
);
600 for (i
= 0; i
< num_ops
; i
++) {
602 if (k
->put
== NULL
&& ops
[i
].id
== hdr
->ops
.put
)
604 if (k
->get
== NULL
&& ops
[i
].id
== hdr
->ops
.get
)
606 if (k
->info
== NULL
&& ops
[i
].id
== hdr
->ops
.info
)
607 k
->info
= ops
[i
].info
;
610 /* standard handlers found ? */
611 if (k
->put
&& k
->get
&& k
->info
)
614 /* nothing to bind */
618 /* bind a widgets to it's evnt handlers */
619 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget
*w
,
620 const struct snd_soc_tplg_widget_events
*events
,
621 int num_events
, u16 event_type
)
627 for (i
= 0; i
< num_events
; i
++) {
628 if (event_type
== events
[i
].type
) {
630 /* found - so assign event */
631 w
->event
= events
[i
].event_handler
;
639 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event
);
641 /* optionally pass new dynamic kcontrol to component driver. */
642 static int soc_tplg_init_kcontrol(struct soc_tplg
*tplg
,
643 struct snd_kcontrol_new
*k
, struct snd_soc_tplg_ctl_hdr
*hdr
)
645 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->control_load
)
646 return tplg
->ops
->control_load(tplg
->comp
, k
, hdr
);
652 static int soc_tplg_create_tlv_db_scale(struct soc_tplg
*tplg
,
653 struct snd_kcontrol_new
*kc
, struct snd_soc_tplg_tlv_dbscale
*scale
)
655 unsigned int item_len
= 2 * sizeof(unsigned int);
658 p
= kzalloc(item_len
+ 2 * sizeof(unsigned int), GFP_KERNEL
);
662 p
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
665 p
[3] = (scale
->step
& TLV_DB_SCALE_MASK
)
666 | (scale
->mute
? TLV_DB_SCALE_MUTE
: 0);
668 kc
->tlv
.p
= (void *)p
;
672 static int soc_tplg_create_tlv(struct soc_tplg
*tplg
,
673 struct snd_kcontrol_new
*kc
, struct snd_soc_tplg_ctl_hdr
*tc
)
675 struct snd_soc_tplg_ctl_tlv
*tplg_tlv
;
677 if (!(tc
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
))
680 if (!(tc
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
)) {
682 switch (tplg_tlv
->type
) {
683 case SNDRV_CTL_TLVT_DB_SCALE
:
684 return soc_tplg_create_tlv_db_scale(tplg
, kc
,
687 /* TODO: add support for other TLV types */
689 dev_dbg(tplg
->dev
, "Unsupported TLV type %d\n",
698 static inline void soc_tplg_free_tlv(struct soc_tplg
*tplg
,
699 struct snd_kcontrol_new
*kc
)
704 static int soc_tplg_dbytes_create(struct soc_tplg
*tplg
, unsigned int count
,
707 struct snd_soc_tplg_bytes_control
*be
;
708 struct soc_bytes_ext
*sbe
;
709 struct snd_kcontrol_new kc
;
712 if (soc_tplg_check_elem_count(tplg
,
713 sizeof(struct snd_soc_tplg_bytes_control
), count
,
714 size
, "mixer bytes")) {
715 dev_err(tplg
->dev
, "ASoC: Invalid count %d for byte control\n",
720 for (i
= 0; i
< count
; i
++) {
721 be
= (struct snd_soc_tplg_bytes_control
*)tplg
->pos
;
723 /* validate kcontrol */
724 if (strnlen(be
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
725 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
728 sbe
= kzalloc(sizeof(*sbe
), GFP_KERNEL
);
732 tplg
->pos
+= (sizeof(struct snd_soc_tplg_bytes_control
) +
736 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
737 be
->hdr
.name
, be
->hdr
.access
);
739 memset(&kc
, 0, sizeof(kc
));
740 kc
.name
= be
->hdr
.name
;
741 kc
.private_value
= (long)sbe
;
742 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
743 kc
.access
= be
->hdr
.access
;
746 sbe
->dobj
.type
= SND_SOC_DOBJ_BYTES
;
747 sbe
->dobj
.ops
= tplg
->ops
;
748 INIT_LIST_HEAD(&sbe
->dobj
.list
);
750 /* map io handlers */
751 err
= soc_tplg_kcontrol_bind_io(&be
->hdr
, &kc
, tplg
);
753 soc_control_err(tplg
, &be
->hdr
, be
->hdr
.name
);
758 /* pass control to driver for optional further init */
759 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
760 (struct snd_soc_tplg_ctl_hdr
*)be
);
762 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
768 /* register control here */
769 err
= soc_tplg_add_kcontrol(tplg
, &kc
,
770 &sbe
->dobj
.control
.kcontrol
);
772 dev_err(tplg
->dev
, "ASoC: failed to add %s\n",
778 list_add(&sbe
->dobj
.list
, &tplg
->comp
->dobj_list
);
784 static int soc_tplg_dmixer_create(struct soc_tplg
*tplg
, unsigned int count
,
787 struct snd_soc_tplg_mixer_control
*mc
;
788 struct soc_mixer_control
*sm
;
789 struct snd_kcontrol_new kc
;
792 if (soc_tplg_check_elem_count(tplg
,
793 sizeof(struct snd_soc_tplg_mixer_control
),
794 count
, size
, "mixers")) {
796 dev_err(tplg
->dev
, "ASoC: invalid count %d for controls\n",
801 for (i
= 0; i
< count
; i
++) {
802 mc
= (struct snd_soc_tplg_mixer_control
*)tplg
->pos
;
804 /* validate kcontrol */
805 if (strnlen(mc
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
806 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
809 sm
= kzalloc(sizeof(*sm
), GFP_KERNEL
);
812 tplg
->pos
+= (sizeof(struct snd_soc_tplg_mixer_control
) +
816 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
817 mc
->hdr
.name
, mc
->hdr
.access
);
819 memset(&kc
, 0, sizeof(kc
));
820 kc
.name
= mc
->hdr
.name
;
821 kc
.private_value
= (long)sm
;
822 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
823 kc
.access
= mc
->hdr
.access
;
825 /* we only support FL/FR channel mapping atm */
826 sm
->reg
= tplc_chan_get_reg(tplg
, mc
->channel
,
828 sm
->rreg
= tplc_chan_get_reg(tplg
, mc
->channel
,
830 sm
->shift
= tplc_chan_get_shift(tplg
, mc
->channel
,
832 sm
->rshift
= tplc_chan_get_shift(tplg
, mc
->channel
,
837 sm
->invert
= mc
->invert
;
838 sm
->platform_max
= mc
->platform_max
;
839 sm
->dobj
.index
= tplg
->index
;
840 sm
->dobj
.ops
= tplg
->ops
;
841 sm
->dobj
.type
= SND_SOC_DOBJ_MIXER
;
842 INIT_LIST_HEAD(&sm
->dobj
.list
);
844 /* map io handlers */
845 err
= soc_tplg_kcontrol_bind_io(&mc
->hdr
, &kc
, tplg
);
847 soc_control_err(tplg
, &mc
->hdr
, mc
->hdr
.name
);
852 /* pass control to driver for optional further init */
853 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
854 (struct snd_soc_tplg_ctl_hdr
*) mc
);
856 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
862 /* create any TLV data */
863 soc_tplg_create_tlv(tplg
, &kc
, &mc
->hdr
);
865 /* register control here */
866 err
= soc_tplg_add_kcontrol(tplg
, &kc
,
867 &sm
->dobj
.control
.kcontrol
);
869 dev_err(tplg
->dev
, "ASoC: failed to add %s\n",
871 soc_tplg_free_tlv(tplg
, &kc
);
876 list_add(&sm
->dobj
.list
, &tplg
->comp
->dobj_list
);
882 static int soc_tplg_denum_create_texts(struct soc_enum
*se
,
883 struct snd_soc_tplg_enum_control
*ec
)
887 se
->dobj
.control
.dtexts
=
888 kcalloc(ec
->items
, sizeof(char *), GFP_KERNEL
);
889 if (se
->dobj
.control
.dtexts
== NULL
)
892 for (i
= 0; i
< ec
->items
; i
++) {
894 if (strnlen(ec
->texts
[i
], SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
895 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) {
900 se
->dobj
.control
.dtexts
[i
] = kstrdup(ec
->texts
[i
], GFP_KERNEL
);
901 if (!se
->dobj
.control
.dtexts
[i
]) {
907 se
->texts
= (const char * const *)se
->dobj
.control
.dtexts
;
911 for (--i
; i
>= 0; i
--)
912 kfree(se
->dobj
.control
.dtexts
[i
]);
913 kfree(se
->dobj
.control
.dtexts
);
917 static int soc_tplg_denum_create_values(struct soc_enum
*se
,
918 struct snd_soc_tplg_enum_control
*ec
)
920 if (ec
->items
> sizeof(*ec
->values
))
923 se
->dobj
.control
.dvalues
= kmemdup(ec
->values
,
924 ec
->items
* sizeof(u32
),
926 if (!se
->dobj
.control
.dvalues
)
932 static int soc_tplg_denum_create(struct soc_tplg
*tplg
, unsigned int count
,
935 struct snd_soc_tplg_enum_control
*ec
;
937 struct snd_kcontrol_new kc
;
940 if (soc_tplg_check_elem_count(tplg
,
941 sizeof(struct snd_soc_tplg_enum_control
),
942 count
, size
, "enums")) {
944 dev_err(tplg
->dev
, "ASoC: invalid count %d for enum controls\n",
949 for (i
= 0; i
< count
; i
++) {
950 ec
= (struct snd_soc_tplg_enum_control
*)tplg
->pos
;
951 tplg
->pos
+= (sizeof(struct snd_soc_tplg_enum_control
) +
954 /* validate kcontrol */
955 if (strnlen(ec
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
956 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
959 se
= kzalloc((sizeof(*se
)), GFP_KERNEL
);
963 dev_dbg(tplg
->dev
, "ASoC: adding enum kcontrol %s size %d\n",
964 ec
->hdr
.name
, ec
->items
);
966 memset(&kc
, 0, sizeof(kc
));
967 kc
.name
= ec
->hdr
.name
;
968 kc
.private_value
= (long)se
;
969 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
970 kc
.access
= ec
->hdr
.access
;
972 se
->reg
= tplc_chan_get_reg(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
973 se
->shift_l
= tplc_chan_get_shift(tplg
, ec
->channel
,
975 se
->shift_r
= tplc_chan_get_shift(tplg
, ec
->channel
,
978 se
->items
= ec
->items
;
980 se
->dobj
.index
= tplg
->index
;
981 se
->dobj
.type
= SND_SOC_DOBJ_ENUM
;
982 se
->dobj
.ops
= tplg
->ops
;
983 INIT_LIST_HEAD(&se
->dobj
.list
);
985 switch (ec
->hdr
.ops
.info
) {
986 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
987 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
988 err
= soc_tplg_denum_create_values(se
, ec
);
991 "ASoC: could not create values for %s\n",
996 /* fall through and create texts */
997 case SND_SOC_TPLG_CTL_ENUM
:
998 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
999 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1000 err
= soc_tplg_denum_create_texts(se
, ec
);
1003 "ASoC: could not create texts for %s\n",
1011 "ASoC: invalid enum control type %d for %s\n",
1012 ec
->hdr
.ops
.info
, ec
->hdr
.name
);
1017 /* map io handlers */
1018 err
= soc_tplg_kcontrol_bind_io(&ec
->hdr
, &kc
, tplg
);
1020 soc_control_err(tplg
, &ec
->hdr
, ec
->hdr
.name
);
1025 /* pass control to driver for optional further init */
1026 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
1027 (struct snd_soc_tplg_ctl_hdr
*) ec
);
1029 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1035 /* register control here */
1036 ret
= soc_tplg_add_kcontrol(tplg
,
1037 &kc
, &se
->dobj
.control
.kcontrol
);
1039 dev_err(tplg
->dev
, "ASoC: could not add kcontrol %s\n",
1045 list_add(&se
->dobj
.list
, &tplg
->comp
->dobj_list
);
1051 static int soc_tplg_kcontrol_elems_load(struct soc_tplg
*tplg
,
1052 struct snd_soc_tplg_hdr
*hdr
)
1054 struct snd_soc_tplg_ctl_hdr
*control_hdr
;
1057 if (tplg
->pass
!= SOC_TPLG_PASS_MIXER
) {
1058 tplg
->pos
+= hdr
->size
+ hdr
->payload_size
;
1062 dev_dbg(tplg
->dev
, "ASoC: adding %d kcontrols at 0x%lx\n", hdr
->count
,
1063 soc_tplg_get_offset(tplg
));
1065 for (i
= 0; i
< hdr
->count
; i
++) {
1067 control_hdr
= (struct snd_soc_tplg_ctl_hdr
*)tplg
->pos
;
1069 if (control_hdr
->size
!= sizeof(*control_hdr
)) {
1070 dev_err(tplg
->dev
, "ASoC: invalid control size\n");
1074 switch (control_hdr
->ops
.info
) {
1075 case SND_SOC_TPLG_CTL_VOLSW
:
1076 case SND_SOC_TPLG_CTL_STROBE
:
1077 case SND_SOC_TPLG_CTL_VOLSW_SX
:
1078 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
1079 case SND_SOC_TPLG_CTL_RANGE
:
1080 case SND_SOC_TPLG_DAPM_CTL_VOLSW
:
1081 case SND_SOC_TPLG_DAPM_CTL_PIN
:
1082 soc_tplg_dmixer_create(tplg
, 1, hdr
->payload_size
);
1084 case SND_SOC_TPLG_CTL_ENUM
:
1085 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1086 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1087 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1088 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1089 soc_tplg_denum_create(tplg
, 1, hdr
->payload_size
);
1091 case SND_SOC_TPLG_CTL_BYTES
:
1092 soc_tplg_dbytes_create(tplg
, 1, hdr
->payload_size
);
1095 soc_bind_err(tplg
, control_hdr
, i
);
1103 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg
*tplg
,
1104 struct snd_soc_tplg_hdr
*hdr
)
1106 struct snd_soc_dapm_context
*dapm
= &tplg
->comp
->dapm
;
1107 struct snd_soc_dapm_route route
;
1108 struct snd_soc_tplg_dapm_graph_elem
*elem
;
1109 int count
= hdr
->count
, i
;
1111 if (tplg
->pass
!= SOC_TPLG_PASS_GRAPH
) {
1112 tplg
->pos
+= hdr
->size
+ hdr
->payload_size
;
1116 if (soc_tplg_check_elem_count(tplg
,
1117 sizeof(struct snd_soc_tplg_dapm_graph_elem
),
1118 count
, hdr
->payload_size
, "graph")) {
1120 dev_err(tplg
->dev
, "ASoC: invalid count %d for DAPM routes\n",
1125 dev_dbg(tplg
->dev
, "ASoC: adding %d DAPM routes for index %d\n", count
,
1128 for (i
= 0; i
< count
; i
++) {
1129 elem
= (struct snd_soc_tplg_dapm_graph_elem
*)tplg
->pos
;
1130 tplg
->pos
+= sizeof(struct snd_soc_tplg_dapm_graph_elem
);
1132 /* validate routes */
1133 if (strnlen(elem
->source
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1134 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1136 if (strnlen(elem
->sink
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1137 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1139 if (strnlen(elem
->control
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1140 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1143 route
.source
= elem
->source
;
1144 route
.sink
= elem
->sink
;
1145 route
.connected
= NULL
; /* set to NULL atm for tplg users */
1146 if (strnlen(elem
->control
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) == 0)
1147 route
.control
= NULL
;
1149 route
.control
= elem
->control
;
1151 /* add route, but keep going if some fail */
1152 snd_soc_dapm_add_routes(dapm
, &route
, 1);
1158 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_dmixer_create(
1159 struct soc_tplg
*tplg
, int num_kcontrols
)
1161 struct snd_kcontrol_new
*kc
;
1162 struct soc_mixer_control
*sm
;
1163 struct snd_soc_tplg_mixer_control
*mc
;
1166 kc
= kcalloc(num_kcontrols
, sizeof(*kc
), GFP_KERNEL
);
1170 for (i
= 0; i
< num_kcontrols
; i
++) {
1171 mc
= (struct snd_soc_tplg_mixer_control
*)tplg
->pos
;
1172 sm
= kzalloc(sizeof(*sm
), GFP_KERNEL
);
1176 tplg
->pos
+= (sizeof(struct snd_soc_tplg_mixer_control
) +
1179 /* validate kcontrol */
1180 if (strnlen(mc
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1181 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1184 dev_dbg(tplg
->dev
, " adding DAPM widget mixer control %s at %d\n",
1187 kc
[i
].name
= kstrdup(mc
->hdr
.name
, GFP_KERNEL
);
1188 if (kc
[i
].name
== NULL
)
1190 kc
[i
].private_value
= (long)sm
;
1191 kc
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1192 kc
[i
].access
= mc
->hdr
.access
;
1194 /* we only support FL/FR channel mapping atm */
1195 sm
->reg
= tplc_chan_get_reg(tplg
, mc
->channel
,
1197 sm
->rreg
= tplc_chan_get_reg(tplg
, mc
->channel
,
1199 sm
->shift
= tplc_chan_get_shift(tplg
, mc
->channel
,
1201 sm
->rshift
= tplc_chan_get_shift(tplg
, mc
->channel
,
1206 sm
->invert
= mc
->invert
;
1207 sm
->platform_max
= mc
->platform_max
;
1208 sm
->dobj
.index
= tplg
->index
;
1209 INIT_LIST_HEAD(&sm
->dobj
.list
);
1211 /* map io handlers */
1212 err
= soc_tplg_kcontrol_bind_io(&mc
->hdr
, &kc
[i
], tplg
);
1214 soc_control_err(tplg
, &mc
->hdr
, mc
->hdr
.name
);
1219 /* pass control to driver for optional further init */
1220 err
= soc_tplg_init_kcontrol(tplg
, &kc
[i
],
1221 (struct snd_soc_tplg_ctl_hdr
*)mc
);
1223 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1229 /* create any TLV data */
1230 soc_tplg_create_tlv(tplg
, &kc
[i
], &mc
->hdr
);
1237 for (--i
; i
>= 0; i
--) {
1238 kfree((void *)kc
[i
].private_value
);
1245 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_denum_create(
1246 struct soc_tplg
*tplg
, int num_kcontrols
)
1248 struct snd_kcontrol_new
*kc
;
1249 struct snd_soc_tplg_enum_control
*ec
;
1250 struct soc_enum
*se
;
1253 kc
= kcalloc(num_kcontrols
, sizeof(*kc
), GFP_KERNEL
);
1257 for (i
= 0; i
< num_kcontrols
; i
++) {
1258 ec
= (struct snd_soc_tplg_enum_control
*)tplg
->pos
;
1259 /* validate kcontrol */
1260 if (strnlen(ec
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1261 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1264 se
= kzalloc(sizeof(*se
), GFP_KERNEL
);
1268 dev_dbg(tplg
->dev
, " adding DAPM widget enum control %s\n",
1271 kc
[i
].name
= kstrdup(ec
->hdr
.name
, GFP_KERNEL
);
1272 if (kc
[i
].name
== NULL
) {
1276 kc
[i
].private_value
= (long)se
;
1277 kc
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1278 kc
[i
].access
= ec
->hdr
.access
;
1280 /* we only support FL/FR channel mapping atm */
1281 se
->reg
= tplc_chan_get_reg(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
1282 se
->shift_l
= tplc_chan_get_shift(tplg
, ec
->channel
,
1284 se
->shift_r
= tplc_chan_get_shift(tplg
, ec
->channel
,
1287 se
->items
= ec
->items
;
1288 se
->mask
= ec
->mask
;
1289 se
->dobj
.index
= tplg
->index
;
1291 switch (ec
->hdr
.ops
.info
) {
1292 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1293 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1294 err
= soc_tplg_denum_create_values(se
, ec
);
1296 dev_err(tplg
->dev
, "ASoC: could not create values for %s\n",
1300 /* fall through to create texts */
1301 case SND_SOC_TPLG_CTL_ENUM
:
1302 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1303 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1304 err
= soc_tplg_denum_create_texts(se
, ec
);
1306 dev_err(tplg
->dev
, "ASoC: could not create texts for %s\n",
1312 dev_err(tplg
->dev
, "ASoC: invalid enum control type %d for %s\n",
1313 ec
->hdr
.ops
.info
, ec
->hdr
.name
);
1317 /* map io handlers */
1318 err
= soc_tplg_kcontrol_bind_io(&ec
->hdr
, &kc
[i
], tplg
);
1320 soc_control_err(tplg
, &ec
->hdr
, ec
->hdr
.name
);
1324 /* pass control to driver for optional further init */
1325 err
= soc_tplg_init_kcontrol(tplg
, &kc
[i
],
1326 (struct snd_soc_tplg_ctl_hdr
*)ec
);
1328 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1333 tplg
->pos
+= (sizeof(struct snd_soc_tplg_enum_control
) +
1340 for (; i
>= 0; i
--) {
1341 /* free values and texts */
1342 se
= (struct soc_enum
*)kc
[i
].private_value
;
1346 kfree(se
->dobj
.control
.dvalues
);
1347 for (j
= 0; j
< ec
->items
; j
++)
1348 kfree(se
->dobj
.control
.dtexts
[j
]);
1359 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_dbytes_create(
1360 struct soc_tplg
*tplg
, int count
)
1362 struct snd_soc_tplg_bytes_control
*be
;
1363 struct soc_bytes_ext
*sbe
;
1364 struct snd_kcontrol_new
*kc
;
1367 kc
= kcalloc(count
, sizeof(*kc
), GFP_KERNEL
);
1371 for (i
= 0; i
< count
; i
++) {
1372 be
= (struct snd_soc_tplg_bytes_control
*)tplg
->pos
;
1374 /* validate kcontrol */
1375 if (strnlen(be
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1376 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1379 sbe
= kzalloc(sizeof(*sbe
), GFP_KERNEL
);
1383 tplg
->pos
+= (sizeof(struct snd_soc_tplg_bytes_control
) +
1387 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1388 be
->hdr
.name
, be
->hdr
.access
);
1390 kc
[i
].name
= kstrdup(be
->hdr
.name
, GFP_KERNEL
);
1391 if (kc
[i
].name
== NULL
) {
1395 kc
[i
].private_value
= (long)sbe
;
1396 kc
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1397 kc
[i
].access
= be
->hdr
.access
;
1400 INIT_LIST_HEAD(&sbe
->dobj
.list
);
1402 /* map standard io handlers and check for external handlers */
1403 err
= soc_tplg_kcontrol_bind_io(&be
->hdr
, &kc
[i
], tplg
);
1405 soc_control_err(tplg
, &be
->hdr
, be
->hdr
.name
);
1410 /* pass control to driver for optional further init */
1411 err
= soc_tplg_init_kcontrol(tplg
, &kc
[i
],
1412 (struct snd_soc_tplg_ctl_hdr
*)be
);
1414 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1424 for (--i
; i
>= 0; i
--) {
1425 kfree((void *)kc
[i
].private_value
);
1433 static int soc_tplg_dapm_widget_create(struct soc_tplg
*tplg
,
1434 struct snd_soc_tplg_dapm_widget
*w
)
1436 struct snd_soc_dapm_context
*dapm
= &tplg
->comp
->dapm
;
1437 struct snd_soc_dapm_widget
template, *widget
;
1438 struct snd_soc_tplg_ctl_hdr
*control_hdr
;
1439 struct snd_soc_card
*card
= tplg
->comp
->card
;
1440 unsigned int kcontrol_type
;
1443 if (strnlen(w
->name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1444 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1446 if (strnlen(w
->sname
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1447 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1450 dev_dbg(tplg
->dev
, "ASoC: creating DAPM widget %s id %d\n",
1453 memset(&template, 0, sizeof(template));
1455 /* map user to kernel widget ID */
1456 template.id
= get_widget_id(w
->id
);
1457 if (template.id
< 0)
1460 /* strings are allocated here, but used and freed by the widget */
1461 template.name
= kstrdup(w
->name
, GFP_KERNEL
);
1464 template.sname
= kstrdup(w
->sname
, GFP_KERNEL
);
1465 if (!template.sname
) {
1469 template.reg
= w
->reg
;
1470 template.shift
= w
->shift
;
1471 template.mask
= w
->mask
;
1472 template.subseq
= w
->subseq
;
1473 template.on_val
= w
->invert
? 0 : 1;
1474 template.off_val
= w
->invert
? 1 : 0;
1475 template.ignore_suspend
= w
->ignore_suspend
;
1476 template.event_flags
= w
->event_flags
;
1477 template.dobj
.index
= tplg
->index
;
1480 (sizeof(struct snd_soc_tplg_dapm_widget
) + w
->priv
.size
);
1481 if (w
->num_kcontrols
== 0) {
1483 template.num_kcontrols
= 0;
1487 control_hdr
= (struct snd_soc_tplg_ctl_hdr
*)tplg
->pos
;
1488 dev_dbg(tplg
->dev
, "ASoC: template %s has %d controls of type %x\n",
1489 w
->name
, w
->num_kcontrols
, control_hdr
->type
);
1491 switch (control_hdr
->ops
.info
) {
1492 case SND_SOC_TPLG_CTL_VOLSW
:
1493 case SND_SOC_TPLG_CTL_STROBE
:
1494 case SND_SOC_TPLG_CTL_VOLSW_SX
:
1495 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
1496 case SND_SOC_TPLG_CTL_RANGE
:
1497 case SND_SOC_TPLG_DAPM_CTL_VOLSW
:
1498 kcontrol_type
= SND_SOC_TPLG_TYPE_MIXER
; /* volume mixer */
1499 template.num_kcontrols
= w
->num_kcontrols
;
1500 template.kcontrol_news
=
1501 soc_tplg_dapm_widget_dmixer_create(tplg
,
1502 template.num_kcontrols
);
1503 if (!template.kcontrol_news
) {
1508 case SND_SOC_TPLG_CTL_ENUM
:
1509 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1510 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1511 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1512 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1513 kcontrol_type
= SND_SOC_TPLG_TYPE_ENUM
; /* enumerated mixer */
1514 template.num_kcontrols
= w
->num_kcontrols
;
1515 template.kcontrol_news
=
1516 soc_tplg_dapm_widget_denum_create(tplg
,
1517 template.num_kcontrols
);
1518 if (!template.kcontrol_news
) {
1523 case SND_SOC_TPLG_CTL_BYTES
:
1524 kcontrol_type
= SND_SOC_TPLG_TYPE_BYTES
; /* bytes control */
1525 template.num_kcontrols
= w
->num_kcontrols
;
1526 template.kcontrol_news
=
1527 soc_tplg_dapm_widget_dbytes_create(tplg
,
1528 template.num_kcontrols
);
1529 if (!template.kcontrol_news
) {
1535 dev_err(tplg
->dev
, "ASoC: invalid widget control type %d:%d:%d\n",
1536 control_hdr
->ops
.get
, control_hdr
->ops
.put
,
1537 control_hdr
->ops
.info
);
1543 ret
= soc_tplg_widget_load(tplg
, &template, w
);
1547 /* card dapm mutex is held by the core if we are loading topology
1548 * data during sound card init. */
1549 if (card
->instantiated
)
1550 widget
= snd_soc_dapm_new_control(dapm
, &template);
1552 widget
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
1553 if (IS_ERR(widget
)) {
1554 ret
= PTR_ERR(widget
);
1555 /* Do not nag about probe deferrals */
1556 if (ret
!= -EPROBE_DEFER
)
1558 "ASoC: failed to create widget %s controls (%d)\n",
1562 if (widget
== NULL
) {
1563 dev_err(tplg
->dev
, "ASoC: failed to create widget %s controls\n",
1569 widget
->dobj
.type
= SND_SOC_DOBJ_WIDGET
;
1570 widget
->dobj
.widget
.kcontrol_type
= kcontrol_type
;
1571 widget
->dobj
.ops
= tplg
->ops
;
1572 widget
->dobj
.index
= tplg
->index
;
1573 list_add(&widget
->dobj
.list
, &tplg
->comp
->dobj_list
);
1575 ret
= soc_tplg_widget_ready(tplg
, widget
, w
);
1582 snd_soc_tplg_widget_remove(widget
);
1583 snd_soc_dapm_free_widget(widget
);
1585 kfree(template.sname
);
1587 kfree(template.name
);
1591 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg
*tplg
,
1592 struct snd_soc_tplg_hdr
*hdr
)
1594 struct snd_soc_tplg_dapm_widget
*widget
;
1595 int ret
, count
= hdr
->count
, i
;
1597 if (tplg
->pass
!= SOC_TPLG_PASS_WIDGET
)
1600 dev_dbg(tplg
->dev
, "ASoC: adding %d DAPM widgets\n", count
);
1602 for (i
= 0; i
< count
; i
++) {
1603 widget
= (struct snd_soc_tplg_dapm_widget
*) tplg
->pos
;
1604 if (widget
->size
!= sizeof(*widget
)) {
1605 dev_err(tplg
->dev
, "ASoC: invalid widget size\n");
1609 ret
= soc_tplg_dapm_widget_create(tplg
, widget
);
1611 dev_err(tplg
->dev
, "ASoC: failed to load widget %s\n",
1620 static int soc_tplg_dapm_complete(struct soc_tplg
*tplg
)
1622 struct snd_soc_card
*card
= tplg
->comp
->card
;
1625 /* Card might not have been registered at this point.
1626 * If so, just return success.
1628 if (!card
|| !card
->instantiated
) {
1629 dev_warn(tplg
->dev
, "ASoC: Parent card not yet available,"
1630 " widget card binding deferred\n");
1634 ret
= snd_soc_dapm_new_widgets(card
);
1636 dev_err(tplg
->dev
, "ASoC: failed to create new widgets %d\n",
1642 static void set_stream_info(struct snd_soc_pcm_stream
*stream
,
1643 struct snd_soc_tplg_stream_caps
*caps
)
1645 stream
->stream_name
= kstrdup(caps
->name
, GFP_KERNEL
);
1646 stream
->channels_min
= caps
->channels_min
;
1647 stream
->channels_max
= caps
->channels_max
;
1648 stream
->rates
= caps
->rates
;
1649 stream
->rate_min
= caps
->rate_min
;
1650 stream
->rate_max
= caps
->rate_max
;
1651 stream
->formats
= caps
->formats
;
1652 stream
->sig_bits
= caps
->sig_bits
;
1655 static void set_dai_flags(struct snd_soc_dai_driver
*dai_drv
,
1656 unsigned int flag_mask
, unsigned int flags
)
1658 if (flag_mask
& SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES
)
1659 dai_drv
->symmetric_rates
=
1660 flags
& SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES
? 1 : 0;
1662 if (flag_mask
& SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS
)
1663 dai_drv
->symmetric_channels
=
1664 flags
& SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS
?
1667 if (flag_mask
& SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS
)
1668 dai_drv
->symmetric_samplebits
=
1669 flags
& SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS
?
1673 static int soc_tplg_dai_create(struct soc_tplg
*tplg
,
1674 struct snd_soc_tplg_pcm
*pcm
)
1676 struct snd_soc_dai_driver
*dai_drv
;
1677 struct snd_soc_pcm_stream
*stream
;
1678 struct snd_soc_tplg_stream_caps
*caps
;
1681 dai_drv
= kzalloc(sizeof(struct snd_soc_dai_driver
), GFP_KERNEL
);
1682 if (dai_drv
== NULL
)
1685 if (strlen(pcm
->dai_name
))
1686 dai_drv
->name
= kstrdup(pcm
->dai_name
, GFP_KERNEL
);
1687 dai_drv
->id
= pcm
->dai_id
;
1689 if (pcm
->playback
) {
1690 stream
= &dai_drv
->playback
;
1691 caps
= &pcm
->caps
[SND_SOC_TPLG_STREAM_PLAYBACK
];
1692 set_stream_info(stream
, caps
);
1696 stream
= &dai_drv
->capture
;
1697 caps
= &pcm
->caps
[SND_SOC_TPLG_STREAM_CAPTURE
];
1698 set_stream_info(stream
, caps
);
1702 dai_drv
->compress_new
= snd_soc_new_compress
;
1704 /* pass control to component driver for optional further init */
1705 ret
= soc_tplg_dai_load(tplg
, dai_drv
);
1707 dev_err(tplg
->comp
->dev
, "ASoC: DAI loading failed\n");
1712 dai_drv
->dobj
.index
= tplg
->index
;
1713 dai_drv
->dobj
.ops
= tplg
->ops
;
1714 dai_drv
->dobj
.type
= SND_SOC_DOBJ_PCM
;
1715 list_add(&dai_drv
->dobj
.list
, &tplg
->comp
->dobj_list
);
1717 /* register the DAI to the component */
1718 return snd_soc_register_dai(tplg
->comp
, dai_drv
);
1721 static void set_link_flags(struct snd_soc_dai_link
*link
,
1722 unsigned int flag_mask
, unsigned int flags
)
1724 if (flag_mask
& SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES
)
1725 link
->symmetric_rates
=
1726 flags
& SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES
? 1 : 0;
1728 if (flag_mask
& SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS
)
1729 link
->symmetric_channels
=
1730 flags
& SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS
?
1733 if (flag_mask
& SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS
)
1734 link
->symmetric_samplebits
=
1735 flags
& SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS
?
1738 if (flag_mask
& SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP
)
1739 link
->ignore_suspend
=
1740 flags
& SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP
?
1744 /* create the FE DAI link */
1745 static int soc_tplg_fe_link_create(struct soc_tplg
*tplg
,
1746 struct snd_soc_tplg_pcm
*pcm
)
1748 struct snd_soc_dai_link
*link
;
1751 link
= kzalloc(sizeof(struct snd_soc_dai_link
), GFP_KERNEL
);
1755 if (strlen(pcm
->pcm_name
)) {
1756 link
->name
= kstrdup(pcm
->pcm_name
, GFP_KERNEL
);
1757 link
->stream_name
= kstrdup(pcm
->pcm_name
, GFP_KERNEL
);
1759 link
->id
= pcm
->pcm_id
;
1761 if (strlen(pcm
->dai_name
))
1762 link
->cpu_dai_name
= kstrdup(pcm
->dai_name
, GFP_KERNEL
);
1764 link
->codec_name
= "snd-soc-dummy";
1765 link
->codec_dai_name
= "snd-soc-dummy-dai";
1769 link
->dpcm_playback
= pcm
->playback
;
1770 link
->dpcm_capture
= pcm
->capture
;
1772 set_link_flags(link
, pcm
->flag_mask
, pcm
->flags
);
1774 /* pass control to component driver for optional further init */
1775 ret
= soc_tplg_dai_link_load(tplg
, link
);
1777 dev_err(tplg
->comp
->dev
, "ASoC: FE link loading failed\n");
1782 link
->dobj
.index
= tplg
->index
;
1783 link
->dobj
.ops
= tplg
->ops
;
1784 link
->dobj
.type
= SND_SOC_DOBJ_DAI_LINK
;
1785 list_add(&link
->dobj
.list
, &tplg
->comp
->dobj_list
);
1787 snd_soc_add_dai_link(tplg
->comp
->card
, link
);
1791 /* create a FE DAI and DAI link from the PCM object */
1792 static int soc_tplg_pcm_create(struct soc_tplg
*tplg
,
1793 struct snd_soc_tplg_pcm
*pcm
)
1797 ret
= soc_tplg_dai_create(tplg
, pcm
);
1801 return soc_tplg_fe_link_create(tplg
, pcm
);
1804 /* copy stream caps from the old version 4 of source */
1805 static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps
*dest
,
1806 struct snd_soc_tplg_stream_caps_v4
*src
)
1808 dest
->size
= sizeof(*dest
);
1809 memcpy(dest
->name
, src
->name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
);
1810 dest
->formats
= src
->formats
;
1811 dest
->rates
= src
->rates
;
1812 dest
->rate_min
= src
->rate_min
;
1813 dest
->rate_max
= src
->rate_max
;
1814 dest
->channels_min
= src
->channels_min
;
1815 dest
->channels_max
= src
->channels_max
;
1816 dest
->periods_min
= src
->periods_min
;
1817 dest
->periods_max
= src
->periods_max
;
1818 dest
->period_size_min
= src
->period_size_min
;
1819 dest
->period_size_max
= src
->period_size_max
;
1820 dest
->buffer_size_min
= src
->buffer_size_min
;
1821 dest
->buffer_size_max
= src
->buffer_size_max
;
1825 * pcm_new_ver - Create the new version of PCM from the old version.
1826 * @tplg: topology context
1827 * @src: older version of pcm as a source
1828 * @pcm: latest version of pcm created from the source
1830 * Support from vesion 4. User should free the returned pcm manually.
1832 static int pcm_new_ver(struct soc_tplg
*tplg
,
1833 struct snd_soc_tplg_pcm
*src
,
1834 struct snd_soc_tplg_pcm
**pcm
)
1836 struct snd_soc_tplg_pcm
*dest
;
1837 struct snd_soc_tplg_pcm_v4
*src_v4
;
1842 if (src
->size
!= sizeof(*src_v4
)) {
1843 dev_err(tplg
->dev
, "ASoC: invalid PCM size\n");
1847 dev_warn(tplg
->dev
, "ASoC: old version of PCM\n");
1848 src_v4
= (struct snd_soc_tplg_pcm_v4
*)src
;
1849 dest
= kzalloc(sizeof(*dest
), GFP_KERNEL
);
1853 dest
->size
= sizeof(*dest
); /* size of latest abi version */
1854 memcpy(dest
->pcm_name
, src_v4
->pcm_name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
);
1855 memcpy(dest
->dai_name
, src_v4
->dai_name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
);
1856 dest
->pcm_id
= src_v4
->pcm_id
;
1857 dest
->dai_id
= src_v4
->dai_id
;
1858 dest
->playback
= src_v4
->playback
;
1859 dest
->capture
= src_v4
->capture
;
1860 dest
->compress
= src_v4
->compress
;
1861 dest
->num_streams
= src_v4
->num_streams
;
1862 for (i
= 0; i
< dest
->num_streams
; i
++)
1863 memcpy(&dest
->stream
[i
], &src_v4
->stream
[i
],
1864 sizeof(struct snd_soc_tplg_stream
));
1866 for (i
= 0; i
< 2; i
++)
1867 stream_caps_new_ver(&dest
->caps
[i
], &src_v4
->caps
[i
]);
1873 static int soc_tplg_pcm_elems_load(struct soc_tplg
*tplg
,
1874 struct snd_soc_tplg_hdr
*hdr
)
1876 struct snd_soc_tplg_pcm
*pcm
, *_pcm
;
1877 int count
= hdr
->count
;
1881 if (tplg
->pass
!= SOC_TPLG_PASS_PCM_DAI
)
1884 /* check the element size and count */
1885 pcm
= (struct snd_soc_tplg_pcm
*)tplg
->pos
;
1886 if (pcm
->size
> sizeof(struct snd_soc_tplg_pcm
)
1887 || pcm
->size
< sizeof(struct snd_soc_tplg_pcm_v4
)) {
1888 dev_err(tplg
->dev
, "ASoC: invalid size %d for PCM elems\n",
1893 if (soc_tplg_check_elem_count(tplg
,
1895 hdr
->payload_size
, "PCM DAI")) {
1896 dev_err(tplg
->dev
, "ASoC: invalid count %d for PCM DAI elems\n",
1901 for (i
= 0; i
< count
; i
++) {
1902 pcm
= (struct snd_soc_tplg_pcm
*)tplg
->pos
;
1904 /* check ABI version by size, create a new version of pcm
1907 if (pcm
->size
== sizeof(*pcm
)) {
1912 pcm_new_ver(tplg
, pcm
, &_pcm
);
1915 /* create the FE DAIs and DAI links */
1916 soc_tplg_pcm_create(tplg
, _pcm
);
1918 /* offset by version-specific struct size and
1919 * real priv data size
1921 tplg
->pos
+= pcm
->size
+ _pcm
->priv
.size
;
1924 kfree(_pcm
); /* free the duplicated one */
1927 dev_dbg(tplg
->dev
, "ASoC: adding %d PCM DAIs\n", count
);
1933 * set_link_hw_format - Set the HW audio format of the physical DAI link.
1934 * @link: &snd_soc_dai_link which should be updated
1935 * @cfg: physical link configs.
1937 * Topology context contains a list of supported HW formats (configs) and
1938 * a default format ID for the physical link. This function will use this
1939 * default ID to choose the HW format to set the link's DAI format for init.
1941 static void set_link_hw_format(struct snd_soc_dai_link
*link
,
1942 struct snd_soc_tplg_link_config
*cfg
)
1944 struct snd_soc_tplg_hw_config
*hw_config
;
1945 unsigned char bclk_master
, fsync_master
;
1946 unsigned char invert_bclk
, invert_fsync
;
1949 for (i
= 0; i
< cfg
->num_hw_configs
; i
++) {
1950 hw_config
= &cfg
->hw_config
[i
];
1951 if (hw_config
->id
!= cfg
->default_hw_config_id
)
1954 link
->dai_fmt
= hw_config
->fmt
& SND_SOC_DAIFMT_FORMAT_MASK
;
1957 switch (hw_config
->clock_gated
) {
1958 case SND_SOC_TPLG_DAI_CLK_GATE_GATED
:
1959 link
->dai_fmt
|= SND_SOC_DAIFMT_GATED
;
1962 case SND_SOC_TPLG_DAI_CLK_GATE_CONT
:
1963 link
->dai_fmt
|= SND_SOC_DAIFMT_CONT
;
1967 /* ignore the value */
1971 /* clock signal polarity */
1972 invert_bclk
= hw_config
->invert_bclk
;
1973 invert_fsync
= hw_config
->invert_fsync
;
1974 if (!invert_bclk
&& !invert_fsync
)
1975 link
->dai_fmt
|= SND_SOC_DAIFMT_NB_NF
;
1976 else if (!invert_bclk
&& invert_fsync
)
1977 link
->dai_fmt
|= SND_SOC_DAIFMT_NB_IF
;
1978 else if (invert_bclk
&& !invert_fsync
)
1979 link
->dai_fmt
|= SND_SOC_DAIFMT_IB_NF
;
1981 link
->dai_fmt
|= SND_SOC_DAIFMT_IB_IF
;
1984 bclk_master
= (hw_config
->bclk_master
==
1985 SND_SOC_TPLG_BCLK_CM
);
1986 fsync_master
= (hw_config
->fsync_master
==
1987 SND_SOC_TPLG_FSYNC_CM
);
1988 if (bclk_master
&& fsync_master
)
1989 link
->dai_fmt
|= SND_SOC_DAIFMT_CBM_CFM
;
1990 else if (!bclk_master
&& fsync_master
)
1991 link
->dai_fmt
|= SND_SOC_DAIFMT_CBS_CFM
;
1992 else if (bclk_master
&& !fsync_master
)
1993 link
->dai_fmt
|= SND_SOC_DAIFMT_CBM_CFS
;
1995 link
->dai_fmt
|= SND_SOC_DAIFMT_CBS_CFS
;
2000 * link_new_ver - Create a new physical link config from the old
2001 * version of source.
2002 * @tplg: topology context
2003 * @src: old version of phyical link config as a source
2004 * @link: latest version of physical link config created from the source
2006 * Support from vesion 4. User need free the returned link config manually.
2008 static int link_new_ver(struct soc_tplg
*tplg
,
2009 struct snd_soc_tplg_link_config
*src
,
2010 struct snd_soc_tplg_link_config
**link
)
2012 struct snd_soc_tplg_link_config
*dest
;
2013 struct snd_soc_tplg_link_config_v4
*src_v4
;
2018 if (src
->size
!= sizeof(struct snd_soc_tplg_link_config_v4
)) {
2019 dev_err(tplg
->dev
, "ASoC: invalid physical link config size\n");
2023 dev_warn(tplg
->dev
, "ASoC: old version of physical link config\n");
2025 src_v4
= (struct snd_soc_tplg_link_config_v4
*)src
;
2026 dest
= kzalloc(sizeof(*dest
), GFP_KERNEL
);
2030 dest
->size
= sizeof(*dest
);
2031 dest
->id
= src_v4
->id
;
2032 dest
->num_streams
= src_v4
->num_streams
;
2033 for (i
= 0; i
< dest
->num_streams
; i
++)
2034 memcpy(&dest
->stream
[i
], &src_v4
->stream
[i
],
2035 sizeof(struct snd_soc_tplg_stream
));
2041 /* Find and configure an existing physical DAI link */
2042 static int soc_tplg_link_config(struct soc_tplg
*tplg
,
2043 struct snd_soc_tplg_link_config
*cfg
)
2045 struct snd_soc_dai_link
*link
;
2046 const char *name
, *stream_name
;
2050 len
= strnlen(cfg
->name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
);
2051 if (len
== SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
2058 len
= strnlen(cfg
->stream_name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
);
2059 if (len
== SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
2062 stream_name
= cfg
->stream_name
;
2066 link
= snd_soc_find_dai_link(tplg
->comp
->card
, cfg
->id
,
2069 dev_err(tplg
->dev
, "ASoC: physical link %s (id %d) not exist\n",
2075 if (cfg
->num_hw_configs
)
2076 set_link_hw_format(link
, cfg
);
2080 set_link_flags(link
, cfg
->flag_mask
, cfg
->flags
);
2082 /* pass control to component driver for optional further init */
2083 ret
= soc_tplg_dai_link_load(tplg
, link
);
2085 dev_err(tplg
->dev
, "ASoC: physical link loading failed\n");
2093 /* Load physical link config elements from the topology context */
2094 static int soc_tplg_link_elems_load(struct soc_tplg
*tplg
,
2095 struct snd_soc_tplg_hdr
*hdr
)
2097 struct snd_soc_tplg_link_config
*link
, *_link
;
2098 int count
= hdr
->count
;
2102 if (tplg
->pass
!= SOC_TPLG_PASS_LINK
) {
2103 tplg
->pos
+= hdr
->size
+ hdr
->payload_size
;
2107 /* check the element size and count */
2108 link
= (struct snd_soc_tplg_link_config
*)tplg
->pos
;
2109 if (link
->size
> sizeof(struct snd_soc_tplg_link_config
)
2110 || link
->size
< sizeof(struct snd_soc_tplg_link_config_v4
)) {
2111 dev_err(tplg
->dev
, "ASoC: invalid size %d for physical link elems\n",
2116 if (soc_tplg_check_elem_count(tplg
,
2118 hdr
->payload_size
, "physical link config")) {
2119 dev_err(tplg
->dev
, "ASoC: invalid count %d for physical link elems\n",
2124 /* config physical DAI links */
2125 for (i
= 0; i
< count
; i
++) {
2126 link
= (struct snd_soc_tplg_link_config
*)tplg
->pos
;
2127 if (link
->size
== sizeof(*link
)) {
2132 ret
= link_new_ver(tplg
, link
, &_link
);
2137 ret
= soc_tplg_link_config(tplg
, _link
);
2141 /* offset by version-specific struct size and
2142 * real priv data size
2144 tplg
->pos
+= link
->size
+ _link
->priv
.size
;
2147 kfree(_link
); /* free the duplicated one */
2154 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2155 * @tplg: topology context
2156 * @d: physical DAI configs.
2158 * The physical dai should already be registered by the platform driver.
2159 * The platform driver should specify the DAI name and ID for matching.
2161 static int soc_tplg_dai_config(struct soc_tplg
*tplg
,
2162 struct snd_soc_tplg_dai
*d
)
2164 struct snd_soc_dai_link_component dai_component
= {0};
2165 struct snd_soc_dai
*dai
;
2166 struct snd_soc_dai_driver
*dai_drv
;
2167 struct snd_soc_pcm_stream
*stream
;
2168 struct snd_soc_tplg_stream_caps
*caps
;
2171 dai_component
.dai_name
= d
->dai_name
;
2172 dai
= snd_soc_find_dai(&dai_component
);
2174 dev_err(tplg
->dev
, "ASoC: physical DAI %s not registered\n",
2179 if (d
->dai_id
!= dai
->id
) {
2180 dev_err(tplg
->dev
, "ASoC: physical DAI %s id mismatch\n",
2185 dai_drv
= dai
->driver
;
2190 stream
= &dai_drv
->playback
;
2191 caps
= &d
->caps
[SND_SOC_TPLG_STREAM_PLAYBACK
];
2192 set_stream_info(stream
, caps
);
2196 stream
= &dai_drv
->capture
;
2197 caps
= &d
->caps
[SND_SOC_TPLG_STREAM_CAPTURE
];
2198 set_stream_info(stream
, caps
);
2202 set_dai_flags(dai_drv
, d
->flag_mask
, d
->flags
);
2204 /* pass control to component driver for optional further init */
2205 ret
= soc_tplg_dai_load(tplg
, dai_drv
);
2207 dev_err(tplg
->comp
->dev
, "ASoC: DAI loading failed\n");
2214 /* load physical DAI elements */
2215 static int soc_tplg_dai_elems_load(struct soc_tplg
*tplg
,
2216 struct snd_soc_tplg_hdr
*hdr
)
2218 struct snd_soc_tplg_dai
*dai
;
2219 int count
= hdr
->count
;
2222 if (tplg
->pass
!= SOC_TPLG_PASS_BE_DAI
)
2225 /* config the existing BE DAIs */
2226 for (i
= 0; i
< count
; i
++) {
2227 dai
= (struct snd_soc_tplg_dai
*)tplg
->pos
;
2228 if (dai
->size
!= sizeof(*dai
)) {
2229 dev_err(tplg
->dev
, "ASoC: invalid physical DAI size\n");
2233 soc_tplg_dai_config(tplg
, dai
);
2234 tplg
->pos
+= (sizeof(*dai
) + dai
->priv
.size
);
2237 dev_dbg(tplg
->dev
, "ASoC: Configure %d BE DAIs\n", count
);
2242 * manifest_new_ver - Create a new version of manifest from the old version
2244 * @tplg: topology context
2245 * @src: old version of manifest as a source
2246 * @manifest: latest version of manifest created from the source
2248 * Support from vesion 4. Users need free the returned manifest manually.
2250 static int manifest_new_ver(struct soc_tplg
*tplg
,
2251 struct snd_soc_tplg_manifest
*src
,
2252 struct snd_soc_tplg_manifest
**manifest
)
2254 struct snd_soc_tplg_manifest
*dest
;
2255 struct snd_soc_tplg_manifest_v4
*src_v4
;
2259 if (src
->size
!= sizeof(*src_v4
)) {
2260 dev_warn(tplg
->dev
, "ASoC: invalid manifest size %d\n",
2264 src
->size
= sizeof(*src_v4
);
2267 dev_warn(tplg
->dev
, "ASoC: old version of manifest\n");
2269 src_v4
= (struct snd_soc_tplg_manifest_v4
*)src
;
2270 dest
= kzalloc(sizeof(*dest
) + src_v4
->priv
.size
, GFP_KERNEL
);
2274 dest
->size
= sizeof(*dest
); /* size of latest abi version */
2275 dest
->control_elems
= src_v4
->control_elems
;
2276 dest
->widget_elems
= src_v4
->widget_elems
;
2277 dest
->graph_elems
= src_v4
->graph_elems
;
2278 dest
->pcm_elems
= src_v4
->pcm_elems
;
2279 dest
->dai_link_elems
= src_v4
->dai_link_elems
;
2280 dest
->priv
.size
= src_v4
->priv
.size
;
2281 if (dest
->priv
.size
)
2282 memcpy(dest
->priv
.data
, src_v4
->priv
.data
,
2289 static int soc_tplg_manifest_load(struct soc_tplg
*tplg
,
2290 struct snd_soc_tplg_hdr
*hdr
)
2292 struct snd_soc_tplg_manifest
*manifest
, *_manifest
;
2296 if (tplg
->pass
!= SOC_TPLG_PASS_MANIFEST
)
2299 manifest
= (struct snd_soc_tplg_manifest
*)tplg
->pos
;
2301 /* check ABI version by size, create a new manifest if abi not match */
2302 if (manifest
->size
== sizeof(*manifest
)) {
2304 _manifest
= manifest
;
2307 err
= manifest_new_ver(tplg
, manifest
, &_manifest
);
2312 /* pass control to component driver for optional further init */
2313 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->manifest
)
2314 return tplg
->ops
->manifest(tplg
->comp
, _manifest
);
2316 if (!abi_match
) /* free the duplicated one */
2322 /* validate header magic, size and type */
2323 static int soc_valid_header(struct soc_tplg
*tplg
,
2324 struct snd_soc_tplg_hdr
*hdr
)
2326 if (soc_tplg_get_hdr_offset(tplg
) >= tplg
->fw
->size
)
2329 if (hdr
->size
!= sizeof(*hdr
)) {
2331 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2332 hdr
->type
, soc_tplg_get_hdr_offset(tplg
),
2337 /* big endian firmware objects not supported atm */
2338 if (hdr
->magic
== cpu_to_be32(SND_SOC_TPLG_MAGIC
)) {
2340 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2341 tplg
->pass
, hdr
->magic
,
2342 soc_tplg_get_hdr_offset(tplg
), tplg
->fw
->size
);
2346 if (hdr
->magic
!= SND_SOC_TPLG_MAGIC
) {
2348 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2349 tplg
->pass
, hdr
->magic
,
2350 soc_tplg_get_hdr_offset(tplg
), tplg
->fw
->size
);
2354 /* Support ABI from version 4 */
2355 if (hdr
->abi
> SND_SOC_TPLG_ABI_VERSION
2356 || hdr
->abi
< SND_SOC_TPLG_ABI_VERSION_MIN
) {
2358 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2359 tplg
->pass
, hdr
->abi
,
2360 SND_SOC_TPLG_ABI_VERSION
, soc_tplg_get_hdr_offset(tplg
),
2365 if (hdr
->payload_size
== 0) {
2366 dev_err(tplg
->dev
, "ASoC: header has 0 size at offset 0x%lx.\n",
2367 soc_tplg_get_hdr_offset(tplg
));
2371 if (tplg
->pass
== hdr
->type
)
2373 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2374 hdr
->payload_size
, hdr
->type
, hdr
->version
,
2375 hdr
->vendor_type
, tplg
->pass
);
2380 /* check header type and call appropriate handler */
2381 static int soc_tplg_load_header(struct soc_tplg
*tplg
,
2382 struct snd_soc_tplg_hdr
*hdr
)
2384 tplg
->pos
= tplg
->hdr_pos
+ sizeof(struct snd_soc_tplg_hdr
);
2386 /* check for matching ID */
2387 if (hdr
->index
!= tplg
->req_index
&&
2388 tplg
->req_index
!= SND_SOC_TPLG_INDEX_ALL
)
2391 tplg
->index
= hdr
->index
;
2393 switch (hdr
->type
) {
2394 case SND_SOC_TPLG_TYPE_MIXER
:
2395 case SND_SOC_TPLG_TYPE_ENUM
:
2396 case SND_SOC_TPLG_TYPE_BYTES
:
2397 return soc_tplg_kcontrol_elems_load(tplg
, hdr
);
2398 case SND_SOC_TPLG_TYPE_DAPM_GRAPH
:
2399 return soc_tplg_dapm_graph_elems_load(tplg
, hdr
);
2400 case SND_SOC_TPLG_TYPE_DAPM_WIDGET
:
2401 return soc_tplg_dapm_widget_elems_load(tplg
, hdr
);
2402 case SND_SOC_TPLG_TYPE_PCM
:
2403 return soc_tplg_pcm_elems_load(tplg
, hdr
);
2404 case SND_SOC_TPLG_TYPE_DAI
:
2405 return soc_tplg_dai_elems_load(tplg
, hdr
);
2406 case SND_SOC_TPLG_TYPE_DAI_LINK
:
2407 case SND_SOC_TPLG_TYPE_BACKEND_LINK
:
2408 /* physical link configurations */
2409 return soc_tplg_link_elems_load(tplg
, hdr
);
2410 case SND_SOC_TPLG_TYPE_MANIFEST
:
2411 return soc_tplg_manifest_load(tplg
, hdr
);
2413 /* bespoke vendor data object */
2414 return soc_tplg_vendor_load(tplg
, hdr
);
2420 /* process the topology file headers */
2421 static int soc_tplg_process_headers(struct soc_tplg
*tplg
)
2423 struct snd_soc_tplg_hdr
*hdr
;
2426 tplg
->pass
= SOC_TPLG_PASS_START
;
2428 /* process the header types from start to end */
2429 while (tplg
->pass
<= SOC_TPLG_PASS_END
) {
2431 tplg
->hdr_pos
= tplg
->fw
->data
;
2432 hdr
= (struct snd_soc_tplg_hdr
*)tplg
->hdr_pos
;
2434 while (!soc_tplg_is_eof(tplg
)) {
2436 /* make sure header is valid before loading */
2437 ret
= soc_valid_header(tplg
, hdr
);
2443 /* load the header object */
2444 ret
= soc_tplg_load_header(tplg
, hdr
);
2448 /* goto next header */
2449 tplg
->hdr_pos
+= hdr
->payload_size
+
2450 sizeof(struct snd_soc_tplg_hdr
);
2451 hdr
= (struct snd_soc_tplg_hdr
*)tplg
->hdr_pos
;
2454 /* next data type pass */
2458 /* signal DAPM we are complete */
2459 ret
= soc_tplg_dapm_complete(tplg
);
2462 "ASoC: failed to initialise DAPM from Firmware\n");
2467 static int soc_tplg_load(struct soc_tplg
*tplg
)
2471 ret
= soc_tplg_process_headers(tplg
);
2473 soc_tplg_complete(tplg
);
2478 /* load audio component topology from "firmware" file */
2479 int snd_soc_tplg_component_load(struct snd_soc_component
*comp
,
2480 struct snd_soc_tplg_ops
*ops
, const struct firmware
*fw
, u32 id
)
2482 struct soc_tplg tplg
;
2484 /* setup parsing context */
2485 memset(&tplg
, 0, sizeof(tplg
));
2487 tplg
.dev
= comp
->dev
;
2490 tplg
.req_index
= id
;
2491 tplg
.io_ops
= ops
->io_ops
;
2492 tplg
.io_ops_count
= ops
->io_ops_count
;
2493 tplg
.bytes_ext_ops
= ops
->bytes_ext_ops
;
2494 tplg
.bytes_ext_ops_count
= ops
->bytes_ext_ops_count
;
2496 return soc_tplg_load(&tplg
);
2498 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load
);
2500 /* remove this dynamic widget */
2501 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget
*w
)
2503 /* make sure we are a widget */
2504 if (w
->dobj
.type
!= SND_SOC_DOBJ_WIDGET
)
2507 remove_widget(w
->dapm
->component
, &w
->dobj
, SOC_TPLG_PASS_WIDGET
);
2509 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove
);
2511 /* remove all dynamic widgets from this DAPM context */
2512 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context
*dapm
,
2515 struct snd_soc_dapm_widget
*w
, *next_w
;
2517 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
2519 /* make sure we are a widget with correct context */
2520 if (w
->dobj
.type
!= SND_SOC_DOBJ_WIDGET
|| w
->dapm
!= dapm
)
2524 if (w
->dobj
.index
!= index
&&
2525 w
->dobj
.index
!= SND_SOC_TPLG_INDEX_ALL
)
2527 /* check and free and dynamic widget kcontrols */
2528 snd_soc_tplg_widget_remove(w
);
2529 snd_soc_dapm_free_widget(w
);
2531 snd_soc_dapm_reset_cache(dapm
);
2533 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all
);
2535 /* remove dynamic controls from the component driver */
2536 int snd_soc_tplg_component_remove(struct snd_soc_component
*comp
, u32 index
)
2538 struct snd_soc_dobj
*dobj
, *next_dobj
;
2539 int pass
= SOC_TPLG_PASS_END
;
2541 /* process the header types from end to start */
2542 while (pass
>= SOC_TPLG_PASS_START
) {
2544 /* remove mixer controls */
2545 list_for_each_entry_safe(dobj
, next_dobj
, &comp
->dobj_list
,
2549 if (dobj
->index
!= index
&&
2550 index
!= SND_SOC_TPLG_INDEX_ALL
)
2553 switch (dobj
->type
) {
2554 case SND_SOC_DOBJ_MIXER
:
2555 remove_mixer(comp
, dobj
, pass
);
2557 case SND_SOC_DOBJ_ENUM
:
2558 remove_enum(comp
, dobj
, pass
);
2560 case SND_SOC_DOBJ_BYTES
:
2561 remove_bytes(comp
, dobj
, pass
);
2563 case SND_SOC_DOBJ_WIDGET
:
2564 remove_widget(comp
, dobj
, pass
);
2566 case SND_SOC_DOBJ_PCM
:
2567 remove_dai(comp
, dobj
, pass
);
2569 case SND_SOC_DOBJ_DAI_LINK
:
2570 remove_link(comp
, dobj
, pass
);
2573 dev_err(comp
->dev
, "ASoC: invalid component type %d for removal\n",
2581 /* let caller know if FW can be freed when no objects are left */
2582 return !list_empty(&comp
->dobj_list
);
2584 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove
);