2 * soc-topology.c -- ALSA SoC Topology
4 * Copyright (C) 2012 Texas Instruments Inc.
5 * Copyright (C) 2015 Intel Corporation.
7 * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
8 * K, Mythri P <mythri.p.k@intel.com>
9 * Prusty, Subhransu S <subhransu.s.prusty@intel.com>
10 * B, Jayachandran <jayachandran.b@intel.com>
11 * Abdullah, Omair M <omair.m.abdullah@intel.com>
12 * Jin, Yao <yao.jin@intel.com>
13 * Lin, Mengdong <mengdong.lin@intel.com>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * Add support to read audio firmware topology alongside firmware text. The
21 * topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
22 * equalizers, firmware, coefficients etc.
24 * This file only manages the core ALSA and ASoC components, all other bespoke
25 * firmware topology data is passed to component drivers for bespoke handling.
28 #include <linux/kernel.h>
29 #include <linux/export.h>
30 #include <linux/list.h>
31 #include <linux/firmware.h>
32 #include <linux/slab.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/soc-topology.h>
36 #include <sound/tlv.h>
39 * We make several passes over the data (since it wont necessarily be ordered)
40 * and process objects in the following order. This guarantees the component
41 * drivers will be ready with any vendor data before the mixers and DAPM objects
42 * are loaded (that may make use of the vendor data).
44 #define SOC_TPLG_PASS_MANIFEST 0
45 #define SOC_TPLG_PASS_VENDOR 1
46 #define SOC_TPLG_PASS_MIXER 2
47 #define SOC_TPLG_PASS_WIDGET 3
48 #define SOC_TPLG_PASS_PCM_DAI 4
49 #define SOC_TPLG_PASS_GRAPH 5
50 #define SOC_TPLG_PASS_PINS 6
52 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
53 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_PINS
56 const struct firmware
*fw
;
58 /* runtime FW parsing */
59 const u8
*pos
; /* read postion */
60 const u8
*hdr_pos
; /* header position */
61 unsigned int pass
; /* pass number */
63 /* component caller */
65 struct snd_soc_component
*comp
;
66 u32 index
; /* current block index */
67 u32 req_index
; /* required index, only loaded/free matching blocks */
69 /* vendor specific kcontrol operations */
70 const struct snd_soc_tplg_kcontrol_ops
*io_ops
;
73 /* vendor specific bytes ext handlers, for TLV bytes controls */
74 const struct snd_soc_tplg_bytes_ext_ops
*bytes_ext_ops
;
75 int bytes_ext_ops_count
;
77 /* optional fw loading callbacks to component drivers */
78 struct snd_soc_tplg_ops
*ops
;
81 static int soc_tplg_process_headers(struct soc_tplg
*tplg
);
82 static void soc_tplg_complete(struct soc_tplg
*tplg
);
83 struct snd_soc_dapm_widget
*
84 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context
*dapm
,
85 const struct snd_soc_dapm_widget
*widget
);
86 struct snd_soc_dapm_widget
*
87 snd_soc_dapm_new_control(struct snd_soc_dapm_context
*dapm
,
88 const struct snd_soc_dapm_widget
*widget
);
90 /* check we dont overflow the data for this control chunk */
91 static int soc_tplg_check_elem_count(struct soc_tplg
*tplg
, size_t elem_size
,
92 unsigned int count
, size_t bytes
, const char *elem_type
)
94 const u8
*end
= tplg
->pos
+ elem_size
* count
;
96 if (end
> tplg
->fw
->data
+ tplg
->fw
->size
) {
97 dev_err(tplg
->dev
, "ASoC: %s overflow end of data\n",
102 /* check there is enough room in chunk for control.
103 extra bytes at the end of control are for vendor data here */
104 if (elem_size
* count
> bytes
) {
106 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
107 elem_type
, count
, elem_size
, bytes
);
114 static inline int soc_tplg_is_eof(struct soc_tplg
*tplg
)
116 const u8
*end
= tplg
->hdr_pos
;
118 if (end
>= tplg
->fw
->data
+ tplg
->fw
->size
)
123 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg
*tplg
)
125 return (unsigned long)(tplg
->hdr_pos
- tplg
->fw
->data
);
128 static inline unsigned long soc_tplg_get_offset(struct soc_tplg
*tplg
)
130 return (unsigned long)(tplg
->pos
- tplg
->fw
->data
);
133 /* mapping of Kcontrol types and associated operations. */
134 static const struct snd_soc_tplg_kcontrol_ops io_ops
[] = {
135 {SND_SOC_TPLG_CTL_VOLSW
, snd_soc_get_volsw
,
136 snd_soc_put_volsw
, snd_soc_info_volsw
},
137 {SND_SOC_TPLG_CTL_VOLSW_SX
, snd_soc_get_volsw_sx
,
138 snd_soc_put_volsw_sx
, NULL
},
139 {SND_SOC_TPLG_CTL_ENUM
, snd_soc_get_enum_double
,
140 snd_soc_put_enum_double
, snd_soc_info_enum_double
},
141 {SND_SOC_TPLG_CTL_ENUM_VALUE
, snd_soc_get_enum_double
,
142 snd_soc_put_enum_double
, NULL
},
143 {SND_SOC_TPLG_CTL_BYTES
, snd_soc_bytes_get
,
144 snd_soc_bytes_put
, snd_soc_bytes_info
},
145 {SND_SOC_TPLG_CTL_RANGE
, snd_soc_get_volsw_range
,
146 snd_soc_put_volsw_range
, snd_soc_info_volsw_range
},
147 {SND_SOC_TPLG_CTL_VOLSW_XR_SX
, snd_soc_get_xr_sx
,
148 snd_soc_put_xr_sx
, snd_soc_info_xr_sx
},
149 {SND_SOC_TPLG_CTL_STROBE
, snd_soc_get_strobe
,
150 snd_soc_put_strobe
, NULL
},
151 {SND_SOC_TPLG_DAPM_CTL_VOLSW
, snd_soc_dapm_get_volsw
,
152 snd_soc_dapm_put_volsw
, snd_soc_info_volsw
},
153 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
, snd_soc_dapm_get_enum_double
,
154 snd_soc_dapm_put_enum_double
, snd_soc_info_enum_double
},
155 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
, snd_soc_dapm_get_enum_double
,
156 snd_soc_dapm_put_enum_double
, NULL
},
157 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
, snd_soc_dapm_get_enum_double
,
158 snd_soc_dapm_put_enum_double
, NULL
},
159 {SND_SOC_TPLG_DAPM_CTL_PIN
, snd_soc_dapm_get_pin_switch
,
160 snd_soc_dapm_put_pin_switch
, snd_soc_dapm_info_pin_switch
},
163 struct soc_tplg_map
{
168 /* mapping of widget types from UAPI IDs to kernel IDs */
169 static const struct soc_tplg_map dapm_map
[] = {
170 {SND_SOC_TPLG_DAPM_INPUT
, snd_soc_dapm_input
},
171 {SND_SOC_TPLG_DAPM_OUTPUT
, snd_soc_dapm_output
},
172 {SND_SOC_TPLG_DAPM_MUX
, snd_soc_dapm_mux
},
173 {SND_SOC_TPLG_DAPM_MIXER
, snd_soc_dapm_mixer
},
174 {SND_SOC_TPLG_DAPM_PGA
, snd_soc_dapm_pga
},
175 {SND_SOC_TPLG_DAPM_OUT_DRV
, snd_soc_dapm_out_drv
},
176 {SND_SOC_TPLG_DAPM_ADC
, snd_soc_dapm_adc
},
177 {SND_SOC_TPLG_DAPM_DAC
, snd_soc_dapm_dac
},
178 {SND_SOC_TPLG_DAPM_SWITCH
, snd_soc_dapm_switch
},
179 {SND_SOC_TPLG_DAPM_PRE
, snd_soc_dapm_pre
},
180 {SND_SOC_TPLG_DAPM_POST
, snd_soc_dapm_post
},
181 {SND_SOC_TPLG_DAPM_AIF_IN
, snd_soc_dapm_aif_in
},
182 {SND_SOC_TPLG_DAPM_AIF_OUT
, snd_soc_dapm_aif_out
},
183 {SND_SOC_TPLG_DAPM_DAI_IN
, snd_soc_dapm_dai_in
},
184 {SND_SOC_TPLG_DAPM_DAI_OUT
, snd_soc_dapm_dai_out
},
185 {SND_SOC_TPLG_DAPM_DAI_LINK
, snd_soc_dapm_dai_link
},
188 static int tplc_chan_get_reg(struct soc_tplg
*tplg
,
189 struct snd_soc_tplg_channel
*chan
, int map
)
193 for (i
= 0; i
< SND_SOC_TPLG_MAX_CHAN
; i
++) {
194 if (chan
[i
].id
== map
)
201 static int tplc_chan_get_shift(struct soc_tplg
*tplg
,
202 struct snd_soc_tplg_channel
*chan
, int map
)
206 for (i
= 0; i
< SND_SOC_TPLG_MAX_CHAN
; i
++) {
207 if (chan
[i
].id
== map
)
208 return chan
[i
].shift
;
214 static int get_widget_id(int tplg_type
)
218 for (i
= 0; i
< ARRAY_SIZE(dapm_map
); i
++) {
219 if (tplg_type
== dapm_map
[i
].uid
)
220 return dapm_map
[i
].kid
;
226 static inline void soc_bind_err(struct soc_tplg
*tplg
,
227 struct snd_soc_tplg_ctl_hdr
*hdr
, int index
)
230 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
231 hdr
->ops
.get
, hdr
->ops
.put
, hdr
->ops
.info
, index
,
232 soc_tplg_get_offset(tplg
));
235 static inline void soc_control_err(struct soc_tplg
*tplg
,
236 struct snd_soc_tplg_ctl_hdr
*hdr
, const char *name
)
239 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
240 name
, hdr
->ops
.get
, hdr
->ops
.put
, hdr
->ops
.info
,
241 soc_tplg_get_offset(tplg
));
244 /* pass vendor data to component driver for processing */
245 static int soc_tplg_vendor_load_(struct soc_tplg
*tplg
,
246 struct snd_soc_tplg_hdr
*hdr
)
250 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->vendor_load
)
251 ret
= tplg
->ops
->vendor_load(tplg
->comp
, hdr
);
253 dev_err(tplg
->dev
, "ASoC: no vendor load callback for ID %d\n",
260 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
261 soc_tplg_get_hdr_offset(tplg
),
262 soc_tplg_get_hdr_offset(tplg
),
263 hdr
->type
, hdr
->vendor_type
);
267 /* pass vendor data to component driver for processing */
268 static int soc_tplg_vendor_load(struct soc_tplg
*tplg
,
269 struct snd_soc_tplg_hdr
*hdr
)
271 if (tplg
->pass
!= SOC_TPLG_PASS_VENDOR
)
274 return soc_tplg_vendor_load_(tplg
, hdr
);
277 /* optionally pass new dynamic widget to component driver. This is mainly for
278 * external widgets where we can assign private data/ops */
279 static int soc_tplg_widget_load(struct soc_tplg
*tplg
,
280 struct snd_soc_dapm_widget
*w
, struct snd_soc_tplg_dapm_widget
*tplg_w
)
282 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->widget_load
)
283 return tplg
->ops
->widget_load(tplg
->comp
, w
, tplg_w
);
288 /* pass DAI configurations to component driver for extra intialization */
289 static int soc_tplg_dai_load(struct soc_tplg
*tplg
,
290 struct snd_soc_dai_driver
*dai_drv
)
292 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->dai_load
)
293 return tplg
->ops
->dai_load(tplg
->comp
, dai_drv
);
298 /* pass link configurations to component driver for extra intialization */
299 static int soc_tplg_dai_link_load(struct soc_tplg
*tplg
,
300 struct snd_soc_dai_link
*link
)
302 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->link_load
)
303 return tplg
->ops
->link_load(tplg
->comp
, link
);
308 /* tell the component driver that all firmware has been loaded in this request */
309 static void soc_tplg_complete(struct soc_tplg
*tplg
)
311 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->complete
)
312 tplg
->ops
->complete(tplg
->comp
);
315 /* add a dynamic kcontrol */
316 static int soc_tplg_add_dcontrol(struct snd_card
*card
, struct device
*dev
,
317 const struct snd_kcontrol_new
*control_new
, const char *prefix
,
318 void *data
, struct snd_kcontrol
**kcontrol
)
322 *kcontrol
= snd_soc_cnew(control_new
, data
, control_new
->name
, prefix
);
323 if (*kcontrol
== NULL
) {
324 dev_err(dev
, "ASoC: Failed to create new kcontrol %s\n",
329 err
= snd_ctl_add(card
, *kcontrol
);
331 dev_err(dev
, "ASoC: Failed to add %s: %d\n",
332 control_new
->name
, err
);
339 /* add a dynamic kcontrol for component driver */
340 static int soc_tplg_add_kcontrol(struct soc_tplg
*tplg
,
341 struct snd_kcontrol_new
*k
, struct snd_kcontrol
**kcontrol
)
343 struct snd_soc_component
*comp
= tplg
->comp
;
345 return soc_tplg_add_dcontrol(comp
->card
->snd_card
,
346 comp
->dev
, k
, NULL
, comp
, kcontrol
);
349 /* remove a mixer kcontrol */
350 static void remove_mixer(struct snd_soc_component
*comp
,
351 struct snd_soc_dobj
*dobj
, int pass
)
353 struct snd_card
*card
= comp
->card
->snd_card
;
354 struct soc_mixer_control
*sm
=
355 container_of(dobj
, struct soc_mixer_control
, dobj
);
356 const unsigned int *p
= NULL
;
358 if (pass
!= SOC_TPLG_PASS_MIXER
)
361 if (dobj
->ops
&& dobj
->ops
->control_unload
)
362 dobj
->ops
->control_unload(comp
, dobj
);
364 if (sm
->dobj
.control
.kcontrol
->tlv
.p
)
365 p
= sm
->dobj
.control
.kcontrol
->tlv
.p
;
366 snd_ctl_remove(card
, sm
->dobj
.control
.kcontrol
);
367 list_del(&sm
->dobj
.list
);
372 /* remove an enum kcontrol */
373 static void remove_enum(struct snd_soc_component
*comp
,
374 struct snd_soc_dobj
*dobj
, int pass
)
376 struct snd_card
*card
= comp
->card
->snd_card
;
377 struct soc_enum
*se
= container_of(dobj
, struct soc_enum
, dobj
);
380 if (pass
!= SOC_TPLG_PASS_MIXER
)
383 if (dobj
->ops
&& dobj
->ops
->control_unload
)
384 dobj
->ops
->control_unload(comp
, dobj
);
386 snd_ctl_remove(card
, se
->dobj
.control
.kcontrol
);
387 list_del(&se
->dobj
.list
);
389 kfree(se
->dobj
.control
.dvalues
);
390 for (i
= 0; i
< se
->items
; i
++)
391 kfree(se
->dobj
.control
.dtexts
[i
]);
395 /* remove a byte kcontrol */
396 static void remove_bytes(struct snd_soc_component
*comp
,
397 struct snd_soc_dobj
*dobj
, int pass
)
399 struct snd_card
*card
= comp
->card
->snd_card
;
400 struct soc_bytes_ext
*sb
=
401 container_of(dobj
, struct soc_bytes_ext
, dobj
);
403 if (pass
!= SOC_TPLG_PASS_MIXER
)
406 if (dobj
->ops
&& dobj
->ops
->control_unload
)
407 dobj
->ops
->control_unload(comp
, dobj
);
409 snd_ctl_remove(card
, sb
->dobj
.control
.kcontrol
);
410 list_del(&sb
->dobj
.list
);
414 /* remove a widget and it's kcontrols - routes must be removed first */
415 static void remove_widget(struct snd_soc_component
*comp
,
416 struct snd_soc_dobj
*dobj
, int pass
)
418 struct snd_card
*card
= comp
->card
->snd_card
;
419 struct snd_soc_dapm_widget
*w
=
420 container_of(dobj
, struct snd_soc_dapm_widget
, dobj
);
423 if (pass
!= SOC_TPLG_PASS_WIDGET
)
426 if (dobj
->ops
&& dobj
->ops
->widget_unload
)
427 dobj
->ops
->widget_unload(comp
, dobj
);
430 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
431 * The enum may either have an array of values or strings.
433 if (dobj
->widget
.kcontrol_enum
) {
434 /* enumerated widget mixer */
435 struct soc_enum
*se
=
436 (struct soc_enum
*)w
->kcontrols
[0]->private_value
;
438 snd_ctl_remove(card
, w
->kcontrols
[0]);
440 kfree(se
->dobj
.control
.dvalues
);
441 for (i
= 0; i
< se
->items
; i
++)
442 kfree(se
->dobj
.control
.dtexts
[i
]);
445 kfree(w
->kcontrol_news
);
447 /* non enumerated widget mixer */
448 for (i
= 0; i
< w
->num_kcontrols
; i
++) {
449 struct snd_kcontrol
*kcontrol
= w
->kcontrols
[i
];
450 struct soc_mixer_control
*sm
=
451 (struct soc_mixer_control
*) kcontrol
->private_value
;
453 kfree(w
->kcontrols
[i
]->tlv
.p
);
455 snd_ctl_remove(card
, w
->kcontrols
[i
]);
458 kfree(w
->kcontrol_news
);
460 /* widget w is freed by soc-dapm.c */
463 /* remove DAI configurations */
464 static void remove_dai(struct snd_soc_component
*comp
,
465 struct snd_soc_dobj
*dobj
, int pass
)
467 struct snd_soc_dai_driver
*dai_drv
=
468 container_of(dobj
, struct snd_soc_dai_driver
, dobj
);
470 if (pass
!= SOC_TPLG_PASS_PCM_DAI
)
473 if (dobj
->ops
&& dobj
->ops
->dai_unload
)
474 dobj
->ops
->dai_unload(comp
, dobj
);
476 list_del(&dobj
->list
);
480 /* remove link configurations */
481 static void remove_link(struct snd_soc_component
*comp
,
482 struct snd_soc_dobj
*dobj
, int pass
)
484 struct snd_soc_dai_link
*link
=
485 container_of(dobj
, struct snd_soc_dai_link
, dobj
);
487 if (pass
!= SOC_TPLG_PASS_PCM_DAI
)
490 if (dobj
->ops
&& dobj
->ops
->link_unload
)
491 dobj
->ops
->link_unload(comp
, dobj
);
493 list_del(&dobj
->list
);
494 snd_soc_remove_dai_link(comp
->card
, link
);
498 /* bind a kcontrol to it's IO handlers */
499 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr
*hdr
,
500 struct snd_kcontrol_new
*k
,
501 const struct soc_tplg
*tplg
)
503 const struct snd_soc_tplg_kcontrol_ops
*ops
;
504 const struct snd_soc_tplg_bytes_ext_ops
*ext_ops
;
507 if (hdr
->ops
.info
== SND_SOC_TPLG_CTL_BYTES
508 && k
->iface
& SNDRV_CTL_ELEM_IFACE_MIXER
509 && k
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
510 && k
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
511 struct soc_bytes_ext
*sbe
;
512 struct snd_soc_tplg_bytes_control
*be
;
514 sbe
= (struct soc_bytes_ext
*)k
->private_value
;
515 be
= container_of(hdr
, struct snd_soc_tplg_bytes_control
, hdr
);
517 /* TLV bytes controls need standard kcontrol info handler,
518 * TLV callback and extended put/get handlers.
520 k
->info
= snd_soc_bytes_info_ext
;
521 k
->tlv
.c
= snd_soc_bytes_tlv_callback
;
523 ext_ops
= tplg
->bytes_ext_ops
;
524 num_ops
= tplg
->bytes_ext_ops_count
;
525 for (i
= 0; i
< num_ops
; i
++) {
526 if (!sbe
->put
&& ext_ops
[i
].id
== be
->ext_ops
.put
)
527 sbe
->put
= ext_ops
[i
].put
;
528 if (!sbe
->get
&& ext_ops
[i
].id
== be
->ext_ops
.get
)
529 sbe
->get
= ext_ops
[i
].get
;
532 if (sbe
->put
&& sbe
->get
)
538 /* try and map vendor specific kcontrol handlers first */
540 num_ops
= tplg
->io_ops_count
;
541 for (i
= 0; i
< num_ops
; i
++) {
543 if (k
->put
== NULL
&& ops
[i
].id
== hdr
->ops
.put
)
545 if (k
->get
== NULL
&& ops
[i
].id
== hdr
->ops
.get
)
547 if (k
->info
== NULL
&& ops
[i
].id
== hdr
->ops
.info
)
548 k
->info
= ops
[i
].info
;
551 /* vendor specific handlers found ? */
552 if (k
->put
&& k
->get
&& k
->info
)
555 /* none found so try standard kcontrol handlers */
557 num_ops
= ARRAY_SIZE(io_ops
);
558 for (i
= 0; i
< num_ops
; i
++) {
560 if (k
->put
== NULL
&& ops
[i
].id
== hdr
->ops
.put
)
562 if (k
->get
== NULL
&& ops
[i
].id
== hdr
->ops
.get
)
564 if (k
->info
== NULL
&& ops
[i
].id
== hdr
->ops
.info
)
565 k
->info
= ops
[i
].info
;
568 /* standard handlers found ? */
569 if (k
->put
&& k
->get
&& k
->info
)
572 /* nothing to bind */
576 /* bind a widgets to it's evnt handlers */
577 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget
*w
,
578 const struct snd_soc_tplg_widget_events
*events
,
579 int num_events
, u16 event_type
)
585 for (i
= 0; i
< num_events
; i
++) {
586 if (event_type
== events
[i
].type
) {
588 /* found - so assign event */
589 w
->event
= events
[i
].event_handler
;
597 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event
);
599 /* optionally pass new dynamic kcontrol to component driver. */
600 static int soc_tplg_init_kcontrol(struct soc_tplg
*tplg
,
601 struct snd_kcontrol_new
*k
, struct snd_soc_tplg_ctl_hdr
*hdr
)
603 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->control_load
)
604 return tplg
->ops
->control_load(tplg
->comp
, k
, hdr
);
610 static int soc_tplg_create_tlv_db_scale(struct soc_tplg
*tplg
,
611 struct snd_kcontrol_new
*kc
, struct snd_soc_tplg_tlv_dbscale
*scale
)
613 unsigned int item_len
= 2 * sizeof(unsigned int);
616 p
= kzalloc(item_len
+ 2 * sizeof(unsigned int), GFP_KERNEL
);
620 p
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
623 p
[3] = (scale
->step
& TLV_DB_SCALE_MASK
)
624 | (scale
->mute
? TLV_DB_SCALE_MUTE
: 0);
626 kc
->tlv
.p
= (void *)p
;
630 static int soc_tplg_create_tlv(struct soc_tplg
*tplg
,
631 struct snd_kcontrol_new
*kc
, struct snd_soc_tplg_ctl_hdr
*tc
)
633 struct snd_soc_tplg_ctl_tlv
*tplg_tlv
;
635 if (!(tc
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
))
638 if (!(tc
->access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
)) {
640 switch (tplg_tlv
->type
) {
641 case SNDRV_CTL_TLVT_DB_SCALE
:
642 return soc_tplg_create_tlv_db_scale(tplg
, kc
,
645 /* TODO: add support for other TLV types */
647 dev_dbg(tplg
->dev
, "Unsupported TLV type %d\n",
656 static inline void soc_tplg_free_tlv(struct soc_tplg
*tplg
,
657 struct snd_kcontrol_new
*kc
)
662 static int soc_tplg_dbytes_create(struct soc_tplg
*tplg
, unsigned int count
,
665 struct snd_soc_tplg_bytes_control
*be
;
666 struct soc_bytes_ext
*sbe
;
667 struct snd_kcontrol_new kc
;
670 if (soc_tplg_check_elem_count(tplg
,
671 sizeof(struct snd_soc_tplg_bytes_control
), count
,
672 size
, "mixer bytes")) {
673 dev_err(tplg
->dev
, "ASoC: Invalid count %d for byte control\n",
678 for (i
= 0; i
< count
; i
++) {
679 be
= (struct snd_soc_tplg_bytes_control
*)tplg
->pos
;
681 /* validate kcontrol */
682 if (strnlen(be
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
683 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
686 sbe
= kzalloc(sizeof(*sbe
), GFP_KERNEL
);
690 tplg
->pos
+= (sizeof(struct snd_soc_tplg_bytes_control
) +
694 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
695 be
->hdr
.name
, be
->hdr
.access
);
697 memset(&kc
, 0, sizeof(kc
));
698 kc
.name
= be
->hdr
.name
;
699 kc
.private_value
= (long)sbe
;
700 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
701 kc
.access
= be
->hdr
.access
;
704 sbe
->dobj
.type
= SND_SOC_DOBJ_BYTES
;
705 sbe
->dobj
.ops
= tplg
->ops
;
706 INIT_LIST_HEAD(&sbe
->dobj
.list
);
708 /* map io handlers */
709 err
= soc_tplg_kcontrol_bind_io(&be
->hdr
, &kc
, tplg
);
711 soc_control_err(tplg
, &be
->hdr
, be
->hdr
.name
);
716 /* pass control to driver for optional further init */
717 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
718 (struct snd_soc_tplg_ctl_hdr
*)be
);
720 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
726 /* register control here */
727 err
= soc_tplg_add_kcontrol(tplg
, &kc
,
728 &sbe
->dobj
.control
.kcontrol
);
730 dev_err(tplg
->dev
, "ASoC: failed to add %s\n",
736 list_add(&sbe
->dobj
.list
, &tplg
->comp
->dobj_list
);
742 static int soc_tplg_dmixer_create(struct soc_tplg
*tplg
, unsigned int count
,
745 struct snd_soc_tplg_mixer_control
*mc
;
746 struct soc_mixer_control
*sm
;
747 struct snd_kcontrol_new kc
;
750 if (soc_tplg_check_elem_count(tplg
,
751 sizeof(struct snd_soc_tplg_mixer_control
),
752 count
, size
, "mixers")) {
754 dev_err(tplg
->dev
, "ASoC: invalid count %d for controls\n",
759 for (i
= 0; i
< count
; i
++) {
760 mc
= (struct snd_soc_tplg_mixer_control
*)tplg
->pos
;
762 /* validate kcontrol */
763 if (strnlen(mc
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
764 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
767 sm
= kzalloc(sizeof(*sm
), GFP_KERNEL
);
770 tplg
->pos
+= (sizeof(struct snd_soc_tplg_mixer_control
) +
774 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
775 mc
->hdr
.name
, mc
->hdr
.access
);
777 memset(&kc
, 0, sizeof(kc
));
778 kc
.name
= mc
->hdr
.name
;
779 kc
.private_value
= (long)sm
;
780 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
781 kc
.access
= mc
->hdr
.access
;
783 /* we only support FL/FR channel mapping atm */
784 sm
->reg
= tplc_chan_get_reg(tplg
, mc
->channel
,
786 sm
->rreg
= tplc_chan_get_reg(tplg
, mc
->channel
,
788 sm
->shift
= tplc_chan_get_shift(tplg
, mc
->channel
,
790 sm
->rshift
= tplc_chan_get_shift(tplg
, mc
->channel
,
795 sm
->invert
= mc
->invert
;
796 sm
->platform_max
= mc
->platform_max
;
797 sm
->dobj
.index
= tplg
->index
;
798 sm
->dobj
.ops
= tplg
->ops
;
799 sm
->dobj
.type
= SND_SOC_DOBJ_MIXER
;
800 INIT_LIST_HEAD(&sm
->dobj
.list
);
802 /* map io handlers */
803 err
= soc_tplg_kcontrol_bind_io(&mc
->hdr
, &kc
, tplg
);
805 soc_control_err(tplg
, &mc
->hdr
, mc
->hdr
.name
);
810 /* pass control to driver for optional further init */
811 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
812 (struct snd_soc_tplg_ctl_hdr
*) mc
);
814 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
820 /* create any TLV data */
821 soc_tplg_create_tlv(tplg
, &kc
, &mc
->hdr
);
823 /* register control here */
824 err
= soc_tplg_add_kcontrol(tplg
, &kc
,
825 &sm
->dobj
.control
.kcontrol
);
827 dev_err(tplg
->dev
, "ASoC: failed to add %s\n",
829 soc_tplg_free_tlv(tplg
, &kc
);
834 list_add(&sm
->dobj
.list
, &tplg
->comp
->dobj_list
);
840 static int soc_tplg_denum_create_texts(struct soc_enum
*se
,
841 struct snd_soc_tplg_enum_control
*ec
)
845 se
->dobj
.control
.dtexts
=
846 kzalloc(sizeof(char *) * ec
->items
, GFP_KERNEL
);
847 if (se
->dobj
.control
.dtexts
== NULL
)
850 for (i
= 0; i
< ec
->items
; i
++) {
852 if (strnlen(ec
->texts
[i
], SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
853 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) {
858 se
->dobj
.control
.dtexts
[i
] = kstrdup(ec
->texts
[i
], GFP_KERNEL
);
859 if (!se
->dobj
.control
.dtexts
[i
]) {
868 for (--i
; i
>= 0; i
--)
869 kfree(se
->dobj
.control
.dtexts
[i
]);
870 kfree(se
->dobj
.control
.dtexts
);
874 static int soc_tplg_denum_create_values(struct soc_enum
*se
,
875 struct snd_soc_tplg_enum_control
*ec
)
877 if (ec
->items
> sizeof(*ec
->values
))
880 se
->dobj
.control
.dvalues
= kmemdup(ec
->values
,
881 ec
->items
* sizeof(u32
),
883 if (!se
->dobj
.control
.dvalues
)
889 static int soc_tplg_denum_create(struct soc_tplg
*tplg
, unsigned int count
,
892 struct snd_soc_tplg_enum_control
*ec
;
894 struct snd_kcontrol_new kc
;
897 if (soc_tplg_check_elem_count(tplg
,
898 sizeof(struct snd_soc_tplg_enum_control
),
899 count
, size
, "enums")) {
901 dev_err(tplg
->dev
, "ASoC: invalid count %d for enum controls\n",
906 for (i
= 0; i
< count
; i
++) {
907 ec
= (struct snd_soc_tplg_enum_control
*)tplg
->pos
;
908 tplg
->pos
+= (sizeof(struct snd_soc_tplg_enum_control
) +
911 /* validate kcontrol */
912 if (strnlen(ec
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
913 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
916 se
= kzalloc((sizeof(*se
)), GFP_KERNEL
);
920 dev_dbg(tplg
->dev
, "ASoC: adding enum kcontrol %s size %d\n",
921 ec
->hdr
.name
, ec
->items
);
923 memset(&kc
, 0, sizeof(kc
));
924 kc
.name
= ec
->hdr
.name
;
925 kc
.private_value
= (long)se
;
926 kc
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
927 kc
.access
= ec
->hdr
.access
;
929 se
->reg
= tplc_chan_get_reg(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
930 se
->shift_l
= tplc_chan_get_shift(tplg
, ec
->channel
,
932 se
->shift_r
= tplc_chan_get_shift(tplg
, ec
->channel
,
935 se
->items
= ec
->items
;
937 se
->dobj
.index
= tplg
->index
;
938 se
->dobj
.type
= SND_SOC_DOBJ_ENUM
;
939 se
->dobj
.ops
= tplg
->ops
;
940 INIT_LIST_HEAD(&se
->dobj
.list
);
942 switch (ec
->hdr
.ops
.info
) {
943 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
944 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
945 err
= soc_tplg_denum_create_values(se
, ec
);
948 "ASoC: could not create values for %s\n",
953 /* fall through and create texts */
954 case SND_SOC_TPLG_CTL_ENUM
:
955 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
956 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
957 err
= soc_tplg_denum_create_texts(se
, ec
);
960 "ASoC: could not create texts for %s\n",
968 "ASoC: invalid enum control type %d for %s\n",
969 ec
->hdr
.ops
.info
, ec
->hdr
.name
);
974 /* map io handlers */
975 err
= soc_tplg_kcontrol_bind_io(&ec
->hdr
, &kc
, tplg
);
977 soc_control_err(tplg
, &ec
->hdr
, ec
->hdr
.name
);
982 /* pass control to driver for optional further init */
983 err
= soc_tplg_init_kcontrol(tplg
, &kc
,
984 (struct snd_soc_tplg_ctl_hdr
*) ec
);
986 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
992 /* register control here */
993 ret
= soc_tplg_add_kcontrol(tplg
,
994 &kc
, &se
->dobj
.control
.kcontrol
);
996 dev_err(tplg
->dev
, "ASoC: could not add kcontrol %s\n",
1002 list_add(&se
->dobj
.list
, &tplg
->comp
->dobj_list
);
1008 static int soc_tplg_kcontrol_elems_load(struct soc_tplg
*tplg
,
1009 struct snd_soc_tplg_hdr
*hdr
)
1011 struct snd_soc_tplg_ctl_hdr
*control_hdr
;
1014 if (tplg
->pass
!= SOC_TPLG_PASS_MIXER
) {
1015 tplg
->pos
+= hdr
->size
+ hdr
->payload_size
;
1019 dev_dbg(tplg
->dev
, "ASoC: adding %d kcontrols at 0x%lx\n", hdr
->count
,
1020 soc_tplg_get_offset(tplg
));
1022 for (i
= 0; i
< hdr
->count
; i
++) {
1024 control_hdr
= (struct snd_soc_tplg_ctl_hdr
*)tplg
->pos
;
1026 if (control_hdr
->size
!= sizeof(*control_hdr
)) {
1027 dev_err(tplg
->dev
, "ASoC: invalid control size\n");
1031 switch (control_hdr
->ops
.info
) {
1032 case SND_SOC_TPLG_CTL_VOLSW
:
1033 case SND_SOC_TPLG_CTL_STROBE
:
1034 case SND_SOC_TPLG_CTL_VOLSW_SX
:
1035 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
1036 case SND_SOC_TPLG_CTL_RANGE
:
1037 case SND_SOC_TPLG_DAPM_CTL_VOLSW
:
1038 case SND_SOC_TPLG_DAPM_CTL_PIN
:
1039 soc_tplg_dmixer_create(tplg
, 1, hdr
->payload_size
);
1041 case SND_SOC_TPLG_CTL_ENUM
:
1042 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1043 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1044 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1045 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1046 soc_tplg_denum_create(tplg
, 1, hdr
->payload_size
);
1048 case SND_SOC_TPLG_CTL_BYTES
:
1049 soc_tplg_dbytes_create(tplg
, 1, hdr
->payload_size
);
1052 soc_bind_err(tplg
, control_hdr
, i
);
1060 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg
*tplg
,
1061 struct snd_soc_tplg_hdr
*hdr
)
1063 struct snd_soc_dapm_context
*dapm
= &tplg
->comp
->dapm
;
1064 struct snd_soc_dapm_route route
;
1065 struct snd_soc_tplg_dapm_graph_elem
*elem
;
1066 int count
= hdr
->count
, i
;
1068 if (tplg
->pass
!= SOC_TPLG_PASS_GRAPH
) {
1069 tplg
->pos
+= hdr
->size
+ hdr
->payload_size
;
1073 if (soc_tplg_check_elem_count(tplg
,
1074 sizeof(struct snd_soc_tplg_dapm_graph_elem
),
1075 count
, hdr
->payload_size
, "graph")) {
1077 dev_err(tplg
->dev
, "ASoC: invalid count %d for DAPM routes\n",
1082 dev_dbg(tplg
->dev
, "ASoC: adding %d DAPM routes\n", count
);
1084 for (i
= 0; i
< count
; i
++) {
1085 elem
= (struct snd_soc_tplg_dapm_graph_elem
*)tplg
->pos
;
1086 tplg
->pos
+= sizeof(struct snd_soc_tplg_dapm_graph_elem
);
1088 /* validate routes */
1089 if (strnlen(elem
->source
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1090 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1092 if (strnlen(elem
->sink
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1093 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1095 if (strnlen(elem
->control
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1096 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1099 route
.source
= elem
->source
;
1100 route
.sink
= elem
->sink
;
1101 route
.connected
= NULL
; /* set to NULL atm for tplg users */
1102 if (strnlen(elem
->control
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) == 0)
1103 route
.control
= NULL
;
1105 route
.control
= elem
->control
;
1107 /* add route, but keep going if some fail */
1108 snd_soc_dapm_add_routes(dapm
, &route
, 1);
1114 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_dmixer_create(
1115 struct soc_tplg
*tplg
, int num_kcontrols
)
1117 struct snd_kcontrol_new
*kc
;
1118 struct soc_mixer_control
*sm
;
1119 struct snd_soc_tplg_mixer_control
*mc
;
1122 kc
= kcalloc(num_kcontrols
, sizeof(*kc
), GFP_KERNEL
);
1126 for (i
= 0; i
< num_kcontrols
; i
++) {
1127 mc
= (struct snd_soc_tplg_mixer_control
*)tplg
->pos
;
1128 sm
= kzalloc(sizeof(*sm
), GFP_KERNEL
);
1132 tplg
->pos
+= (sizeof(struct snd_soc_tplg_mixer_control
) +
1135 /* validate kcontrol */
1136 if (strnlen(mc
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1137 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1140 dev_dbg(tplg
->dev
, " adding DAPM widget mixer control %s at %d\n",
1143 kc
[i
].name
= mc
->hdr
.name
;
1144 kc
[i
].private_value
= (long)sm
;
1145 kc
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1146 kc
[i
].access
= mc
->hdr
.access
;
1148 /* we only support FL/FR channel mapping atm */
1149 sm
->reg
= tplc_chan_get_reg(tplg
, mc
->channel
,
1151 sm
->rreg
= tplc_chan_get_reg(tplg
, mc
->channel
,
1153 sm
->shift
= tplc_chan_get_shift(tplg
, mc
->channel
,
1155 sm
->rshift
= tplc_chan_get_shift(tplg
, mc
->channel
,
1160 sm
->invert
= mc
->invert
;
1161 sm
->platform_max
= mc
->platform_max
;
1162 sm
->dobj
.index
= tplg
->index
;
1163 INIT_LIST_HEAD(&sm
->dobj
.list
);
1165 /* map io handlers */
1166 err
= soc_tplg_kcontrol_bind_io(&mc
->hdr
, &kc
[i
], tplg
);
1168 soc_control_err(tplg
, &mc
->hdr
, mc
->hdr
.name
);
1173 /* pass control to driver for optional further init */
1174 err
= soc_tplg_init_kcontrol(tplg
, &kc
[i
],
1175 (struct snd_soc_tplg_ctl_hdr
*)mc
);
1177 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1188 for (--i
; i
>= 0; i
--)
1189 kfree((void *)kc
[i
].private_value
);
1194 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_denum_create(
1195 struct soc_tplg
*tplg
)
1197 struct snd_kcontrol_new
*kc
;
1198 struct snd_soc_tplg_enum_control
*ec
;
1199 struct soc_enum
*se
;
1202 ec
= (struct snd_soc_tplg_enum_control
*)tplg
->pos
;
1203 tplg
->pos
+= (sizeof(struct snd_soc_tplg_enum_control
) +
1206 /* validate kcontrol */
1207 if (strnlen(ec
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1208 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1211 kc
= kzalloc(sizeof(*kc
), GFP_KERNEL
);
1215 se
= kzalloc(sizeof(*se
), GFP_KERNEL
);
1219 dev_dbg(tplg
->dev
, " adding DAPM widget enum control %s\n",
1222 kc
->name
= ec
->hdr
.name
;
1223 kc
->private_value
= (long)se
;
1224 kc
->iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1225 kc
->access
= ec
->hdr
.access
;
1227 /* we only support FL/FR channel mapping atm */
1228 se
->reg
= tplc_chan_get_reg(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
1229 se
->shift_l
= tplc_chan_get_shift(tplg
, ec
->channel
, SNDRV_CHMAP_FL
);
1230 se
->shift_r
= tplc_chan_get_shift(tplg
, ec
->channel
, SNDRV_CHMAP_FR
);
1232 se
->items
= ec
->items
;
1233 se
->mask
= ec
->mask
;
1234 se
->dobj
.index
= tplg
->index
;
1236 switch (ec
->hdr
.ops
.info
) {
1237 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1238 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1239 err
= soc_tplg_denum_create_values(se
, ec
);
1241 dev_err(tplg
->dev
, "ASoC: could not create values for %s\n",
1245 /* fall through to create texts */
1246 case SND_SOC_TPLG_CTL_ENUM
:
1247 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1248 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1249 err
= soc_tplg_denum_create_texts(se
, ec
);
1251 dev_err(tplg
->dev
, "ASoC: could not create texts for %s\n",
1257 dev_err(tplg
->dev
, "ASoC: invalid enum control type %d for %s\n",
1258 ec
->hdr
.ops
.info
, ec
->hdr
.name
);
1262 /* map io handlers */
1263 err
= soc_tplg_kcontrol_bind_io(&ec
->hdr
, kc
, tplg
);
1265 soc_control_err(tplg
, &ec
->hdr
, ec
->hdr
.name
);
1269 /* pass control to driver for optional further init */
1270 err
= soc_tplg_init_kcontrol(tplg
, kc
,
1271 (struct snd_soc_tplg_ctl_hdr
*)ec
);
1273 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1281 /* free values and texts */
1282 kfree(se
->dobj
.control
.dvalues
);
1283 for (i
= 0; i
< ec
->items
; i
++)
1284 kfree(se
->dobj
.control
.dtexts
[i
]);
1293 static struct snd_kcontrol_new
*soc_tplg_dapm_widget_dbytes_create(
1294 struct soc_tplg
*tplg
, int count
)
1296 struct snd_soc_tplg_bytes_control
*be
;
1297 struct soc_bytes_ext
*sbe
;
1298 struct snd_kcontrol_new
*kc
;
1301 kc
= kcalloc(count
, sizeof(*kc
), GFP_KERNEL
);
1305 for (i
= 0; i
< count
; i
++) {
1306 be
= (struct snd_soc_tplg_bytes_control
*)tplg
->pos
;
1308 /* validate kcontrol */
1309 if (strnlen(be
->hdr
.name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1310 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1313 sbe
= kzalloc(sizeof(*sbe
), GFP_KERNEL
);
1317 tplg
->pos
+= (sizeof(struct snd_soc_tplg_bytes_control
) +
1321 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1322 be
->hdr
.name
, be
->hdr
.access
);
1324 kc
[i
].name
= be
->hdr
.name
;
1325 kc
[i
].private_value
= (long)sbe
;
1326 kc
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1327 kc
[i
].access
= be
->hdr
.access
;
1330 INIT_LIST_HEAD(&sbe
->dobj
.list
);
1332 /* map standard io handlers and check for external handlers */
1333 err
= soc_tplg_kcontrol_bind_io(&be
->hdr
, &kc
[i
], tplg
);
1335 soc_control_err(tplg
, &be
->hdr
, be
->hdr
.name
);
1340 /* pass control to driver for optional further init */
1341 err
= soc_tplg_init_kcontrol(tplg
, &kc
[i
],
1342 (struct snd_soc_tplg_ctl_hdr
*)be
);
1344 dev_err(tplg
->dev
, "ASoC: failed to init %s\n",
1354 for (--i
; i
>= 0; i
--)
1355 kfree((void *)kc
[i
].private_value
);
1361 static int soc_tplg_dapm_widget_create(struct soc_tplg
*tplg
,
1362 struct snd_soc_tplg_dapm_widget
*w
)
1364 struct snd_soc_dapm_context
*dapm
= &tplg
->comp
->dapm
;
1365 struct snd_soc_dapm_widget
template, *widget
;
1366 struct snd_soc_tplg_ctl_hdr
*control_hdr
;
1367 struct snd_soc_card
*card
= tplg
->comp
->card
;
1370 if (strnlen(w
->name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1371 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1373 if (strnlen(w
->sname
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
) ==
1374 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
1377 dev_dbg(tplg
->dev
, "ASoC: creating DAPM widget %s id %d\n",
1380 memset(&template, 0, sizeof(template));
1382 /* map user to kernel widget ID */
1383 template.id
= get_widget_id(w
->id
);
1384 if (template.id
< 0)
1387 template.name
= kstrdup(w
->name
, GFP_KERNEL
);
1390 template.sname
= kstrdup(w
->sname
, GFP_KERNEL
);
1391 if (!template.sname
) {
1395 template.reg
= w
->reg
;
1396 template.shift
= w
->shift
;
1397 template.mask
= w
->mask
;
1398 template.subseq
= w
->subseq
;
1399 template.on_val
= w
->invert
? 0 : 1;
1400 template.off_val
= w
->invert
? 1 : 0;
1401 template.ignore_suspend
= w
->ignore_suspend
;
1402 template.event_flags
= w
->event_flags
;
1403 template.dobj
.index
= tplg
->index
;
1406 (sizeof(struct snd_soc_tplg_dapm_widget
) + w
->priv
.size
);
1407 if (w
->num_kcontrols
== 0) {
1408 template.num_kcontrols
= 0;
1412 control_hdr
= (struct snd_soc_tplg_ctl_hdr
*)tplg
->pos
;
1413 dev_dbg(tplg
->dev
, "ASoC: template %s has %d controls of type %x\n",
1414 w
->name
, w
->num_kcontrols
, control_hdr
->type
);
1416 switch (control_hdr
->ops
.info
) {
1417 case SND_SOC_TPLG_CTL_VOLSW
:
1418 case SND_SOC_TPLG_CTL_STROBE
:
1419 case SND_SOC_TPLG_CTL_VOLSW_SX
:
1420 case SND_SOC_TPLG_CTL_VOLSW_XR_SX
:
1421 case SND_SOC_TPLG_CTL_RANGE
:
1422 case SND_SOC_TPLG_DAPM_CTL_VOLSW
:
1423 template.num_kcontrols
= w
->num_kcontrols
;
1424 template.kcontrol_news
=
1425 soc_tplg_dapm_widget_dmixer_create(tplg
,
1426 template.num_kcontrols
);
1427 if (!template.kcontrol_news
) {
1432 case SND_SOC_TPLG_CTL_ENUM
:
1433 case SND_SOC_TPLG_CTL_ENUM_VALUE
:
1434 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE
:
1435 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT
:
1436 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE
:
1437 template.dobj
.widget
.kcontrol_enum
= 1;
1438 template.num_kcontrols
= 1;
1439 template.kcontrol_news
=
1440 soc_tplg_dapm_widget_denum_create(tplg
);
1441 if (!template.kcontrol_news
) {
1446 case SND_SOC_TPLG_CTL_BYTES
:
1447 template.num_kcontrols
= w
->num_kcontrols
;
1448 template.kcontrol_news
=
1449 soc_tplg_dapm_widget_dbytes_create(tplg
,
1450 template.num_kcontrols
);
1451 if (!template.kcontrol_news
) {
1457 dev_err(tplg
->dev
, "ASoC: invalid widget control type %d:%d:%d\n",
1458 control_hdr
->ops
.get
, control_hdr
->ops
.put
,
1459 control_hdr
->ops
.info
);
1465 ret
= soc_tplg_widget_load(tplg
, &template, w
);
1469 /* card dapm mutex is held by the core if we are loading topology
1470 * data during sound card init. */
1471 if (card
->instantiated
)
1472 widget
= snd_soc_dapm_new_control(dapm
, &template);
1474 widget
= snd_soc_dapm_new_control_unlocked(dapm
, &template);
1475 if (widget
== NULL
) {
1476 dev_err(tplg
->dev
, "ASoC: failed to create widget %s controls\n",
1481 widget
->dobj
.type
= SND_SOC_DOBJ_WIDGET
;
1482 widget
->dobj
.ops
= tplg
->ops
;
1483 widget
->dobj
.index
= tplg
->index
;
1484 kfree(template.sname
);
1485 kfree(template.name
);
1486 list_add(&widget
->dobj
.list
, &tplg
->comp
->dobj_list
);
1490 kfree(template.sname
);
1492 kfree(template.name
);
1496 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg
*tplg
,
1497 struct snd_soc_tplg_hdr
*hdr
)
1499 struct snd_soc_tplg_dapm_widget
*widget
;
1500 int ret
, count
= hdr
->count
, i
;
1502 if (tplg
->pass
!= SOC_TPLG_PASS_WIDGET
)
1505 dev_dbg(tplg
->dev
, "ASoC: adding %d DAPM widgets\n", count
);
1507 for (i
= 0; i
< count
; i
++) {
1508 widget
= (struct snd_soc_tplg_dapm_widget
*) tplg
->pos
;
1509 if (widget
->size
!= sizeof(*widget
)) {
1510 dev_err(tplg
->dev
, "ASoC: invalid widget size\n");
1514 ret
= soc_tplg_dapm_widget_create(tplg
, widget
);
1516 dev_err(tplg
->dev
, "ASoC: failed to load widget %s\n",
1525 static int soc_tplg_dapm_complete(struct soc_tplg
*tplg
)
1527 struct snd_soc_card
*card
= tplg
->comp
->card
;
1530 /* Card might not have been registered at this point.
1531 * If so, just return success.
1533 if (!card
|| !card
->instantiated
) {
1534 dev_warn(tplg
->dev
, "ASoC: Parent card not yet available,"
1535 "Do not add new widgets now\n");
1539 ret
= snd_soc_dapm_new_widgets(card
);
1541 dev_err(tplg
->dev
, "ASoC: failed to create new widgets %d\n",
1547 static void set_stream_info(struct snd_soc_pcm_stream
*stream
,
1548 struct snd_soc_tplg_stream_caps
*caps
)
1550 stream
->stream_name
= kstrdup(caps
->name
, GFP_KERNEL
);
1551 stream
->channels_min
= caps
->channels_min
;
1552 stream
->channels_max
= caps
->channels_max
;
1553 stream
->rates
= caps
->rates
;
1554 stream
->rate_min
= caps
->rate_min
;
1555 stream
->rate_max
= caps
->rate_max
;
1556 stream
->formats
= caps
->formats
;
1559 static int soc_tplg_dai_create(struct soc_tplg
*tplg
,
1560 struct snd_soc_tplg_pcm
*pcm
)
1562 struct snd_soc_dai_driver
*dai_drv
;
1563 struct snd_soc_pcm_stream
*stream
;
1564 struct snd_soc_tplg_stream_caps
*caps
;
1567 dai_drv
= kzalloc(sizeof(struct snd_soc_dai_driver
), GFP_KERNEL
);
1568 if (dai_drv
== NULL
)
1571 dai_drv
->name
= pcm
->dai_name
;
1572 dai_drv
->id
= pcm
->dai_id
;
1574 if (pcm
->playback
) {
1575 stream
= &dai_drv
->playback
;
1576 caps
= &pcm
->caps
[SND_SOC_TPLG_STREAM_PLAYBACK
];
1577 set_stream_info(stream
, caps
);
1581 stream
= &dai_drv
->capture
;
1582 caps
= &pcm
->caps
[SND_SOC_TPLG_STREAM_CAPTURE
];
1583 set_stream_info(stream
, caps
);
1586 /* pass control to component driver for optional further init */
1587 ret
= soc_tplg_dai_load(tplg
, dai_drv
);
1589 dev_err(tplg
->comp
->dev
, "ASoC: DAI loading failed\n");
1594 dai_drv
->dobj
.index
= tplg
->index
;
1595 dai_drv
->dobj
.ops
= tplg
->ops
;
1596 dai_drv
->dobj
.type
= SND_SOC_DOBJ_PCM
;
1597 list_add(&dai_drv
->dobj
.list
, &tplg
->comp
->dobj_list
);
1599 /* register the DAI to the component */
1600 return snd_soc_register_dai(tplg
->comp
, dai_drv
);
1603 /* create the FE DAI link */
1604 static int soc_tplg_link_create(struct soc_tplg
*tplg
,
1605 struct snd_soc_tplg_pcm
*pcm
)
1607 struct snd_soc_dai_link
*link
;
1610 link
= kzalloc(sizeof(struct snd_soc_dai_link
), GFP_KERNEL
);
1614 link
->name
= pcm
->pcm_name
;
1615 link
->stream_name
= pcm
->pcm_name
;
1616 link
->id
= pcm
->pcm_id
;
1618 link
->cpu_dai_name
= pcm
->dai_name
;
1619 link
->codec_name
= "snd-soc-dummy";
1620 link
->codec_dai_name
= "snd-soc-dummy-dai";
1624 link
->dpcm_playback
= pcm
->playback
;
1625 link
->dpcm_capture
= pcm
->capture
;
1627 /* pass control to component driver for optional further init */
1628 ret
= soc_tplg_dai_link_load(tplg
, link
);
1630 dev_err(tplg
->comp
->dev
, "ASoC: FE link loading failed\n");
1635 link
->dobj
.index
= tplg
->index
;
1636 link
->dobj
.ops
= tplg
->ops
;
1637 link
->dobj
.type
= SND_SOC_DOBJ_DAI_LINK
;
1638 list_add(&link
->dobj
.list
, &tplg
->comp
->dobj_list
);
1640 snd_soc_add_dai_link(tplg
->comp
->card
, link
);
1644 /* create a FE DAI and DAI link from the PCM object */
1645 static int soc_tplg_pcm_create(struct soc_tplg
*tplg
,
1646 struct snd_soc_tplg_pcm
*pcm
)
1650 ret
= soc_tplg_dai_create(tplg
, pcm
);
1654 return soc_tplg_link_create(tplg
, pcm
);
1657 static int soc_tplg_pcm_elems_load(struct soc_tplg
*tplg
,
1658 struct snd_soc_tplg_hdr
*hdr
)
1660 struct snd_soc_tplg_pcm
*pcm
;
1661 int count
= hdr
->count
;
1664 if (tplg
->pass
!= SOC_TPLG_PASS_PCM_DAI
)
1667 if (soc_tplg_check_elem_count(tplg
,
1668 sizeof(struct snd_soc_tplg_pcm
), count
,
1669 hdr
->payload_size
, "PCM DAI")) {
1670 dev_err(tplg
->dev
, "ASoC: invalid count %d for PCM DAI elems\n",
1675 /* create the FE DAIs and DAI links */
1676 pcm
= (struct snd_soc_tplg_pcm
*)tplg
->pos
;
1677 for (i
= 0; i
< count
; i
++) {
1678 if (pcm
->size
!= sizeof(*pcm
)) {
1679 dev_err(tplg
->dev
, "ASoC: invalid pcm size\n");
1683 soc_tplg_pcm_create(tplg
, pcm
);
1687 dev_dbg(tplg
->dev
, "ASoC: adding %d PCM DAIs\n", count
);
1688 tplg
->pos
+= sizeof(struct snd_soc_tplg_pcm
) * count
;
1693 static int soc_tplg_manifest_load(struct soc_tplg
*tplg
,
1694 struct snd_soc_tplg_hdr
*hdr
)
1696 struct snd_soc_tplg_manifest
*manifest
;
1698 if (tplg
->pass
!= SOC_TPLG_PASS_MANIFEST
)
1701 manifest
= (struct snd_soc_tplg_manifest
*)tplg
->pos
;
1702 if (manifest
->size
!= sizeof(*manifest
)) {
1703 dev_err(tplg
->dev
, "ASoC: invalid manifest size\n");
1707 tplg
->pos
+= sizeof(struct snd_soc_tplg_manifest
);
1709 if (tplg
->comp
&& tplg
->ops
&& tplg
->ops
->manifest
)
1710 return tplg
->ops
->manifest(tplg
->comp
, manifest
);
1712 dev_err(tplg
->dev
, "ASoC: Firmware manifest not supported\n");
1716 /* validate header magic, size and type */
1717 static int soc_valid_header(struct soc_tplg
*tplg
,
1718 struct snd_soc_tplg_hdr
*hdr
)
1720 if (soc_tplg_get_hdr_offset(tplg
) >= tplg
->fw
->size
)
1723 if (hdr
->size
!= sizeof(*hdr
)) {
1725 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
1726 hdr
->type
, soc_tplg_get_hdr_offset(tplg
),
1731 /* big endian firmware objects not supported atm */
1732 if (hdr
->magic
== cpu_to_be32(SND_SOC_TPLG_MAGIC
)) {
1734 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1735 tplg
->pass
, hdr
->magic
,
1736 soc_tplg_get_hdr_offset(tplg
), tplg
->fw
->size
);
1740 if (hdr
->magic
!= SND_SOC_TPLG_MAGIC
) {
1742 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1743 tplg
->pass
, hdr
->magic
,
1744 soc_tplg_get_hdr_offset(tplg
), tplg
->fw
->size
);
1748 if (hdr
->abi
!= SND_SOC_TPLG_ABI_VERSION
) {
1750 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1751 tplg
->pass
, hdr
->abi
,
1752 SND_SOC_TPLG_ABI_VERSION
, soc_tplg_get_hdr_offset(tplg
),
1757 if (hdr
->payload_size
== 0) {
1758 dev_err(tplg
->dev
, "ASoC: header has 0 size at offset 0x%lx.\n",
1759 soc_tplg_get_hdr_offset(tplg
));
1763 if (tplg
->pass
== hdr
->type
)
1765 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
1766 hdr
->payload_size
, hdr
->type
, hdr
->version
,
1767 hdr
->vendor_type
, tplg
->pass
);
1772 /* check header type and call appropriate handler */
1773 static int soc_tplg_load_header(struct soc_tplg
*tplg
,
1774 struct snd_soc_tplg_hdr
*hdr
)
1776 tplg
->pos
= tplg
->hdr_pos
+ sizeof(struct snd_soc_tplg_hdr
);
1778 /* check for matching ID */
1779 if (hdr
->index
!= tplg
->req_index
&&
1780 hdr
->index
!= SND_SOC_TPLG_INDEX_ALL
)
1783 tplg
->index
= hdr
->index
;
1785 switch (hdr
->type
) {
1786 case SND_SOC_TPLG_TYPE_MIXER
:
1787 case SND_SOC_TPLG_TYPE_ENUM
:
1788 case SND_SOC_TPLG_TYPE_BYTES
:
1789 return soc_tplg_kcontrol_elems_load(tplg
, hdr
);
1790 case SND_SOC_TPLG_TYPE_DAPM_GRAPH
:
1791 return soc_tplg_dapm_graph_elems_load(tplg
, hdr
);
1792 case SND_SOC_TPLG_TYPE_DAPM_WIDGET
:
1793 return soc_tplg_dapm_widget_elems_load(tplg
, hdr
);
1794 case SND_SOC_TPLG_TYPE_PCM
:
1795 return soc_tplg_pcm_elems_load(tplg
, hdr
);
1796 case SND_SOC_TPLG_TYPE_MANIFEST
:
1797 return soc_tplg_manifest_load(tplg
, hdr
);
1799 /* bespoke vendor data object */
1800 return soc_tplg_vendor_load(tplg
, hdr
);
1806 /* process the topology file headers */
1807 static int soc_tplg_process_headers(struct soc_tplg
*tplg
)
1809 struct snd_soc_tplg_hdr
*hdr
;
1812 tplg
->pass
= SOC_TPLG_PASS_START
;
1814 /* process the header types from start to end */
1815 while (tplg
->pass
<= SOC_TPLG_PASS_END
) {
1817 tplg
->hdr_pos
= tplg
->fw
->data
;
1818 hdr
= (struct snd_soc_tplg_hdr
*)tplg
->hdr_pos
;
1820 while (!soc_tplg_is_eof(tplg
)) {
1822 /* make sure header is valid before loading */
1823 ret
= soc_valid_header(tplg
, hdr
);
1829 /* load the header object */
1830 ret
= soc_tplg_load_header(tplg
, hdr
);
1834 /* goto next header */
1835 tplg
->hdr_pos
+= hdr
->payload_size
+
1836 sizeof(struct snd_soc_tplg_hdr
);
1837 hdr
= (struct snd_soc_tplg_hdr
*)tplg
->hdr_pos
;
1840 /* next data type pass */
1844 /* signal DAPM we are complete */
1845 ret
= soc_tplg_dapm_complete(tplg
);
1848 "ASoC: failed to initialise DAPM from Firmware\n");
1853 static int soc_tplg_load(struct soc_tplg
*tplg
)
1857 ret
= soc_tplg_process_headers(tplg
);
1859 soc_tplg_complete(tplg
);
1864 /* load audio component topology from "firmware" file */
1865 int snd_soc_tplg_component_load(struct snd_soc_component
*comp
,
1866 struct snd_soc_tplg_ops
*ops
, const struct firmware
*fw
, u32 id
)
1868 struct soc_tplg tplg
;
1870 /* setup parsing context */
1871 memset(&tplg
, 0, sizeof(tplg
));
1873 tplg
.dev
= comp
->dev
;
1876 tplg
.req_index
= id
;
1877 tplg
.io_ops
= ops
->io_ops
;
1878 tplg
.io_ops_count
= ops
->io_ops_count
;
1879 tplg
.bytes_ext_ops
= ops
->bytes_ext_ops
;
1880 tplg
.bytes_ext_ops_count
= ops
->bytes_ext_ops_count
;
1882 return soc_tplg_load(&tplg
);
1884 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load
);
1886 /* remove this dynamic widget */
1887 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget
*w
)
1889 /* make sure we are a widget */
1890 if (w
->dobj
.type
!= SND_SOC_DOBJ_WIDGET
)
1893 remove_widget(w
->dapm
->component
, &w
->dobj
, SOC_TPLG_PASS_WIDGET
);
1895 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove
);
1897 /* remove all dynamic widgets from this DAPM context */
1898 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context
*dapm
,
1901 struct snd_soc_dapm_widget
*w
, *next_w
;
1903 list_for_each_entry_safe(w
, next_w
, &dapm
->card
->widgets
, list
) {
1905 /* make sure we are a widget with correct context */
1906 if (w
->dobj
.type
!= SND_SOC_DOBJ_WIDGET
|| w
->dapm
!= dapm
)
1910 if (w
->dobj
.index
!= index
&&
1911 w
->dobj
.index
!= SND_SOC_TPLG_INDEX_ALL
)
1913 /* check and free and dynamic widget kcontrols */
1914 snd_soc_tplg_widget_remove(w
);
1915 snd_soc_dapm_free_widget(w
);
1917 snd_soc_dapm_reset_cache(dapm
);
1919 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all
);
1921 /* remove dynamic controls from the component driver */
1922 int snd_soc_tplg_component_remove(struct snd_soc_component
*comp
, u32 index
)
1924 struct snd_soc_dobj
*dobj
, *next_dobj
;
1925 int pass
= SOC_TPLG_PASS_END
;
1927 /* process the header types from end to start */
1928 while (pass
>= SOC_TPLG_PASS_START
) {
1930 /* remove mixer controls */
1931 list_for_each_entry_safe(dobj
, next_dobj
, &comp
->dobj_list
,
1935 if (dobj
->index
!= index
&&
1936 dobj
->index
!= SND_SOC_TPLG_INDEX_ALL
)
1939 switch (dobj
->type
) {
1940 case SND_SOC_DOBJ_MIXER
:
1941 remove_mixer(comp
, dobj
, pass
);
1943 case SND_SOC_DOBJ_ENUM
:
1944 remove_enum(comp
, dobj
, pass
);
1946 case SND_SOC_DOBJ_BYTES
:
1947 remove_bytes(comp
, dobj
, pass
);
1949 case SND_SOC_DOBJ_WIDGET
:
1950 remove_widget(comp
, dobj
, pass
);
1952 case SND_SOC_DOBJ_PCM
:
1953 remove_dai(comp
, dobj
, pass
);
1955 case SND_SOC_DOBJ_DAI_LINK
:
1956 remove_link(comp
, dobj
, pass
);
1959 dev_err(comp
->dev
, "ASoC: invalid component type %d for removal\n",
1967 /* let caller know if FW can be freed when no objects are left */
1968 return !list_empty(&comp
->dobj_list
);
1970 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove
);