blob: dd3a391476ae26bb614a17afd9caabfe0a2e6230 [file] [log] [blame]
Liam Girdwood8a978232015-05-29 19:06:14 +01001/*
2 * soc-topology.c -- ALSA SoC Topology
3 *
4 * Copyright (C) 2012 Texas Instruments Inc.
5 * Copyright (C) 2015 Intel Corporation.
6 *
7 * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
8 * K, Mythri P <mythri.p.k@intel.com>
9 * Prusty, Subhransu S <subhransu.s.prusty@intel.com>
10 * B, Jayachandran <jayachandran.b@intel.com>
11 * Abdullah, Omair M <omair.m.abdullah@intel.com>
12 * Jin, Yao <yao.jin@intel.com>
13 * Lin, Mengdong <mengdong.lin@intel.com>
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Add support to read audio firmware topology alongside firmware text. The
21 * topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
22 * equalizers, firmware, coefficients etc.
23 *
24 * This file only manages the core ALSA and ASoC components, all other bespoke
25 * firmware topology data is passed to component drivers for bespoke handling.
26 */
27
28#include <linux/kernel.h>
29#include <linux/export.h>
30#include <linux/list.h>
31#include <linux/firmware.h>
32#include <linux/slab.h>
33#include <sound/soc.h>
34#include <sound/soc-dapm.h>
35#include <sound/soc-topology.h>
Mengdong Lin28a87ee2015-08-05 14:41:13 +010036#include <sound/tlv.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010037
38/*
39 * We make several passes over the data (since it wont necessarily be ordered)
40 * and process objects in the following order. This guarantees the component
41 * drivers will be ready with any vendor data before the mixers and DAPM objects
42 * are loaded (that may make use of the vendor data).
43 */
44#define SOC_TPLG_PASS_MANIFEST 0
45#define SOC_TPLG_PASS_VENDOR 1
46#define SOC_TPLG_PASS_MIXER 2
47#define SOC_TPLG_PASS_WIDGET 3
Mengdong Lin1a8e7fa2015-08-10 22:48:30 +080048#define SOC_TPLG_PASS_PCM_DAI 4
49#define SOC_TPLG_PASS_GRAPH 5
50#define SOC_TPLG_PASS_PINS 6
Mengdong Lin0038be92016-07-26 14:32:37 +080051#define SOC_TPLG_PASS_BE_DAI 7
Mengdong Lin593d9e52016-11-03 01:04:27 +080052#define SOC_TPLG_PASS_LINK 8
Liam Girdwood8a978232015-05-29 19:06:14 +010053
54#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
Mengdong Lin593d9e52016-11-03 01:04:27 +080055#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
Liam Girdwood8a978232015-05-29 19:06:14 +010056
Mengdong Lin583958f2016-10-11 14:36:42 +080057/*
58 * Old version of ABI structs, supported for backward compatibility.
59 */
60
61/* Manifest v4 */
62struct snd_soc_tplg_manifest_v4 {
63 __le32 size; /* in bytes of this structure */
64 __le32 control_elems; /* number of control elements */
65 __le32 widget_elems; /* number of widget elements */
66 __le32 graph_elems; /* number of graph elements */
67 __le32 pcm_elems; /* number of PCM elements */
68 __le32 dai_link_elems; /* number of DAI link elements */
69 struct snd_soc_tplg_private priv;
70} __packed;
71
Mengdong Lin55726dc2016-11-03 01:00:16 +080072/* Stream Capabilities v4 */
73struct snd_soc_tplg_stream_caps_v4 {
74 __le32 size; /* in bytes of this structure */
75 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
76 __le64 formats; /* supported formats SNDRV_PCM_FMTBIT_* */
77 __le32 rates; /* supported rates SNDRV_PCM_RATE_* */
78 __le32 rate_min; /* min rate */
79 __le32 rate_max; /* max rate */
80 __le32 channels_min; /* min channels */
81 __le32 channels_max; /* max channels */
82 __le32 periods_min; /* min number of periods */
83 __le32 periods_max; /* max number of periods */
84 __le32 period_size_min; /* min period size bytes */
85 __le32 period_size_max; /* max period size bytes */
86 __le32 buffer_size_min; /* min buffer size bytes */
87 __le32 buffer_size_max; /* max buffer size bytes */
88} __packed;
89
90/* PCM v4 */
91struct snd_soc_tplg_pcm_v4 {
92 __le32 size; /* in bytes of this structure */
93 char pcm_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
94 char dai_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
95 __le32 pcm_id; /* unique ID - used to match with DAI link */
96 __le32 dai_id; /* unique ID - used to match */
97 __le32 playback; /* supports playback mode */
98 __le32 capture; /* supports capture mode */
99 __le32 compress; /* 1 = compressed; 0 = PCM */
100 struct snd_soc_tplg_stream stream[SND_SOC_TPLG_STREAM_CONFIG_MAX]; /* for DAI link */
101 __le32 num_streams; /* number of streams */
102 struct snd_soc_tplg_stream_caps_v4 caps[2]; /* playback and capture for DAI */
103} __packed;
104
Mengdong Lin593d9e52016-11-03 01:04:27 +0800105/* Physical link config v4 */
106struct snd_soc_tplg_link_config_v4 {
107 __le32 size; /* in bytes of this structure */
108 __le32 id; /* unique ID - used to match */
109 struct snd_soc_tplg_stream stream[SND_SOC_TPLG_STREAM_CONFIG_MAX]; /* supported configs playback and captrure */
110 __le32 num_streams; /* number of streams */
111} __packed;
112
Mengdong Lin583958f2016-10-11 14:36:42 +0800113/* topology context */
Liam Girdwood8a978232015-05-29 19:06:14 +0100114struct soc_tplg {
115 const struct firmware *fw;
116
117 /* runtime FW parsing */
118 const u8 *pos; /* read postion */
119 const u8 *hdr_pos; /* header position */
120 unsigned int pass; /* pass number */
121
122 /* component caller */
123 struct device *dev;
124 struct snd_soc_component *comp;
125 u32 index; /* current block index */
126 u32 req_index; /* required index, only loaded/free matching blocks */
127
Mengdong Lin88a17d82015-08-18 18:11:51 +0800128 /* vendor specific kcontrol operations */
Liam Girdwood8a978232015-05-29 19:06:14 +0100129 const struct snd_soc_tplg_kcontrol_ops *io_ops;
130 int io_ops_count;
131
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800132 /* vendor specific bytes ext handlers, for TLV bytes controls */
133 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
134 int bytes_ext_ops_count;
135
Liam Girdwood8a978232015-05-29 19:06:14 +0100136 /* optional fw loading callbacks to component drivers */
137 struct snd_soc_tplg_ops *ops;
138};
139
140static int soc_tplg_process_headers(struct soc_tplg *tplg);
141static void soc_tplg_complete(struct soc_tplg *tplg);
142struct snd_soc_dapm_widget *
143snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
144 const struct snd_soc_dapm_widget *widget);
145struct snd_soc_dapm_widget *
146snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
147 const struct snd_soc_dapm_widget *widget);
148
149/* check we dont overflow the data for this control chunk */
150static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
151 unsigned int count, size_t bytes, const char *elem_type)
152{
153 const u8 *end = tplg->pos + elem_size * count;
154
155 if (end > tplg->fw->data + tplg->fw->size) {
156 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
157 elem_type);
158 return -EINVAL;
159 }
160
161 /* check there is enough room in chunk for control.
162 extra bytes at the end of control are for vendor data here */
163 if (elem_size * count > bytes) {
164 dev_err(tplg->dev,
165 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
166 elem_type, count, elem_size, bytes);
167 return -EINVAL;
168 }
169
170 return 0;
171}
172
173static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
174{
175 const u8 *end = tplg->hdr_pos;
176
177 if (end >= tplg->fw->data + tplg->fw->size)
178 return 1;
179 return 0;
180}
181
182static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
183{
184 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
185}
186
187static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
188{
189 return (unsigned long)(tplg->pos - tplg->fw->data);
190}
191
192/* mapping of Kcontrol types and associated operations. */
193static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
194 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
195 snd_soc_put_volsw, snd_soc_info_volsw},
196 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
197 snd_soc_put_volsw_sx, NULL},
198 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
199 snd_soc_put_enum_double, snd_soc_info_enum_double},
200 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
201 snd_soc_put_enum_double, NULL},
202 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
203 snd_soc_bytes_put, snd_soc_bytes_info},
204 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
205 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
206 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
207 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
208 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
209 snd_soc_put_strobe, NULL},
210 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530211 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100212 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
213 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
214 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
215 snd_soc_dapm_put_enum_double, NULL},
216 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
217 snd_soc_dapm_put_enum_double, NULL},
218 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
219 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
220};
221
222struct soc_tplg_map {
223 int uid;
224 int kid;
225};
226
227/* mapping of widget types from UAPI IDs to kernel IDs */
228static const struct soc_tplg_map dapm_map[] = {
229 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
230 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
231 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
232 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
233 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
234 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
235 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
236 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
237 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
238 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
239 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
240 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
241 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
242 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
243 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
244 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
Liam Girdwood8a70b452017-06-29 14:22:24 +0100245 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
246 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
247 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
248 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
249 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
250 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
251 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
252 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
Liam Girdwood8a978232015-05-29 19:06:14 +0100253};
254
255static int tplc_chan_get_reg(struct soc_tplg *tplg,
256 struct snd_soc_tplg_channel *chan, int map)
257{
258 int i;
259
260 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
261 if (chan[i].id == map)
262 return chan[i].reg;
263 }
264
265 return -EINVAL;
266}
267
268static int tplc_chan_get_shift(struct soc_tplg *tplg,
269 struct snd_soc_tplg_channel *chan, int map)
270{
271 int i;
272
273 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
274 if (chan[i].id == map)
275 return chan[i].shift;
276 }
277
278 return -EINVAL;
279}
280
281static int get_widget_id(int tplg_type)
282{
283 int i;
284
285 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
286 if (tplg_type == dapm_map[i].uid)
287 return dapm_map[i].kid;
288 }
289
290 return -EINVAL;
291}
292
Liam Girdwood8a978232015-05-29 19:06:14 +0100293static inline void soc_bind_err(struct soc_tplg *tplg,
294 struct snd_soc_tplg_ctl_hdr *hdr, int index)
295{
296 dev_err(tplg->dev,
297 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
298 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
299 soc_tplg_get_offset(tplg));
300}
301
302static inline void soc_control_err(struct soc_tplg *tplg,
303 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
304{
305 dev_err(tplg->dev,
306 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
307 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
308 soc_tplg_get_offset(tplg));
309}
310
311/* pass vendor data to component driver for processing */
312static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
313 struct snd_soc_tplg_hdr *hdr)
314{
315 int ret = 0;
316
317 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
318 ret = tplg->ops->vendor_load(tplg->comp, hdr);
319 else {
320 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
321 hdr->vendor_type);
322 return -EINVAL;
323 }
324
325 if (ret < 0)
326 dev_err(tplg->dev,
327 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
328 soc_tplg_get_hdr_offset(tplg),
329 soc_tplg_get_hdr_offset(tplg),
330 hdr->type, hdr->vendor_type);
331 return ret;
332}
333
334/* pass vendor data to component driver for processing */
335static int soc_tplg_vendor_load(struct soc_tplg *tplg,
336 struct snd_soc_tplg_hdr *hdr)
337{
338 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
339 return 0;
340
341 return soc_tplg_vendor_load_(tplg, hdr);
342}
343
344/* optionally pass new dynamic widget to component driver. This is mainly for
345 * external widgets where we can assign private data/ops */
346static int soc_tplg_widget_load(struct soc_tplg *tplg,
347 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
348{
349 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
350 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
351
352 return 0;
353}
354
Masahiro Yamada183b8022017-02-27 14:29:20 -0800355/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800356static int soc_tplg_dai_load(struct soc_tplg *tplg,
357 struct snd_soc_dai_driver *dai_drv)
Liam Girdwood8a978232015-05-29 19:06:14 +0100358{
Mengdong Lin64527e82016-01-15 16:13:28 +0800359 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
360 return tplg->ops->dai_load(tplg->comp, dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100361
362 return 0;
363}
364
Masahiro Yamada183b8022017-02-27 14:29:20 -0800365/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800366static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
367 struct snd_soc_dai_link *link)
368{
369 if (tplg->comp && tplg->ops && tplg->ops->link_load)
370 return tplg->ops->link_load(tplg->comp, link);
371
372 return 0;
373}
374
Liam Girdwood8a978232015-05-29 19:06:14 +0100375/* tell the component driver that all firmware has been loaded in this request */
376static void soc_tplg_complete(struct soc_tplg *tplg)
377{
378 if (tplg->comp && tplg->ops && tplg->ops->complete)
379 tplg->ops->complete(tplg->comp);
380}
381
382/* add a dynamic kcontrol */
383static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
384 const struct snd_kcontrol_new *control_new, const char *prefix,
385 void *data, struct snd_kcontrol **kcontrol)
386{
387 int err;
388
389 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
390 if (*kcontrol == NULL) {
391 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
392 control_new->name);
393 return -ENOMEM;
394 }
395
396 err = snd_ctl_add(card, *kcontrol);
397 if (err < 0) {
398 dev_err(dev, "ASoC: Failed to add %s: %d\n",
399 control_new->name, err);
400 return err;
401 }
402
403 return 0;
404}
405
406/* add a dynamic kcontrol for component driver */
407static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
408 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
409{
410 struct snd_soc_component *comp = tplg->comp;
411
412 return soc_tplg_add_dcontrol(comp->card->snd_card,
413 comp->dev, k, NULL, comp, kcontrol);
414}
415
416/* remove a mixer kcontrol */
417static void remove_mixer(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_mixer_control *sm =
422 container_of(dobj, struct soc_mixer_control, dobj);
423 const unsigned int *p = NULL;
424
425 if (pass != SOC_TPLG_PASS_MIXER)
426 return;
427
428 if (dobj->ops && dobj->ops->control_unload)
429 dobj->ops->control_unload(comp, dobj);
430
431 if (sm->dobj.control.kcontrol->tlv.p)
432 p = sm->dobj.control.kcontrol->tlv.p;
433 snd_ctl_remove(card, sm->dobj.control.kcontrol);
434 list_del(&sm->dobj.list);
435 kfree(sm);
436 kfree(p);
437}
438
439/* remove an enum kcontrol */
440static void remove_enum(struct snd_soc_component *comp,
441 struct snd_soc_dobj *dobj, int pass)
442{
443 struct snd_card *card = comp->card->snd_card;
444 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
445 int i;
446
447 if (pass != SOC_TPLG_PASS_MIXER)
448 return;
449
450 if (dobj->ops && dobj->ops->control_unload)
451 dobj->ops->control_unload(comp, dobj);
452
453 snd_ctl_remove(card, se->dobj.control.kcontrol);
454 list_del(&se->dobj.list);
455
456 kfree(se->dobj.control.dvalues);
457 for (i = 0; i < se->items; i++)
458 kfree(se->dobj.control.dtexts[i]);
459 kfree(se);
460}
461
462/* remove a byte kcontrol */
463static void remove_bytes(struct snd_soc_component *comp,
464 struct snd_soc_dobj *dobj, int pass)
465{
466 struct snd_card *card = comp->card->snd_card;
467 struct soc_bytes_ext *sb =
468 container_of(dobj, struct soc_bytes_ext, dobj);
469
470 if (pass != SOC_TPLG_PASS_MIXER)
471 return;
472
473 if (dobj->ops && dobj->ops->control_unload)
474 dobj->ops->control_unload(comp, dobj);
475
476 snd_ctl_remove(card, sb->dobj.control.kcontrol);
477 list_del(&sb->dobj.list);
478 kfree(sb);
479}
480
481/* remove a widget and it's kcontrols - routes must be removed first */
482static void remove_widget(struct snd_soc_component *comp,
483 struct snd_soc_dobj *dobj, int pass)
484{
485 struct snd_card *card = comp->card->snd_card;
486 struct snd_soc_dapm_widget *w =
487 container_of(dobj, struct snd_soc_dapm_widget, dobj);
488 int i;
489
490 if (pass != SOC_TPLG_PASS_WIDGET)
491 return;
492
493 if (dobj->ops && dobj->ops->widget_unload)
494 dobj->ops->widget_unload(comp, dobj);
495
496 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800497 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100498 * The enum may either have an array of values or strings.
499 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800500 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100501 /* enumerated widget mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800502 for (i = 0; i < w->num_kcontrols; i++) {
503 struct snd_kcontrol *kcontrol = w->kcontrols[i];
504 struct soc_enum *se =
505 (struct soc_enum *)kcontrol->private_value;
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100506 int j;
Liam Girdwood8a978232015-05-29 19:06:14 +0100507
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800508 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100509
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800510 kfree(se->dobj.control.dvalues);
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100511 for (j = 0; j < se->items; j++)
512 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +0100513
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800514 kfree(se);
515 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100516 kfree(w->kcontrol_news);
517 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800518 /* volume mixer or bytes controls */
Liam Girdwood8a978232015-05-29 19:06:14 +0100519 for (i = 0; i < w->num_kcontrols; i++) {
520 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100521
Mengdong Lineea3dd42016-11-25 16:09:17 +0800522 if (dobj->widget.kcontrol_type
523 == SND_SOC_TPLG_TYPE_MIXER)
524 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100525
Mengdong Lineea3dd42016-11-25 16:09:17 +0800526 /* Private value is used as struct soc_mixer_control
527 * for volume mixers or soc_bytes_ext for bytes
528 * controls.
529 */
530 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000531 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100532 }
533 kfree(w->kcontrol_news);
534 }
535 /* widget w is freed by soc-dapm.c */
536}
537
Mengdong Lin64527e82016-01-15 16:13:28 +0800538/* remove DAI configurations */
539static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100540 struct snd_soc_dobj *dobj, int pass)
541{
Mengdong Lin64527e82016-01-15 16:13:28 +0800542 struct snd_soc_dai_driver *dai_drv =
543 container_of(dobj, struct snd_soc_dai_driver, dobj);
544
Liam Girdwood8a978232015-05-29 19:06:14 +0100545 if (pass != SOC_TPLG_PASS_PCM_DAI)
546 return;
547
Mengdong Lin64527e82016-01-15 16:13:28 +0800548 if (dobj->ops && dobj->ops->dai_unload)
549 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100550
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800551 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100552 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800553 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100554}
555
Mengdong Linacfc7d42016-01-15 16:13:37 +0800556/* remove link configurations */
557static void remove_link(struct snd_soc_component *comp,
558 struct snd_soc_dobj *dobj, int pass)
559{
560 struct snd_soc_dai_link *link =
561 container_of(dobj, struct snd_soc_dai_link, dobj);
562
563 if (pass != SOC_TPLG_PASS_PCM_DAI)
564 return;
565
566 if (dobj->ops && dobj->ops->link_unload)
567 dobj->ops->link_unload(comp, dobj);
568
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800569 kfree(link->name);
570 kfree(link->stream_name);
571 kfree(link->cpu_dai_name);
572
Mengdong Linacfc7d42016-01-15 16:13:37 +0800573 list_del(&dobj->list);
574 snd_soc_remove_dai_link(comp->card, link);
575 kfree(link);
576}
577
Liam Girdwood8a978232015-05-29 19:06:14 +0100578/* bind a kcontrol to it's IO handlers */
579static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
580 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800581 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100582{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800583 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800584 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800585 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100586
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800587 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
588 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
589 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
590 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
591 struct soc_bytes_ext *sbe;
592 struct snd_soc_tplg_bytes_control *be;
593
594 sbe = (struct soc_bytes_ext *)k->private_value;
595 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
596
597 /* TLV bytes controls need standard kcontrol info handler,
598 * TLV callback and extended put/get handlers.
599 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530600 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800601 k->tlv.c = snd_soc_bytes_tlv_callback;
602
603 ext_ops = tplg->bytes_ext_ops;
604 num_ops = tplg->bytes_ext_ops_count;
605 for (i = 0; i < num_ops; i++) {
606 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
607 sbe->put = ext_ops[i].put;
608 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
609 sbe->get = ext_ops[i].get;
610 }
611
612 if (sbe->put && sbe->get)
613 return 0;
614 else
615 return -EINVAL;
616 }
617
Mengdong Lin88a17d82015-08-18 18:11:51 +0800618 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800619 ops = tplg->io_ops;
620 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100621 for (i = 0; i < num_ops; i++) {
622
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800623 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100624 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800625 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100626 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800627 if (k->info == NULL && ops[i].id == hdr->ops.info)
628 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100629 }
630
Mengdong Lin88a17d82015-08-18 18:11:51 +0800631 /* vendor specific handlers found ? */
632 if (k->put && k->get && k->info)
633 return 0;
634
635 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800636 ops = io_ops;
637 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800638 for (i = 0; i < num_ops; i++) {
639
640 if (k->put == NULL && ops[i].id == hdr->ops.put)
641 k->put = ops[i].put;
642 if (k->get == NULL && ops[i].id == hdr->ops.get)
643 k->get = ops[i].get;
644 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100645 k->info = ops[i].info;
646 }
647
648 /* standard handlers found ? */
649 if (k->put && k->get && k->info)
650 return 0;
651
Liam Girdwood8a978232015-05-29 19:06:14 +0100652 /* nothing to bind */
653 return -EINVAL;
654}
655
656/* bind a widgets to it's evnt handlers */
657int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
658 const struct snd_soc_tplg_widget_events *events,
659 int num_events, u16 event_type)
660{
661 int i;
662
663 w->event = NULL;
664
665 for (i = 0; i < num_events; i++) {
666 if (event_type == events[i].type) {
667
668 /* found - so assign event */
669 w->event = events[i].event_handler;
670 return 0;
671 }
672 }
673
674 /* not found */
675 return -EINVAL;
676}
677EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
678
679/* optionally pass new dynamic kcontrol to component driver. */
680static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
681 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
682{
683 if (tplg->comp && tplg->ops && tplg->ops->control_load)
684 return tplg->ops->control_load(tplg->comp, k, hdr);
685
686 return 0;
687}
688
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100689
690static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
691 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100692{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100693 unsigned int item_len = 2 * sizeof(unsigned int);
694 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100695
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100696 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
697 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100698 return -ENOMEM;
699
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100700 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
701 p[1] = item_len;
702 p[2] = scale->min;
703 p[3] = (scale->step & TLV_DB_SCALE_MASK)
704 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100705
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100706 kc->tlv.p = (void *)p;
707 return 0;
708}
709
710static int soc_tplg_create_tlv(struct soc_tplg *tplg,
711 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
712{
713 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
714
715 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
716 return 0;
717
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800718 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100719 tplg_tlv = &tc->tlv;
720 switch (tplg_tlv->type) {
721 case SNDRV_CTL_TLVT_DB_SCALE:
722 return soc_tplg_create_tlv_db_scale(tplg, kc,
723 &tplg_tlv->scale);
724
725 /* TODO: add support for other TLV types */
726 default:
727 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
728 tplg_tlv->type);
729 return -EINVAL;
730 }
731 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100732
733 return 0;
734}
735
736static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
737 struct snd_kcontrol_new *kc)
738{
739 kfree(kc->tlv.p);
740}
741
742static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
743 size_t size)
744{
745 struct snd_soc_tplg_bytes_control *be;
746 struct soc_bytes_ext *sbe;
747 struct snd_kcontrol_new kc;
748 int i, err;
749
750 if (soc_tplg_check_elem_count(tplg,
751 sizeof(struct snd_soc_tplg_bytes_control), count,
752 size, "mixer bytes")) {
753 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
754 count);
755 return -EINVAL;
756 }
757
758 for (i = 0; i < count; i++) {
759 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
760
761 /* validate kcontrol */
762 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
763 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
764 return -EINVAL;
765
766 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
767 if (sbe == NULL)
768 return -ENOMEM;
769
770 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
771 be->priv.size);
772
773 dev_dbg(tplg->dev,
774 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
775 be->hdr.name, be->hdr.access);
776
777 memset(&kc, 0, sizeof(kc));
778 kc.name = be->hdr.name;
779 kc.private_value = (long)sbe;
780 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
781 kc.access = be->hdr.access;
782
783 sbe->max = be->max;
784 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
785 sbe->dobj.ops = tplg->ops;
786 INIT_LIST_HEAD(&sbe->dobj.list);
787
788 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800789 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100790 if (err) {
791 soc_control_err(tplg, &be->hdr, be->hdr.name);
792 kfree(sbe);
793 continue;
794 }
795
796 /* pass control to driver for optional further init */
797 err = soc_tplg_init_kcontrol(tplg, &kc,
798 (struct snd_soc_tplg_ctl_hdr *)be);
799 if (err < 0) {
800 dev_err(tplg->dev, "ASoC: failed to init %s\n",
801 be->hdr.name);
802 kfree(sbe);
803 continue;
804 }
805
806 /* register control here */
807 err = soc_tplg_add_kcontrol(tplg, &kc,
808 &sbe->dobj.control.kcontrol);
809 if (err < 0) {
810 dev_err(tplg->dev, "ASoC: failed to add %s\n",
811 be->hdr.name);
812 kfree(sbe);
813 continue;
814 }
815
816 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
817 }
818 return 0;
819
820}
821
822static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
823 size_t size)
824{
825 struct snd_soc_tplg_mixer_control *mc;
826 struct soc_mixer_control *sm;
827 struct snd_kcontrol_new kc;
828 int i, err;
829
830 if (soc_tplg_check_elem_count(tplg,
831 sizeof(struct snd_soc_tplg_mixer_control),
832 count, size, "mixers")) {
833
834 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
835 count);
836 return -EINVAL;
837 }
838
839 for (i = 0; i < count; i++) {
840 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
841
842 /* validate kcontrol */
843 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
844 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
845 return -EINVAL;
846
847 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
848 if (sm == NULL)
849 return -ENOMEM;
850 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
851 mc->priv.size);
852
853 dev_dbg(tplg->dev,
854 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
855 mc->hdr.name, mc->hdr.access);
856
857 memset(&kc, 0, sizeof(kc));
858 kc.name = mc->hdr.name;
859 kc.private_value = (long)sm;
860 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
861 kc.access = mc->hdr.access;
862
863 /* we only support FL/FR channel mapping atm */
864 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
865 SNDRV_CHMAP_FL);
866 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
867 SNDRV_CHMAP_FR);
868 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
869 SNDRV_CHMAP_FL);
870 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
871 SNDRV_CHMAP_FR);
872
873 sm->max = mc->max;
874 sm->min = mc->min;
875 sm->invert = mc->invert;
876 sm->platform_max = mc->platform_max;
877 sm->dobj.index = tplg->index;
878 sm->dobj.ops = tplg->ops;
879 sm->dobj.type = SND_SOC_DOBJ_MIXER;
880 INIT_LIST_HEAD(&sm->dobj.list);
881
882 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800883 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100884 if (err) {
885 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
886 kfree(sm);
887 continue;
888 }
889
890 /* pass control to driver for optional further init */
891 err = soc_tplg_init_kcontrol(tplg, &kc,
892 (struct snd_soc_tplg_ctl_hdr *) mc);
893 if (err < 0) {
894 dev_err(tplg->dev, "ASoC: failed to init %s\n",
895 mc->hdr.name);
896 kfree(sm);
897 continue;
898 }
899
900 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100901 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100902
903 /* register control here */
904 err = soc_tplg_add_kcontrol(tplg, &kc,
905 &sm->dobj.control.kcontrol);
906 if (err < 0) {
907 dev_err(tplg->dev, "ASoC: failed to add %s\n",
908 mc->hdr.name);
909 soc_tplg_free_tlv(tplg, &kc);
910 kfree(sm);
911 continue;
912 }
913
914 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
915 }
916
917 return 0;
918}
919
920static int soc_tplg_denum_create_texts(struct soc_enum *se,
921 struct snd_soc_tplg_enum_control *ec)
922{
923 int i, ret;
924
925 se->dobj.control.dtexts =
926 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
927 if (se->dobj.control.dtexts == NULL)
928 return -ENOMEM;
929
930 for (i = 0; i < ec->items; i++) {
931
932 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
933 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
934 ret = -EINVAL;
935 goto err;
936 }
937
938 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
939 if (!se->dobj.control.dtexts[i]) {
940 ret = -ENOMEM;
941 goto err;
942 }
943 }
944
Mousumi Janab6e38b22017-04-11 13:06:22 +0530945 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100946 return 0;
947
948err:
949 for (--i; i >= 0; i--)
950 kfree(se->dobj.control.dtexts[i]);
951 kfree(se->dobj.control.dtexts);
952 return ret;
953}
954
955static int soc_tplg_denum_create_values(struct soc_enum *se,
956 struct snd_soc_tplg_enum_control *ec)
957{
958 if (ec->items > sizeof(*ec->values))
959 return -EINVAL;
960
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200961 se->dobj.control.dvalues = kmemdup(ec->values,
962 ec->items * sizeof(u32),
963 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100964 if (!se->dobj.control.dvalues)
965 return -ENOMEM;
966
Liam Girdwood8a978232015-05-29 19:06:14 +0100967 return 0;
968}
969
970static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
971 size_t size)
972{
973 struct snd_soc_tplg_enum_control *ec;
974 struct soc_enum *se;
975 struct snd_kcontrol_new kc;
976 int i, ret, err;
977
978 if (soc_tplg_check_elem_count(tplg,
979 sizeof(struct snd_soc_tplg_enum_control),
980 count, size, "enums")) {
981
982 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
983 count);
984 return -EINVAL;
985 }
986
987 for (i = 0; i < count; i++) {
988 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
989 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
990 ec->priv.size);
991
992 /* validate kcontrol */
993 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
994 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
995 return -EINVAL;
996
997 se = kzalloc((sizeof(*se)), GFP_KERNEL);
998 if (se == NULL)
999 return -ENOMEM;
1000
1001 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1002 ec->hdr.name, ec->items);
1003
1004 memset(&kc, 0, sizeof(kc));
1005 kc.name = ec->hdr.name;
1006 kc.private_value = (long)se;
1007 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1008 kc.access = ec->hdr.access;
1009
1010 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1011 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1012 SNDRV_CHMAP_FL);
1013 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1014 SNDRV_CHMAP_FL);
1015
1016 se->items = ec->items;
1017 se->mask = ec->mask;
1018 se->dobj.index = tplg->index;
1019 se->dobj.type = SND_SOC_DOBJ_ENUM;
1020 se->dobj.ops = tplg->ops;
1021 INIT_LIST_HEAD(&se->dobj.list);
1022
1023 switch (ec->hdr.ops.info) {
1024 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1025 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1026 err = soc_tplg_denum_create_values(se, ec);
1027 if (err < 0) {
1028 dev_err(tplg->dev,
1029 "ASoC: could not create values for %s\n",
1030 ec->hdr.name);
1031 kfree(se);
1032 continue;
1033 }
1034 /* fall through and create texts */
1035 case SND_SOC_TPLG_CTL_ENUM:
1036 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1037 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1038 err = soc_tplg_denum_create_texts(se, ec);
1039 if (err < 0) {
1040 dev_err(tplg->dev,
1041 "ASoC: could not create texts for %s\n",
1042 ec->hdr.name);
1043 kfree(se);
1044 continue;
1045 }
1046 break;
1047 default:
1048 dev_err(tplg->dev,
1049 "ASoC: invalid enum control type %d for %s\n",
1050 ec->hdr.ops.info, ec->hdr.name);
1051 kfree(se);
1052 continue;
1053 }
1054
1055 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001056 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001057 if (err) {
1058 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1059 kfree(se);
1060 continue;
1061 }
1062
1063 /* pass control to driver for optional further init */
1064 err = soc_tplg_init_kcontrol(tplg, &kc,
1065 (struct snd_soc_tplg_ctl_hdr *) ec);
1066 if (err < 0) {
1067 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1068 ec->hdr.name);
1069 kfree(se);
1070 continue;
1071 }
1072
1073 /* register control here */
1074 ret = soc_tplg_add_kcontrol(tplg,
1075 &kc, &se->dobj.control.kcontrol);
1076 if (ret < 0) {
1077 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1078 ec->hdr.name);
1079 kfree(se);
1080 continue;
1081 }
1082
1083 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1084 }
1085
1086 return 0;
1087}
1088
1089static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1090 struct snd_soc_tplg_hdr *hdr)
1091{
1092 struct snd_soc_tplg_ctl_hdr *control_hdr;
1093 int i;
1094
1095 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1096 tplg->pos += hdr->size + hdr->payload_size;
1097 return 0;
1098 }
1099
1100 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1101 soc_tplg_get_offset(tplg));
1102
1103 for (i = 0; i < hdr->count; i++) {
1104
1105 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1106
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001107 if (control_hdr->size != sizeof(*control_hdr)) {
1108 dev_err(tplg->dev, "ASoC: invalid control size\n");
1109 return -EINVAL;
1110 }
1111
Liam Girdwood8a978232015-05-29 19:06:14 +01001112 switch (control_hdr->ops.info) {
1113 case SND_SOC_TPLG_CTL_VOLSW:
1114 case SND_SOC_TPLG_CTL_STROBE:
1115 case SND_SOC_TPLG_CTL_VOLSW_SX:
1116 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1117 case SND_SOC_TPLG_CTL_RANGE:
1118 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1119 case SND_SOC_TPLG_DAPM_CTL_PIN:
1120 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1121 break;
1122 case SND_SOC_TPLG_CTL_ENUM:
1123 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1124 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1125 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1126 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1127 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1128 break;
1129 case SND_SOC_TPLG_CTL_BYTES:
1130 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1131 break;
1132 default:
1133 soc_bind_err(tplg, control_hdr, i);
1134 return -EINVAL;
1135 }
1136 }
1137
1138 return 0;
1139}
1140
1141static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1142 struct snd_soc_tplg_hdr *hdr)
1143{
1144 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1145 struct snd_soc_dapm_route route;
1146 struct snd_soc_tplg_dapm_graph_elem *elem;
1147 int count = hdr->count, i;
1148
1149 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1150 tplg->pos += hdr->size + hdr->payload_size;
1151 return 0;
1152 }
1153
1154 if (soc_tplg_check_elem_count(tplg,
1155 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1156 count, hdr->payload_size, "graph")) {
1157
1158 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1159 count);
1160 return -EINVAL;
1161 }
1162
1163 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1164
1165 for (i = 0; i < count; i++) {
1166 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1167 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1168
1169 /* validate routes */
1170 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1171 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1172 return -EINVAL;
1173 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1174 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1175 return -EINVAL;
1176 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1177 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1178 return -EINVAL;
1179
1180 route.source = elem->source;
1181 route.sink = elem->sink;
1182 route.connected = NULL; /* set to NULL atm for tplg users */
1183 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1184 route.control = NULL;
1185 else
1186 route.control = elem->control;
1187
1188 /* add route, but keep going if some fail */
1189 snd_soc_dapm_add_routes(dapm, &route, 1);
1190 }
1191
1192 return 0;
1193}
1194
1195static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1196 struct soc_tplg *tplg, int num_kcontrols)
1197{
1198 struct snd_kcontrol_new *kc;
1199 struct soc_mixer_control *sm;
1200 struct snd_soc_tplg_mixer_control *mc;
1201 int i, err;
1202
Axel Lin4ca7deb2015-08-05 22:34:22 +08001203 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001204 if (kc == NULL)
1205 return NULL;
1206
1207 for (i = 0; i < num_kcontrols; i++) {
1208 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1209 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1210 if (sm == NULL)
1211 goto err;
1212
1213 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1214 mc->priv.size);
1215
1216 /* validate kcontrol */
1217 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1218 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1219 goto err_str;
1220
1221 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1222 mc->hdr.name, i);
1223
1224 kc[i].name = mc->hdr.name;
1225 kc[i].private_value = (long)sm;
1226 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1227 kc[i].access = mc->hdr.access;
1228
1229 /* we only support FL/FR channel mapping atm */
1230 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1231 SNDRV_CHMAP_FL);
1232 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1233 SNDRV_CHMAP_FR);
1234 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1235 SNDRV_CHMAP_FL);
1236 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1237 SNDRV_CHMAP_FR);
1238
1239 sm->max = mc->max;
1240 sm->min = mc->min;
1241 sm->invert = mc->invert;
1242 sm->platform_max = mc->platform_max;
1243 sm->dobj.index = tplg->index;
1244 INIT_LIST_HEAD(&sm->dobj.list);
1245
1246 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001247 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001248 if (err) {
1249 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1250 kfree(sm);
1251 continue;
1252 }
1253
1254 /* pass control to driver for optional further init */
1255 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1256 (struct snd_soc_tplg_ctl_hdr *)mc);
1257 if (err < 0) {
1258 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1259 mc->hdr.name);
1260 kfree(sm);
1261 continue;
1262 }
1263 }
1264 return kc;
1265
1266err_str:
1267 kfree(sm);
1268err:
1269 for (--i; i >= 0; i--)
1270 kfree((void *)kc[i].private_value);
1271 kfree(kc);
1272 return NULL;
1273}
1274
1275static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001276 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001277{
1278 struct snd_kcontrol_new *kc;
1279 struct snd_soc_tplg_enum_control *ec;
1280 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001281 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001282
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001283 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001284 if (kc == NULL)
1285 return NULL;
1286
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001287 for (i = 0; i < num_kcontrols; i++) {
1288 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1289 /* validate kcontrol */
1290 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1291 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1292 return NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001293
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001294 se = kzalloc(sizeof(*se), GFP_KERNEL);
1295 if (se == NULL)
1296 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001297
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001298 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001299 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001300
1301 kc[i].name = ec->hdr.name;
1302 kc[i].private_value = (long)se;
1303 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1304 kc[i].access = ec->hdr.access;
1305
1306 /* we only support FL/FR channel mapping atm */
1307 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1308 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1309 SNDRV_CHMAP_FL);
1310 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1311 SNDRV_CHMAP_FR);
1312
1313 se->items = ec->items;
1314 se->mask = ec->mask;
1315 se->dobj.index = tplg->index;
1316
1317 switch (ec->hdr.ops.info) {
1318 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1319 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1320 err = soc_tplg_denum_create_values(se, ec);
1321 if (err < 0) {
1322 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1323 ec->hdr.name);
1324 goto err_se;
1325 }
1326 /* fall through to create texts */
1327 case SND_SOC_TPLG_CTL_ENUM:
1328 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1329 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1330 err = soc_tplg_denum_create_texts(se, ec);
1331 if (err < 0) {
1332 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1333 ec->hdr.name);
1334 goto err_se;
1335 }
1336 break;
1337 default:
1338 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1339 ec->hdr.ops.info, ec->hdr.name);
1340 goto err_se;
1341 }
1342
1343 /* map io handlers */
1344 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1345 if (err) {
1346 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1347 goto err_se;
1348 }
1349
1350 /* pass control to driver for optional further init */
1351 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1352 (struct snd_soc_tplg_ctl_hdr *)ec);
1353 if (err < 0) {
1354 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1355 ec->hdr.name);
1356 goto err_se;
1357 }
1358
1359 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1360 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001361 }
1362
1363 return kc;
1364
1365err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001366 for (; i >= 0; i--) {
1367 /* free values and texts */
1368 se = (struct soc_enum *)kc[i].private_value;
1369 kfree(se->dobj.control.dvalues);
1370 for (j = 0; j < ec->items; j++)
1371 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001372
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001373 kfree(se);
1374 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001375err:
1376 kfree(kc);
1377
1378 return NULL;
1379}
1380
1381static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1382 struct soc_tplg *tplg, int count)
1383{
1384 struct snd_soc_tplg_bytes_control *be;
1385 struct soc_bytes_ext *sbe;
1386 struct snd_kcontrol_new *kc;
1387 int i, err;
1388
Axel Lin4ca7deb2015-08-05 22:34:22 +08001389 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001390 if (!kc)
1391 return NULL;
1392
1393 for (i = 0; i < count; i++) {
1394 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1395
1396 /* validate kcontrol */
1397 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1398 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1399 goto err;
1400
1401 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1402 if (sbe == NULL)
1403 goto err;
1404
1405 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1406 be->priv.size);
1407
1408 dev_dbg(tplg->dev,
1409 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1410 be->hdr.name, be->hdr.access);
1411
Liam Girdwood8a978232015-05-29 19:06:14 +01001412 kc[i].name = be->hdr.name;
1413 kc[i].private_value = (long)sbe;
1414 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1415 kc[i].access = be->hdr.access;
1416
1417 sbe->max = be->max;
1418 INIT_LIST_HEAD(&sbe->dobj.list);
1419
1420 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001421 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001422 if (err) {
1423 soc_control_err(tplg, &be->hdr, be->hdr.name);
1424 kfree(sbe);
1425 continue;
1426 }
1427
1428 /* pass control to driver for optional further init */
1429 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1430 (struct snd_soc_tplg_ctl_hdr *)be);
1431 if (err < 0) {
1432 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1433 be->hdr.name);
1434 kfree(sbe);
1435 continue;
1436 }
1437 }
1438
1439 return kc;
1440
1441err:
1442 for (--i; i >= 0; i--)
1443 kfree((void *)kc[i].private_value);
1444
1445 kfree(kc);
1446 return NULL;
1447}
1448
1449static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1450 struct snd_soc_tplg_dapm_widget *w)
1451{
1452 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1453 struct snd_soc_dapm_widget template, *widget;
1454 struct snd_soc_tplg_ctl_hdr *control_hdr;
1455 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001456 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001457 int ret = 0;
1458
1459 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1460 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1461 return -EINVAL;
1462 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1463 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1464 return -EINVAL;
1465
1466 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1467 w->name, w->id);
1468
1469 memset(&template, 0, sizeof(template));
1470
1471 /* map user to kernel widget ID */
1472 template.id = get_widget_id(w->id);
1473 if (template.id < 0)
1474 return template.id;
1475
1476 template.name = kstrdup(w->name, GFP_KERNEL);
1477 if (!template.name)
1478 return -ENOMEM;
1479 template.sname = kstrdup(w->sname, GFP_KERNEL);
1480 if (!template.sname) {
1481 ret = -ENOMEM;
1482 goto err;
1483 }
1484 template.reg = w->reg;
1485 template.shift = w->shift;
1486 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001487 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001488 template.on_val = w->invert ? 0 : 1;
1489 template.off_val = w->invert ? 1 : 0;
1490 template.ignore_suspend = w->ignore_suspend;
1491 template.event_flags = w->event_flags;
1492 template.dobj.index = tplg->index;
1493
1494 tplg->pos +=
1495 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1496 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001497 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001498 template.num_kcontrols = 0;
1499 goto widget;
1500 }
1501
1502 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1503 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1504 w->name, w->num_kcontrols, control_hdr->type);
1505
1506 switch (control_hdr->ops.info) {
1507 case SND_SOC_TPLG_CTL_VOLSW:
1508 case SND_SOC_TPLG_CTL_STROBE:
1509 case SND_SOC_TPLG_CTL_VOLSW_SX:
1510 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1511 case SND_SOC_TPLG_CTL_RANGE:
1512 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001513 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Liam Girdwood8a978232015-05-29 19:06:14 +01001514 template.num_kcontrols = w->num_kcontrols;
1515 template.kcontrol_news =
1516 soc_tplg_dapm_widget_dmixer_create(tplg,
1517 template.num_kcontrols);
1518 if (!template.kcontrol_news) {
1519 ret = -ENOMEM;
1520 goto hdr_err;
1521 }
1522 break;
1523 case SND_SOC_TPLG_CTL_ENUM:
1524 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1525 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1526 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1527 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001528 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001529 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001530 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001531 soc_tplg_dapm_widget_denum_create(tplg,
1532 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001533 if (!template.kcontrol_news) {
1534 ret = -ENOMEM;
1535 goto hdr_err;
1536 }
1537 break;
1538 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001539 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Liam Girdwood8a978232015-05-29 19:06:14 +01001540 template.num_kcontrols = w->num_kcontrols;
1541 template.kcontrol_news =
1542 soc_tplg_dapm_widget_dbytes_create(tplg,
1543 template.num_kcontrols);
1544 if (!template.kcontrol_news) {
1545 ret = -ENOMEM;
1546 goto hdr_err;
1547 }
1548 break;
1549 default:
1550 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1551 control_hdr->ops.get, control_hdr->ops.put,
1552 control_hdr->ops.info);
1553 ret = -EINVAL;
1554 goto hdr_err;
1555 }
1556
1557widget:
1558 ret = soc_tplg_widget_load(tplg, &template, w);
1559 if (ret < 0)
1560 goto hdr_err;
1561
1562 /* card dapm mutex is held by the core if we are loading topology
1563 * data during sound card init. */
1564 if (card->instantiated)
1565 widget = snd_soc_dapm_new_control(dapm, &template);
1566 else
1567 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001568 if (IS_ERR(widget)) {
1569 ret = PTR_ERR(widget);
1570 /* Do not nag about probe deferrals */
1571 if (ret != -EPROBE_DEFER)
1572 dev_err(tplg->dev,
1573 "ASoC: failed to create widget %s controls (%d)\n",
1574 w->name, ret);
1575 goto hdr_err;
1576 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001577 if (widget == NULL) {
1578 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1579 w->name);
Wei Yongjun8ae3ea42016-08-10 13:43:12 +00001580 ret = -ENOMEM;
Liam Girdwood8a978232015-05-29 19:06:14 +01001581 goto hdr_err;
1582 }
1583
1584 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001585 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001586 widget->dobj.ops = tplg->ops;
1587 widget->dobj.index = tplg->index;
Jeeja KP8ea41672016-05-05 11:19:18 +05301588 kfree(template.sname);
1589 kfree(template.name);
Liam Girdwood8a978232015-05-29 19:06:14 +01001590 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1591 return 0;
1592
1593hdr_err:
1594 kfree(template.sname);
1595err:
1596 kfree(template.name);
1597 return ret;
1598}
1599
1600static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1601 struct snd_soc_tplg_hdr *hdr)
1602{
1603 struct snd_soc_tplg_dapm_widget *widget;
1604 int ret, count = hdr->count, i;
1605
1606 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1607 return 0;
1608
1609 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1610
1611 for (i = 0; i < count; i++) {
1612 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001613 if (widget->size != sizeof(*widget)) {
1614 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1615 return -EINVAL;
1616 }
1617
Liam Girdwood8a978232015-05-29 19:06:14 +01001618 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001619 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001620 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1621 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001622 return ret;
1623 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001624 }
1625
1626 return 0;
1627}
1628
1629static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1630{
1631 struct snd_soc_card *card = tplg->comp->card;
1632 int ret;
1633
1634 /* Card might not have been registered at this point.
1635 * If so, just return success.
1636 */
1637 if (!card || !card->instantiated) {
1638 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1639 "Do not add new widgets now\n");
1640 return 0;
1641 }
1642
1643 ret = snd_soc_dapm_new_widgets(card);
1644 if (ret < 0)
1645 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1646 ret);
1647
1648 return 0;
1649}
1650
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001651static void set_stream_info(struct snd_soc_pcm_stream *stream,
1652 struct snd_soc_tplg_stream_caps *caps)
1653{
1654 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1655 stream->channels_min = caps->channels_min;
1656 stream->channels_max = caps->channels_max;
1657 stream->rates = caps->rates;
1658 stream->rate_min = caps->rate_min;
1659 stream->rate_max = caps->rate_max;
1660 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001661 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001662}
1663
Mengdong Lin0038be92016-07-26 14:32:37 +08001664static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1665 unsigned int flag_mask, unsigned int flags)
1666{
1667 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1668 dai_drv->symmetric_rates =
1669 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1670
1671 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1672 dai_drv->symmetric_channels =
1673 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1674 1 : 0;
1675
1676 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1677 dai_drv->symmetric_samplebits =
1678 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1679 1 : 0;
1680}
1681
Mengdong Lin64527e82016-01-15 16:13:28 +08001682static int soc_tplg_dai_create(struct soc_tplg *tplg,
1683 struct snd_soc_tplg_pcm *pcm)
1684{
1685 struct snd_soc_dai_driver *dai_drv;
1686 struct snd_soc_pcm_stream *stream;
1687 struct snd_soc_tplg_stream_caps *caps;
1688 int ret;
1689
1690 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1691 if (dai_drv == NULL)
1692 return -ENOMEM;
1693
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001694 if (strlen(pcm->dai_name))
1695 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001696 dai_drv->id = pcm->dai_id;
1697
1698 if (pcm->playback) {
1699 stream = &dai_drv->playback;
1700 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001701 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001702 }
1703
1704 if (pcm->capture) {
1705 stream = &dai_drv->capture;
1706 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001707 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001708 }
1709
1710 /* pass control to component driver for optional further init */
1711 ret = soc_tplg_dai_load(tplg, dai_drv);
1712 if (ret < 0) {
1713 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1714 kfree(dai_drv);
1715 return ret;
1716 }
1717
1718 dai_drv->dobj.index = tplg->index;
1719 dai_drv->dobj.ops = tplg->ops;
1720 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1721 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1722
1723 /* register the DAI to the component */
1724 return snd_soc_register_dai(tplg->comp, dai_drv);
1725}
1726
Mengdong Lin717a8e72016-11-03 01:03:34 +08001727static void set_link_flags(struct snd_soc_dai_link *link,
1728 unsigned int flag_mask, unsigned int flags)
1729{
1730 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1731 link->symmetric_rates =
1732 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1733
1734 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1735 link->symmetric_channels =
1736 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1737 1 : 0;
1738
1739 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1740 link->symmetric_samplebits =
1741 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1742 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001743
1744 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1745 link->ignore_suspend =
1746 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1747 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001748}
1749
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001750/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001751static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001752 struct snd_soc_tplg_pcm *pcm)
1753{
1754 struct snd_soc_dai_link *link;
1755 int ret;
1756
1757 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1758 if (link == NULL)
1759 return -ENOMEM;
1760
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001761 if (strlen(pcm->pcm_name)) {
1762 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1763 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1764 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001765 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001766
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001767 if (strlen(pcm->dai_name))
1768 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1769
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001770 link->codec_name = "snd-soc-dummy";
1771 link->codec_dai_name = "snd-soc-dummy-dai";
1772
1773 /* enable DPCM */
1774 link->dynamic = 1;
1775 link->dpcm_playback = pcm->playback;
1776 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001777 if (pcm->flag_mask)
1778 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001779
Mengdong Linacfc7d42016-01-15 16:13:37 +08001780 /* pass control to component driver for optional further init */
1781 ret = soc_tplg_dai_link_load(tplg, link);
1782 if (ret < 0) {
1783 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1784 kfree(link);
1785 return ret;
1786 }
1787
1788 link->dobj.index = tplg->index;
1789 link->dobj.ops = tplg->ops;
1790 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1791 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1792
1793 snd_soc_add_dai_link(tplg->comp->card, link);
1794 return 0;
1795}
1796
1797/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001798static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1799 struct snd_soc_tplg_pcm *pcm)
1800{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001801 int ret;
1802
1803 ret = soc_tplg_dai_create(tplg, pcm);
1804 if (ret < 0)
1805 return ret;
1806
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001807 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001808}
1809
Mengdong Lin55726dc2016-11-03 01:00:16 +08001810/* copy stream caps from the old version 4 of source */
1811static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1812 struct snd_soc_tplg_stream_caps_v4 *src)
1813{
1814 dest->size = sizeof(*dest);
1815 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1816 dest->formats = src->formats;
1817 dest->rates = src->rates;
1818 dest->rate_min = src->rate_min;
1819 dest->rate_max = src->rate_max;
1820 dest->channels_min = src->channels_min;
1821 dest->channels_max = src->channels_max;
1822 dest->periods_min = src->periods_min;
1823 dest->periods_max = src->periods_max;
1824 dest->period_size_min = src->period_size_min;
1825 dest->period_size_max = src->period_size_max;
1826 dest->buffer_size_min = src->buffer_size_min;
1827 dest->buffer_size_max = src->buffer_size_max;
1828}
1829
1830/**
1831 * pcm_new_ver - Create the new version of PCM from the old version.
1832 * @tplg: topology context
1833 * @src: older version of pcm as a source
1834 * @pcm: latest version of pcm created from the source
1835 *
1836 * Support from vesion 4. User should free the returned pcm manually.
1837 */
1838static int pcm_new_ver(struct soc_tplg *tplg,
1839 struct snd_soc_tplg_pcm *src,
1840 struct snd_soc_tplg_pcm **pcm)
1841{
1842 struct snd_soc_tplg_pcm *dest;
1843 struct snd_soc_tplg_pcm_v4 *src_v4;
1844 int i;
1845
1846 *pcm = NULL;
1847
1848 if (src->size != sizeof(*src_v4)) {
1849 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1850 return -EINVAL;
1851 }
1852
1853 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1854 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1855 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1856 if (!dest)
1857 return -ENOMEM;
1858
1859 dest->size = sizeof(*dest); /* size of latest abi version */
1860 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1861 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1862 dest->pcm_id = src_v4->pcm_id;
1863 dest->dai_id = src_v4->dai_id;
1864 dest->playback = src_v4->playback;
1865 dest->capture = src_v4->capture;
1866 dest->compress = src_v4->compress;
1867 dest->num_streams = src_v4->num_streams;
1868 for (i = 0; i < dest->num_streams; i++)
1869 memcpy(&dest->stream[i], &src_v4->stream[i],
1870 sizeof(struct snd_soc_tplg_stream));
1871
1872 for (i = 0; i < 2; i++)
1873 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1874
1875 *pcm = dest;
1876 return 0;
1877}
1878
Mengdong Lin64527e82016-01-15 16:13:28 +08001879static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001880 struct snd_soc_tplg_hdr *hdr)
1881{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001882 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001883 int count = hdr->count;
Vinod Koulfd340452016-12-08 23:01:27 +05301884 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001885 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001886
1887 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1888 return 0;
1889
Mengdong Lin55726dc2016-11-03 01:00:16 +08001890 /* check the element size and count */
1891 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1892 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1893 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1894 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1895 pcm->size);
1896 return -EINVAL;
1897 }
1898
Liam Girdwood8a978232015-05-29 19:06:14 +01001899 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08001900 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001901 hdr->payload_size, "PCM DAI")) {
1902 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1903 count);
1904 return -EINVAL;
1905 }
1906
Mengdong Lin64527e82016-01-15 16:13:28 +08001907 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001908 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1909
1910 /* check ABI version by size, create a new version of pcm
1911 * if abi not match.
1912 */
1913 if (pcm->size == sizeof(*pcm)) {
1914 abi_match = true;
1915 _pcm = pcm;
1916 } else {
1917 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05301918 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001919 }
1920
Mengdong Lin55726dc2016-11-03 01:00:16 +08001921 /* create the FE DAIs and DAI links */
1922 soc_tplg_pcm_create(tplg, _pcm);
1923
Mengdong Lin717a8e72016-11-03 01:03:34 +08001924 /* offset by version-specific struct size and
1925 * real priv data size
1926 */
1927 tplg->pos += pcm->size + _pcm->priv.size;
1928
Mengdong Lin55726dc2016-11-03 01:00:16 +08001929 if (!abi_match)
1930 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08001931 }
1932
Liam Girdwood8a978232015-05-29 19:06:14 +01001933 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001934
Liam Girdwood8a978232015-05-29 19:06:14 +01001935 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001936}
1937
Mengdong Lin593d9e52016-11-03 01:04:27 +08001938/**
1939 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00001940 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08001941 * @cfg: physical link configs.
1942 *
1943 * Topology context contains a list of supported HW formats (configs) and
1944 * a default format ID for the physical link. This function will use this
1945 * default ID to choose the HW format to set the link's DAI format for init.
1946 */
1947static void set_link_hw_format(struct snd_soc_dai_link *link,
1948 struct snd_soc_tplg_link_config *cfg)
1949{
1950 struct snd_soc_tplg_hw_config *hw_config;
1951 unsigned char bclk_master, fsync_master;
1952 unsigned char invert_bclk, invert_fsync;
1953 int i;
1954
1955 for (i = 0; i < cfg->num_hw_configs; i++) {
1956 hw_config = &cfg->hw_config[i];
1957 if (hw_config->id != cfg->default_hw_config_id)
1958 continue;
1959
1960 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
1961
1962 /* clock signal polarity */
1963 invert_bclk = hw_config->invert_bclk;
1964 invert_fsync = hw_config->invert_fsync;
1965 if (!invert_bclk && !invert_fsync)
1966 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1967 else if (!invert_bclk && invert_fsync)
1968 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1969 else if (invert_bclk && !invert_fsync)
1970 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1971 else
1972 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1973
1974 /* clock masters */
1975 bclk_master = hw_config->bclk_master;
1976 fsync_master = hw_config->fsync_master;
1977 if (!bclk_master && !fsync_master)
1978 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1979 else if (bclk_master && !fsync_master)
1980 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1981 else if (!bclk_master && fsync_master)
1982 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1983 else
1984 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1985 }
1986}
1987
1988/**
1989 * link_new_ver - Create a new physical link config from the old
1990 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00001991 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08001992 * @src: old version of phyical link config as a source
1993 * @link: latest version of physical link config created from the source
1994 *
1995 * Support from vesion 4. User need free the returned link config manually.
1996 */
1997static int link_new_ver(struct soc_tplg *tplg,
1998 struct snd_soc_tplg_link_config *src,
1999 struct snd_soc_tplg_link_config **link)
2000{
2001 struct snd_soc_tplg_link_config *dest;
2002 struct snd_soc_tplg_link_config_v4 *src_v4;
2003 int i;
2004
2005 *link = NULL;
2006
2007 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2008 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2009 return -EINVAL;
2010 }
2011
2012 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2013
2014 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2015 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2016 if (!dest)
2017 return -ENOMEM;
2018
2019 dest->size = sizeof(*dest);
2020 dest->id = src_v4->id;
2021 dest->num_streams = src_v4->num_streams;
2022 for (i = 0; i < dest->num_streams; i++)
2023 memcpy(&dest->stream[i], &src_v4->stream[i],
2024 sizeof(struct snd_soc_tplg_stream));
2025
2026 *link = dest;
2027 return 0;
2028}
2029
2030/* Find and configure an existing physical DAI link */
2031static int soc_tplg_link_config(struct soc_tplg *tplg,
2032 struct snd_soc_tplg_link_config *cfg)
2033{
2034 struct snd_soc_dai_link *link;
2035 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002036 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002037 int ret;
2038
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002039 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2040 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2041 return -EINVAL;
2042 else if (len)
2043 name = cfg->name;
2044 else
2045 name = NULL;
2046
2047 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2048 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2049 return -EINVAL;
2050 else if (len)
2051 stream_name = cfg->stream_name;
2052 else
2053 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002054
2055 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2056 name, stream_name);
2057 if (!link) {
2058 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2059 name, cfg->id);
2060 return -EINVAL;
2061 }
2062
2063 /* hw format */
2064 if (cfg->num_hw_configs)
2065 set_link_hw_format(link, cfg);
2066
2067 /* flags */
2068 if (cfg->flag_mask)
2069 set_link_flags(link, cfg->flag_mask, cfg->flags);
2070
2071 /* pass control to component driver for optional further init */
2072 ret = soc_tplg_dai_link_load(tplg, link);
2073 if (ret < 0) {
2074 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2075 return ret;
2076 }
2077
2078 return 0;
2079}
2080
2081
2082/* Load physical link config elements from the topology context */
2083static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2084 struct snd_soc_tplg_hdr *hdr)
2085{
2086 struct snd_soc_tplg_link_config *link, *_link;
2087 int count = hdr->count;
2088 int i, ret;
2089 bool abi_match;
2090
2091 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2092 tplg->pos += hdr->size + hdr->payload_size;
2093 return 0;
2094 };
2095
2096 /* check the element size and count */
2097 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2098 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2099 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2100 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2101 link->size);
2102 return -EINVAL;
2103 }
2104
2105 if (soc_tplg_check_elem_count(tplg,
2106 link->size, count,
2107 hdr->payload_size, "physical link config")) {
2108 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2109 count);
2110 return -EINVAL;
2111 }
2112
2113 /* config physical DAI links */
2114 for (i = 0; i < count; i++) {
2115 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2116 if (link->size == sizeof(*link)) {
2117 abi_match = true;
2118 _link = link;
2119 } else {
2120 abi_match = false;
2121 ret = link_new_ver(tplg, link, &_link);
2122 if (ret < 0)
2123 return ret;
2124 }
2125
2126 ret = soc_tplg_link_config(tplg, _link);
2127 if (ret < 0)
2128 return ret;
2129
2130 /* offset by version-specific struct size and
2131 * real priv data size
2132 */
2133 tplg->pos += link->size + _link->priv.size;
2134
2135 if (!abi_match)
2136 kfree(_link); /* free the duplicated one */
2137 }
2138
2139 return 0;
2140}
2141
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002142/**
2143 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002144 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002145 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002146 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002147 * The physical dai should already be registered by the platform driver.
2148 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002149 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002150static int soc_tplg_dai_config(struct soc_tplg *tplg,
2151 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002152{
2153 struct snd_soc_dai_link_component dai_component = {0};
2154 struct snd_soc_dai *dai;
2155 struct snd_soc_dai_driver *dai_drv;
2156 struct snd_soc_pcm_stream *stream;
2157 struct snd_soc_tplg_stream_caps *caps;
2158 int ret;
2159
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002160 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002161 dai = snd_soc_find_dai(&dai_component);
2162 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002163 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2164 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002165 return -EINVAL;
2166 }
2167
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002168 if (d->dai_id != dai->id) {
2169 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2170 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002171 return -EINVAL;
2172 }
2173
2174 dai_drv = dai->driver;
2175 if (!dai_drv)
2176 return -EINVAL;
2177
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002178 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002179 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002180 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002181 set_stream_info(stream, caps);
2182 }
2183
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002184 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002185 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002186 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002187 set_stream_info(stream, caps);
2188 }
2189
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002190 if (d->flag_mask)
2191 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002192
2193 /* pass control to component driver for optional further init */
2194 ret = soc_tplg_dai_load(tplg, dai_drv);
2195 if (ret < 0) {
2196 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2197 return ret;
2198 }
2199
2200 return 0;
2201}
2202
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002203/* load physical DAI elements */
2204static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2205 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002206{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002207 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002208 int count = hdr->count;
2209 int i;
2210
2211 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2212 return 0;
2213
2214 /* config the existing BE DAIs */
2215 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002216 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2217 if (dai->size != sizeof(*dai)) {
2218 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002219 return -EINVAL;
2220 }
2221
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002222 soc_tplg_dai_config(tplg, dai);
2223 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002224 }
2225
2226 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2227 return 0;
2228}
2229
Mengdong Lin583958f2016-10-11 14:36:42 +08002230/**
2231 * manifest_new_ver - Create a new version of manifest from the old version
2232 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002233 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002234 * @src: old version of manifest as a source
2235 * @manifest: latest version of manifest created from the source
2236 *
2237 * Support from vesion 4. Users need free the returned manifest manually.
2238 */
2239static int manifest_new_ver(struct soc_tplg *tplg,
2240 struct snd_soc_tplg_manifest *src,
2241 struct snd_soc_tplg_manifest **manifest)
2242{
2243 struct snd_soc_tplg_manifest *dest;
2244 struct snd_soc_tplg_manifest_v4 *src_v4;
2245
2246 *manifest = NULL;
2247
2248 if (src->size != sizeof(*src_v4)) {
2249 dev_err(tplg->dev, "ASoC: invalid manifest size\n");
2250 return -EINVAL;
2251 }
2252
2253 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2254
2255 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2256 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2257 if (!dest)
2258 return -ENOMEM;
2259
2260 dest->size = sizeof(*dest); /* size of latest abi version */
2261 dest->control_elems = src_v4->control_elems;
2262 dest->widget_elems = src_v4->widget_elems;
2263 dest->graph_elems = src_v4->graph_elems;
2264 dest->pcm_elems = src_v4->pcm_elems;
2265 dest->dai_link_elems = src_v4->dai_link_elems;
2266 dest->priv.size = src_v4->priv.size;
2267 if (dest->priv.size)
2268 memcpy(dest->priv.data, src_v4->priv.data,
2269 src_v4->priv.size);
2270
2271 *manifest = dest;
2272 return 0;
2273}
Mengdong Lin0038be92016-07-26 14:32:37 +08002274
Liam Girdwood8a978232015-05-29 19:06:14 +01002275static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002276 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002277{
Mengdong Lin583958f2016-10-11 14:36:42 +08002278 struct snd_soc_tplg_manifest *manifest, *_manifest;
2279 bool abi_match;
2280 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002281
2282 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2283 return 0;
2284
2285 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002286
2287 /* check ABI version by size, create a new manifest if abi not match */
2288 if (manifest->size == sizeof(*manifest)) {
2289 abi_match = true;
2290 _manifest = manifest;
2291 } else {
2292 abi_match = false;
2293 err = manifest_new_ver(tplg, manifest, &_manifest);
2294 if (err < 0)
2295 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002296 }
2297
Mengdong Lin583958f2016-10-11 14:36:42 +08002298 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002299 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Mengdong Lin583958f2016-10-11 14:36:42 +08002300 return tplg->ops->manifest(tplg->comp, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002301
Mengdong Lin583958f2016-10-11 14:36:42 +08002302 if (!abi_match) /* free the duplicated one */
2303 kfree(_manifest);
2304
Liam Girdwood8a978232015-05-29 19:06:14 +01002305 return 0;
2306}
2307
2308/* validate header magic, size and type */
2309static int soc_valid_header(struct soc_tplg *tplg,
2310 struct snd_soc_tplg_hdr *hdr)
2311{
2312 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2313 return 0;
2314
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002315 if (hdr->size != sizeof(*hdr)) {
2316 dev_err(tplg->dev,
2317 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2318 hdr->type, soc_tplg_get_hdr_offset(tplg),
2319 tplg->fw->size);
2320 return -EINVAL;
2321 }
2322
Liam Girdwood8a978232015-05-29 19:06:14 +01002323 /* big endian firmware objects not supported atm */
2324 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2325 dev_err(tplg->dev,
2326 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2327 tplg->pass, hdr->magic,
2328 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2329 return -EINVAL;
2330 }
2331
2332 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2333 dev_err(tplg->dev,
2334 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2335 tplg->pass, hdr->magic,
2336 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2337 return -EINVAL;
2338 }
2339
Mengdong Lin288b8da2016-11-03 01:03:17 +08002340 /* Support ABI from version 4 */
2341 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2342 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002343 dev_err(tplg->dev,
2344 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2345 tplg->pass, hdr->abi,
2346 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2347 tplg->fw->size);
2348 return -EINVAL;
2349 }
2350
2351 if (hdr->payload_size == 0) {
2352 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2353 soc_tplg_get_hdr_offset(tplg));
2354 return -EINVAL;
2355 }
2356
2357 if (tplg->pass == hdr->type)
2358 dev_dbg(tplg->dev,
2359 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2360 hdr->payload_size, hdr->type, hdr->version,
2361 hdr->vendor_type, tplg->pass);
2362
2363 return 1;
2364}
2365
2366/* check header type and call appropriate handler */
2367static int soc_tplg_load_header(struct soc_tplg *tplg,
2368 struct snd_soc_tplg_hdr *hdr)
2369{
2370 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2371
2372 /* check for matching ID */
2373 if (hdr->index != tplg->req_index &&
2374 hdr->index != SND_SOC_TPLG_INDEX_ALL)
2375 return 0;
2376
2377 tplg->index = hdr->index;
2378
2379 switch (hdr->type) {
2380 case SND_SOC_TPLG_TYPE_MIXER:
2381 case SND_SOC_TPLG_TYPE_ENUM:
2382 case SND_SOC_TPLG_TYPE_BYTES:
2383 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2384 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2385 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2386 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2387 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2388 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002389 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002390 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002391 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002392 case SND_SOC_TPLG_TYPE_DAI_LINK:
2393 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2394 /* physical link configurations */
2395 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002396 case SND_SOC_TPLG_TYPE_MANIFEST:
2397 return soc_tplg_manifest_load(tplg, hdr);
2398 default:
2399 /* bespoke vendor data object */
2400 return soc_tplg_vendor_load(tplg, hdr);
2401 }
2402
2403 return 0;
2404}
2405
2406/* process the topology file headers */
2407static int soc_tplg_process_headers(struct soc_tplg *tplg)
2408{
2409 struct snd_soc_tplg_hdr *hdr;
2410 int ret;
2411
2412 tplg->pass = SOC_TPLG_PASS_START;
2413
2414 /* process the header types from start to end */
2415 while (tplg->pass <= SOC_TPLG_PASS_END) {
2416
2417 tplg->hdr_pos = tplg->fw->data;
2418 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2419
2420 while (!soc_tplg_is_eof(tplg)) {
2421
2422 /* make sure header is valid before loading */
2423 ret = soc_valid_header(tplg, hdr);
2424 if (ret < 0)
2425 return ret;
2426 else if (ret == 0)
2427 break;
2428
2429 /* load the header object */
2430 ret = soc_tplg_load_header(tplg, hdr);
2431 if (ret < 0)
2432 return ret;
2433
2434 /* goto next header */
2435 tplg->hdr_pos += hdr->payload_size +
2436 sizeof(struct snd_soc_tplg_hdr);
2437 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2438 }
2439
2440 /* next data type pass */
2441 tplg->pass++;
2442 }
2443
2444 /* signal DAPM we are complete */
2445 ret = soc_tplg_dapm_complete(tplg);
2446 if (ret < 0)
2447 dev_err(tplg->dev,
2448 "ASoC: failed to initialise DAPM from Firmware\n");
2449
2450 return ret;
2451}
2452
2453static int soc_tplg_load(struct soc_tplg *tplg)
2454{
2455 int ret;
2456
2457 ret = soc_tplg_process_headers(tplg);
2458 if (ret == 0)
2459 soc_tplg_complete(tplg);
2460
2461 return ret;
2462}
2463
2464/* load audio component topology from "firmware" file */
2465int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2466 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2467{
2468 struct soc_tplg tplg;
2469
2470 /* setup parsing context */
2471 memset(&tplg, 0, sizeof(tplg));
2472 tplg.fw = fw;
2473 tplg.dev = comp->dev;
2474 tplg.comp = comp;
2475 tplg.ops = ops;
2476 tplg.req_index = id;
2477 tplg.io_ops = ops->io_ops;
2478 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002479 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2480 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002481
2482 return soc_tplg_load(&tplg);
2483}
2484EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2485
2486/* remove this dynamic widget */
2487void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2488{
2489 /* make sure we are a widget */
2490 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2491 return;
2492
2493 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2494}
2495EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2496
2497/* remove all dynamic widgets from this DAPM context */
2498void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2499 u32 index)
2500{
2501 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002502
2503 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2504
2505 /* make sure we are a widget with correct context */
2506 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2507 continue;
2508
2509 /* match ID */
2510 if (w->dobj.index != index &&
2511 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2512 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002513 /* check and free and dynamic widget kcontrols */
2514 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002515 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002516 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002517 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002518}
2519EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2520
2521/* remove dynamic controls from the component driver */
2522int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2523{
2524 struct snd_soc_dobj *dobj, *next_dobj;
2525 int pass = SOC_TPLG_PASS_END;
2526
2527 /* process the header types from end to start */
2528 while (pass >= SOC_TPLG_PASS_START) {
2529
2530 /* remove mixer controls */
2531 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2532 list) {
2533
2534 /* match index */
2535 if (dobj->index != index &&
2536 dobj->index != SND_SOC_TPLG_INDEX_ALL)
2537 continue;
2538
2539 switch (dobj->type) {
2540 case SND_SOC_DOBJ_MIXER:
2541 remove_mixer(comp, dobj, pass);
2542 break;
2543 case SND_SOC_DOBJ_ENUM:
2544 remove_enum(comp, dobj, pass);
2545 break;
2546 case SND_SOC_DOBJ_BYTES:
2547 remove_bytes(comp, dobj, pass);
2548 break;
2549 case SND_SOC_DOBJ_WIDGET:
2550 remove_widget(comp, dobj, pass);
2551 break;
2552 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002553 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002554 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002555 case SND_SOC_DOBJ_DAI_LINK:
2556 remove_link(comp, dobj, pass);
2557 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002558 default:
2559 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2560 dobj->type);
2561 break;
2562 }
2563 }
2564 pass--;
2565 }
2566
2567 /* let caller know if FW can be freed when no objects are left */
2568 return !list_empty(&comp->dobj_list);
2569}
2570EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);