Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / sound / soc / soc-topology.c
CommitLineData
f2b6a1b2
KM
1// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-topology.c -- ALSA SoC Topology
4//
5// Copyright (C) 2012 Texas Instruments Inc.
6// Copyright (C) 2015 Intel Corporation.
7//
8// Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9// K, Mythri P <mythri.p.k@intel.com>
10// Prusty, Subhransu S <subhransu.s.prusty@intel.com>
11// B, Jayachandran <jayachandran.b@intel.com>
12// Abdullah, Omair M <omair.m.abdullah@intel.com>
13// Jin, Yao <yao.jin@intel.com>
14// Lin, Mengdong <mengdong.lin@intel.com>
15//
16// Add support to read audio firmware topology alongside firmware text. The
17// topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
18// equalizers, firmware, coefficients etc.
19//
20// This file only manages the core ALSA and ASoC components, all other bespoke
21// firmware topology data is passed to component drivers for bespoke handling.
8a978234
LG
22
23#include <linux/kernel.h>
24#include <linux/export.h>
25#include <linux/list.h>
26#include <linux/firmware.h>
27#include <linux/slab.h>
28#include <sound/soc.h>
29#include <sound/soc-dapm.h>
30#include <sound/soc-topology.h>
28a87eeb 31#include <sound/tlv.h>
8a978234 32
2114171d
PLB
33#define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */
34
8a978234
LG
35/*
36 * We make several passes over the data (since it wont necessarily be ordered)
37 * and process objects in the following order. This guarantees the component
38 * drivers will be ready with any vendor data before the mixers and DAPM objects
39 * are loaded (that may make use of the vendor data).
40 */
41#define SOC_TPLG_PASS_MANIFEST 0
42#define SOC_TPLG_PASS_VENDOR 1
5e2cd47a 43#define SOC_TPLG_PASS_CONTROL 2
8a978234 44#define SOC_TPLG_PASS_WIDGET 3
1a8e7fab
ML
45#define SOC_TPLG_PASS_PCM_DAI 4
46#define SOC_TPLG_PASS_GRAPH 5
6257d224
AS
47#define SOC_TPLG_PASS_BE_DAI 6
48#define SOC_TPLG_PASS_LINK 7
8a978234
LG
49
50#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
593d9e52 51#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
8a978234 52
583958fa 53/* topology context */
8a978234
LG
54struct soc_tplg {
55 const struct firmware *fw;
56
57 /* runtime FW parsing */
00ac8389 58 const u8 *pos; /* read position */
8a978234
LG
59 const u8 *hdr_pos; /* header position */
60 unsigned int pass; /* pass number */
61
62 /* component caller */
63 struct device *dev;
64 struct snd_soc_component *comp;
65 u32 index; /* current block index */
8a978234 66
88a17d8f 67 /* vendor specific kcontrol operations */
8a978234
LG
68 const struct snd_soc_tplg_kcontrol_ops *io_ops;
69 int io_ops_count;
70
1a3232d2
ML
71 /* vendor specific bytes ext handlers, for TLV bytes controls */
72 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
73 int bytes_ext_ops_count;
74
8a978234 75 /* optional fw loading callbacks to component drivers */
73444768 76 const struct snd_soc_tplg_ops *ops;
8a978234
LG
77};
78
8a978234
LG
79/* check we dont overflow the data for this control chunk */
80static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
81 unsigned int count, size_t bytes, const char *elem_type)
82{
83 const u8 *end = tplg->pos + elem_size * count;
84
85 if (end > tplg->fw->data + tplg->fw->size) {
86 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
87 elem_type);
88 return -EINVAL;
89 }
90
91 /* check there is enough room in chunk for control.
92 extra bytes at the end of control are for vendor data here */
93 if (elem_size * count > bytes) {
94 dev_err(tplg->dev,
95 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
96 elem_type, count, elem_size, bytes);
97 return -EINVAL;
98 }
99
100 return 0;
101}
102
4fad3cc6 103static inline bool soc_tplg_is_eof(struct soc_tplg *tplg)
8a978234
LG
104{
105 const u8 *end = tplg->hdr_pos;
106
107 if (end >= tplg->fw->data + tplg->fw->size)
4fad3cc6
AS
108 return true;
109 return false;
8a978234
LG
110}
111
112static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
113{
114 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
115}
116
117static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
118{
119 return (unsigned long)(tplg->pos - tplg->fw->data);
120}
121
122/* mapping of Kcontrol types and associated operations. */
123static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
124 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
125 snd_soc_put_volsw, snd_soc_info_volsw},
126 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
127 snd_soc_put_volsw_sx, NULL},
128 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
129 snd_soc_put_enum_double, snd_soc_info_enum_double},
130 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
131 snd_soc_put_enum_double, NULL},
132 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
133 snd_soc_bytes_put, snd_soc_bytes_info},
fd744256 134 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw,
7d5df968 135 snd_soc_put_volsw, snd_soc_info_volsw},
8a978234
LG
136 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
137 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
138 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
139 snd_soc_put_strobe, NULL},
140 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
2c57d478 141 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
8a978234
LG
142 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
143 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
144 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
145 snd_soc_dapm_put_enum_double, NULL},
146 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
147 snd_soc_dapm_put_enum_double, NULL},
148 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
149 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
150};
151
152struct soc_tplg_map {
153 int uid;
154 int kid;
155};
156
157/* mapping of widget types from UAPI IDs to kernel IDs */
158static const struct soc_tplg_map dapm_map[] = {
159 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
160 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
161 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
162 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
163 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
164 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
165 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
166 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
167 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
168 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
169 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
170 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
171 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
172 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
173 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
174 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
8a70b454
LG
175 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
176 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
177 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
178 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
179 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
180 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
181 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
182 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
8a978234
LG
183};
184
8f9974d9 185static int tplg_chan_get_reg(struct soc_tplg *tplg,
8a978234
LG
186 struct snd_soc_tplg_channel *chan, int map)
187{
188 int i;
189
190 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
5aebe7c7
PLB
191 if (le32_to_cpu(chan[i].id) == map)
192 return le32_to_cpu(chan[i].reg);
8a978234
LG
193 }
194
195 return -EINVAL;
196}
197
8f9974d9 198static int tplg_chan_get_shift(struct soc_tplg *tplg,
8a978234
LG
199 struct snd_soc_tplg_channel *chan, int map)
200{
201 int i;
202
203 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
5aebe7c7
PLB
204 if (le32_to_cpu(chan[i].id) == map)
205 return le32_to_cpu(chan[i].shift);
8a978234
LG
206 }
207
208 return -EINVAL;
209}
210
211static int get_widget_id(int tplg_type)
212{
213 int i;
214
215 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
216 if (tplg_type == dapm_map[i].uid)
217 return dapm_map[i].kid;
218 }
219
220 return -EINVAL;
221}
222
8a978234
LG
223static inline void soc_control_err(struct soc_tplg *tplg,
224 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
225{
226 dev_err(tplg->dev,
34b31045 227 "ASoC: no complete control IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
8a978234
LG
228 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
229 soc_tplg_get_offset(tplg));
230}
231
232/* pass vendor data to component driver for processing */
c2cbd0a7
KJ
233static int soc_tplg_vendor_load(struct soc_tplg *tplg,
234 struct snd_soc_tplg_hdr *hdr)
8a978234
LG
235{
236 int ret = 0;
237
c42464a4 238 if (tplg->ops && tplg->ops->vendor_load)
c60b613a 239 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
8a978234
LG
240 else {
241 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
242 hdr->vendor_type);
243 return -EINVAL;
244 }
245
246 if (ret < 0)
247 dev_err(tplg->dev,
248 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
249 soc_tplg_get_hdr_offset(tplg),
250 soc_tplg_get_hdr_offset(tplg),
251 hdr->type, hdr->vendor_type);
252 return ret;
253}
254
8a978234
LG
255/* optionally pass new dynamic widget to component driver. This is mainly for
256 * external widgets where we can assign private data/ops */
257static int soc_tplg_widget_load(struct soc_tplg *tplg,
258 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
259{
c42464a4 260 if (tplg->ops && tplg->ops->widget_load)
c60b613a
LG
261 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
262 tplg_w);
8a978234
LG
263
264 return 0;
265}
266
ebd259d3
LG
267/* optionally pass new dynamic widget to component driver. This is mainly for
268 * external widgets where we can assign private data/ops */
269static int soc_tplg_widget_ready(struct soc_tplg *tplg,
270 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
271{
c42464a4 272 if (tplg->ops && tplg->ops->widget_ready)
c60b613a
LG
273 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
274 tplg_w);
ebd259d3
LG
275
276 return 0;
277}
278
183b8021 279/* pass DAI configurations to component driver for extra initialization */
64527e8a 280static int soc_tplg_dai_load(struct soc_tplg *tplg,
c60b613a
LG
281 struct snd_soc_dai_driver *dai_drv,
282 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
8a978234 283{
c42464a4 284 if (tplg->ops && tplg->ops->dai_load)
c60b613a
LG
285 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
286 pcm, dai);
8a978234
LG
287
288 return 0;
289}
290
183b8021 291/* pass link configurations to component driver for extra initialization */
acfc7d46 292static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
c60b613a 293 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
acfc7d46 294{
c42464a4 295 if (tplg->ops && tplg->ops->link_load)
c60b613a 296 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
acfc7d46
ML
297
298 return 0;
299}
300
8a978234 301/* tell the component driver that all firmware has been loaded in this request */
415717e1 302static int soc_tplg_complete(struct soc_tplg *tplg)
8a978234 303{
c42464a4 304 if (tplg->ops && tplg->ops->complete)
415717e1
RS
305 return tplg->ops->complete(tplg->comp);
306
307 return 0;
8a978234
LG
308}
309
310/* add a dynamic kcontrol */
311static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
312 const struct snd_kcontrol_new *control_new, const char *prefix,
313 void *data, struct snd_kcontrol **kcontrol)
314{
315 int err;
316
317 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
318 if (*kcontrol == NULL) {
319 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
320 control_new->name);
321 return -ENOMEM;
322 }
323
324 err = snd_ctl_add(card, *kcontrol);
325 if (err < 0) {
326 dev_err(dev, "ASoC: Failed to add %s: %d\n",
327 control_new->name, err);
328 return err;
329 }
330
331 return 0;
332}
333
334/* add a dynamic kcontrol for component driver */
335static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
336 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
337{
338 struct snd_soc_component *comp = tplg->comp;
339
340 return soc_tplg_add_dcontrol(comp->card->snd_card,
2a710bb3 341 tplg->dev, k, comp->name_prefix, comp, kcontrol);
8a978234
LG
342}
343
fdfa3661
AS
344/* remove kcontrol */
345static void soc_tplg_remove_kcontrol(struct snd_soc_component *comp, struct snd_soc_dobj *dobj,
346 int pass)
8a978234
LG
347{
348 struct snd_card *card = comp->card->snd_card;
8a978234 349
5e2cd47a 350 if (pass != SOC_TPLG_PASS_CONTROL)
8a978234
LG
351 return;
352
31e92739
AS
353 if (dobj->unload)
354 dobj->unload(comp, dobj);
8a978234 355
33ae6ae2
AS
356 snd_ctl_remove(card, dobj->control.kcontrol);
357 list_del(&dobj->list);
8a978234
LG
358}
359
7df04ea7 360/* remove a route */
2abfd4bd 361static void soc_tplg_remove_route(struct snd_soc_component *comp,
7df04ea7
RS
362 struct snd_soc_dobj *dobj, int pass)
363{
7df04ea7
RS
364 if (pass != SOC_TPLG_PASS_GRAPH)
365 return;
366
31e92739
AS
367 if (dobj->unload)
368 dobj->unload(comp, dobj);
7df04ea7
RS
369
370 list_del(&dobj->list);
7df04ea7
RS
371}
372
8a978234 373/* remove a widget and it's kcontrols - routes must be removed first */
2abfd4bd 374static void soc_tplg_remove_widget(struct snd_soc_component *comp,
8a978234
LG
375 struct snd_soc_dobj *dobj, int pass)
376{
377 struct snd_card *card = comp->card->snd_card;
378 struct snd_soc_dapm_widget *w =
379 container_of(dobj, struct snd_soc_dapm_widget, dobj);
380 int i;
381
382 if (pass != SOC_TPLG_PASS_WIDGET)
383 return;
384
31e92739
AS
385 if (dobj->unload)
386 dobj->unload(comp, dobj);
8a978234 387
97a05cb4
AS
388 if (w->kcontrols)
389 for (i = 0; i < w->num_kcontrols; i++)
390 snd_ctl_remove(card, w->kcontrols[i]);
05bdcf12 391
a46e8393
AS
392 list_del(&dobj->list);
393
8a978234
LG
394 /* widget w is freed by soc-dapm.c */
395}
396
64527e8a 397/* remove DAI configurations */
2abfd4bd 398static void soc_tplg_remove_dai(struct snd_soc_component *comp,
8a978234
LG
399 struct snd_soc_dobj *dobj, int pass)
400{
64527e8a
ML
401 struct snd_soc_dai_driver *dai_drv =
402 container_of(dobj, struct snd_soc_dai_driver, dobj);
fc4cb1e1 403 struct snd_soc_dai *dai, *_dai;
64527e8a 404
8a978234
LG
405 if (pass != SOC_TPLG_PASS_PCM_DAI)
406 return;
407
31e92739
AS
408 if (dobj->unload)
409 dobj->unload(comp, dobj);
8a978234 410
fc4cb1e1 411 for_each_component_dais_safe(comp, dai, _dai)
52abe6cc 412 if (dai->driver == dai_drv)
fc4cb1e1 413 snd_soc_unregister_dai(dai);
52abe6cc 414
8a978234 415 list_del(&dobj->list);
8a978234
LG
416}
417
acfc7d46 418/* remove link configurations */
2abfd4bd 419static void soc_tplg_remove_link(struct snd_soc_component *comp,
acfc7d46
ML
420 struct snd_soc_dobj *dobj, int pass)
421{
422 struct snd_soc_dai_link *link =
423 container_of(dobj, struct snd_soc_dai_link, dobj);
424
425 if (pass != SOC_TPLG_PASS_PCM_DAI)
426 return;
427
31e92739
AS
428 if (dobj->unload)
429 dobj->unload(comp, dobj);
acfc7d46 430
dd836ddf 431 list_del(&dobj->list);
50cd9b53
KM
432 snd_soc_remove_pcm_runtime(comp->card,
433 snd_soc_get_pcm_runtime(comp->card, link));
acfc7d46
ML
434}
435
adfebb51
B
436/* unload dai link */
437static void remove_backend_link(struct snd_soc_component *comp,
438 struct snd_soc_dobj *dobj, int pass)
439{
440 if (pass != SOC_TPLG_PASS_LINK)
441 return;
442
31e92739
AS
443 if (dobj->unload)
444 dobj->unload(comp, dobj);
adfebb51
B
445
446 /*
2abfd4bd 447 * We don't free the link here as what soc_tplg_remove_link() do since BE
adfebb51
B
448 * links are not allocated by topology.
449 * We however need to reset the dobj type to its initial values
450 */
451 dobj->type = SND_SOC_DOBJ_NONE;
452 list_del(&dobj->list);
453}
454
8a978234
LG
455/* bind a kcontrol to it's IO handlers */
456static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
457 struct snd_kcontrol_new *k,
2b5cdb91 458 const struct soc_tplg *tplg)
8a978234 459{
2b5cdb91 460 const struct snd_soc_tplg_kcontrol_ops *ops;
1a3232d2 461 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
2b5cdb91 462 int num_ops, i;
8a978234 463
5aebe7c7 464 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
1a3232d2 465 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
feb00b73
AS
466 && (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ
467 || k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
1a3232d2
ML
468 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
469 struct soc_bytes_ext *sbe;
470 struct snd_soc_tplg_bytes_control *be;
471
472 sbe = (struct soc_bytes_ext *)k->private_value;
473 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
474
475 /* TLV bytes controls need standard kcontrol info handler,
476 * TLV callback and extended put/get handlers.
477 */
f4be978b 478 k->info = snd_soc_bytes_info_ext;
1a3232d2
ML
479 k->tlv.c = snd_soc_bytes_tlv_callback;
480
6788fc1a
PLB
481 /*
482 * When a topology-based implementation abuses the
483 * control interface and uses bytes_ext controls of
484 * more than 512 bytes, we need to disable the size
485 * checks, otherwise accesses to such controls will
486 * return an -EINVAL error and prevent the card from
487 * being configured.
488 */
2c7463d0 489 if (sbe->max > 512)
6788fc1a
PLB
490 k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
491
1a3232d2
ML
492 ext_ops = tplg->bytes_ext_ops;
493 num_ops = tplg->bytes_ext_ops_count;
494 for (i = 0; i < num_ops; i++) {
72bbeda0
PLB
495 if (!sbe->put &&
496 ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
1a3232d2 497 sbe->put = ext_ops[i].put;
72bbeda0
PLB
498 if (!sbe->get &&
499 ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
1a3232d2
ML
500 sbe->get = ext_ops[i].get;
501 }
502
819b9f60 503 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
1a3232d2 504 return -EINVAL;
819b9f60
D
505 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
506 return -EINVAL;
507 return 0;
1a3232d2 508 }
8a978234 509
88a17d8f 510 /* try and map vendor specific kcontrol handlers first */
2b5cdb91
ML
511 ops = tplg->io_ops;
512 num_ops = tplg->io_ops_count;
8a978234
LG
513 for (i = 0; i < num_ops; i++) {
514
72bbeda0 515 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
8a978234 516 k->put = ops[i].put;
72bbeda0 517 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
8a978234 518 k->get = ops[i].get;
72bbeda0 519 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
8a978234
LG
520 k->info = ops[i].info;
521 }
522
88a17d8f 523 /* vendor specific handlers found ? */
8a978234
LG
524 if (k->put && k->get && k->info)
525 return 0;
526
88a17d8f 527 /* none found so try standard kcontrol handlers */
2b5cdb91
ML
528 ops = io_ops;
529 num_ops = ARRAY_SIZE(io_ops);
88a17d8f 530 for (i = 0; i < num_ops; i++) {
8a978234 531
72bbeda0 532 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
88a17d8f 533 k->put = ops[i].put;
72bbeda0 534 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
88a17d8f 535 k->get = ops[i].get;
72bbeda0 536 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
88a17d8f 537 k->info = ops[i].info;
8a978234
LG
538 }
539
88a17d8f 540 /* standard handlers found ? */
8a978234
LG
541 if (k->put && k->get && k->info)
542 return 0;
543
544 /* nothing to bind */
545 return -EINVAL;
546}
547
548/* bind a widgets to it's evnt handlers */
549int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
550 const struct snd_soc_tplg_widget_events *events,
551 int num_events, u16 event_type)
552{
553 int i;
554
555 w->event = NULL;
556
557 for (i = 0; i < num_events; i++) {
558 if (event_type == events[i].type) {
559
560 /* found - so assign event */
561 w->event = events[i].event_handler;
562 return 0;
563 }
564 }
565
566 /* not found */
567 return -EINVAL;
568}
569EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
570
571/* optionally pass new dynamic kcontrol to component driver. */
430791dd 572static int soc_tplg_control_load(struct soc_tplg *tplg,
8a978234
LG
573 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
574{
ec5dffcd
AS
575 int ret = 0;
576
c42464a4 577 if (tplg->ops && tplg->ops->control_load)
ec5dffcd 578 ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr);
8a978234 579
ec5dffcd
AS
580 if (ret)
581 dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name);
582
583 return ret;
8a978234
LG
584}
585
8a978234 586
28a87eeb
ML
587static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
588 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
589{
590 unsigned int item_len = 2 * sizeof(unsigned int);
591 unsigned int *p;
8a978234 592
ff922622 593 p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
28a87eeb 594 if (!p)
8a978234
LG
595 return -ENOMEM;
596
28a87eeb
ML
597 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
598 p[1] = item_len;
5aebe7c7
PLB
599 p[2] = le32_to_cpu(scale->min);
600 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
601 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
28a87eeb
ML
602
603 kc->tlv.p = (void *)p;
604 return 0;
605}
606
607static int soc_tplg_create_tlv(struct soc_tplg *tplg,
608 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
609{
610 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
5aebe7c7 611 u32 access = le32_to_cpu(tc->access);
28a87eeb 612
5aebe7c7 613 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
28a87eeb 614 return 0;
8a978234 615
5aebe7c7 616 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
28a87eeb 617 tplg_tlv = &tc->tlv;
5aebe7c7 618 switch (le32_to_cpu(tplg_tlv->type)) {
28a87eeb
ML
619 case SNDRV_CTL_TLVT_DB_SCALE:
620 return soc_tplg_create_tlv_db_scale(tplg, kc,
621 &tplg_tlv->scale);
622
623 /* TODO: add support for other TLV types */
624 default:
625 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
626 tplg_tlv->type);
627 return -EINVAL;
628 }
629 }
8a978234
LG
630
631 return 0;
632}
633
c2dad0db
AS
634static int soc_tplg_control_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
635{
636 struct snd_soc_tplg_mixer_control *mc;
637 struct soc_mixer_control *sm;
638 int err;
639
640 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
641
642 /* validate kcontrol */
643 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
644 return -EINVAL;
645
646 sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
647 if (!sm)
648 return -ENOMEM;
649
650 tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) + le32_to_cpu(mc->priv.size);
651
652 dev_dbg(tplg->dev, "ASoC: adding mixer kcontrol %s with access 0x%x\n",
653 mc->hdr.name, mc->hdr.access);
654
655 kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL);
656 if (!kc->name)
657 return -ENOMEM;
658 kc->private_value = (long)sm;
659 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
660 kc->access = le32_to_cpu(mc->hdr.access);
661
662 /* we only support FL/FR channel mapping atm */
663 sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL);
664 sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR);
665 sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL);
666 sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR);
667
668 sm->max = le32_to_cpu(mc->max);
669 sm->min = le32_to_cpu(mc->min);
670 sm->invert = le32_to_cpu(mc->invert);
671 sm->platform_max = le32_to_cpu(mc->platform_max);
81eb3a2b 672 sm->num_channels = le32_to_cpu(mc->num_channels);
c2dad0db
AS
673
674 /* map io handlers */
675 err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg);
676 if (err) {
677 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
678 return err;
679 }
680
681 /* create any TLV data */
682 err = soc_tplg_create_tlv(tplg, kc, &mc->hdr);
683 if (err < 0) {
684 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name);
685 return err;
686 }
687
688 /* pass control to driver for optional further init */
689 return soc_tplg_control_load(tplg, kc, &mc->hdr);
690}
691
692static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se,
693 struct snd_soc_tplg_enum_control *ec)
694{
695 int i, ret;
696
697 if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts))
698 return -EINVAL;
699
700 se->dobj.control.dtexts =
701 devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
702 if (se->dobj.control.dtexts == NULL)
703 return -ENOMEM;
704
705 for (i = 0; i < le32_to_cpu(ec->items); i++) {
706
707 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
708 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
709 ret = -EINVAL;
710 goto err;
711 }
712
713 se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL);
714 if (!se->dobj.control.dtexts[i]) {
715 ret = -ENOMEM;
716 goto err;
717 }
718 }
719
720 se->items = le32_to_cpu(ec->items);
721 se->texts = (const char * const *)se->dobj.control.dtexts;
722 return 0;
723
724err:
725 return ret;
726}
727
728static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se,
729 struct snd_soc_tplg_enum_control *ec)
730{
731 int i;
732
733 /*
734 * Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS
735 * values instead of using ARRAY_SIZE(ec->values) due to the fact that
736 * it is oversized for its purpose. Additionally it is done so because
737 * it is defined in UAPI header where it can't be easily changed.
738 */
739 if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS)
740 return -EINVAL;
741
742 se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items),
743 sizeof(*se->dobj.control.dvalues),
744 GFP_KERNEL);
745 if (!se->dobj.control.dvalues)
746 return -ENOMEM;
747
748 /* convert from little-endian */
749 for (i = 0; i < le32_to_cpu(ec->items); i++) {
750 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
751 }
752
753 se->items = le32_to_cpu(ec->items);
754 se->values = (const unsigned int *)se->dobj.control.dvalues;
755 return 0;
756}
757
758static int soc_tplg_control_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
759{
760 struct snd_soc_tplg_enum_control *ec;
761 struct soc_enum *se;
762 int err;
763
764 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
765
766 /* validate kcontrol */
767 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
768 return -EINVAL;
769
770 se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL);
771 if (!se)
772 return -ENOMEM;
773
774 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + le32_to_cpu(ec->priv.size));
775
776 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", ec->hdr.name, ec->items);
777
778 kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL);
779 if (!kc->name)
780 return -ENOMEM;
781 kc->private_value = (long)se;
782 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
783 kc->access = le32_to_cpu(ec->hdr.access);
784
785 /* we only support FL/FR channel mapping atm */
786 se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
787 se->shift_l = tplg_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
788 se->shift_r = tplg_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
789
790 se->mask = le32_to_cpu(ec->mask);
791
792 switch (le32_to_cpu(ec->hdr.ops.info)) {
793 case SND_SOC_TPLG_CTL_ENUM_VALUE:
794 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
795 err = soc_tplg_denum_create_values(tplg, se, ec);
796 if (err < 0) {
797 dev_err(tplg->dev, "ASoC: could not create values for %s\n", ec->hdr.name);
798 return err;
799 }
800 fallthrough;
801 case SND_SOC_TPLG_CTL_ENUM:
802 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
803 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
804 err = soc_tplg_denum_create_texts(tplg, se, ec);
805 if (err < 0) {
806 dev_err(tplg->dev, "ASoC: could not create texts for %s\n", ec->hdr.name);
807 return err;
808 }
809 break;
810 default:
811 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
812 ec->hdr.ops.info, ec->hdr.name);
813 return -EINVAL;
814 }
815
816 /* map io handlers */
817 err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
818 if (err) {
819 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
820 return err;
821 }
822
823 /* pass control to driver for optional further init */
824 return soc_tplg_control_load(tplg, kc, &ec->hdr);
825}
826
827static int soc_tplg_control_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
828{
829 struct snd_soc_tplg_bytes_control *be;
830 struct soc_bytes_ext *sbe;
831 int err;
832
833 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
834
835 /* validate kcontrol */
836 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
837 return -EINVAL;
838
839 sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
840 if (!sbe)
841 return -ENOMEM;
842
843 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + le32_to_cpu(be->priv.size));
844
845 dev_dbg(tplg->dev, "ASoC: adding bytes kcontrol %s with access 0x%x\n",
846 be->hdr.name, be->hdr.access);
847
848 kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL);
849 if (!kc->name)
850 return -ENOMEM;
851 kc->private_value = (long)sbe;
852 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
853 kc->access = le32_to_cpu(be->hdr.access);
854
855 sbe->max = le32_to_cpu(be->max);
856
857 /* map standard io handlers and check for external handlers */
858 err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg);
859 if (err) {
860 soc_control_err(tplg, &be->hdr, be->hdr.name);
861 return err;
862 }
863
864 /* pass control to driver for optional further init */
865 return soc_tplg_control_load(tplg, kc, &be->hdr);
866}
867
0db627c4 868static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size)
8a978234 869{
08672782 870 struct snd_kcontrol_new kc = {0};
8a978234 871 struct soc_bytes_ext *sbe;
08672782 872 int ret;
8a978234
LG
873
874 if (soc_tplg_check_elem_count(tplg,
3ce57f22 875 sizeof(struct snd_soc_tplg_bytes_control),
0db627c4 876 1, size, "mixer bytes"))
8a978234 877 return -EINVAL;
8a978234 878
08672782
AS
879 ret = soc_tplg_control_dbytes_create(tplg, &kc);
880 if (ret)
881 return ret;
0db627c4 882
08672782 883 /* register dynamic object */
85109780 884 sbe = (struct soc_bytes_ext *)kc.private_value;
f14a33fc
AS
885
886 INIT_LIST_HEAD(&sbe->dobj.list);
0db627c4 887 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
f14a33fc 888 sbe->dobj.index = tplg->index;
31e92739
AS
889 if (tplg->ops)
890 sbe->dobj.unload = tplg->ops->control_unload;
0db627c4 891
08672782 892 /* create control directly */
0db627c4 893 ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol);
2316c11f 894 if (ret < 0)
08672782 895 return ret;
8a978234 896
0db627c4
AS
897 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
898
0db627c4 899 return ret;
8a978234
LG
900}
901
0db627c4 902static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size)
8a978234 903{
4654ca7c 904 struct snd_kcontrol_new kc = {0};
8a978234 905 struct soc_mixer_control *sm;
4654ca7c 906 int ret;
8a978234
LG
907
908 if (soc_tplg_check_elem_count(tplg,
3ce57f22 909 sizeof(struct snd_soc_tplg_mixer_control),
0db627c4 910 1, size, "mixers"))
8a978234 911 return -EINVAL;
8a978234 912
4654ca7c
AS
913 ret = soc_tplg_control_dmixer_create(tplg, &kc);
914 if (ret)
915 return ret;
0db627c4 916
4654ca7c 917 /* register dynamic object */
85109780 918 sm = (struct soc_mixer_control *)kc.private_value;
f14a33fc
AS
919
920 INIT_LIST_HEAD(&sm->dobj.list);
0db627c4 921 sm->dobj.type = SND_SOC_DOBJ_MIXER;
f14a33fc 922 sm->dobj.index = tplg->index;
31e92739
AS
923 if (tplg->ops)
924 sm->dobj.unload = tplg->ops->control_unload;
0db627c4 925
4654ca7c 926 /* create control directly */
0db627c4 927 ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol);
2316c11f 928 if (ret < 0)
4654ca7c 929 return ret;
8a978234 930
0db627c4
AS
931 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
932
0db627c4 933 return ret;
8a978234
LG
934}
935
0db627c4 936static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size)
8a978234 937{
8f2942b9 938 struct snd_kcontrol_new kc = {0};
8a978234 939 struct soc_enum *se;
8f2942b9 940 int ret;
8a978234
LG
941
942 if (soc_tplg_check_elem_count(tplg,
3ce57f22 943 sizeof(struct snd_soc_tplg_enum_control),
0db627c4 944 1, size, "enums"))
8a978234 945 return -EINVAL;
8a978234 946
8f2942b9
AS
947 ret = soc_tplg_control_denum_create(tplg, &kc);
948 if (ret)
949 return ret;
0db627c4 950
8f2942b9
AS
951 /* register dynamic object */
952 se = (struct soc_enum *)kc.private_value;
f14a33fc
AS
953
954 INIT_LIST_HEAD(&se->dobj.list);
0db627c4 955 se->dobj.type = SND_SOC_DOBJ_ENUM;
f14a33fc 956 se->dobj.index = tplg->index;
31e92739
AS
957 if (tplg->ops)
958 se->dobj.unload = tplg->ops->control_unload;
0db627c4 959
8f2942b9 960 /* create control directly */
0db627c4 961 ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol);
2316c11f 962 if (ret < 0)
8f2942b9 963 return ret;
0db627c4
AS
964
965 list_add(&se->dobj.list, &tplg->comp->dobj_list);
966
0db627c4 967 return ret;
8a978234
LG
968}
969
970static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
971 struct snd_soc_tplg_hdr *hdr)
972{
2ae548f3 973 int ret;
8a978234
LG
974 int i;
975
8a978234
LG
976 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
977 soc_tplg_get_offset(tplg));
978
5aebe7c7 979 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
ea8f6b29 980 struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
8a978234 981
5aebe7c7 982 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
06eb49f7
ML
983 dev_err(tplg->dev, "ASoC: invalid control size\n");
984 return -EINVAL;
985 }
986
758beab0
CR
987 switch (le32_to_cpu(control_hdr->type)) {
988 case SND_SOC_TPLG_TYPE_MIXER:
0db627c4 989 ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size));
8a978234 990 break;
758beab0 991 case SND_SOC_TPLG_TYPE_ENUM:
0db627c4 992 ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size));
8a978234 993 break;
758beab0 994 case SND_SOC_TPLG_TYPE_BYTES:
0db627c4 995 ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size));
8a978234
LG
996 break;
997 default:
758beab0
CR
998 ret = -EINVAL;
999 break;
8a978234 1000 }
758beab0 1001
2ae548f3 1002 if (ret < 0) {
758beab0
CR
1003 dev_err(tplg->dev, "ASoC: invalid control type: %d, index: %d at 0x%lx\n",
1004 control_hdr->type, i, soc_tplg_get_offset(tplg));
2ae548f3
AS
1005 return ret;
1006 }
8a978234
LG
1007 }
1008
1009 return 0;
1010}
1011
503e79b7
LG
1012/* optionally pass new dynamic kcontrol to component driver. */
1013static int soc_tplg_add_route(struct soc_tplg *tplg,
1014 struct snd_soc_dapm_route *route)
1015{
c42464a4 1016 if (tplg->ops && tplg->ops->dapm_route_load)
503e79b7
LG
1017 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1018 route);
1019
1020 return 0;
1021}
1022
8a978234
LG
1023static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1024 struct snd_soc_tplg_hdr *hdr)
1025{
1026 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
e0e7bc2c 1027 const size_t maxlen = SNDRV_CTL_ELEM_ID_NAME_MAXLEN;
8a978234 1028 struct snd_soc_tplg_dapm_graph_elem *elem;
cc44c749 1029 struct snd_soc_dapm_route *route;
ff922622 1030 int count, i;
7df04ea7 1031 int ret = 0;
8a978234 1032
5aebe7c7
PLB
1033 count = le32_to_cpu(hdr->count);
1034
8a978234 1035 if (soc_tplg_check_elem_count(tplg,
3ce57f22
AS
1036 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1037 count, le32_to_cpu(hdr->payload_size), "graph"))
8a978234 1038 return -EINVAL;
8a978234 1039
b75a6511
LG
1040 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1041 hdr->index);
8a978234 1042
7df04ea7 1043 for (i = 0; i < count; i++) {
cc44c749
AS
1044 route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL);
1045 if (!route)
7df04ea7 1046 return -ENOMEM;
8a978234
LG
1047 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1048 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1049
1050 /* validate routes */
e0e7bc2c
AS
1051 if ((strnlen(elem->source, maxlen) == maxlen) ||
1052 (strnlen(elem->sink, maxlen) == maxlen) ||
1053 (strnlen(elem->control, maxlen) == maxlen)) {
7df04ea7
RS
1054 ret = -EINVAL;
1055 break;
1056 }
7df04ea7 1057
0298f516
AS
1058 route->source = devm_kstrdup(tplg->dev, elem->source, GFP_KERNEL);
1059 route->sink = devm_kstrdup(tplg->dev, elem->sink, GFP_KERNEL);
97ab304e
AS
1060 if (!route->source || !route->sink) {
1061 ret = -ENOMEM;
7df04ea7
RS
1062 break;
1063 }
1064
e0e7bc2c 1065 if (strnlen(elem->control, maxlen) != 0) {
0298f516 1066 route->control = devm_kstrdup(tplg->dev, elem->control, GFP_KERNEL);
97ab304e
AS
1067 if (!route->control) {
1068 ret = -ENOMEM;
1069 break;
1070 }
1071 }
7df04ea7
RS
1072
1073 /* add route dobj to dobj_list */
cc44c749 1074 route->dobj.type = SND_SOC_DOBJ_GRAPH;
31e92739 1075 if (tplg->ops)
dd184c40 1076 route->dobj.unload = tplg->ops->dapm_route_unload;
cc44c749
AS
1077 route->dobj.index = tplg->index;
1078 list_add(&route->dobj.list, &tplg->comp->dobj_list);
8a978234 1079
cc44c749 1080 ret = soc_tplg_add_route(tplg, route);
6f0307df 1081 if (ret < 0) {
8bf9475f 1082 dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
6856e887 1083 break;
6f0307df 1084 }
503e79b7 1085
6974857c 1086 ret = snd_soc_dapm_add_routes(dapm, route, 1);
dd239516
KM
1087 if (ret)
1088 break;
8a978234
LG
1089 }
1090
7df04ea7 1091 return ret;
8a978234
LG
1092}
1093
8a978234
LG
1094static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1095 struct snd_soc_tplg_dapm_widget *w)
1096{
1097 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1098 struct snd_soc_dapm_widget template, *widget;
1099 struct snd_soc_tplg_ctl_hdr *control_hdr;
1100 struct snd_soc_card *card = tplg->comp->card;
b9c035aa 1101 unsigned int *kcontrol_type = NULL;
d29d41e2
JU
1102 struct snd_kcontrol_new *kc;
1103 int mixer_count = 0;
1104 int bytes_count = 0;
1105 int enum_count = 0;
8a978234 1106 int ret = 0;
d29d41e2 1107 int i;
8a978234
LG
1108
1109 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1110 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1111 return -EINVAL;
1112 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1113 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1114 return -EINVAL;
1115
1116 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1117 w->name, w->id);
1118
1119 memset(&template, 0, sizeof(template));
1120
1121 /* map user to kernel widget ID */
5aebe7c7 1122 template.id = get_widget_id(le32_to_cpu(w->id));
752c938a 1123 if ((int)template.id < 0)
8a978234
LG
1124 return template.id;
1125
c3421a6a 1126 /* strings are allocated here, but used and freed by the widget */
8a978234
LG
1127 template.name = kstrdup(w->name, GFP_KERNEL);
1128 if (!template.name)
1129 return -ENOMEM;
1130 template.sname = kstrdup(w->sname, GFP_KERNEL);
1131 if (!template.sname) {
1132 ret = -ENOMEM;
1133 goto err;
1134 }
5aebe7c7
PLB
1135 template.reg = le32_to_cpu(w->reg);
1136 template.shift = le32_to_cpu(w->shift);
1137 template.mask = le32_to_cpu(w->mask);
1138 template.subseq = le32_to_cpu(w->subseq);
8a978234
LG
1139 template.on_val = w->invert ? 0 : 1;
1140 template.off_val = w->invert ? 1 : 0;
5aebe7c7
PLB
1141 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1142 template.event_flags = le16_to_cpu(w->event_flags);
8a978234
LG
1143 template.dobj.index = tplg->index;
1144
1145 tplg->pos +=
5aebe7c7
PLB
1146 (sizeof(struct snd_soc_tplg_dapm_widget) +
1147 le32_to_cpu(w->priv.size));
1148
8a978234
LG
1149 if (w->num_kcontrols == 0) {
1150 template.num_kcontrols = 0;
1151 goto widget;
1152 }
1153
d29d41e2
JU
1154 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1155 kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL);
c173ee5b
AS
1156 if (!kc) {
1157 ret = -ENOMEM;
9c363532 1158 goto hdr_err;
c173ee5b 1159 }
d29d41e2
JU
1160
1161 kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int),
1162 GFP_KERNEL);
c173ee5b
AS
1163 if (!kcontrol_type) {
1164 ret = -ENOMEM;
9c363532 1165 goto hdr_err;
c173ee5b 1166 }
d29d41e2 1167
1baad7da 1168 for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) {
d29d41e2 1169 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
758beab0
CR
1170
1171 switch (le32_to_cpu(control_hdr->type)) {
1172 case SND_SOC_TPLG_TYPE_MIXER:
d29d41e2
JU
1173 /* volume mixer */
1174 kc[i].index = mixer_count;
1175 kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER;
1176 mixer_count++;
76d8aa0f 1177 ret = soc_tplg_control_dmixer_create(tplg, &kc[i]);
d29d41e2
JU
1178 if (ret < 0)
1179 goto hdr_err;
1180 break;
758beab0 1181 case SND_SOC_TPLG_TYPE_ENUM:
d29d41e2
JU
1182 /* enumerated mixer */
1183 kc[i].index = enum_count;
1184 kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM;
1185 enum_count++;
76d8aa0f 1186 ret = soc_tplg_control_denum_create(tplg, &kc[i]);
d29d41e2
JU
1187 if (ret < 0)
1188 goto hdr_err;
1189 break;
758beab0 1190 case SND_SOC_TPLG_TYPE_BYTES:
d29d41e2
JU
1191 /* bytes control */
1192 kc[i].index = bytes_count;
1193 kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES;
1194 bytes_count++;
76d8aa0f 1195 ret = soc_tplg_control_dbytes_create(tplg, &kc[i]);
d29d41e2
JU
1196 if (ret < 0)
1197 goto hdr_err;
1198 break;
1199 default:
1200 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1201 control_hdr->ops.get, control_hdr->ops.put,
1202 le32_to_cpu(control_hdr->ops.info));
1203 ret = -EINVAL;
8a978234
LG
1204 goto hdr_err;
1205 }
8a978234
LG
1206 }
1207
d29d41e2 1208 template.kcontrol_news = kc;
8facf84b
PU
1209 dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n",
1210 w->name, mixer_count, enum_count, bytes_count);
d29d41e2 1211
8a978234
LG
1212widget:
1213 ret = soc_tplg_widget_load(tplg, &template, w);
1214 if (ret < 0)
1215 goto hdr_err;
1216
1217 /* card dapm mutex is held by the core if we are loading topology
1218 * data during sound card init. */
2b34c135 1219 if (snd_soc_card_is_instantiated(card))
8a978234
LG
1220 widget = snd_soc_dapm_new_control(dapm, &template);
1221 else
1222 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
37e1df8c
LW
1223 if (IS_ERR(widget)) {
1224 ret = PTR_ERR(widget);
8a978234
LG
1225 goto hdr_err;
1226 }
1227
1228 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
eea3dd4f 1229 widget->dobj.widget.kcontrol_type = kcontrol_type;
31e92739
AS
1230 if (tplg->ops)
1231 widget->dobj.unload = tplg->ops->widget_unload;
8a978234
LG
1232 widget->dobj.index = tplg->index;
1233 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
ebd259d3
LG
1234
1235 ret = soc_tplg_widget_ready(tplg, widget, w);
1236 if (ret < 0)
1237 goto ready_err;
1238
7620fe91
B
1239 kfree(template.sname);
1240 kfree(template.name);
1241
8a978234
LG
1242 return 0;
1243
ebd259d3 1244ready_err:
2abfd4bd 1245 soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET);
ebd259d3 1246 snd_soc_dapm_free_widget(widget);
8a978234
LG
1247hdr_err:
1248 kfree(template.sname);
1249err:
1250 kfree(template.name);
1251 return ret;
1252}
1253
1254static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1255 struct snd_soc_tplg_hdr *hdr)
1256{
e9aa139f 1257 int count, i;
5aebe7c7
PLB
1258
1259 count = le32_to_cpu(hdr->count);
8a978234 1260
8a978234
LG
1261 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1262
1263 for (i = 0; i < count; i++) {
e9aa139f
KM
1264 struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1265 int ret;
1266
2e288333
AS
1267 /*
1268 * check if widget itself fits within topology file
1269 * use sizeof instead of widget->size, as we can't be sure
1270 * it is set properly yet (file may end before it is present)
1271 */
1272 if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) {
1273 dev_err(tplg->dev, "ASoC: invalid widget data size\n");
1274 return -EINVAL;
1275 }
1276
1277 /* check if widget has proper size */
5aebe7c7 1278 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
06eb49f7
ML
1279 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1280 return -EINVAL;
1281 }
1282
2e288333
AS
1283 /* check if widget private data fits within topology file */
1284 if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) {
1285 dev_err(tplg->dev, "ASoC: invalid widget private data size\n");
1286 return -EINVAL;
1287 }
1288
8a978234 1289 ret = soc_tplg_dapm_widget_create(tplg, widget);
7de76b62 1290 if (ret < 0) {
8a978234
LG
1291 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1292 widget->name);
7de76b62
ML
1293 return ret;
1294 }
8a978234
LG
1295 }
1296
1297 return 0;
1298}
1299
1300static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1301{
1302 struct snd_soc_card *card = tplg->comp->card;
1303 int ret;
1304
1305 /* Card might not have been registered at this point.
1306 * If so, just return success.
1307 */
2b34c135 1308 if (!snd_soc_card_is_instantiated(card)) {
53085402 1309 dev_warn(tplg->dev, "ASoC: Parent card not yet available, widget card binding deferred\n");
8a978234
LG
1310 return 0;
1311 }
1312
1313 ret = snd_soc_dapm_new_widgets(card);
1314 if (ret < 0)
53085402 1315 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", ret);
8a978234 1316
b784617a 1317 return ret;
8a978234
LG
1318}
1319
ff922622
AS
1320static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream,
1321 struct snd_soc_tplg_stream_caps *caps)
b6b6e4d6 1322{
ff922622 1323 stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL);
abc3caac
AS
1324 if (!stream->stream_name)
1325 return -ENOMEM;
1326
5aebe7c7
PLB
1327 stream->channels_min = le32_to_cpu(caps->channels_min);
1328 stream->channels_max = le32_to_cpu(caps->channels_max);
1329 stream->rates = le32_to_cpu(caps->rates);
1330 stream->rate_min = le32_to_cpu(caps->rate_min);
1331 stream->rate_max = le32_to_cpu(caps->rate_max);
1332 stream->formats = le64_to_cpu(caps->formats);
1333 stream->sig_bits = le32_to_cpu(caps->sig_bits);
abc3caac
AS
1334
1335 return 0;
b6b6e4d6
ML
1336}
1337
0038be9a
ML
1338static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1339 unsigned int flag_mask, unsigned int flags)
1340{
1341 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
f14654dd 1342 dai_drv->symmetric_rate =
47108a61 1343 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
0038be9a
ML
1344
1345 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1346 dai_drv->symmetric_channels =
47108a61 1347 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ?
0038be9a
ML
1348 1 : 0;
1349
1350 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
f14654dd 1351 dai_drv->symmetric_sample_bits =
47108a61 1352 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
0038be9a
ML
1353 1 : 0;
1354}
1355
80585b0c
KM
1356static const struct snd_soc_dai_ops tplg_dai_ops = {
1357 .compress_new = snd_soc_new_compress,
1358};
1359
64527e8a
ML
1360static int soc_tplg_dai_create(struct soc_tplg *tplg,
1361 struct snd_soc_tplg_pcm *pcm)
1362{
1363 struct snd_soc_dai_driver *dai_drv;
1364 struct snd_soc_pcm_stream *stream;
1365 struct snd_soc_tplg_stream_caps *caps;
e443c205
KM
1366 struct snd_soc_dai *dai;
1367 struct snd_soc_dapm_context *dapm =
1368 snd_soc_component_get_dapm(tplg->comp);
64527e8a
ML
1369 int ret;
1370
ff922622 1371 dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
64527e8a
ML
1372 if (dai_drv == NULL)
1373 return -ENOMEM;
1374
abc3caac 1375 if (strlen(pcm->dai_name)) {
ff922622 1376 dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
abc3caac
AS
1377 if (!dai_drv->name) {
1378 ret = -ENOMEM;
1379 goto err;
1380 }
1381 }
5aebe7c7 1382 dai_drv->id = le32_to_cpu(pcm->dai_id);
64527e8a
ML
1383
1384 if (pcm->playback) {
1385 stream = &dai_drv->playback;
1386 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
ff922622 1387 ret = set_stream_info(tplg, stream, caps);
abc3caac
AS
1388 if (ret < 0)
1389 goto err;
64527e8a
ML
1390 }
1391
1392 if (pcm->capture) {
1393 stream = &dai_drv->capture;
1394 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
ff922622 1395 ret = set_stream_info(tplg, stream, caps);
abc3caac
AS
1396 if (ret < 0)
1397 goto err;
64527e8a
ML
1398 }
1399
5db6aab6 1400 if (pcm->compress)
80585b0c 1401 dai_drv->ops = &tplg_dai_ops;
5db6aab6 1402
64527e8a 1403 /* pass control to component driver for optional further init */
c60b613a 1404 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
64527e8a 1405 if (ret < 0) {
e59db12b 1406 dev_err(tplg->dev, "ASoC: DAI loading failed\n");
abc3caac 1407 goto err;
64527e8a
ML
1408 }
1409
1410 dai_drv->dobj.index = tplg->index;
64527e8a 1411 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
31e92739
AS
1412 if (tplg->ops)
1413 dai_drv->dobj.unload = tplg->ops->dai_unload;
64527e8a
ML
1414 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1415
1416 /* register the DAI to the component */
fc4cb1e1 1417 dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
e443c205
KM
1418 if (!dai)
1419 return -ENOMEM;
1420
1421 /* Create the DAI widgets here */
1422 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1423 if (ret != 0) {
1424 dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
fc4cb1e1 1425 snd_soc_unregister_dai(dai);
e443c205
KM
1426 return ret;
1427 }
1428
abc3caac
AS
1429 return 0;
1430
1431err:
e443c205 1432 return ret;
64527e8a
ML
1433}
1434
717a8e72
ML
1435static void set_link_flags(struct snd_soc_dai_link *link,
1436 unsigned int flag_mask, unsigned int flags)
1437{
1438 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
f14654dd 1439 link->symmetric_rate =
47108a61 1440 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
717a8e72
ML
1441
1442 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1443 link->symmetric_channels =
47108a61 1444 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ?
717a8e72
ML
1445 1 : 0;
1446
1447 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
f14654dd 1448 link->symmetric_sample_bits =
47108a61 1449 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
717a8e72 1450 1 : 0;
6ff67cca
ML
1451
1452 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1453 link->ignore_suspend =
47108a61
PLB
1454 (flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ?
1455 1 : 0;
717a8e72
ML
1456}
1457
67d1c21e 1458/* create the FE DAI link */
ab4bc5ee 1459static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
acfc7d46
ML
1460 struct snd_soc_tplg_pcm *pcm)
1461{
1462 struct snd_soc_dai_link *link;
23b946ce 1463 struct snd_soc_dai_link_component *dlc;
acfc7d46
ML
1464 int ret;
1465
6a7c51b4
KM
1466 /* link + cpu + codec + platform */
1467 link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
acfc7d46
ML
1468 if (link == NULL)
1469 return -ENOMEM;
1470
23b946ce
KM
1471 dlc = (struct snd_soc_dai_link_component *)(link + 1);
1472
1473 link->cpus = &dlc[0];
23b946ce 1474 link->num_cpus = 1;
23b946ce 1475
8ce1cbd6 1476 link->dobj.index = tplg->index;
8ce1cbd6 1477 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
31e92739
AS
1478 if (tplg->ops)
1479 link->dobj.unload = tplg->ops->link_unload;
8ce1cbd6 1480
8f27c4ab 1481 if (strlen(pcm->pcm_name)) {
ff922622
AS
1482 link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1483 link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
abc3caac
AS
1484 if (!link->name || !link->stream_name) {
1485 ret = -ENOMEM;
1486 goto err;
1487 }
8f27c4ab 1488 }
5aebe7c7 1489 link->id = le32_to_cpu(pcm->pcm_id);
acfc7d46 1490
abc3caac 1491 if (strlen(pcm->dai_name)) {
ff922622 1492 link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
abc3caac
AS
1493 if (!link->cpus->dai_name) {
1494 ret = -ENOMEM;
1495 goto err;
1496 }
1497 }
8f27c4ab 1498
6a7c51b4 1499 /*
5a7bec81
KM
1500 * Many topology are assuming link has Codec / Platform, and
1501 * these might be overwritten at soc_tplg_dai_link_load().
8bfbdb18 1502 * Don't use &snd_soc_dummy_dlc here.
6a7c51b4 1503 */
8bfbdb18 1504 link->codecs = &dlc[1]; /* Don't use &snd_soc_dummy_dlc here */
5a7bec81
KM
1505 link->codecs->name = "snd-soc-dummy";
1506 link->codecs->dai_name = "snd-soc-dummy-dai";
1507 link->num_codecs = 1;
1508
8bfbdb18 1509 link->platforms = &dlc[2]; /* Don't use &snd_soc_dummy_dlc here */
5a7bec81
KM
1510 link->platforms->name = "snd-soc-dummy";
1511 link->num_platforms = 1;
6a7c51b4 1512
67d1c21e
GS
1513 /* enable DPCM */
1514 link->dynamic = 1;
c403dcd8 1515 link->ignore_pmdown_time = 1;
44b6f240
KM
1516 link->playback_only = le32_to_cpu(pcm->playback) && !le32_to_cpu(pcm->capture);
1517 link->capture_only = !le32_to_cpu(pcm->playback) && le32_to_cpu(pcm->capture);
717a8e72 1518 if (pcm->flag_mask)
5aebe7c7
PLB
1519 set_link_flags(link,
1520 le32_to_cpu(pcm->flag_mask),
1521 le32_to_cpu(pcm->flags));
67d1c21e 1522
acfc7d46 1523 /* pass control to component driver for optional further init */
c60b613a 1524 ret = soc_tplg_dai_link_load(tplg, link, NULL);
acfc7d46 1525 if (ret < 0) {
e59db12b 1526 dev_err(tplg->dev, "ASoC: FE link loading failed\n");
76d27036
DT
1527 goto err;
1528 }
1529
ffaf886e 1530 ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1);
76d27036 1531 if (ret < 0) {
b6c3bdda
JH
1532 if (ret != -EPROBE_DEFER)
1533 dev_err(tplg->dev, "ASoC: adding FE link failed\n");
76d27036 1534 goto err;
acfc7d46
ML
1535 }
1536
acfc7d46
ML
1537 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1538
acfc7d46 1539 return 0;
76d27036 1540err:
76d27036 1541 return ret;
acfc7d46
ML
1542}
1543
1544/* create a FE DAI and DAI link from the PCM object */
64527e8a
ML
1545static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1546 struct snd_soc_tplg_pcm *pcm)
1547{
acfc7d46
ML
1548 int ret;
1549
1550 ret = soc_tplg_dai_create(tplg, pcm);
1551 if (ret < 0)
1552 return ret;
1553
ab4bc5ee 1554 return soc_tplg_fe_link_create(tplg, pcm);
64527e8a
ML
1555}
1556
1557static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
8a978234
LG
1558 struct snd_soc_tplg_hdr *hdr)
1559{
82c19254 1560 struct snd_soc_tplg_pcm *pcm;
5aebe7c7
PLB
1561 int count;
1562 int size;
fd340455 1563 int i;
a3039aef 1564 int ret;
8a978234 1565
5aebe7c7
PLB
1566 count = le32_to_cpu(hdr->count);
1567
55726dc9
ML
1568 /* check the element size and count */
1569 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
5aebe7c7 1570 size = le32_to_cpu(pcm->size);
c57468dc 1571 if (size > sizeof(struct snd_soc_tplg_pcm)) {
55726dc9 1572 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
5aebe7c7 1573 size);
55726dc9
ML
1574 return -EINVAL;
1575 }
1576
8a978234 1577 if (soc_tplg_check_elem_count(tplg,
5aebe7c7
PLB
1578 size, count,
1579 le32_to_cpu(hdr->payload_size),
3ce57f22 1580 "PCM DAI"))
8a978234 1581 return -EINVAL;
8a978234 1582
64527e8a 1583 for (i = 0; i < count; i++) {
55726dc9 1584 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
5aebe7c7 1585 size = le32_to_cpu(pcm->size);
55726dc9
ML
1586
1587 /* check ABI version by size, create a new version of pcm
1588 * if abi not match.
1589 */
c57468dc
CR
1590 if (size != sizeof(*pcm))
1591 return -EINVAL;
06eb49f7 1592
55726dc9 1593 /* create the FE DAIs and DAI links */
82c19254
CR
1594 ret = soc_tplg_pcm_create(tplg, pcm);
1595 if (ret < 0)
a3039aef 1596 return ret;
55726dc9 1597
717a8e72
ML
1598 /* offset by version-specific struct size and
1599 * real priv data size
1600 */
82c19254 1601 tplg->pos += size + le32_to_cpu(pcm->priv.size);
64527e8a
ML
1602 }
1603
8a978234 1604 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
8a978234 1605
8a978234 1606 return 0;
8a978234
LG
1607}
1608
593d9e52
ML
1609/**
1610 * set_link_hw_format - Set the HW audio format of the physical DAI link.
8abab35f 1611 * @link: &snd_soc_dai_link which should be updated
593d9e52
ML
1612 * @cfg: physical link configs.
1613 *
1614 * Topology context contains a list of supported HW formats (configs) and
1615 * a default format ID for the physical link. This function will use this
1616 * default ID to choose the HW format to set the link's DAI format for init.
1617 */
1618static void set_link_hw_format(struct snd_soc_dai_link *link,
1619 struct snd_soc_tplg_link_config *cfg)
1620{
1621 struct snd_soc_tplg_hw_config *hw_config;
f026c123 1622 unsigned char bclk_provider, fsync_provider;
593d9e52
ML
1623 unsigned char invert_bclk, invert_fsync;
1624 int i;
1625
5aebe7c7 1626 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
593d9e52
ML
1627 hw_config = &cfg->hw_config[i];
1628 if (hw_config->id != cfg->default_hw_config_id)
1629 continue;
1630
5aebe7c7
PLB
1631 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
1632 SND_SOC_DAIFMT_FORMAT_MASK;
593d9e52 1633
933e1c4a 1634 /* clock gating */
fbeabd09
KM
1635 switch (hw_config->clock_gated) {
1636 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
933e1c4a 1637 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
fbeabd09
KM
1638 break;
1639
1640 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
933e1c4a 1641 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
fbeabd09
KM
1642 break;
1643
1644 default:
1645 /* ignore the value */
1646 break;
1647 }
933e1c4a 1648
593d9e52
ML
1649 /* clock signal polarity */
1650 invert_bclk = hw_config->invert_bclk;
1651 invert_fsync = hw_config->invert_fsync;
1652 if (!invert_bclk && !invert_fsync)
1653 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1654 else if (!invert_bclk && invert_fsync)
1655 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1656 else if (invert_bclk && !invert_fsync)
1657 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1658 else
1659 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1660
1661 /* clock masters */
f026c123
PLB
1662 bclk_provider = (hw_config->bclk_provider ==
1663 SND_SOC_TPLG_BCLK_CP);
1664 fsync_provider = (hw_config->fsync_provider ==
1665 SND_SOC_TPLG_FSYNC_CP);
1666 if (bclk_provider && fsync_provider)
1667 link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
1668 else if (!bclk_provider && fsync_provider)
1669 link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
1670 else if (bclk_provider && !fsync_provider)
1671 link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
593d9e52 1672 else
f026c123 1673 link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
593d9e52
ML
1674 }
1675}
1676
d6f31e0e
KM
1677/**
1678 * snd_soc_find_dai_link - Find a DAI link
1679 *
1680 * @card: soc card
1681 * @id: DAI link ID to match
1682 * @name: DAI link name to match, optional
1683 * @stream_name: DAI link stream name to match, optional
1684 *
1685 * This function will search all existing DAI links of the soc card to
1686 * find the link of the same ID. Since DAI links may not have their
1687 * unique ID, so name and stream name should also match if being
1688 * specified.
1689 *
1690 * Return: pointer of DAI link, or NULL if not found.
1691 */
1692static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
1693 int id, const char *name,
1694 const char *stream_name)
1695{
1696 struct snd_soc_pcm_runtime *rtd;
d6f31e0e
KM
1697
1698 for_each_card_rtds(card, rtd) {
b81e8efa 1699 struct snd_soc_dai_link *link = rtd->dai_link;
d6f31e0e
KM
1700
1701 if (link->id != id)
1702 continue;
1703
e018e0b3 1704 if (name && (!link->name || !strstr(link->name, name)))
d6f31e0e
KM
1705 continue;
1706
e018e0b3
RS
1707 if (stream_name && (!link->stream_name ||
1708 !strstr(link->stream_name, stream_name)))
d6f31e0e
KM
1709 continue;
1710
1711 return link;
1712 }
1713
1714 return NULL;
1715}
1716
593d9e52
ML
1717/* Find and configure an existing physical DAI link */
1718static int soc_tplg_link_config(struct soc_tplg *tplg,
1719 struct snd_soc_tplg_link_config *cfg)
1720{
1721 struct snd_soc_dai_link *link;
1722 const char *name, *stream_name;
dbab1cb8 1723 size_t len;
593d9e52
ML
1724 int ret;
1725
dbab1cb8
ML
1726 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1727 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1728 return -EINVAL;
1729 else if (len)
1730 name = cfg->name;
1731 else
1732 name = NULL;
1733
1734 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1735 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1736 return -EINVAL;
1737 else if (len)
1738 stream_name = cfg->stream_name;
1739 else
1740 stream_name = NULL;
593d9e52 1741
5aebe7c7 1742 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
593d9e52
ML
1743 name, stream_name);
1744 if (!link) {
1745 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
1746 name, cfg->id);
1747 return -EINVAL;
1748 }
1749
1750 /* hw format */
1751 if (cfg->num_hw_configs)
1752 set_link_hw_format(link, cfg);
1753
1754 /* flags */
1755 if (cfg->flag_mask)
5aebe7c7
PLB
1756 set_link_flags(link,
1757 le32_to_cpu(cfg->flag_mask),
1758 le32_to_cpu(cfg->flags));
593d9e52
ML
1759
1760 /* pass control to component driver for optional further init */
c60b613a 1761 ret = soc_tplg_dai_link_load(tplg, link, cfg);
593d9e52
ML
1762 if (ret < 0) {
1763 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
1764 return ret;
1765 }
1766
adfebb51
B
1767 /* for unloading it in snd_soc_tplg_component_remove */
1768 link->dobj.index = tplg->index;
adfebb51 1769 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
31e92739
AS
1770 if (tplg->ops)
1771 link->dobj.unload = tplg->ops->link_unload;
adfebb51
B
1772 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1773
593d9e52
ML
1774 return 0;
1775}
1776
1777
1778/* Load physical link config elements from the topology context */
1779static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
1780 struct snd_soc_tplg_hdr *hdr)
1781{
82c19254 1782 struct snd_soc_tplg_link_config *link;
5aebe7c7
PLB
1783 int count;
1784 int size;
593d9e52 1785 int i, ret;
593d9e52 1786
5aebe7c7
PLB
1787 count = le32_to_cpu(hdr->count);
1788
593d9e52
ML
1789 /* check the element size and count */
1790 link = (struct snd_soc_tplg_link_config *)tplg->pos;
5aebe7c7 1791 size = le32_to_cpu(link->size);
c57468dc 1792 if (size > sizeof(struct snd_soc_tplg_link_config)) {
593d9e52 1793 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
5aebe7c7 1794 size);
593d9e52
ML
1795 return -EINVAL;
1796 }
1797
3ce57f22 1798 if (soc_tplg_check_elem_count(tplg, size, count,
5aebe7c7 1799 le32_to_cpu(hdr->payload_size),
3ce57f22 1800 "physical link config"))
593d9e52 1801 return -EINVAL;
593d9e52
ML
1802
1803 /* config physical DAI links */
1804 for (i = 0; i < count; i++) {
1805 link = (struct snd_soc_tplg_link_config *)tplg->pos;
5aebe7c7 1806 size = le32_to_cpu(link->size);
c57468dc
CR
1807 if (size != sizeof(*link))
1808 return -EINVAL;
593d9e52 1809
82c19254
CR
1810 ret = soc_tplg_link_config(tplg, link);
1811 if (ret < 0)
593d9e52
ML
1812 return ret;
1813
1814 /* offset by version-specific struct size and
1815 * real priv data size
1816 */
82c19254 1817 tplg->pos += size + le32_to_cpu(link->priv.size);
593d9e52
ML
1818 }
1819
1820 return 0;
1821}
1822
9aa3f034
ML
1823/**
1824 * soc_tplg_dai_config - Find and configure an existing physical DAI.
0038be9a 1825 * @tplg: topology context
9aa3f034 1826 * @d: physical DAI configs.
0038be9a 1827 *
9aa3f034
ML
1828 * The physical dai should already be registered by the platform driver.
1829 * The platform driver should specify the DAI name and ID for matching.
0038be9a 1830 */
9aa3f034
ML
1831static int soc_tplg_dai_config(struct soc_tplg *tplg,
1832 struct snd_soc_tplg_dai *d)
0038be9a 1833{
5aebe7c7 1834 struct snd_soc_dai_link_component dai_component;
0038be9a
ML
1835 struct snd_soc_dai *dai;
1836 struct snd_soc_dai_driver *dai_drv;
1837 struct snd_soc_pcm_stream *stream;
1838 struct snd_soc_tplg_stream_caps *caps;
1839 int ret;
1840
5aebe7c7
PLB
1841 memset(&dai_component, 0, sizeof(dai_component));
1842
9aa3f034 1843 dai_component.dai_name = d->dai_name;
0038be9a
ML
1844 dai = snd_soc_find_dai(&dai_component);
1845 if (!dai) {
9aa3f034
ML
1846 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
1847 d->dai_name);
0038be9a
ML
1848 return -EINVAL;
1849 }
1850
5aebe7c7 1851 if (le32_to_cpu(d->dai_id) != dai->id) {
9aa3f034
ML
1852 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
1853 d->dai_name);
0038be9a
ML
1854 return -EINVAL;
1855 }
1856
1857 dai_drv = dai->driver;
1858 if (!dai_drv)
1859 return -EINVAL;
1860
9aa3f034 1861 if (d->playback) {
0038be9a 1862 stream = &dai_drv->playback;
9aa3f034 1863 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
ff922622 1864 ret = set_stream_info(tplg, stream, caps);
abc3caac 1865 if (ret < 0)
2772ee6d 1866 return ret;
0038be9a
ML
1867 }
1868
9aa3f034 1869 if (d->capture) {
0038be9a 1870 stream = &dai_drv->capture;
9aa3f034 1871 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
ff922622 1872 ret = set_stream_info(tplg, stream, caps);
abc3caac 1873 if (ret < 0)
2772ee6d 1874 return ret;
0038be9a
ML
1875 }
1876
9aa3f034 1877 if (d->flag_mask)
5aebe7c7
PLB
1878 set_dai_flags(dai_drv,
1879 le32_to_cpu(d->flag_mask),
1880 le32_to_cpu(d->flags));
0038be9a
ML
1881
1882 /* pass control to component driver for optional further init */
c60b613a 1883 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
0038be9a 1884 if (ret < 0) {
e59db12b 1885 dev_err(tplg->dev, "ASoC: DAI loading failed\n");
2772ee6d 1886 return ret;
0038be9a
ML
1887 }
1888
1889 return 0;
1890}
1891
9aa3f034
ML
1892/* load physical DAI elements */
1893static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
1894 struct snd_soc_tplg_hdr *hdr)
0038be9a 1895{
5aebe7c7 1896 int count;
65a4cfdd 1897 int i;
0038be9a 1898
5aebe7c7
PLB
1899 count = le32_to_cpu(hdr->count);
1900
0038be9a
ML
1901 /* config the existing BE DAIs */
1902 for (i = 0; i < count; i++) {
65a4cfdd
KM
1903 struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos;
1904 int ret;
1905
5aebe7c7 1906 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
9aa3f034 1907 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
0038be9a
ML
1908 return -EINVAL;
1909 }
1910
dd8e871d
AS
1911 ret = soc_tplg_dai_config(tplg, dai);
1912 if (ret < 0) {
1913 dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
1914 return ret;
1915 }
1916
5aebe7c7 1917 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
0038be9a
ML
1918 }
1919
1920 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
1921 return 0;
1922}
1923
8a978234 1924static int soc_tplg_manifest_load(struct soc_tplg *tplg,
0038be9a 1925 struct snd_soc_tplg_hdr *hdr)
8a978234 1926{
82c19254 1927 struct snd_soc_tplg_manifest *manifest;
242c46c0 1928 int ret = 0;
8a978234 1929
8a978234 1930 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
06eb49f7 1931
583958fa 1932 /* check ABI version by size, create a new manifest if abi not match */
c57468dc
CR
1933 if (le32_to_cpu(manifest->size) != sizeof(*manifest))
1934 return -EINVAL;
8a978234 1935
583958fa 1936 /* pass control to component driver for optional further init */
c42464a4 1937 if (tplg->ops && tplg->ops->manifest)
82c19254 1938 ret = tplg->ops->manifest(tplg->comp, tplg->index, manifest);
8a978234 1939
242c46c0 1940 return ret;
8a978234
LG
1941}
1942
1943/* validate header magic, size and type */
23e591dc 1944static int soc_tplg_valid_header(struct soc_tplg *tplg,
8a978234
LG
1945 struct snd_soc_tplg_hdr *hdr)
1946{
5aebe7c7 1947 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
06eb49f7
ML
1948 dev_err(tplg->dev,
1949 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
5aebe7c7 1950 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
06eb49f7
ML
1951 tplg->fw->size);
1952 return -EINVAL;
1953 }
1954
c5d184c9 1955 if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) {
86e2d14b
CR
1956 dev_err(tplg->dev,
1957 "ASoC: invalid header of type %d at offset %ld payload_size %d\n",
1958 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
1959 hdr->payload_size);
1960 return -EINVAL;
1961 }
1962
8a978234 1963 /* big endian firmware objects not supported atm */
26d87881 1964 if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
8a978234
LG
1965 dev_err(tplg->dev,
1966 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1967 tplg->pass, hdr->magic,
1968 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1969 return -EINVAL;
1970 }
1971
5aebe7c7 1972 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
8a978234
LG
1973 dev_err(tplg->dev,
1974 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1975 tplg->pass, hdr->magic,
1976 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1977 return -EINVAL;
1978 }
1979
288b8da7 1980 /* Support ABI from version 4 */
5aebe7c7
PLB
1981 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
1982 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
8a978234
LG
1983 dev_err(tplg->dev,
1984 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1985 tplg->pass, hdr->abi,
1986 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
1987 tplg->fw->size);
1988 return -EINVAL;
1989 }
1990
1991 if (hdr->payload_size == 0) {
1992 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
1993 soc_tplg_get_hdr_offset(tplg));
1994 return -EINVAL;
1995 }
1996
d9b07b79 1997 return 0;
8a978234
LG
1998}
1999
2000/* check header type and call appropriate handler */
2001static int soc_tplg_load_header(struct soc_tplg *tplg,
2002 struct snd_soc_tplg_hdr *hdr)
2003{
82ed7418
KJ
2004 int (*elem_load)(struct soc_tplg *tplg,
2005 struct snd_soc_tplg_hdr *hdr);
2006 unsigned int hdr_pass;
2007
8a978234
LG
2008 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2009
5aebe7c7 2010 tplg->index = le32_to_cpu(hdr->index);
8a978234 2011
5aebe7c7 2012 switch (le32_to_cpu(hdr->type)) {
8a978234
LG
2013 case SND_SOC_TPLG_TYPE_MIXER:
2014 case SND_SOC_TPLG_TYPE_ENUM:
2015 case SND_SOC_TPLG_TYPE_BYTES:
5e2cd47a 2016 hdr_pass = SOC_TPLG_PASS_CONTROL;
82ed7418
KJ
2017 elem_load = soc_tplg_kcontrol_elems_load;
2018 break;
8a978234 2019 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
82ed7418
KJ
2020 hdr_pass = SOC_TPLG_PASS_GRAPH;
2021 elem_load = soc_tplg_dapm_graph_elems_load;
2022 break;
8a978234 2023 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
82ed7418
KJ
2024 hdr_pass = SOC_TPLG_PASS_WIDGET;
2025 elem_load = soc_tplg_dapm_widget_elems_load;
2026 break;
8a978234 2027 case SND_SOC_TPLG_TYPE_PCM:
82ed7418
KJ
2028 hdr_pass = SOC_TPLG_PASS_PCM_DAI;
2029 elem_load = soc_tplg_pcm_elems_load;
2030 break;
3fbf7935 2031 case SND_SOC_TPLG_TYPE_DAI:
82ed7418
KJ
2032 hdr_pass = SOC_TPLG_PASS_BE_DAI;
2033 elem_load = soc_tplg_dai_elems_load;
2034 break;
593d9e52
ML
2035 case SND_SOC_TPLG_TYPE_DAI_LINK:
2036 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2037 /* physical link configurations */
82ed7418
KJ
2038 hdr_pass = SOC_TPLG_PASS_LINK;
2039 elem_load = soc_tplg_link_elems_load;
2040 break;
8a978234 2041 case SND_SOC_TPLG_TYPE_MANIFEST:
82ed7418
KJ
2042 hdr_pass = SOC_TPLG_PASS_MANIFEST;
2043 elem_load = soc_tplg_manifest_load;
2044 break;
8a978234
LG
2045 default:
2046 /* bespoke vendor data object */
82ed7418
KJ
2047 hdr_pass = SOC_TPLG_PASS_VENDOR;
2048 elem_load = soc_tplg_vendor_load;
2049 break;
2050 }
2051
2052 if (tplg->pass == hdr_pass) {
2053 dev_dbg(tplg->dev,
2054 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2055 hdr->payload_size, hdr->type, hdr->version,
2056 hdr->vendor_type, tplg->pass);
2057 return elem_load(tplg, hdr);
8a978234
LG
2058 }
2059
2060 return 0;
2061}
2062
2063/* process the topology file headers */
2064static int soc_tplg_process_headers(struct soc_tplg *tplg)
2065{
8a978234
LG
2066 int ret;
2067
8a978234 2068 /* process the header types from start to end */
395f8fd6 2069 for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) {
f79e4b2a 2070 struct snd_soc_tplg_hdr *hdr;
8a978234
LG
2071
2072 tplg->hdr_pos = tplg->fw->data;
2073 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2074
2075 while (!soc_tplg_is_eof(tplg)) {
2076
2077 /* make sure header is valid before loading */
23e591dc 2078 ret = soc_tplg_valid_header(tplg, hdr);
f9d1fe7e 2079 if (ret < 0)
8a978234 2080 return ret;
8a978234
LG
2081
2082 /* load the header object */
2083 ret = soc_tplg_load_header(tplg, hdr);
8bf9475f 2084 if (ret < 0) {
b6c3bdda
JH
2085 if (ret != -EPROBE_DEFER) {
2086 dev_err(tplg->dev,
2087 "ASoC: topology: could not load header: %d\n",
2088 ret);
2089 }
8a978234 2090 return ret;
8bf9475f 2091 }
8a978234
LG
2092
2093 /* goto next header */
5aebe7c7 2094 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
8a978234
LG
2095 sizeof(struct snd_soc_tplg_hdr);
2096 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2097 }
2098
8a978234
LG
2099 }
2100
2101 /* signal DAPM we are complete */
2102 ret = soc_tplg_dapm_complete(tplg);
8a978234
LG
2103
2104 return ret;
2105}
2106
2107static int soc_tplg_load(struct soc_tplg *tplg)
2108{
2109 int ret;
2110
2111 ret = soc_tplg_process_headers(tplg);
2112 if (ret == 0)
415717e1 2113 return soc_tplg_complete(tplg);
8a978234
LG
2114
2115 return ret;
2116}
2117
2118/* load audio component topology from "firmware" file */
2119int snd_soc_tplg_component_load(struct snd_soc_component *comp,
73444768 2120 const struct snd_soc_tplg_ops *ops, const struct firmware *fw)
8a978234
LG
2121{
2122 struct soc_tplg tplg;
304017d3 2123 int ret;
8a978234 2124
d40ab86f
AS
2125 /*
2126 * check if we have sane parameters:
2127 * comp - needs to exist to keep and reference data while parsing
d40ab86f 2128 * comp->card - used for setting card related parameters
f714fbc1 2129 * comp->card->dev - used for resource management and prints
d40ab86f
AS
2130 * fw - we need it, as it is the very thing we parse
2131 */
f714fbc1 2132 if (!comp || !comp->card || !comp->card->dev || !fw)
c42464a4 2133 return -EINVAL;
8a978234
LG
2134
2135 /* setup parsing context */
2136 memset(&tplg, 0, sizeof(tplg));
2137 tplg.fw = fw;
f714fbc1 2138 tplg.dev = comp->card->dev;
8a978234 2139 tplg.comp = comp;
9c88a983
AS
2140 if (ops) {
2141 tplg.ops = ops;
2142 tplg.io_ops = ops->io_ops;
2143 tplg.io_ops_count = ops->io_ops_count;
2144 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2145 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2146 }
8a978234 2147
304017d3
B
2148 ret = soc_tplg_load(&tplg);
2149 /* free the created components if fail to load topology */
2150 if (ret)
a5b8f71c 2151 snd_soc_tplg_component_remove(comp);
304017d3
B
2152
2153 return ret;
8a978234
LG
2154}
2155EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2156
8a978234 2157/* remove dynamic controls from the component driver */
a5b8f71c 2158int snd_soc_tplg_component_remove(struct snd_soc_component *comp)
8a978234
LG
2159{
2160 struct snd_soc_dobj *dobj, *next_dobj;
395f8fd6 2161 int pass;
8a978234
LG
2162
2163 /* process the header types from end to start */
395f8fd6 2164 for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) {
8a978234
LG
2165
2166 /* remove mixer controls */
2167 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2168 list) {
2169
8a978234 2170 switch (dobj->type) {
8a978234 2171 case SND_SOC_DOBJ_BYTES:
fdfa3661
AS
2172 case SND_SOC_DOBJ_ENUM:
2173 case SND_SOC_DOBJ_MIXER:
2174 soc_tplg_remove_kcontrol(comp, dobj, pass);
8a978234 2175 break;
7df04ea7 2176 case SND_SOC_DOBJ_GRAPH:
2abfd4bd 2177 soc_tplg_remove_route(comp, dobj, pass);
7df04ea7 2178 break;
8a978234 2179 case SND_SOC_DOBJ_WIDGET:
2abfd4bd 2180 soc_tplg_remove_widget(comp, dobj, pass);
8a978234
LG
2181 break;
2182 case SND_SOC_DOBJ_PCM:
2abfd4bd 2183 soc_tplg_remove_dai(comp, dobj, pass);
8a978234 2184 break;
acfc7d46 2185 case SND_SOC_DOBJ_DAI_LINK:
2abfd4bd 2186 soc_tplg_remove_link(comp, dobj, pass);
acfc7d46 2187 break;
adfebb51
B
2188 case SND_SOC_DOBJ_BACKEND_LINK:
2189 /*
2190 * call link_unload ops if extra
2191 * deinitialization is needed.
2192 */
2193 remove_backend_link(comp, dobj, pass);
2194 break;
8a978234
LG
2195 default:
2196 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2197 dobj->type);
2198 break;
2199 }
2200 }
8a978234
LG
2201 }
2202
2203 /* let caller know if FW can be freed when no objects are left */
2204 return !list_empty(&comp->dobj_list);
2205}
2206EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);