blob: 66e77e020745b282983c8c0f994d93485f17210d [file] [log] [blame]
Kuninori Morimotof2b6a1b2018-07-02 06:24:45 +00001// 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.
Liam Girdwood8a978232015-05-29 19:06:14 +010022
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>
Mengdong Lin28a87ee2015-08-05 14:41:13 +010031#include <sound/tlv.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010032
33/*
34 * We make several passes over the data (since it wont necessarily be ordered)
35 * and process objects in the following order. This guarantees the component
36 * drivers will be ready with any vendor data before the mixers and DAPM objects
37 * are loaded (that may make use of the vendor data).
38 */
39#define SOC_TPLG_PASS_MANIFEST 0
40#define SOC_TPLG_PASS_VENDOR 1
41#define SOC_TPLG_PASS_MIXER 2
42#define SOC_TPLG_PASS_WIDGET 3
Mengdong Lin1a8e7fa2015-08-10 22:48:30 +080043#define SOC_TPLG_PASS_PCM_DAI 4
44#define SOC_TPLG_PASS_GRAPH 5
45#define SOC_TPLG_PASS_PINS 6
Mengdong Lin0038be92016-07-26 14:32:37 +080046#define SOC_TPLG_PASS_BE_DAI 7
Mengdong Lin593d9e52016-11-03 01:04:27 +080047#define SOC_TPLG_PASS_LINK 8
Liam Girdwood8a978232015-05-29 19:06:14 +010048
49#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
Mengdong Lin593d9e52016-11-03 01:04:27 +080050#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
Liam Girdwood8a978232015-05-29 19:06:14 +010051
Mengdong Lin583958f2016-10-11 14:36:42 +080052/* topology context */
Liam Girdwood8a978232015-05-29 19:06:14 +010053struct soc_tplg {
54 const struct firmware *fw;
55
56 /* runtime FW parsing */
57 const u8 *pos; /* read postion */
58 const u8 *hdr_pos; /* header position */
59 unsigned int pass; /* pass number */
60
61 /* component caller */
62 struct device *dev;
63 struct snd_soc_component *comp;
64 u32 index; /* current block index */
65 u32 req_index; /* required index, only loaded/free matching blocks */
66
Mengdong Lin88a17d82015-08-18 18:11:51 +080067 /* vendor specific kcontrol operations */
Liam Girdwood8a978232015-05-29 19:06:14 +010068 const struct snd_soc_tplg_kcontrol_ops *io_ops;
69 int io_ops_count;
70
Mengdong Lin1a3232d2015-08-18 18:12:20 +080071 /* 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
Liam Girdwood8a978232015-05-29 19:06:14 +010075 /* optional fw loading callbacks to component drivers */
76 struct snd_soc_tplg_ops *ops;
77};
78
79static int soc_tplg_process_headers(struct soc_tplg *tplg);
80static void soc_tplg_complete(struct soc_tplg *tplg);
81struct snd_soc_dapm_widget *
82snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
83 const struct snd_soc_dapm_widget *widget);
84struct snd_soc_dapm_widget *
85snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
86 const struct snd_soc_dapm_widget *widget);
87
88/* check we dont overflow the data for this control chunk */
89static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
90 unsigned int count, size_t bytes, const char *elem_type)
91{
92 const u8 *end = tplg->pos + elem_size * count;
93
94 if (end > tplg->fw->data + tplg->fw->size) {
95 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
96 elem_type);
97 return -EINVAL;
98 }
99
100 /* check there is enough room in chunk for control.
101 extra bytes at the end of control are for vendor data here */
102 if (elem_size * count > bytes) {
103 dev_err(tplg->dev,
104 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
105 elem_type, count, elem_size, bytes);
106 return -EINVAL;
107 }
108
109 return 0;
110}
111
112static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
113{
114 const u8 *end = tplg->hdr_pos;
115
116 if (end >= tplg->fw->data + tplg->fw->size)
117 return 1;
118 return 0;
119}
120
121static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
122{
123 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
124}
125
126static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
127{
128 return (unsigned long)(tplg->pos - tplg->fw->data);
129}
130
131/* mapping of Kcontrol types and associated operations. */
132static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
133 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
134 snd_soc_put_volsw, snd_soc_info_volsw},
135 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
136 snd_soc_put_volsw_sx, NULL},
137 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
138 snd_soc_put_enum_double, snd_soc_info_enum_double},
139 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
140 snd_soc_put_enum_double, NULL},
141 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
142 snd_soc_bytes_put, snd_soc_bytes_info},
143 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
144 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
145 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
146 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
147 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
148 snd_soc_put_strobe, NULL},
149 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530150 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100151 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
152 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
153 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
154 snd_soc_dapm_put_enum_double, NULL},
155 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
156 snd_soc_dapm_put_enum_double, NULL},
157 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
158 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
159};
160
161struct soc_tplg_map {
162 int uid;
163 int kid;
164};
165
166/* mapping of widget types from UAPI IDs to kernel IDs */
167static const struct soc_tplg_map dapm_map[] = {
168 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
169 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
170 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
171 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
172 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
173 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
174 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
175 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
176 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
177 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
178 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
179 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
180 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
181 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
182 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
183 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
Liam Girdwood8a70b452017-06-29 14:22:24 +0100184 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
185 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
186 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
187 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
188 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
189 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
190 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
191 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
Liam Girdwood8a978232015-05-29 19:06:14 +0100192};
193
194static int tplc_chan_get_reg(struct soc_tplg *tplg,
195 struct snd_soc_tplg_channel *chan, int map)
196{
197 int i;
198
199 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
200 if (chan[i].id == map)
201 return chan[i].reg;
202 }
203
204 return -EINVAL;
205}
206
207static int tplc_chan_get_shift(struct soc_tplg *tplg,
208 struct snd_soc_tplg_channel *chan, int map)
209{
210 int i;
211
212 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
213 if (chan[i].id == map)
214 return chan[i].shift;
215 }
216
217 return -EINVAL;
218}
219
220static int get_widget_id(int tplg_type)
221{
222 int i;
223
224 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
225 if (tplg_type == dapm_map[i].uid)
226 return dapm_map[i].kid;
227 }
228
229 return -EINVAL;
230}
231
Liam Girdwood8a978232015-05-29 19:06:14 +0100232static inline void soc_bind_err(struct soc_tplg *tplg,
233 struct snd_soc_tplg_ctl_hdr *hdr, int index)
234{
235 dev_err(tplg->dev,
236 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
237 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
238 soc_tplg_get_offset(tplg));
239}
240
241static inline void soc_control_err(struct soc_tplg *tplg,
242 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
243{
244 dev_err(tplg->dev,
245 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
246 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
247 soc_tplg_get_offset(tplg));
248}
249
250/* pass vendor data to component driver for processing */
251static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
252 struct snd_soc_tplg_hdr *hdr)
253{
254 int ret = 0;
255
256 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100257 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100258 else {
259 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
260 hdr->vendor_type);
261 return -EINVAL;
262 }
263
264 if (ret < 0)
265 dev_err(tplg->dev,
266 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
267 soc_tplg_get_hdr_offset(tplg),
268 soc_tplg_get_hdr_offset(tplg),
269 hdr->type, hdr->vendor_type);
270 return ret;
271}
272
273/* pass vendor data to component driver for processing */
274static int soc_tplg_vendor_load(struct soc_tplg *tplg,
275 struct snd_soc_tplg_hdr *hdr)
276{
277 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
278 return 0;
279
280 return soc_tplg_vendor_load_(tplg, hdr);
281}
282
283/* optionally pass new dynamic widget to component driver. This is mainly for
284 * external widgets where we can assign private data/ops */
285static int soc_tplg_widget_load(struct soc_tplg *tplg,
286 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
287{
288 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100289 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
290 tplg_w);
Liam Girdwood8a978232015-05-29 19:06:14 +0100291
292 return 0;
293}
294
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100295/* optionally pass new dynamic widget to component driver. This is mainly for
296 * external widgets where we can assign private data/ops */
297static int soc_tplg_widget_ready(struct soc_tplg *tplg,
298 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
299{
300 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100301 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
302 tplg_w);
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100303
304 return 0;
305}
306
Masahiro Yamada183b8022017-02-27 14:29:20 -0800307/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800308static int soc_tplg_dai_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100309 struct snd_soc_dai_driver *dai_drv,
310 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
Liam Girdwood8a978232015-05-29 19:06:14 +0100311{
Mengdong Lin64527e82016-01-15 16:13:28 +0800312 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100313 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
314 pcm, dai);
Liam Girdwood8a978232015-05-29 19:06:14 +0100315
316 return 0;
317}
318
Masahiro Yamada183b8022017-02-27 14:29:20 -0800319/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800320static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100321 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
Mengdong Linacfc7d42016-01-15 16:13:37 +0800322{
323 if (tplg->comp && tplg->ops && tplg->ops->link_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100324 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
Mengdong Linacfc7d42016-01-15 16:13:37 +0800325
326 return 0;
327}
328
Liam Girdwood8a978232015-05-29 19:06:14 +0100329/* tell the component driver that all firmware has been loaded in this request */
330static void soc_tplg_complete(struct soc_tplg *tplg)
331{
332 if (tplg->comp && tplg->ops && tplg->ops->complete)
333 tplg->ops->complete(tplg->comp);
334}
335
336/* add a dynamic kcontrol */
337static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
338 const struct snd_kcontrol_new *control_new, const char *prefix,
339 void *data, struct snd_kcontrol **kcontrol)
340{
341 int err;
342
343 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
344 if (*kcontrol == NULL) {
345 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
346 control_new->name);
347 return -ENOMEM;
348 }
349
350 err = snd_ctl_add(card, *kcontrol);
351 if (err < 0) {
352 dev_err(dev, "ASoC: Failed to add %s: %d\n",
353 control_new->name, err);
354 return err;
355 }
356
357 return 0;
358}
359
360/* add a dynamic kcontrol for component driver */
361static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
362 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
363{
364 struct snd_soc_component *comp = tplg->comp;
365
366 return soc_tplg_add_dcontrol(comp->card->snd_card,
367 comp->dev, k, NULL, comp, kcontrol);
368}
369
370/* remove a mixer kcontrol */
371static void remove_mixer(struct snd_soc_component *comp,
372 struct snd_soc_dobj *dobj, int pass)
373{
374 struct snd_card *card = comp->card->snd_card;
375 struct soc_mixer_control *sm =
376 container_of(dobj, struct soc_mixer_control, dobj);
377 const unsigned int *p = NULL;
378
379 if (pass != SOC_TPLG_PASS_MIXER)
380 return;
381
382 if (dobj->ops && dobj->ops->control_unload)
383 dobj->ops->control_unload(comp, dobj);
384
385 if (sm->dobj.control.kcontrol->tlv.p)
386 p = sm->dobj.control.kcontrol->tlv.p;
387 snd_ctl_remove(card, sm->dobj.control.kcontrol);
388 list_del(&sm->dobj.list);
389 kfree(sm);
390 kfree(p);
391}
392
393/* remove an enum kcontrol */
394static void remove_enum(struct snd_soc_component *comp,
395 struct snd_soc_dobj *dobj, int pass)
396{
397 struct snd_card *card = comp->card->snd_card;
398 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
399 int i;
400
401 if (pass != SOC_TPLG_PASS_MIXER)
402 return;
403
404 if (dobj->ops && dobj->ops->control_unload)
405 dobj->ops->control_unload(comp, dobj);
406
407 snd_ctl_remove(card, se->dobj.control.kcontrol);
408 list_del(&se->dobj.list);
409
410 kfree(se->dobj.control.dvalues);
411 for (i = 0; i < se->items; i++)
412 kfree(se->dobj.control.dtexts[i]);
413 kfree(se);
414}
415
416/* remove a byte kcontrol */
417static void remove_bytes(struct snd_soc_component *comp,
418 struct snd_soc_dobj *dobj, int pass)
419{
420 struct snd_card *card = comp->card->snd_card;
421 struct soc_bytes_ext *sb =
422 container_of(dobj, struct soc_bytes_ext, dobj);
423
424 if (pass != SOC_TPLG_PASS_MIXER)
425 return;
426
427 if (dobj->ops && dobj->ops->control_unload)
428 dobj->ops->control_unload(comp, dobj);
429
430 snd_ctl_remove(card, sb->dobj.control.kcontrol);
431 list_del(&sb->dobj.list);
432 kfree(sb);
433}
434
435/* remove a widget and it's kcontrols - routes must be removed first */
436static void remove_widget(struct snd_soc_component *comp,
437 struct snd_soc_dobj *dobj, int pass)
438{
439 struct snd_card *card = comp->card->snd_card;
440 struct snd_soc_dapm_widget *w =
441 container_of(dobj, struct snd_soc_dapm_widget, dobj);
442 int i;
443
444 if (pass != SOC_TPLG_PASS_WIDGET)
445 return;
446
447 if (dobj->ops && dobj->ops->widget_unload)
448 dobj->ops->widget_unload(comp, dobj);
449
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000450 if (!w->kcontrols)
451 goto free_news;
452
Liam Girdwood8a978232015-05-29 19:06:14 +0100453 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800454 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100455 * The enum may either have an array of values or strings.
456 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800457 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100458 /* enumerated widget mixer */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100459 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800460 struct snd_kcontrol *kcontrol = w->kcontrols[i];
461 struct soc_enum *se =
462 (struct soc_enum *)kcontrol->private_value;
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100463 int j;
Liam Girdwood8a978232015-05-29 19:06:14 +0100464
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800465 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100466
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800467 kfree(se->dobj.control.dvalues);
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100468 for (j = 0; j < se->items; j++)
469 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +0100470
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800471 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100472 kfree(w->kcontrol_news[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800473 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100474 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800475 /* volume mixer or bytes controls */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100476 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100477 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100478
Mengdong Lineea3dd42016-11-25 16:09:17 +0800479 if (dobj->widget.kcontrol_type
480 == SND_SOC_TPLG_TYPE_MIXER)
481 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100482
Mengdong Lineea3dd42016-11-25 16:09:17 +0800483 /* Private value is used as struct soc_mixer_control
484 * for volume mixers or soc_bytes_ext for bytes
485 * controls.
486 */
487 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000488 snd_ctl_remove(card, kcontrol);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100489 kfree(w->kcontrol_news[i].name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100490 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100491 }
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000492
493free_news:
494 kfree(w->kcontrol_news);
495
Liam Girdwood8a978232015-05-29 19:06:14 +0100496 /* widget w is freed by soc-dapm.c */
497}
498
Mengdong Lin64527e82016-01-15 16:13:28 +0800499/* remove DAI configurations */
500static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100501 struct snd_soc_dobj *dobj, int pass)
502{
Mengdong Lin64527e82016-01-15 16:13:28 +0800503 struct snd_soc_dai_driver *dai_drv =
504 container_of(dobj, struct snd_soc_dai_driver, dobj);
505
Liam Girdwood8a978232015-05-29 19:06:14 +0100506 if (pass != SOC_TPLG_PASS_PCM_DAI)
507 return;
508
Mengdong Lin64527e82016-01-15 16:13:28 +0800509 if (dobj->ops && dobj->ops->dai_unload)
510 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100511
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800512 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100513 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800514 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100515}
516
Mengdong Linacfc7d42016-01-15 16:13:37 +0800517/* remove link configurations */
518static void remove_link(struct snd_soc_component *comp,
519 struct snd_soc_dobj *dobj, int pass)
520{
521 struct snd_soc_dai_link *link =
522 container_of(dobj, struct snd_soc_dai_link, dobj);
523
524 if (pass != SOC_TPLG_PASS_PCM_DAI)
525 return;
526
527 if (dobj->ops && dobj->ops->link_unload)
528 dobj->ops->link_unload(comp, dobj);
529
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800530 kfree(link->name);
531 kfree(link->stream_name);
532 kfree(link->cpu_dai_name);
533
Mengdong Linacfc7d42016-01-15 16:13:37 +0800534 list_del(&dobj->list);
535 snd_soc_remove_dai_link(comp->card, link);
536 kfree(link);
537}
538
Liam Girdwood8a978232015-05-29 19:06:14 +0100539/* bind a kcontrol to it's IO handlers */
540static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
541 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800542 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100543{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800544 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800545 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800546 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100547
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800548 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
549 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
550 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
551 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
552 struct soc_bytes_ext *sbe;
553 struct snd_soc_tplg_bytes_control *be;
554
555 sbe = (struct soc_bytes_ext *)k->private_value;
556 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
557
558 /* TLV bytes controls need standard kcontrol info handler,
559 * TLV callback and extended put/get handlers.
560 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530561 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800562 k->tlv.c = snd_soc_bytes_tlv_callback;
563
564 ext_ops = tplg->bytes_ext_ops;
565 num_ops = tplg->bytes_ext_ops_count;
566 for (i = 0; i < num_ops; i++) {
567 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
568 sbe->put = ext_ops[i].put;
569 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
570 sbe->get = ext_ops[i].get;
571 }
572
573 if (sbe->put && sbe->get)
574 return 0;
575 else
576 return -EINVAL;
577 }
578
Mengdong Lin88a17d82015-08-18 18:11:51 +0800579 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800580 ops = tplg->io_ops;
581 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100582 for (i = 0; i < num_ops; i++) {
583
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800584 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100585 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800586 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100587 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800588 if (k->info == NULL && ops[i].id == hdr->ops.info)
589 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100590 }
591
Mengdong Lin88a17d82015-08-18 18:11:51 +0800592 /* vendor specific handlers found ? */
593 if (k->put && k->get && k->info)
594 return 0;
595
596 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800597 ops = io_ops;
598 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800599 for (i = 0; i < num_ops; i++) {
600
601 if (k->put == NULL && ops[i].id == hdr->ops.put)
602 k->put = ops[i].put;
603 if (k->get == NULL && ops[i].id == hdr->ops.get)
604 k->get = ops[i].get;
605 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100606 k->info = ops[i].info;
607 }
608
609 /* standard handlers found ? */
610 if (k->put && k->get && k->info)
611 return 0;
612
Liam Girdwood8a978232015-05-29 19:06:14 +0100613 /* nothing to bind */
614 return -EINVAL;
615}
616
617/* bind a widgets to it's evnt handlers */
618int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
619 const struct snd_soc_tplg_widget_events *events,
620 int num_events, u16 event_type)
621{
622 int i;
623
624 w->event = NULL;
625
626 for (i = 0; i < num_events; i++) {
627 if (event_type == events[i].type) {
628
629 /* found - so assign event */
630 w->event = events[i].event_handler;
631 return 0;
632 }
633 }
634
635 /* not found */
636 return -EINVAL;
637}
638EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
639
640/* optionally pass new dynamic kcontrol to component driver. */
641static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
642 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
643{
644 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100645 return tplg->ops->control_load(tplg->comp, tplg->index, k,
646 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100647
648 return 0;
649}
650
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100651
652static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
653 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100654{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100655 unsigned int item_len = 2 * sizeof(unsigned int);
656 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100657
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100658 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
659 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100660 return -ENOMEM;
661
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100662 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
663 p[1] = item_len;
664 p[2] = scale->min;
665 p[3] = (scale->step & TLV_DB_SCALE_MASK)
666 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100667
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100668 kc->tlv.p = (void *)p;
669 return 0;
670}
671
672static int soc_tplg_create_tlv(struct soc_tplg *tplg,
673 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
674{
675 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
676
677 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
678 return 0;
679
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800680 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100681 tplg_tlv = &tc->tlv;
682 switch (tplg_tlv->type) {
683 case SNDRV_CTL_TLVT_DB_SCALE:
684 return soc_tplg_create_tlv_db_scale(tplg, kc,
685 &tplg_tlv->scale);
686
687 /* TODO: add support for other TLV types */
688 default:
689 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
690 tplg_tlv->type);
691 return -EINVAL;
692 }
693 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100694
695 return 0;
696}
697
698static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
699 struct snd_kcontrol_new *kc)
700{
701 kfree(kc->tlv.p);
702}
703
704static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
705 size_t size)
706{
707 struct snd_soc_tplg_bytes_control *be;
708 struct soc_bytes_ext *sbe;
709 struct snd_kcontrol_new kc;
710 int i, err;
711
712 if (soc_tplg_check_elem_count(tplg,
713 sizeof(struct snd_soc_tplg_bytes_control), count,
714 size, "mixer bytes")) {
715 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
716 count);
717 return -EINVAL;
718 }
719
720 for (i = 0; i < count; i++) {
721 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
722
723 /* validate kcontrol */
724 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
725 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
726 return -EINVAL;
727
728 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
729 if (sbe == NULL)
730 return -ENOMEM;
731
732 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
733 be->priv.size);
734
735 dev_dbg(tplg->dev,
736 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
737 be->hdr.name, be->hdr.access);
738
739 memset(&kc, 0, sizeof(kc));
740 kc.name = be->hdr.name;
741 kc.private_value = (long)sbe;
742 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
743 kc.access = be->hdr.access;
744
745 sbe->max = be->max;
746 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
747 sbe->dobj.ops = tplg->ops;
748 INIT_LIST_HEAD(&sbe->dobj.list);
749
750 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800751 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100752 if (err) {
753 soc_control_err(tplg, &be->hdr, be->hdr.name);
754 kfree(sbe);
755 continue;
756 }
757
758 /* pass control to driver for optional further init */
759 err = soc_tplg_init_kcontrol(tplg, &kc,
760 (struct snd_soc_tplg_ctl_hdr *)be);
761 if (err < 0) {
762 dev_err(tplg->dev, "ASoC: failed to init %s\n",
763 be->hdr.name);
764 kfree(sbe);
765 continue;
766 }
767
768 /* register control here */
769 err = soc_tplg_add_kcontrol(tplg, &kc,
770 &sbe->dobj.control.kcontrol);
771 if (err < 0) {
772 dev_err(tplg->dev, "ASoC: failed to add %s\n",
773 be->hdr.name);
774 kfree(sbe);
775 continue;
776 }
777
778 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
779 }
780 return 0;
781
782}
783
784static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
785 size_t size)
786{
787 struct snd_soc_tplg_mixer_control *mc;
788 struct soc_mixer_control *sm;
789 struct snd_kcontrol_new kc;
790 int i, err;
791
792 if (soc_tplg_check_elem_count(tplg,
793 sizeof(struct snd_soc_tplg_mixer_control),
794 count, size, "mixers")) {
795
796 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
797 count);
798 return -EINVAL;
799 }
800
801 for (i = 0; i < count; i++) {
802 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
803
804 /* validate kcontrol */
805 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
806 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
807 return -EINVAL;
808
809 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
810 if (sm == NULL)
811 return -ENOMEM;
812 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
813 mc->priv.size);
814
815 dev_dbg(tplg->dev,
816 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
817 mc->hdr.name, mc->hdr.access);
818
819 memset(&kc, 0, sizeof(kc));
820 kc.name = mc->hdr.name;
821 kc.private_value = (long)sm;
822 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
823 kc.access = mc->hdr.access;
824
825 /* we only support FL/FR channel mapping atm */
826 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
827 SNDRV_CHMAP_FL);
828 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
829 SNDRV_CHMAP_FR);
830 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
831 SNDRV_CHMAP_FL);
832 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
833 SNDRV_CHMAP_FR);
834
835 sm->max = mc->max;
836 sm->min = mc->min;
837 sm->invert = mc->invert;
838 sm->platform_max = mc->platform_max;
839 sm->dobj.index = tplg->index;
840 sm->dobj.ops = tplg->ops;
841 sm->dobj.type = SND_SOC_DOBJ_MIXER;
842 INIT_LIST_HEAD(&sm->dobj.list);
843
844 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800845 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100846 if (err) {
847 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
848 kfree(sm);
849 continue;
850 }
851
852 /* pass control to driver for optional further init */
853 err = soc_tplg_init_kcontrol(tplg, &kc,
854 (struct snd_soc_tplg_ctl_hdr *) mc);
855 if (err < 0) {
856 dev_err(tplg->dev, "ASoC: failed to init %s\n",
857 mc->hdr.name);
858 kfree(sm);
859 continue;
860 }
861
862 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100863 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100864
865 /* register control here */
866 err = soc_tplg_add_kcontrol(tplg, &kc,
867 &sm->dobj.control.kcontrol);
868 if (err < 0) {
869 dev_err(tplg->dev, "ASoC: failed to add %s\n",
870 mc->hdr.name);
871 soc_tplg_free_tlv(tplg, &kc);
872 kfree(sm);
873 continue;
874 }
875
876 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
877 }
878
879 return 0;
880}
881
882static int soc_tplg_denum_create_texts(struct soc_enum *se,
883 struct snd_soc_tplg_enum_control *ec)
884{
885 int i, ret;
886
887 se->dobj.control.dtexts =
Kees Cook6396bb22018-06-12 14:03:40 -0700888 kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100889 if (se->dobj.control.dtexts == NULL)
890 return -ENOMEM;
891
892 for (i = 0; i < ec->items; i++) {
893
894 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
895 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
896 ret = -EINVAL;
897 goto err;
898 }
899
900 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
901 if (!se->dobj.control.dtexts[i]) {
902 ret = -ENOMEM;
903 goto err;
904 }
905 }
906
Mousumi Janab6e38b22017-04-11 13:06:22 +0530907 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100908 return 0;
909
910err:
911 for (--i; i >= 0; i--)
912 kfree(se->dobj.control.dtexts[i]);
913 kfree(se->dobj.control.dtexts);
914 return ret;
915}
916
917static int soc_tplg_denum_create_values(struct soc_enum *se,
918 struct snd_soc_tplg_enum_control *ec)
919{
920 if (ec->items > sizeof(*ec->values))
921 return -EINVAL;
922
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200923 se->dobj.control.dvalues = kmemdup(ec->values,
924 ec->items * sizeof(u32),
925 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100926 if (!se->dobj.control.dvalues)
927 return -ENOMEM;
928
Liam Girdwood8a978232015-05-29 19:06:14 +0100929 return 0;
930}
931
932static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
933 size_t size)
934{
935 struct snd_soc_tplg_enum_control *ec;
936 struct soc_enum *se;
937 struct snd_kcontrol_new kc;
938 int i, ret, err;
939
940 if (soc_tplg_check_elem_count(tplg,
941 sizeof(struct snd_soc_tplg_enum_control),
942 count, size, "enums")) {
943
944 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
945 count);
946 return -EINVAL;
947 }
948
949 for (i = 0; i < count; i++) {
950 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
951 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
952 ec->priv.size);
953
954 /* validate kcontrol */
955 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
956 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
957 return -EINVAL;
958
959 se = kzalloc((sizeof(*se)), GFP_KERNEL);
960 if (se == NULL)
961 return -ENOMEM;
962
963 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
964 ec->hdr.name, ec->items);
965
966 memset(&kc, 0, sizeof(kc));
967 kc.name = ec->hdr.name;
968 kc.private_value = (long)se;
969 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
970 kc.access = ec->hdr.access;
971
972 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
973 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
974 SNDRV_CHMAP_FL);
975 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
976 SNDRV_CHMAP_FL);
977
978 se->items = ec->items;
979 se->mask = ec->mask;
980 se->dobj.index = tplg->index;
981 se->dobj.type = SND_SOC_DOBJ_ENUM;
982 se->dobj.ops = tplg->ops;
983 INIT_LIST_HEAD(&se->dobj.list);
984
985 switch (ec->hdr.ops.info) {
986 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
987 case SND_SOC_TPLG_CTL_ENUM_VALUE:
988 err = soc_tplg_denum_create_values(se, ec);
989 if (err < 0) {
990 dev_err(tplg->dev,
991 "ASoC: could not create values for %s\n",
992 ec->hdr.name);
993 kfree(se);
994 continue;
995 }
996 /* fall through and create texts */
997 case SND_SOC_TPLG_CTL_ENUM:
998 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
999 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1000 err = soc_tplg_denum_create_texts(se, ec);
1001 if (err < 0) {
1002 dev_err(tplg->dev,
1003 "ASoC: could not create texts for %s\n",
1004 ec->hdr.name);
1005 kfree(se);
1006 continue;
1007 }
1008 break;
1009 default:
1010 dev_err(tplg->dev,
1011 "ASoC: invalid enum control type %d for %s\n",
1012 ec->hdr.ops.info, ec->hdr.name);
1013 kfree(se);
1014 continue;
1015 }
1016
1017 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001018 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001019 if (err) {
1020 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1021 kfree(se);
1022 continue;
1023 }
1024
1025 /* pass control to driver for optional further init */
1026 err = soc_tplg_init_kcontrol(tplg, &kc,
1027 (struct snd_soc_tplg_ctl_hdr *) ec);
1028 if (err < 0) {
1029 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1030 ec->hdr.name);
1031 kfree(se);
1032 continue;
1033 }
1034
1035 /* register control here */
1036 ret = soc_tplg_add_kcontrol(tplg,
1037 &kc, &se->dobj.control.kcontrol);
1038 if (ret < 0) {
1039 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1040 ec->hdr.name);
1041 kfree(se);
1042 continue;
1043 }
1044
1045 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1046 }
1047
1048 return 0;
1049}
1050
1051static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1052 struct snd_soc_tplg_hdr *hdr)
1053{
1054 struct snd_soc_tplg_ctl_hdr *control_hdr;
1055 int i;
1056
1057 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1058 tplg->pos += hdr->size + hdr->payload_size;
1059 return 0;
1060 }
1061
1062 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1063 soc_tplg_get_offset(tplg));
1064
1065 for (i = 0; i < hdr->count; i++) {
1066
1067 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1068
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001069 if (control_hdr->size != sizeof(*control_hdr)) {
1070 dev_err(tplg->dev, "ASoC: invalid control size\n");
1071 return -EINVAL;
1072 }
1073
Liam Girdwood8a978232015-05-29 19:06:14 +01001074 switch (control_hdr->ops.info) {
1075 case SND_SOC_TPLG_CTL_VOLSW:
1076 case SND_SOC_TPLG_CTL_STROBE:
1077 case SND_SOC_TPLG_CTL_VOLSW_SX:
1078 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1079 case SND_SOC_TPLG_CTL_RANGE:
1080 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1081 case SND_SOC_TPLG_DAPM_CTL_PIN:
1082 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1083 break;
1084 case SND_SOC_TPLG_CTL_ENUM:
1085 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1086 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1087 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1088 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1089 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1090 break;
1091 case SND_SOC_TPLG_CTL_BYTES:
1092 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1093 break;
1094 default:
1095 soc_bind_err(tplg, control_hdr, i);
1096 return -EINVAL;
1097 }
1098 }
1099
1100 return 0;
1101}
1102
Liam Girdwood503e79b2018-06-14 20:53:59 +01001103/* optionally pass new dynamic kcontrol to component driver. */
1104static int soc_tplg_add_route(struct soc_tplg *tplg,
1105 struct snd_soc_dapm_route *route)
1106{
1107 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1108 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1109 route);
1110
1111 return 0;
1112}
1113
Liam Girdwood8a978232015-05-29 19:06:14 +01001114static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1115 struct snd_soc_tplg_hdr *hdr)
1116{
1117 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1118 struct snd_soc_dapm_route route;
1119 struct snd_soc_tplg_dapm_graph_elem *elem;
1120 int count = hdr->count, i;
1121
1122 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1123 tplg->pos += hdr->size + hdr->payload_size;
1124 return 0;
1125 }
1126
1127 if (soc_tplg_check_elem_count(tplg,
1128 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1129 count, hdr->payload_size, "graph")) {
1130
1131 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1132 count);
1133 return -EINVAL;
1134 }
1135
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001136 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1137 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001138
1139 for (i = 0; i < count; i++) {
1140 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1141 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1142
1143 /* validate routes */
1144 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1145 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1146 return -EINVAL;
1147 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1148 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1149 return -EINVAL;
1150 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1151 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1152 return -EINVAL;
1153
1154 route.source = elem->source;
1155 route.sink = elem->sink;
1156 route.connected = NULL; /* set to NULL atm for tplg users */
1157 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1158 route.control = NULL;
1159 else
1160 route.control = elem->control;
1161
Liam Girdwood503e79b2018-06-14 20:53:59 +01001162 soc_tplg_add_route(tplg, &route);
1163
Liam Girdwood8a978232015-05-29 19:06:14 +01001164 /* add route, but keep going if some fail */
1165 snd_soc_dapm_add_routes(dapm, &route, 1);
1166 }
1167
1168 return 0;
1169}
1170
1171static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1172 struct soc_tplg *tplg, int num_kcontrols)
1173{
1174 struct snd_kcontrol_new *kc;
1175 struct soc_mixer_control *sm;
1176 struct snd_soc_tplg_mixer_control *mc;
1177 int i, err;
1178
Axel Lin4ca7deb2015-08-05 22:34:22 +08001179 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001180 if (kc == NULL)
1181 return NULL;
1182
1183 for (i = 0; i < num_kcontrols; i++) {
1184 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1185 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1186 if (sm == NULL)
1187 goto err;
1188
1189 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1190 mc->priv.size);
1191
1192 /* validate kcontrol */
1193 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1194 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1195 goto err_str;
1196
1197 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1198 mc->hdr.name, i);
1199
Liam Girdwood267e2c62018-03-27 12:04:04 +01001200 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1201 if (kc[i].name == NULL)
1202 goto err_str;
Liam Girdwood8a978232015-05-29 19:06:14 +01001203 kc[i].private_value = (long)sm;
1204 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1205 kc[i].access = mc->hdr.access;
1206
1207 /* we only support FL/FR channel mapping atm */
1208 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1209 SNDRV_CHMAP_FL);
1210 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1211 SNDRV_CHMAP_FR);
1212 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1213 SNDRV_CHMAP_FL);
1214 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1215 SNDRV_CHMAP_FR);
1216
1217 sm->max = mc->max;
1218 sm->min = mc->min;
1219 sm->invert = mc->invert;
1220 sm->platform_max = mc->platform_max;
1221 sm->dobj.index = tplg->index;
1222 INIT_LIST_HEAD(&sm->dobj.list);
1223
1224 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001225 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001226 if (err) {
1227 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1228 kfree(sm);
1229 continue;
1230 }
1231
1232 /* pass control to driver for optional further init */
1233 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1234 (struct snd_soc_tplg_ctl_hdr *)mc);
1235 if (err < 0) {
1236 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1237 mc->hdr.name);
1238 kfree(sm);
1239 continue;
1240 }
Ranjani Sridharanbde8b382018-03-09 11:11:17 -08001241
1242 /* create any TLV data */
1243 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01001244 }
1245 return kc;
1246
1247err_str:
1248 kfree(sm);
1249err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001250 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001251 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001252 kfree(kc[i].name);
1253 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001254 kfree(kc);
1255 return NULL;
1256}
1257
1258static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001259 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001260{
1261 struct snd_kcontrol_new *kc;
1262 struct snd_soc_tplg_enum_control *ec;
1263 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001264 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001265
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001266 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001267 if (kc == NULL)
1268 return NULL;
1269
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001270 for (i = 0; i < num_kcontrols; i++) {
1271 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1272 /* validate kcontrol */
1273 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1274 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Christophe JAILLETf3ee9092017-09-14 22:44:24 +02001275 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001276
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001277 se = kzalloc(sizeof(*se), GFP_KERNEL);
1278 if (se == NULL)
1279 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001280
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001281 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001282 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001283
Liam Girdwood267e2c62018-03-27 12:04:04 +01001284 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001285 if (kc[i].name == NULL) {
1286 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001287 goto err_se;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001288 }
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001289 kc[i].private_value = (long)se;
1290 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1291 kc[i].access = ec->hdr.access;
1292
1293 /* we only support FL/FR channel mapping atm */
1294 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1295 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1296 SNDRV_CHMAP_FL);
1297 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1298 SNDRV_CHMAP_FR);
1299
1300 se->items = ec->items;
1301 se->mask = ec->mask;
1302 se->dobj.index = tplg->index;
1303
1304 switch (ec->hdr.ops.info) {
1305 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1306 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1307 err = soc_tplg_denum_create_values(se, ec);
1308 if (err < 0) {
1309 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1310 ec->hdr.name);
1311 goto err_se;
1312 }
1313 /* fall through to create texts */
1314 case SND_SOC_TPLG_CTL_ENUM:
1315 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1316 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1317 err = soc_tplg_denum_create_texts(se, ec);
1318 if (err < 0) {
1319 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1320 ec->hdr.name);
1321 goto err_se;
1322 }
1323 break;
1324 default:
1325 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1326 ec->hdr.ops.info, ec->hdr.name);
1327 goto err_se;
1328 }
1329
1330 /* map io handlers */
1331 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1332 if (err) {
1333 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1334 goto err_se;
1335 }
1336
1337 /* pass control to driver for optional further init */
1338 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1339 (struct snd_soc_tplg_ctl_hdr *)ec);
1340 if (err < 0) {
1341 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1342 ec->hdr.name);
1343 goto err_se;
1344 }
1345
1346 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1347 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001348 }
1349
1350 return kc;
1351
1352err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001353 for (; i >= 0; i--) {
1354 /* free values and texts */
1355 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001356 if (!se)
1357 continue;
1358
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001359 kfree(se->dobj.control.dvalues);
1360 for (j = 0; j < ec->items; j++)
1361 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001362
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001363 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001364 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001365 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001366err:
1367 kfree(kc);
1368
1369 return NULL;
1370}
1371
1372static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1373 struct soc_tplg *tplg, int count)
1374{
1375 struct snd_soc_tplg_bytes_control *be;
1376 struct soc_bytes_ext *sbe;
1377 struct snd_kcontrol_new *kc;
1378 int i, err;
1379
Axel Lin4ca7deb2015-08-05 22:34:22 +08001380 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001381 if (!kc)
1382 return NULL;
1383
1384 for (i = 0; i < count; i++) {
1385 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1386
1387 /* validate kcontrol */
1388 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1389 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1390 goto err;
1391
1392 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1393 if (sbe == NULL)
1394 goto err;
1395
1396 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1397 be->priv.size);
1398
1399 dev_dbg(tplg->dev,
1400 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1401 be->hdr.name, be->hdr.access);
1402
Liam Girdwood267e2c62018-03-27 12:04:04 +01001403 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001404 if (kc[i].name == NULL) {
1405 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001406 goto err;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001407 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001408 kc[i].private_value = (long)sbe;
1409 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1410 kc[i].access = be->hdr.access;
1411
1412 sbe->max = be->max;
1413 INIT_LIST_HEAD(&sbe->dobj.list);
1414
1415 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001416 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001417 if (err) {
1418 soc_control_err(tplg, &be->hdr, be->hdr.name);
1419 kfree(sbe);
1420 continue;
1421 }
1422
1423 /* pass control to driver for optional further init */
1424 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1425 (struct snd_soc_tplg_ctl_hdr *)be);
1426 if (err < 0) {
1427 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1428 be->hdr.name);
1429 kfree(sbe);
1430 continue;
1431 }
1432 }
1433
1434 return kc;
1435
1436err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001437 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001438 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001439 kfree(kc[i].name);
1440 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001441
1442 kfree(kc);
1443 return NULL;
1444}
1445
1446static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1447 struct snd_soc_tplg_dapm_widget *w)
1448{
1449 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1450 struct snd_soc_dapm_widget template, *widget;
1451 struct snd_soc_tplg_ctl_hdr *control_hdr;
1452 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001453 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001454 int ret = 0;
1455
1456 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1457 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1458 return -EINVAL;
1459 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1460 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1461 return -EINVAL;
1462
1463 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1464 w->name, w->id);
1465
1466 memset(&template, 0, sizeof(template));
1467
1468 /* map user to kernel widget ID */
1469 template.id = get_widget_id(w->id);
1470 if (template.id < 0)
1471 return template.id;
1472
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001473 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001474 template.name = kstrdup(w->name, GFP_KERNEL);
1475 if (!template.name)
1476 return -ENOMEM;
1477 template.sname = kstrdup(w->sname, GFP_KERNEL);
1478 if (!template.sname) {
1479 ret = -ENOMEM;
1480 goto err;
1481 }
1482 template.reg = w->reg;
1483 template.shift = w->shift;
1484 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001485 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001486 template.on_val = w->invert ? 0 : 1;
1487 template.off_val = w->invert ? 1 : 0;
1488 template.ignore_suspend = w->ignore_suspend;
1489 template.event_flags = w->event_flags;
1490 template.dobj.index = tplg->index;
1491
1492 tplg->pos +=
1493 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1494 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001495 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001496 template.num_kcontrols = 0;
1497 goto widget;
1498 }
1499
1500 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1501 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1502 w->name, w->num_kcontrols, control_hdr->type);
1503
1504 switch (control_hdr->ops.info) {
1505 case SND_SOC_TPLG_CTL_VOLSW:
1506 case SND_SOC_TPLG_CTL_STROBE:
1507 case SND_SOC_TPLG_CTL_VOLSW_SX:
1508 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1509 case SND_SOC_TPLG_CTL_RANGE:
1510 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001511 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Liam Girdwood8a978232015-05-29 19:06:14 +01001512 template.num_kcontrols = w->num_kcontrols;
1513 template.kcontrol_news =
1514 soc_tplg_dapm_widget_dmixer_create(tplg,
1515 template.num_kcontrols);
1516 if (!template.kcontrol_news) {
1517 ret = -ENOMEM;
1518 goto hdr_err;
1519 }
1520 break;
1521 case SND_SOC_TPLG_CTL_ENUM:
1522 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1523 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1524 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1525 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001526 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001527 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001528 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001529 soc_tplg_dapm_widget_denum_create(tplg,
1530 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001531 if (!template.kcontrol_news) {
1532 ret = -ENOMEM;
1533 goto hdr_err;
1534 }
1535 break;
1536 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001537 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Liam Girdwood8a978232015-05-29 19:06:14 +01001538 template.num_kcontrols = w->num_kcontrols;
1539 template.kcontrol_news =
1540 soc_tplg_dapm_widget_dbytes_create(tplg,
1541 template.num_kcontrols);
1542 if (!template.kcontrol_news) {
1543 ret = -ENOMEM;
1544 goto hdr_err;
1545 }
1546 break;
1547 default:
1548 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1549 control_hdr->ops.get, control_hdr->ops.put,
1550 control_hdr->ops.info);
1551 ret = -EINVAL;
1552 goto hdr_err;
1553 }
1554
1555widget:
1556 ret = soc_tplg_widget_load(tplg, &template, w);
1557 if (ret < 0)
1558 goto hdr_err;
1559
1560 /* card dapm mutex is held by the core if we are loading topology
1561 * data during sound card init. */
1562 if (card->instantiated)
1563 widget = snd_soc_dapm_new_control(dapm, &template);
1564 else
1565 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001566 if (IS_ERR(widget)) {
1567 ret = PTR_ERR(widget);
1568 /* Do not nag about probe deferrals */
1569 if (ret != -EPROBE_DEFER)
1570 dev_err(tplg->dev,
1571 "ASoC: failed to create widget %s controls (%d)\n",
1572 w->name, ret);
1573 goto hdr_err;
1574 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001575 if (widget == NULL) {
1576 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1577 w->name);
Wei Yongjun8ae3ea42016-08-10 13:43:12 +00001578 ret = -ENOMEM;
Liam Girdwood8a978232015-05-29 19:06:14 +01001579 goto hdr_err;
1580 }
1581
1582 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001583 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001584 widget->dobj.ops = tplg->ops;
1585 widget->dobj.index = tplg->index;
1586 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001587
1588 ret = soc_tplg_widget_ready(tplg, widget, w);
1589 if (ret < 0)
1590 goto ready_err;
1591
Liam Girdwood8a978232015-05-29 19:06:14 +01001592 return 0;
1593
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001594ready_err:
1595 snd_soc_tplg_widget_remove(widget);
1596 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001597hdr_err:
1598 kfree(template.sname);
1599err:
1600 kfree(template.name);
1601 return ret;
1602}
1603
1604static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1605 struct snd_soc_tplg_hdr *hdr)
1606{
1607 struct snd_soc_tplg_dapm_widget *widget;
1608 int ret, count = hdr->count, i;
1609
1610 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1611 return 0;
1612
1613 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1614
1615 for (i = 0; i < count; i++) {
1616 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001617 if (widget->size != sizeof(*widget)) {
1618 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1619 return -EINVAL;
1620 }
1621
Liam Girdwood8a978232015-05-29 19:06:14 +01001622 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001623 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001624 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1625 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001626 return ret;
1627 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001628 }
1629
1630 return 0;
1631}
1632
1633static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1634{
1635 struct snd_soc_card *card = tplg->comp->card;
1636 int ret;
1637
1638 /* Card might not have been registered at this point.
1639 * If so, just return success.
1640 */
1641 if (!card || !card->instantiated) {
1642 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001643 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001644 return 0;
1645 }
1646
1647 ret = snd_soc_dapm_new_widgets(card);
1648 if (ret < 0)
1649 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1650 ret);
1651
1652 return 0;
1653}
1654
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001655static void set_stream_info(struct snd_soc_pcm_stream *stream,
1656 struct snd_soc_tplg_stream_caps *caps)
1657{
1658 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1659 stream->channels_min = caps->channels_min;
1660 stream->channels_max = caps->channels_max;
1661 stream->rates = caps->rates;
1662 stream->rate_min = caps->rate_min;
1663 stream->rate_max = caps->rate_max;
1664 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001665 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001666}
1667
Mengdong Lin0038be92016-07-26 14:32:37 +08001668static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1669 unsigned int flag_mask, unsigned int flags)
1670{
1671 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1672 dai_drv->symmetric_rates =
1673 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1674
1675 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1676 dai_drv->symmetric_channels =
1677 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1678 1 : 0;
1679
1680 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1681 dai_drv->symmetric_samplebits =
1682 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1683 1 : 0;
1684}
1685
Mengdong Lin64527e82016-01-15 16:13:28 +08001686static int soc_tplg_dai_create(struct soc_tplg *tplg,
1687 struct snd_soc_tplg_pcm *pcm)
1688{
1689 struct snd_soc_dai_driver *dai_drv;
1690 struct snd_soc_pcm_stream *stream;
1691 struct snd_soc_tplg_stream_caps *caps;
1692 int ret;
1693
1694 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1695 if (dai_drv == NULL)
1696 return -ENOMEM;
1697
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001698 if (strlen(pcm->dai_name))
1699 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001700 dai_drv->id = pcm->dai_id;
1701
1702 if (pcm->playback) {
1703 stream = &dai_drv->playback;
1704 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001705 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001706 }
1707
1708 if (pcm->capture) {
1709 stream = &dai_drv->capture;
1710 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001711 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001712 }
1713
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001714 if (pcm->compress)
1715 dai_drv->compress_new = snd_soc_new_compress;
1716
Mengdong Lin64527e82016-01-15 16:13:28 +08001717 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001718 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001719 if (ret < 0) {
1720 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1721 kfree(dai_drv);
1722 return ret;
1723 }
1724
1725 dai_drv->dobj.index = tplg->index;
1726 dai_drv->dobj.ops = tplg->ops;
1727 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1728 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1729
1730 /* register the DAI to the component */
1731 return snd_soc_register_dai(tplg->comp, dai_drv);
1732}
1733
Mengdong Lin717a8e72016-11-03 01:03:34 +08001734static void set_link_flags(struct snd_soc_dai_link *link,
1735 unsigned int flag_mask, unsigned int flags)
1736{
1737 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1738 link->symmetric_rates =
1739 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1740
1741 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1742 link->symmetric_channels =
1743 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1744 1 : 0;
1745
1746 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1747 link->symmetric_samplebits =
1748 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1749 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001750
1751 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1752 link->ignore_suspend =
1753 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1754 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001755}
1756
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001757/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001758static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001759 struct snd_soc_tplg_pcm *pcm)
1760{
1761 struct snd_soc_dai_link *link;
1762 int ret;
1763
1764 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1765 if (link == NULL)
1766 return -ENOMEM;
1767
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001768 if (strlen(pcm->pcm_name)) {
1769 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1770 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1771 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001772 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001773
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001774 if (strlen(pcm->dai_name))
1775 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1776
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001777 link->codec_name = "snd-soc-dummy";
1778 link->codec_dai_name = "snd-soc-dummy-dai";
1779
1780 /* enable DPCM */
1781 link->dynamic = 1;
1782 link->dpcm_playback = pcm->playback;
1783 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001784 if (pcm->flag_mask)
1785 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001786
Mengdong Linacfc7d42016-01-15 16:13:37 +08001787 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001788 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001789 if (ret < 0) {
1790 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1791 kfree(link);
1792 return ret;
1793 }
1794
1795 link->dobj.index = tplg->index;
1796 link->dobj.ops = tplg->ops;
1797 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1798 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1799
1800 snd_soc_add_dai_link(tplg->comp->card, link);
1801 return 0;
1802}
1803
1804/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001805static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1806 struct snd_soc_tplg_pcm *pcm)
1807{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001808 int ret;
1809
1810 ret = soc_tplg_dai_create(tplg, pcm);
1811 if (ret < 0)
1812 return ret;
1813
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001814 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001815}
1816
Mengdong Lin55726dc2016-11-03 01:00:16 +08001817/* copy stream caps from the old version 4 of source */
1818static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1819 struct snd_soc_tplg_stream_caps_v4 *src)
1820{
1821 dest->size = sizeof(*dest);
1822 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1823 dest->formats = src->formats;
1824 dest->rates = src->rates;
1825 dest->rate_min = src->rate_min;
1826 dest->rate_max = src->rate_max;
1827 dest->channels_min = src->channels_min;
1828 dest->channels_max = src->channels_max;
1829 dest->periods_min = src->periods_min;
1830 dest->periods_max = src->periods_max;
1831 dest->period_size_min = src->period_size_min;
1832 dest->period_size_max = src->period_size_max;
1833 dest->buffer_size_min = src->buffer_size_min;
1834 dest->buffer_size_max = src->buffer_size_max;
1835}
1836
1837/**
1838 * pcm_new_ver - Create the new version of PCM from the old version.
1839 * @tplg: topology context
1840 * @src: older version of pcm as a source
1841 * @pcm: latest version of pcm created from the source
1842 *
1843 * Support from vesion 4. User should free the returned pcm manually.
1844 */
1845static int pcm_new_ver(struct soc_tplg *tplg,
1846 struct snd_soc_tplg_pcm *src,
1847 struct snd_soc_tplg_pcm **pcm)
1848{
1849 struct snd_soc_tplg_pcm *dest;
1850 struct snd_soc_tplg_pcm_v4 *src_v4;
1851 int i;
1852
1853 *pcm = NULL;
1854
1855 if (src->size != sizeof(*src_v4)) {
1856 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1857 return -EINVAL;
1858 }
1859
1860 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1861 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1862 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1863 if (!dest)
1864 return -ENOMEM;
1865
1866 dest->size = sizeof(*dest); /* size of latest abi version */
1867 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1868 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1869 dest->pcm_id = src_v4->pcm_id;
1870 dest->dai_id = src_v4->dai_id;
1871 dest->playback = src_v4->playback;
1872 dest->capture = src_v4->capture;
1873 dest->compress = src_v4->compress;
1874 dest->num_streams = src_v4->num_streams;
1875 for (i = 0; i < dest->num_streams; i++)
1876 memcpy(&dest->stream[i], &src_v4->stream[i],
1877 sizeof(struct snd_soc_tplg_stream));
1878
1879 for (i = 0; i < 2; i++)
1880 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1881
1882 *pcm = dest;
1883 return 0;
1884}
1885
Mengdong Lin64527e82016-01-15 16:13:28 +08001886static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001887 struct snd_soc_tplg_hdr *hdr)
1888{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001889 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001890 int count = hdr->count;
Vinod Koulfd340452016-12-08 23:01:27 +05301891 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001892 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001893
1894 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1895 return 0;
1896
Mengdong Lin55726dc2016-11-03 01:00:16 +08001897 /* check the element size and count */
1898 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1899 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1900 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1901 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1902 pcm->size);
1903 return -EINVAL;
1904 }
1905
Liam Girdwood8a978232015-05-29 19:06:14 +01001906 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08001907 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001908 hdr->payload_size, "PCM DAI")) {
1909 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1910 count);
1911 return -EINVAL;
1912 }
1913
Mengdong Lin64527e82016-01-15 16:13:28 +08001914 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001915 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1916
1917 /* check ABI version by size, create a new version of pcm
1918 * if abi not match.
1919 */
1920 if (pcm->size == sizeof(*pcm)) {
1921 abi_match = true;
1922 _pcm = pcm;
1923 } else {
1924 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05301925 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001926 }
1927
Mengdong Lin55726dc2016-11-03 01:00:16 +08001928 /* create the FE DAIs and DAI links */
1929 soc_tplg_pcm_create(tplg, _pcm);
1930
Mengdong Lin717a8e72016-11-03 01:03:34 +08001931 /* offset by version-specific struct size and
1932 * real priv data size
1933 */
1934 tplg->pos += pcm->size + _pcm->priv.size;
1935
Mengdong Lin55726dc2016-11-03 01:00:16 +08001936 if (!abi_match)
1937 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08001938 }
1939
Liam Girdwood8a978232015-05-29 19:06:14 +01001940 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001941
Liam Girdwood8a978232015-05-29 19:06:14 +01001942 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001943}
1944
Mengdong Lin593d9e52016-11-03 01:04:27 +08001945/**
1946 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00001947 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08001948 * @cfg: physical link configs.
1949 *
1950 * Topology context contains a list of supported HW formats (configs) and
1951 * a default format ID for the physical link. This function will use this
1952 * default ID to choose the HW format to set the link's DAI format for init.
1953 */
1954static void set_link_hw_format(struct snd_soc_dai_link *link,
1955 struct snd_soc_tplg_link_config *cfg)
1956{
1957 struct snd_soc_tplg_hw_config *hw_config;
1958 unsigned char bclk_master, fsync_master;
1959 unsigned char invert_bclk, invert_fsync;
1960 int i;
1961
1962 for (i = 0; i < cfg->num_hw_configs; i++) {
1963 hw_config = &cfg->hw_config[i];
1964 if (hw_config->id != cfg->default_hw_config_id)
1965 continue;
1966
1967 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
1968
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001969 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02001970 switch (hw_config->clock_gated) {
1971 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001972 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02001973 break;
1974
1975 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001976 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02001977 break;
1978
1979 default:
1980 /* ignore the value */
1981 break;
1982 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001983
Mengdong Lin593d9e52016-11-03 01:04:27 +08001984 /* clock signal polarity */
1985 invert_bclk = hw_config->invert_bclk;
1986 invert_fsync = hw_config->invert_fsync;
1987 if (!invert_bclk && !invert_fsync)
1988 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1989 else if (!invert_bclk && invert_fsync)
1990 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1991 else if (invert_bclk && !invert_fsync)
1992 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1993 else
1994 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1995
1996 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02001997 bclk_master = (hw_config->bclk_master ==
1998 SND_SOC_TPLG_BCLK_CM);
1999 fsync_master = (hw_config->fsync_master ==
2000 SND_SOC_TPLG_FSYNC_CM);
2001 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002002 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002003 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002004 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2005 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002006 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2007 else
2008 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2009 }
2010}
2011
2012/**
2013 * link_new_ver - Create a new physical link config from the old
2014 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002015 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002016 * @src: old version of phyical link config as a source
2017 * @link: latest version of physical link config created from the source
2018 *
2019 * Support from vesion 4. User need free the returned link config manually.
2020 */
2021static int link_new_ver(struct soc_tplg *tplg,
2022 struct snd_soc_tplg_link_config *src,
2023 struct snd_soc_tplg_link_config **link)
2024{
2025 struct snd_soc_tplg_link_config *dest;
2026 struct snd_soc_tplg_link_config_v4 *src_v4;
2027 int i;
2028
2029 *link = NULL;
2030
2031 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2032 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2033 return -EINVAL;
2034 }
2035
2036 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2037
2038 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2039 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2040 if (!dest)
2041 return -ENOMEM;
2042
2043 dest->size = sizeof(*dest);
2044 dest->id = src_v4->id;
2045 dest->num_streams = src_v4->num_streams;
2046 for (i = 0; i < dest->num_streams; i++)
2047 memcpy(&dest->stream[i], &src_v4->stream[i],
2048 sizeof(struct snd_soc_tplg_stream));
2049
2050 *link = dest;
2051 return 0;
2052}
2053
2054/* Find and configure an existing physical DAI link */
2055static int soc_tplg_link_config(struct soc_tplg *tplg,
2056 struct snd_soc_tplg_link_config *cfg)
2057{
2058 struct snd_soc_dai_link *link;
2059 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002060 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002061 int ret;
2062
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002063 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2064 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2065 return -EINVAL;
2066 else if (len)
2067 name = cfg->name;
2068 else
2069 name = NULL;
2070
2071 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2072 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2073 return -EINVAL;
2074 else if (len)
2075 stream_name = cfg->stream_name;
2076 else
2077 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002078
2079 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2080 name, stream_name);
2081 if (!link) {
2082 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2083 name, cfg->id);
2084 return -EINVAL;
2085 }
2086
2087 /* hw format */
2088 if (cfg->num_hw_configs)
2089 set_link_hw_format(link, cfg);
2090
2091 /* flags */
2092 if (cfg->flag_mask)
2093 set_link_flags(link, cfg->flag_mask, cfg->flags);
2094
2095 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002096 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002097 if (ret < 0) {
2098 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2099 return ret;
2100 }
2101
2102 return 0;
2103}
2104
2105
2106/* Load physical link config elements from the topology context */
2107static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2108 struct snd_soc_tplg_hdr *hdr)
2109{
2110 struct snd_soc_tplg_link_config *link, *_link;
2111 int count = hdr->count;
2112 int i, ret;
2113 bool abi_match;
2114
2115 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2116 tplg->pos += hdr->size + hdr->payload_size;
2117 return 0;
2118 };
2119
2120 /* check the element size and count */
2121 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2122 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2123 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2124 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2125 link->size);
2126 return -EINVAL;
2127 }
2128
2129 if (soc_tplg_check_elem_count(tplg,
2130 link->size, count,
2131 hdr->payload_size, "physical link config")) {
2132 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2133 count);
2134 return -EINVAL;
2135 }
2136
2137 /* config physical DAI links */
2138 for (i = 0; i < count; i++) {
2139 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2140 if (link->size == sizeof(*link)) {
2141 abi_match = true;
2142 _link = link;
2143 } else {
2144 abi_match = false;
2145 ret = link_new_ver(tplg, link, &_link);
2146 if (ret < 0)
2147 return ret;
2148 }
2149
2150 ret = soc_tplg_link_config(tplg, _link);
2151 if (ret < 0)
2152 return ret;
2153
2154 /* offset by version-specific struct size and
2155 * real priv data size
2156 */
2157 tplg->pos += link->size + _link->priv.size;
2158
2159 if (!abi_match)
2160 kfree(_link); /* free the duplicated one */
2161 }
2162
2163 return 0;
2164}
2165
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002166/**
2167 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002168 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002169 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002170 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002171 * The physical dai should already be registered by the platform driver.
2172 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002173 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002174static int soc_tplg_dai_config(struct soc_tplg *tplg,
2175 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002176{
2177 struct snd_soc_dai_link_component dai_component = {0};
2178 struct snd_soc_dai *dai;
2179 struct snd_soc_dai_driver *dai_drv;
2180 struct snd_soc_pcm_stream *stream;
2181 struct snd_soc_tplg_stream_caps *caps;
2182 int ret;
2183
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002184 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002185 dai = snd_soc_find_dai(&dai_component);
2186 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002187 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2188 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002189 return -EINVAL;
2190 }
2191
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002192 if (d->dai_id != dai->id) {
2193 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2194 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002195 return -EINVAL;
2196 }
2197
2198 dai_drv = dai->driver;
2199 if (!dai_drv)
2200 return -EINVAL;
2201
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002202 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002203 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002204 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002205 set_stream_info(stream, caps);
2206 }
2207
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002208 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002209 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002210 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002211 set_stream_info(stream, caps);
2212 }
2213
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002214 if (d->flag_mask)
2215 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002216
2217 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002218 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002219 if (ret < 0) {
2220 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2221 return ret;
2222 }
2223
2224 return 0;
2225}
2226
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002227/* load physical DAI elements */
2228static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2229 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002230{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002231 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002232 int count = hdr->count;
2233 int i;
2234
2235 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2236 return 0;
2237
2238 /* config the existing BE DAIs */
2239 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002240 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2241 if (dai->size != sizeof(*dai)) {
2242 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002243 return -EINVAL;
2244 }
2245
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002246 soc_tplg_dai_config(tplg, dai);
2247 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002248 }
2249
2250 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2251 return 0;
2252}
2253
Mengdong Lin583958f2016-10-11 14:36:42 +08002254/**
2255 * manifest_new_ver - Create a new version of manifest from the old version
2256 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002257 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002258 * @src: old version of manifest as a source
2259 * @manifest: latest version of manifest created from the source
2260 *
2261 * Support from vesion 4. Users need free the returned manifest manually.
2262 */
2263static int manifest_new_ver(struct soc_tplg *tplg,
2264 struct snd_soc_tplg_manifest *src,
2265 struct snd_soc_tplg_manifest **manifest)
2266{
2267 struct snd_soc_tplg_manifest *dest;
2268 struct snd_soc_tplg_manifest_v4 *src_v4;
2269
2270 *manifest = NULL;
2271
2272 if (src->size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002273 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2274 src->size);
2275 if (src->size)
2276 return -EINVAL;
2277 src->size = sizeof(*src_v4);
Mengdong Lin583958f2016-10-11 14:36:42 +08002278 }
2279
2280 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2281
2282 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2283 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2284 if (!dest)
2285 return -ENOMEM;
2286
2287 dest->size = sizeof(*dest); /* size of latest abi version */
2288 dest->control_elems = src_v4->control_elems;
2289 dest->widget_elems = src_v4->widget_elems;
2290 dest->graph_elems = src_v4->graph_elems;
2291 dest->pcm_elems = src_v4->pcm_elems;
2292 dest->dai_link_elems = src_v4->dai_link_elems;
2293 dest->priv.size = src_v4->priv.size;
2294 if (dest->priv.size)
2295 memcpy(dest->priv.data, src_v4->priv.data,
2296 src_v4->priv.size);
2297
2298 *manifest = dest;
2299 return 0;
2300}
Mengdong Lin0038be92016-07-26 14:32:37 +08002301
Liam Girdwood8a978232015-05-29 19:06:14 +01002302static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002303 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002304{
Mengdong Lin583958f2016-10-11 14:36:42 +08002305 struct snd_soc_tplg_manifest *manifest, *_manifest;
2306 bool abi_match;
2307 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002308
2309 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2310 return 0;
2311
2312 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002313
2314 /* check ABI version by size, create a new manifest if abi not match */
2315 if (manifest->size == sizeof(*manifest)) {
2316 abi_match = true;
2317 _manifest = manifest;
2318 } else {
2319 abi_match = false;
2320 err = manifest_new_ver(tplg, manifest, &_manifest);
2321 if (err < 0)
2322 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002323 }
2324
Mengdong Lin583958f2016-10-11 14:36:42 +08002325 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002326 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002327 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002328
Mengdong Lin583958f2016-10-11 14:36:42 +08002329 if (!abi_match) /* free the duplicated one */
2330 kfree(_manifest);
2331
Liam Girdwood8a978232015-05-29 19:06:14 +01002332 return 0;
2333}
2334
2335/* validate header magic, size and type */
2336static int soc_valid_header(struct soc_tplg *tplg,
2337 struct snd_soc_tplg_hdr *hdr)
2338{
2339 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2340 return 0;
2341
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002342 if (hdr->size != sizeof(*hdr)) {
2343 dev_err(tplg->dev,
2344 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2345 hdr->type, soc_tplg_get_hdr_offset(tplg),
2346 tplg->fw->size);
2347 return -EINVAL;
2348 }
2349
Liam Girdwood8a978232015-05-29 19:06:14 +01002350 /* big endian firmware objects not supported atm */
2351 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2352 dev_err(tplg->dev,
2353 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2354 tplg->pass, hdr->magic,
2355 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2356 return -EINVAL;
2357 }
2358
2359 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2360 dev_err(tplg->dev,
2361 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2362 tplg->pass, hdr->magic,
2363 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2364 return -EINVAL;
2365 }
2366
Mengdong Lin288b8da2016-11-03 01:03:17 +08002367 /* Support ABI from version 4 */
2368 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2369 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002370 dev_err(tplg->dev,
2371 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2372 tplg->pass, hdr->abi,
2373 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2374 tplg->fw->size);
2375 return -EINVAL;
2376 }
2377
2378 if (hdr->payload_size == 0) {
2379 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2380 soc_tplg_get_hdr_offset(tplg));
2381 return -EINVAL;
2382 }
2383
2384 if (tplg->pass == hdr->type)
2385 dev_dbg(tplg->dev,
2386 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2387 hdr->payload_size, hdr->type, hdr->version,
2388 hdr->vendor_type, tplg->pass);
2389
2390 return 1;
2391}
2392
2393/* check header type and call appropriate handler */
2394static int soc_tplg_load_header(struct soc_tplg *tplg,
2395 struct snd_soc_tplg_hdr *hdr)
2396{
2397 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2398
2399 /* check for matching ID */
2400 if (hdr->index != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002401 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002402 return 0;
2403
2404 tplg->index = hdr->index;
2405
2406 switch (hdr->type) {
2407 case SND_SOC_TPLG_TYPE_MIXER:
2408 case SND_SOC_TPLG_TYPE_ENUM:
2409 case SND_SOC_TPLG_TYPE_BYTES:
2410 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2411 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2412 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2413 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2414 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2415 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002416 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002417 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002418 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002419 case SND_SOC_TPLG_TYPE_DAI_LINK:
2420 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2421 /* physical link configurations */
2422 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002423 case SND_SOC_TPLG_TYPE_MANIFEST:
2424 return soc_tplg_manifest_load(tplg, hdr);
2425 default:
2426 /* bespoke vendor data object */
2427 return soc_tplg_vendor_load(tplg, hdr);
2428 }
2429
2430 return 0;
2431}
2432
2433/* process the topology file headers */
2434static int soc_tplg_process_headers(struct soc_tplg *tplg)
2435{
2436 struct snd_soc_tplg_hdr *hdr;
2437 int ret;
2438
2439 tplg->pass = SOC_TPLG_PASS_START;
2440
2441 /* process the header types from start to end */
2442 while (tplg->pass <= SOC_TPLG_PASS_END) {
2443
2444 tplg->hdr_pos = tplg->fw->data;
2445 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2446
2447 while (!soc_tplg_is_eof(tplg)) {
2448
2449 /* make sure header is valid before loading */
2450 ret = soc_valid_header(tplg, hdr);
2451 if (ret < 0)
2452 return ret;
2453 else if (ret == 0)
2454 break;
2455
2456 /* load the header object */
2457 ret = soc_tplg_load_header(tplg, hdr);
2458 if (ret < 0)
2459 return ret;
2460
2461 /* goto next header */
2462 tplg->hdr_pos += hdr->payload_size +
2463 sizeof(struct snd_soc_tplg_hdr);
2464 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2465 }
2466
2467 /* next data type pass */
2468 tplg->pass++;
2469 }
2470
2471 /* signal DAPM we are complete */
2472 ret = soc_tplg_dapm_complete(tplg);
2473 if (ret < 0)
2474 dev_err(tplg->dev,
2475 "ASoC: failed to initialise DAPM from Firmware\n");
2476
2477 return ret;
2478}
2479
2480static int soc_tplg_load(struct soc_tplg *tplg)
2481{
2482 int ret;
2483
2484 ret = soc_tplg_process_headers(tplg);
2485 if (ret == 0)
2486 soc_tplg_complete(tplg);
2487
2488 return ret;
2489}
2490
2491/* load audio component topology from "firmware" file */
2492int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2493 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2494{
2495 struct soc_tplg tplg;
2496
2497 /* setup parsing context */
2498 memset(&tplg, 0, sizeof(tplg));
2499 tplg.fw = fw;
2500 tplg.dev = comp->dev;
2501 tplg.comp = comp;
2502 tplg.ops = ops;
2503 tplg.req_index = id;
2504 tplg.io_ops = ops->io_ops;
2505 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002506 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2507 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002508
2509 return soc_tplg_load(&tplg);
2510}
2511EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2512
2513/* remove this dynamic widget */
2514void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2515{
2516 /* make sure we are a widget */
2517 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2518 return;
2519
2520 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2521}
2522EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2523
2524/* remove all dynamic widgets from this DAPM context */
2525void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2526 u32 index)
2527{
2528 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002529
2530 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2531
2532 /* make sure we are a widget with correct context */
2533 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2534 continue;
2535
2536 /* match ID */
2537 if (w->dobj.index != index &&
2538 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2539 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002540 /* check and free and dynamic widget kcontrols */
2541 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002542 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002543 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002544 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002545}
2546EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2547
2548/* remove dynamic controls from the component driver */
2549int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2550{
2551 struct snd_soc_dobj *dobj, *next_dobj;
2552 int pass = SOC_TPLG_PASS_END;
2553
2554 /* process the header types from end to start */
2555 while (pass >= SOC_TPLG_PASS_START) {
2556
2557 /* remove mixer controls */
2558 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2559 list) {
2560
2561 /* match index */
2562 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002563 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002564 continue;
2565
2566 switch (dobj->type) {
2567 case SND_SOC_DOBJ_MIXER:
2568 remove_mixer(comp, dobj, pass);
2569 break;
2570 case SND_SOC_DOBJ_ENUM:
2571 remove_enum(comp, dobj, pass);
2572 break;
2573 case SND_SOC_DOBJ_BYTES:
2574 remove_bytes(comp, dobj, pass);
2575 break;
2576 case SND_SOC_DOBJ_WIDGET:
2577 remove_widget(comp, dobj, pass);
2578 break;
2579 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002580 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002581 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002582 case SND_SOC_DOBJ_DAI_LINK:
2583 remove_link(comp, dobj, pass);
2584 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002585 default:
2586 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2587 dobj->type);
2588 break;
2589 }
2590 }
2591 pass--;
2592 }
2593
2594 /* let caller know if FW can be freed when no objects are left */
2595 return !list_empty(&comp->dobj_list);
2596}
2597EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);