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