blob: 53f121a50c97bd6858a2b846b1a3f53e03026dd6 [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/* topology context */
Liam Girdwood8a978232015-05-29 19:06:14 +010058struct soc_tplg {
59 const struct firmware *fw;
60
61 /* runtime FW parsing */
62 const u8 *pos; /* read postion */
63 const u8 *hdr_pos; /* header position */
64 unsigned int pass; /* pass number */
65
66 /* component caller */
67 struct device *dev;
68 struct snd_soc_component *comp;
69 u32 index; /* current block index */
70 u32 req_index; /* required index, only loaded/free matching blocks */
71
Mengdong Lin88a17d82015-08-18 18:11:51 +080072 /* vendor specific kcontrol operations */
Liam Girdwood8a978232015-05-29 19:06:14 +010073 const struct snd_soc_tplg_kcontrol_ops *io_ops;
74 int io_ops_count;
75
Mengdong Lin1a3232d2015-08-18 18:12:20 +080076 /* vendor specific bytes ext handlers, for TLV bytes controls */
77 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
78 int bytes_ext_ops_count;
79
Liam Girdwood8a978232015-05-29 19:06:14 +010080 /* optional fw loading callbacks to component drivers */
81 struct snd_soc_tplg_ops *ops;
82};
83
84static int soc_tplg_process_headers(struct soc_tplg *tplg);
85static void soc_tplg_complete(struct soc_tplg *tplg);
86struct snd_soc_dapm_widget *
87snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
88 const struct snd_soc_dapm_widget *widget);
89struct snd_soc_dapm_widget *
90snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
91 const struct snd_soc_dapm_widget *widget);
92
93/* check we dont overflow the data for this control chunk */
94static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
95 unsigned int count, size_t bytes, const char *elem_type)
96{
97 const u8 *end = tplg->pos + elem_size * count;
98
99 if (end > tplg->fw->data + tplg->fw->size) {
100 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
101 elem_type);
102 return -EINVAL;
103 }
104
105 /* check there is enough room in chunk for control.
106 extra bytes at the end of control are for vendor data here */
107 if (elem_size * count > bytes) {
108 dev_err(tplg->dev,
109 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
110 elem_type, count, elem_size, bytes);
111 return -EINVAL;
112 }
113
114 return 0;
115}
116
117static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
118{
119 const u8 *end = tplg->hdr_pos;
120
121 if (end >= tplg->fw->data + tplg->fw->size)
122 return 1;
123 return 0;
124}
125
126static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
127{
128 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
129}
130
131static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
132{
133 return (unsigned long)(tplg->pos - tplg->fw->data);
134}
135
136/* mapping of Kcontrol types and associated operations. */
137static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
138 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
139 snd_soc_put_volsw, snd_soc_info_volsw},
140 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
141 snd_soc_put_volsw_sx, NULL},
142 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
143 snd_soc_put_enum_double, snd_soc_info_enum_double},
144 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
145 snd_soc_put_enum_double, NULL},
146 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
147 snd_soc_bytes_put, snd_soc_bytes_info},
148 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
149 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
150 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
151 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
152 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
153 snd_soc_put_strobe, NULL},
154 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530155 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100156 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
157 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
158 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
159 snd_soc_dapm_put_enum_double, NULL},
160 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
161 snd_soc_dapm_put_enum_double, NULL},
162 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
163 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
164};
165
166struct soc_tplg_map {
167 int uid;
168 int kid;
169};
170
171/* mapping of widget types from UAPI IDs to kernel IDs */
172static const struct soc_tplg_map dapm_map[] = {
173 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
174 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
175 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
176 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
177 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
178 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
179 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
180 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
181 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
182 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
183 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
184 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
185 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
186 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
187 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
188 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
Liam Girdwood8a70b452017-06-29 14:22:24 +0100189 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
190 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
191 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
192 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
193 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
194 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
195 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
196 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
Liam Girdwood8a978232015-05-29 19:06:14 +0100197};
198
199static int tplc_chan_get_reg(struct soc_tplg *tplg,
200 struct snd_soc_tplg_channel *chan, int map)
201{
202 int i;
203
204 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
205 if (chan[i].id == map)
206 return chan[i].reg;
207 }
208
209 return -EINVAL;
210}
211
212static int tplc_chan_get_shift(struct soc_tplg *tplg,
213 struct snd_soc_tplg_channel *chan, int map)
214{
215 int i;
216
217 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
218 if (chan[i].id == map)
219 return chan[i].shift;
220 }
221
222 return -EINVAL;
223}
224
225static int get_widget_id(int tplg_type)
226{
227 int i;
228
229 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
230 if (tplg_type == dapm_map[i].uid)
231 return dapm_map[i].kid;
232 }
233
234 return -EINVAL;
235}
236
Liam Girdwood8a978232015-05-29 19:06:14 +0100237static inline void soc_bind_err(struct soc_tplg *tplg,
238 struct snd_soc_tplg_ctl_hdr *hdr, int index)
239{
240 dev_err(tplg->dev,
241 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
242 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
243 soc_tplg_get_offset(tplg));
244}
245
246static inline void soc_control_err(struct soc_tplg *tplg,
247 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
248{
249 dev_err(tplg->dev,
250 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
251 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
252 soc_tplg_get_offset(tplg));
253}
254
255/* pass vendor data to component driver for processing */
256static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
257 struct snd_soc_tplg_hdr *hdr)
258{
259 int ret = 0;
260
261 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
Mark Brown24ada0352018-04-18 15:40:41 +0100262 ret = tplg->ops->vendor_load(tplg->comp, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100263 else {
264 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
265 hdr->vendor_type);
266 return -EINVAL;
267 }
268
269 if (ret < 0)
270 dev_err(tplg->dev,
271 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
272 soc_tplg_get_hdr_offset(tplg),
273 soc_tplg_get_hdr_offset(tplg),
274 hdr->type, hdr->vendor_type);
275 return ret;
276}
277
278/* pass vendor data to component driver for processing */
279static int soc_tplg_vendor_load(struct soc_tplg *tplg,
280 struct snd_soc_tplg_hdr *hdr)
281{
282 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
283 return 0;
284
285 return soc_tplg_vendor_load_(tplg, hdr);
286}
287
288/* optionally pass new dynamic widget to component driver. This is mainly for
289 * external widgets where we can assign private data/ops */
290static int soc_tplg_widget_load(struct soc_tplg *tplg,
291 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
292{
293 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
Mark Brown24ada0352018-04-18 15:40:41 +0100294 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
Liam Girdwood8a978232015-05-29 19:06:14 +0100295
296 return 0;
297}
298
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100299/* optionally pass new dynamic widget to component driver. This is mainly for
300 * external widgets where we can assign private data/ops */
301static int soc_tplg_widget_ready(struct soc_tplg *tplg,
302 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
303{
304 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
Mark Brown24ada0352018-04-18 15:40:41 +0100305 return tplg->ops->widget_ready(tplg->comp, w, tplg_w);
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100306
307 return 0;
308}
309
Masahiro Yamada183b8022017-02-27 14:29:20 -0800310/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800311static int soc_tplg_dai_load(struct soc_tplg *tplg,
Mark Brown24ada0352018-04-18 15:40:41 +0100312 struct snd_soc_dai_driver *dai_drv)
Liam Girdwood8a978232015-05-29 19:06:14 +0100313{
Mengdong Lin64527e82016-01-15 16:13:28 +0800314 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
Mark Brown24ada0352018-04-18 15:40:41 +0100315 return tplg->ops->dai_load(tplg->comp, dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100316
317 return 0;
318}
319
Masahiro Yamada183b8022017-02-27 14:29:20 -0800320/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800321static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
Mark Brown24ada0352018-04-18 15:40:41 +0100322 struct snd_soc_dai_link *link)
Mengdong Linacfc7d42016-01-15 16:13:37 +0800323{
324 if (tplg->comp && tplg->ops && tplg->ops->link_load)
Mark Brown24ada0352018-04-18 15:40:41 +0100325 return tplg->ops->link_load(tplg->comp, link);
Mengdong Linacfc7d42016-01-15 16:13:37 +0800326
327 return 0;
328}
329
Liam Girdwood8a978232015-05-29 19:06:14 +0100330/* tell the component driver that all firmware has been loaded in this request */
331static void soc_tplg_complete(struct soc_tplg *tplg)
332{
333 if (tplg->comp && tplg->ops && tplg->ops->complete)
334 tplg->ops->complete(tplg->comp);
335}
336
337/* add a dynamic kcontrol */
338static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
339 const struct snd_kcontrol_new *control_new, const char *prefix,
340 void *data, struct snd_kcontrol **kcontrol)
341{
342 int err;
343
344 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
345 if (*kcontrol == NULL) {
346 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
347 control_new->name);
348 return -ENOMEM;
349 }
350
351 err = snd_ctl_add(card, *kcontrol);
352 if (err < 0) {
353 dev_err(dev, "ASoC: Failed to add %s: %d\n",
354 control_new->name, err);
355 return err;
356 }
357
358 return 0;
359}
360
361/* add a dynamic kcontrol for component driver */
362static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
363 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
364{
365 struct snd_soc_component *comp = tplg->comp;
366
367 return soc_tplg_add_dcontrol(comp->card->snd_card,
368 comp->dev, k, NULL, comp, kcontrol);
369}
370
371/* remove a mixer kcontrol */
372static void remove_mixer(struct snd_soc_component *comp,
373 struct snd_soc_dobj *dobj, int pass)
374{
375 struct snd_card *card = comp->card->snd_card;
376 struct soc_mixer_control *sm =
377 container_of(dobj, struct soc_mixer_control, dobj);
378 const unsigned int *p = NULL;
379
380 if (pass != SOC_TPLG_PASS_MIXER)
381 return;
382
383 if (dobj->ops && dobj->ops->control_unload)
384 dobj->ops->control_unload(comp, dobj);
385
386 if (sm->dobj.control.kcontrol->tlv.p)
387 p = sm->dobj.control.kcontrol->tlv.p;
388 snd_ctl_remove(card, sm->dobj.control.kcontrol);
389 list_del(&sm->dobj.list);
390 kfree(sm);
391 kfree(p);
392}
393
394/* remove an enum kcontrol */
395static void remove_enum(struct snd_soc_component *comp,
396 struct snd_soc_dobj *dobj, int pass)
397{
398 struct snd_card *card = comp->card->snd_card;
399 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
400 int i;
401
402 if (pass != SOC_TPLG_PASS_MIXER)
403 return;
404
405 if (dobj->ops && dobj->ops->control_unload)
406 dobj->ops->control_unload(comp, dobj);
407
408 snd_ctl_remove(card, se->dobj.control.kcontrol);
409 list_del(&se->dobj.list);
410
411 kfree(se->dobj.control.dvalues);
412 for (i = 0; i < se->items; i++)
413 kfree(se->dobj.control.dtexts[i]);
414 kfree(se);
415}
416
417/* remove a byte kcontrol */
418static void remove_bytes(struct snd_soc_component *comp,
419 struct snd_soc_dobj *dobj, int pass)
420{
421 struct snd_card *card = comp->card->snd_card;
422 struct soc_bytes_ext *sb =
423 container_of(dobj, struct soc_bytes_ext, dobj);
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 snd_ctl_remove(card, sb->dobj.control.kcontrol);
432 list_del(&sb->dobj.list);
433 kfree(sb);
434}
435
436/* remove a widget and it's kcontrols - routes must be removed first */
437static void remove_widget(struct snd_soc_component *comp,
438 struct snd_soc_dobj *dobj, int pass)
439{
440 struct snd_card *card = comp->card->snd_card;
441 struct snd_soc_dapm_widget *w =
442 container_of(dobj, struct snd_soc_dapm_widget, dobj);
443 int i;
444
445 if (pass != SOC_TPLG_PASS_WIDGET)
446 return;
447
448 if (dobj->ops && dobj->ops->widget_unload)
449 dobj->ops->widget_unload(comp, dobj);
450
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000451 if (!w->kcontrols)
452 goto free_news;
453
Liam Girdwood8a978232015-05-29 19:06:14 +0100454 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800455 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100456 * The enum may either have an array of values or strings.
457 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800458 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100459 /* enumerated widget mixer */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100460 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800461 struct snd_kcontrol *kcontrol = w->kcontrols[i];
462 struct soc_enum *se =
463 (struct soc_enum *)kcontrol->private_value;
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100464 int j;
Liam Girdwood8a978232015-05-29 19:06:14 +0100465
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800466 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100467
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800468 kfree(se->dobj.control.dvalues);
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100469 for (j = 0; j < se->items; j++)
470 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +0100471
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800472 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100473 kfree(w->kcontrol_news[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800474 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100475 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800476 /* volume mixer or bytes controls */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100477 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100478 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100479
Mengdong Lineea3dd42016-11-25 16:09:17 +0800480 if (dobj->widget.kcontrol_type
481 == SND_SOC_TPLG_TYPE_MIXER)
482 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100483
Mengdong Lineea3dd42016-11-25 16:09:17 +0800484 /* Private value is used as struct soc_mixer_control
485 * for volume mixers or soc_bytes_ext for bytes
486 * controls.
487 */
488 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000489 snd_ctl_remove(card, kcontrol);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100490 kfree(w->kcontrol_news[i].name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100491 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100492 }
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000493
494free_news:
495 kfree(w->kcontrol_news);
496
Liam Girdwood8a978232015-05-29 19:06:14 +0100497 /* widget w is freed by soc-dapm.c */
498}
499
Mengdong Lin64527e82016-01-15 16:13:28 +0800500/* remove DAI configurations */
501static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100502 struct snd_soc_dobj *dobj, int pass)
503{
Mengdong Lin64527e82016-01-15 16:13:28 +0800504 struct snd_soc_dai_driver *dai_drv =
505 container_of(dobj, struct snd_soc_dai_driver, dobj);
506
Liam Girdwood8a978232015-05-29 19:06:14 +0100507 if (pass != SOC_TPLG_PASS_PCM_DAI)
508 return;
509
Mengdong Lin64527e82016-01-15 16:13:28 +0800510 if (dobj->ops && dobj->ops->dai_unload)
511 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100512
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800513 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100514 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800515 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100516}
517
Mengdong Linacfc7d42016-01-15 16:13:37 +0800518/* remove link configurations */
519static void remove_link(struct snd_soc_component *comp,
520 struct snd_soc_dobj *dobj, int pass)
521{
522 struct snd_soc_dai_link *link =
523 container_of(dobj, struct snd_soc_dai_link, dobj);
524
525 if (pass != SOC_TPLG_PASS_PCM_DAI)
526 return;
527
528 if (dobj->ops && dobj->ops->link_unload)
529 dobj->ops->link_unload(comp, dobj);
530
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800531 kfree(link->name);
532 kfree(link->stream_name);
533 kfree(link->cpu_dai_name);
534
Mengdong Linacfc7d42016-01-15 16:13:37 +0800535 list_del(&dobj->list);
536 snd_soc_remove_dai_link(comp->card, link);
537 kfree(link);
538}
539
Liam Girdwood8a978232015-05-29 19:06:14 +0100540/* bind a kcontrol to it's IO handlers */
541static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
542 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800543 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100544{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800545 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800546 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800547 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100548
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800549 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
550 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
551 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
552 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
553 struct soc_bytes_ext *sbe;
554 struct snd_soc_tplg_bytes_control *be;
555
556 sbe = (struct soc_bytes_ext *)k->private_value;
557 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
558
559 /* TLV bytes controls need standard kcontrol info handler,
560 * TLV callback and extended put/get handlers.
561 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530562 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800563 k->tlv.c = snd_soc_bytes_tlv_callback;
564
565 ext_ops = tplg->bytes_ext_ops;
566 num_ops = tplg->bytes_ext_ops_count;
567 for (i = 0; i < num_ops; i++) {
568 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
569 sbe->put = ext_ops[i].put;
570 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
571 sbe->get = ext_ops[i].get;
572 }
573
574 if (sbe->put && sbe->get)
575 return 0;
576 else
577 return -EINVAL;
578 }
579
Mengdong Lin88a17d82015-08-18 18:11:51 +0800580 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800581 ops = tplg->io_ops;
582 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100583 for (i = 0; i < num_ops; i++) {
584
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800585 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100586 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800587 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100588 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800589 if (k->info == NULL && ops[i].id == hdr->ops.info)
590 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100591 }
592
Mengdong Lin88a17d82015-08-18 18:11:51 +0800593 /* vendor specific handlers found ? */
594 if (k->put && k->get && k->info)
595 return 0;
596
597 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800598 ops = io_ops;
599 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800600 for (i = 0; i < num_ops; i++) {
601
602 if (k->put == NULL && ops[i].id == hdr->ops.put)
603 k->put = ops[i].put;
604 if (k->get == NULL && ops[i].id == hdr->ops.get)
605 k->get = ops[i].get;
606 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100607 k->info = ops[i].info;
608 }
609
610 /* standard handlers found ? */
611 if (k->put && k->get && k->info)
612 return 0;
613
Liam Girdwood8a978232015-05-29 19:06:14 +0100614 /* nothing to bind */
615 return -EINVAL;
616}
617
618/* bind a widgets to it's evnt handlers */
619int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
620 const struct snd_soc_tplg_widget_events *events,
621 int num_events, u16 event_type)
622{
623 int i;
624
625 w->event = NULL;
626
627 for (i = 0; i < num_events; i++) {
628 if (event_type == events[i].type) {
629
630 /* found - so assign event */
631 w->event = events[i].event_handler;
632 return 0;
633 }
634 }
635
636 /* not found */
637 return -EINVAL;
638}
639EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
640
641/* optionally pass new dynamic kcontrol to component driver. */
642static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
643 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
644{
645 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Mark Brown24ada0352018-04-18 15:40:41 +0100646 return tplg->ops->control_load(tplg->comp, k, 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
1103static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1104 struct snd_soc_tplg_hdr *hdr)
1105{
1106 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1107 struct snd_soc_dapm_route route;
1108 struct snd_soc_tplg_dapm_graph_elem *elem;
1109 int count = hdr->count, i;
1110
1111 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1112 tplg->pos += hdr->size + hdr->payload_size;
1113 return 0;
1114 }
1115
1116 if (soc_tplg_check_elem_count(tplg,
1117 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1118 count, hdr->payload_size, "graph")) {
1119
1120 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1121 count);
1122 return -EINVAL;
1123 }
1124
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001125 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1126 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001127
1128 for (i = 0; i < count; i++) {
1129 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1130 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1131
1132 /* validate routes */
1133 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1134 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1135 return -EINVAL;
1136 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1137 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1138 return -EINVAL;
1139 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1140 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1141 return -EINVAL;
1142
1143 route.source = elem->source;
1144 route.sink = elem->sink;
1145 route.connected = NULL; /* set to NULL atm for tplg users */
1146 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1147 route.control = NULL;
1148 else
1149 route.control = elem->control;
1150
1151 /* add route, but keep going if some fail */
1152 snd_soc_dapm_add_routes(dapm, &route, 1);
1153 }
1154
1155 return 0;
1156}
1157
1158static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1159 struct soc_tplg *tplg, int num_kcontrols)
1160{
1161 struct snd_kcontrol_new *kc;
1162 struct soc_mixer_control *sm;
1163 struct snd_soc_tplg_mixer_control *mc;
1164 int i, err;
1165
Axel Lin4ca7deb2015-08-05 22:34:22 +08001166 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001167 if (kc == NULL)
1168 return NULL;
1169
1170 for (i = 0; i < num_kcontrols; i++) {
1171 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1172 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1173 if (sm == NULL)
1174 goto err;
1175
1176 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1177 mc->priv.size);
1178
1179 /* validate kcontrol */
1180 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1181 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1182 goto err_str;
1183
1184 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1185 mc->hdr.name, i);
1186
Liam Girdwood267e2c62018-03-27 12:04:04 +01001187 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1188 if (kc[i].name == NULL)
1189 goto err_str;
Liam Girdwood8a978232015-05-29 19:06:14 +01001190 kc[i].private_value = (long)sm;
1191 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1192 kc[i].access = mc->hdr.access;
1193
1194 /* we only support FL/FR channel mapping atm */
1195 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1196 SNDRV_CHMAP_FL);
1197 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1198 SNDRV_CHMAP_FR);
1199 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1200 SNDRV_CHMAP_FL);
1201 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1202 SNDRV_CHMAP_FR);
1203
1204 sm->max = mc->max;
1205 sm->min = mc->min;
1206 sm->invert = mc->invert;
1207 sm->platform_max = mc->platform_max;
1208 sm->dobj.index = tplg->index;
1209 INIT_LIST_HEAD(&sm->dobj.list);
1210
1211 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001212 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001213 if (err) {
1214 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1215 kfree(sm);
1216 continue;
1217 }
1218
1219 /* pass control to driver for optional further init */
1220 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1221 (struct snd_soc_tplg_ctl_hdr *)mc);
1222 if (err < 0) {
1223 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1224 mc->hdr.name);
1225 kfree(sm);
1226 continue;
1227 }
Ranjani Sridharanbde8b382018-03-09 11:11:17 -08001228
1229 /* create any TLV data */
1230 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01001231 }
1232 return kc;
1233
1234err_str:
1235 kfree(sm);
1236err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001237 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001238 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001239 kfree(kc[i].name);
1240 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001241 kfree(kc);
1242 return NULL;
1243}
1244
1245static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001246 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001247{
1248 struct snd_kcontrol_new *kc;
1249 struct snd_soc_tplg_enum_control *ec;
1250 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001251 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001252
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001253 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001254 if (kc == NULL)
1255 return NULL;
1256
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001257 for (i = 0; i < num_kcontrols; i++) {
1258 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1259 /* validate kcontrol */
1260 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1261 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Christophe JAILLETf3ee9092017-09-14 22:44:24 +02001262 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001263
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001264 se = kzalloc(sizeof(*se), GFP_KERNEL);
1265 if (se == NULL)
1266 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001267
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001268 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001269 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001270
Liam Girdwood267e2c62018-03-27 12:04:04 +01001271 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001272 if (kc[i].name == NULL) {
1273 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001274 goto err_se;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001275 }
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001276 kc[i].private_value = (long)se;
1277 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1278 kc[i].access = ec->hdr.access;
1279
1280 /* we only support FL/FR channel mapping atm */
1281 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1282 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1283 SNDRV_CHMAP_FL);
1284 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1285 SNDRV_CHMAP_FR);
1286
1287 se->items = ec->items;
1288 se->mask = ec->mask;
1289 se->dobj.index = tplg->index;
1290
1291 switch (ec->hdr.ops.info) {
1292 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1293 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1294 err = soc_tplg_denum_create_values(se, ec);
1295 if (err < 0) {
1296 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1297 ec->hdr.name);
1298 goto err_se;
1299 }
1300 /* fall through to create texts */
1301 case SND_SOC_TPLG_CTL_ENUM:
1302 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1303 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1304 err = soc_tplg_denum_create_texts(se, ec);
1305 if (err < 0) {
1306 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1307 ec->hdr.name);
1308 goto err_se;
1309 }
1310 break;
1311 default:
1312 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1313 ec->hdr.ops.info, ec->hdr.name);
1314 goto err_se;
1315 }
1316
1317 /* map io handlers */
1318 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1319 if (err) {
1320 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1321 goto err_se;
1322 }
1323
1324 /* pass control to driver for optional further init */
1325 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1326 (struct snd_soc_tplg_ctl_hdr *)ec);
1327 if (err < 0) {
1328 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1329 ec->hdr.name);
1330 goto err_se;
1331 }
1332
1333 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1334 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001335 }
1336
1337 return kc;
1338
1339err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001340 for (; i >= 0; i--) {
1341 /* free values and texts */
1342 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001343 if (!se)
1344 continue;
1345
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001346 kfree(se->dobj.control.dvalues);
1347 for (j = 0; j < ec->items; j++)
1348 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001349
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001350 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001351 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001352 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001353err:
1354 kfree(kc);
1355
1356 return NULL;
1357}
1358
1359static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1360 struct soc_tplg *tplg, int count)
1361{
1362 struct snd_soc_tplg_bytes_control *be;
1363 struct soc_bytes_ext *sbe;
1364 struct snd_kcontrol_new *kc;
1365 int i, err;
1366
Axel Lin4ca7deb2015-08-05 22:34:22 +08001367 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001368 if (!kc)
1369 return NULL;
1370
1371 for (i = 0; i < count; i++) {
1372 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1373
1374 /* validate kcontrol */
1375 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1376 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1377 goto err;
1378
1379 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1380 if (sbe == NULL)
1381 goto err;
1382
1383 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1384 be->priv.size);
1385
1386 dev_dbg(tplg->dev,
1387 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1388 be->hdr.name, be->hdr.access);
1389
Liam Girdwood267e2c62018-03-27 12:04:04 +01001390 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001391 if (kc[i].name == NULL) {
1392 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001393 goto err;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001394 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001395 kc[i].private_value = (long)sbe;
1396 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1397 kc[i].access = be->hdr.access;
1398
1399 sbe->max = be->max;
1400 INIT_LIST_HEAD(&sbe->dobj.list);
1401
1402 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001403 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001404 if (err) {
1405 soc_control_err(tplg, &be->hdr, be->hdr.name);
1406 kfree(sbe);
1407 continue;
1408 }
1409
1410 /* pass control to driver for optional further init */
1411 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1412 (struct snd_soc_tplg_ctl_hdr *)be);
1413 if (err < 0) {
1414 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1415 be->hdr.name);
1416 kfree(sbe);
1417 continue;
1418 }
1419 }
1420
1421 return kc;
1422
1423err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001424 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001425 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001426 kfree(kc[i].name);
1427 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001428
1429 kfree(kc);
1430 return NULL;
1431}
1432
1433static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1434 struct snd_soc_tplg_dapm_widget *w)
1435{
1436 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1437 struct snd_soc_dapm_widget template, *widget;
1438 struct snd_soc_tplg_ctl_hdr *control_hdr;
1439 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001440 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001441 int ret = 0;
1442
1443 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1444 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1445 return -EINVAL;
1446 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1447 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1448 return -EINVAL;
1449
1450 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1451 w->name, w->id);
1452
1453 memset(&template, 0, sizeof(template));
1454
1455 /* map user to kernel widget ID */
1456 template.id = get_widget_id(w->id);
1457 if (template.id < 0)
1458 return template.id;
1459
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001460 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001461 template.name = kstrdup(w->name, GFP_KERNEL);
1462 if (!template.name)
1463 return -ENOMEM;
1464 template.sname = kstrdup(w->sname, GFP_KERNEL);
1465 if (!template.sname) {
1466 ret = -ENOMEM;
1467 goto err;
1468 }
1469 template.reg = w->reg;
1470 template.shift = w->shift;
1471 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001472 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001473 template.on_val = w->invert ? 0 : 1;
1474 template.off_val = w->invert ? 1 : 0;
1475 template.ignore_suspend = w->ignore_suspend;
1476 template.event_flags = w->event_flags;
1477 template.dobj.index = tplg->index;
1478
1479 tplg->pos +=
1480 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1481 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001482 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001483 template.num_kcontrols = 0;
1484 goto widget;
1485 }
1486
1487 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1488 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1489 w->name, w->num_kcontrols, control_hdr->type);
1490
1491 switch (control_hdr->ops.info) {
1492 case SND_SOC_TPLG_CTL_VOLSW:
1493 case SND_SOC_TPLG_CTL_STROBE:
1494 case SND_SOC_TPLG_CTL_VOLSW_SX:
1495 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1496 case SND_SOC_TPLG_CTL_RANGE:
1497 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001498 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Liam Girdwood8a978232015-05-29 19:06:14 +01001499 template.num_kcontrols = w->num_kcontrols;
1500 template.kcontrol_news =
1501 soc_tplg_dapm_widget_dmixer_create(tplg,
1502 template.num_kcontrols);
1503 if (!template.kcontrol_news) {
1504 ret = -ENOMEM;
1505 goto hdr_err;
1506 }
1507 break;
1508 case SND_SOC_TPLG_CTL_ENUM:
1509 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1510 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1511 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1512 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001513 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001514 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001515 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001516 soc_tplg_dapm_widget_denum_create(tplg,
1517 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001518 if (!template.kcontrol_news) {
1519 ret = -ENOMEM;
1520 goto hdr_err;
1521 }
1522 break;
1523 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001524 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Liam Girdwood8a978232015-05-29 19:06:14 +01001525 template.num_kcontrols = w->num_kcontrols;
1526 template.kcontrol_news =
1527 soc_tplg_dapm_widget_dbytes_create(tplg,
1528 template.num_kcontrols);
1529 if (!template.kcontrol_news) {
1530 ret = -ENOMEM;
1531 goto hdr_err;
1532 }
1533 break;
1534 default:
1535 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1536 control_hdr->ops.get, control_hdr->ops.put,
1537 control_hdr->ops.info);
1538 ret = -EINVAL;
1539 goto hdr_err;
1540 }
1541
1542widget:
1543 ret = soc_tplg_widget_load(tplg, &template, w);
1544 if (ret < 0)
1545 goto hdr_err;
1546
1547 /* card dapm mutex is held by the core if we are loading topology
1548 * data during sound card init. */
1549 if (card->instantiated)
1550 widget = snd_soc_dapm_new_control(dapm, &template);
1551 else
1552 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001553 if (IS_ERR(widget)) {
1554 ret = PTR_ERR(widget);
1555 /* Do not nag about probe deferrals */
1556 if (ret != -EPROBE_DEFER)
1557 dev_err(tplg->dev,
1558 "ASoC: failed to create widget %s controls (%d)\n",
1559 w->name, ret);
1560 goto hdr_err;
1561 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001562 if (widget == NULL) {
1563 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1564 w->name);
Wei Yongjun8ae3ea42016-08-10 13:43:12 +00001565 ret = -ENOMEM;
Liam Girdwood8a978232015-05-29 19:06:14 +01001566 goto hdr_err;
1567 }
1568
1569 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001570 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001571 widget->dobj.ops = tplg->ops;
1572 widget->dobj.index = tplg->index;
1573 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001574
1575 ret = soc_tplg_widget_ready(tplg, widget, w);
1576 if (ret < 0)
1577 goto ready_err;
1578
Liam Girdwood8a978232015-05-29 19:06:14 +01001579 return 0;
1580
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001581ready_err:
1582 snd_soc_tplg_widget_remove(widget);
1583 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001584hdr_err:
1585 kfree(template.sname);
1586err:
1587 kfree(template.name);
1588 return ret;
1589}
1590
1591static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1592 struct snd_soc_tplg_hdr *hdr)
1593{
1594 struct snd_soc_tplg_dapm_widget *widget;
1595 int ret, count = hdr->count, i;
1596
1597 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1598 return 0;
1599
1600 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1601
1602 for (i = 0; i < count; i++) {
1603 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001604 if (widget->size != sizeof(*widget)) {
1605 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1606 return -EINVAL;
1607 }
1608
Liam Girdwood8a978232015-05-29 19:06:14 +01001609 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001610 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001611 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1612 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001613 return ret;
1614 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001615 }
1616
1617 return 0;
1618}
1619
1620static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1621{
1622 struct snd_soc_card *card = tplg->comp->card;
1623 int ret;
1624
1625 /* Card might not have been registered at this point.
1626 * If so, just return success.
1627 */
1628 if (!card || !card->instantiated) {
1629 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001630 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001631 return 0;
1632 }
1633
1634 ret = snd_soc_dapm_new_widgets(card);
1635 if (ret < 0)
1636 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1637 ret);
1638
1639 return 0;
1640}
1641
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001642static void set_stream_info(struct snd_soc_pcm_stream *stream,
1643 struct snd_soc_tplg_stream_caps *caps)
1644{
1645 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1646 stream->channels_min = caps->channels_min;
1647 stream->channels_max = caps->channels_max;
1648 stream->rates = caps->rates;
1649 stream->rate_min = caps->rate_min;
1650 stream->rate_max = caps->rate_max;
1651 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001652 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001653}
1654
Mengdong Lin0038be92016-07-26 14:32:37 +08001655static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1656 unsigned int flag_mask, unsigned int flags)
1657{
1658 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1659 dai_drv->symmetric_rates =
1660 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1661
1662 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1663 dai_drv->symmetric_channels =
1664 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1665 1 : 0;
1666
1667 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1668 dai_drv->symmetric_samplebits =
1669 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1670 1 : 0;
1671}
1672
Mengdong Lin64527e82016-01-15 16:13:28 +08001673static int soc_tplg_dai_create(struct soc_tplg *tplg,
1674 struct snd_soc_tplg_pcm *pcm)
1675{
1676 struct snd_soc_dai_driver *dai_drv;
1677 struct snd_soc_pcm_stream *stream;
1678 struct snd_soc_tplg_stream_caps *caps;
1679 int ret;
1680
1681 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1682 if (dai_drv == NULL)
1683 return -ENOMEM;
1684
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001685 if (strlen(pcm->dai_name))
1686 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001687 dai_drv->id = pcm->dai_id;
1688
1689 if (pcm->playback) {
1690 stream = &dai_drv->playback;
1691 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001692 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001693 }
1694
1695 if (pcm->capture) {
1696 stream = &dai_drv->capture;
1697 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001698 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001699 }
1700
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001701 if (pcm->compress)
1702 dai_drv->compress_new = snd_soc_new_compress;
1703
Mengdong Lin64527e82016-01-15 16:13:28 +08001704 /* pass control to component driver for optional further init */
Mark Brown24ada0352018-04-18 15:40:41 +01001705 ret = soc_tplg_dai_load(tplg, dai_drv);
Mengdong Lin64527e82016-01-15 16:13:28 +08001706 if (ret < 0) {
1707 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1708 kfree(dai_drv);
1709 return ret;
1710 }
1711
1712 dai_drv->dobj.index = tplg->index;
1713 dai_drv->dobj.ops = tplg->ops;
1714 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1715 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1716
1717 /* register the DAI to the component */
1718 return snd_soc_register_dai(tplg->comp, dai_drv);
1719}
1720
Mengdong Lin717a8e72016-11-03 01:03:34 +08001721static void set_link_flags(struct snd_soc_dai_link *link,
1722 unsigned int flag_mask, unsigned int flags)
1723{
1724 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1725 link->symmetric_rates =
1726 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1727
1728 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1729 link->symmetric_channels =
1730 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1731 1 : 0;
1732
1733 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1734 link->symmetric_samplebits =
1735 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1736 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001737
1738 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1739 link->ignore_suspend =
1740 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1741 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001742}
1743
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001744/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001745static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001746 struct snd_soc_tplg_pcm *pcm)
1747{
1748 struct snd_soc_dai_link *link;
1749 int ret;
1750
1751 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1752 if (link == NULL)
1753 return -ENOMEM;
1754
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001755 if (strlen(pcm->pcm_name)) {
1756 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1757 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1758 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001759 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001760
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001761 if (strlen(pcm->dai_name))
1762 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1763
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001764 link->codec_name = "snd-soc-dummy";
1765 link->codec_dai_name = "snd-soc-dummy-dai";
1766
1767 /* enable DPCM */
1768 link->dynamic = 1;
1769 link->dpcm_playback = pcm->playback;
1770 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001771 if (pcm->flag_mask)
1772 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001773
Mengdong Linacfc7d42016-01-15 16:13:37 +08001774 /* pass control to component driver for optional further init */
Mark Brown24ada0352018-04-18 15:40:41 +01001775 ret = soc_tplg_dai_link_load(tplg, link);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001776 if (ret < 0) {
1777 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1778 kfree(link);
1779 return ret;
1780 }
1781
1782 link->dobj.index = tplg->index;
1783 link->dobj.ops = tplg->ops;
1784 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1785 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1786
1787 snd_soc_add_dai_link(tplg->comp->card, link);
1788 return 0;
1789}
1790
1791/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001792static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1793 struct snd_soc_tplg_pcm *pcm)
1794{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001795 int ret;
1796
1797 ret = soc_tplg_dai_create(tplg, pcm);
1798 if (ret < 0)
1799 return ret;
1800
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001801 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001802}
1803
Mengdong Lin55726dc2016-11-03 01:00:16 +08001804/* copy stream caps from the old version 4 of source */
1805static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1806 struct snd_soc_tplg_stream_caps_v4 *src)
1807{
1808 dest->size = sizeof(*dest);
1809 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1810 dest->formats = src->formats;
1811 dest->rates = src->rates;
1812 dest->rate_min = src->rate_min;
1813 dest->rate_max = src->rate_max;
1814 dest->channels_min = src->channels_min;
1815 dest->channels_max = src->channels_max;
1816 dest->periods_min = src->periods_min;
1817 dest->periods_max = src->periods_max;
1818 dest->period_size_min = src->period_size_min;
1819 dest->period_size_max = src->period_size_max;
1820 dest->buffer_size_min = src->buffer_size_min;
1821 dest->buffer_size_max = src->buffer_size_max;
1822}
1823
1824/**
1825 * pcm_new_ver - Create the new version of PCM from the old version.
1826 * @tplg: topology context
1827 * @src: older version of pcm as a source
1828 * @pcm: latest version of pcm created from the source
1829 *
1830 * Support from vesion 4. User should free the returned pcm manually.
1831 */
1832static int pcm_new_ver(struct soc_tplg *tplg,
1833 struct snd_soc_tplg_pcm *src,
1834 struct snd_soc_tplg_pcm **pcm)
1835{
1836 struct snd_soc_tplg_pcm *dest;
1837 struct snd_soc_tplg_pcm_v4 *src_v4;
1838 int i;
1839
1840 *pcm = NULL;
1841
1842 if (src->size != sizeof(*src_v4)) {
1843 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1844 return -EINVAL;
1845 }
1846
1847 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1848 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1849 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1850 if (!dest)
1851 return -ENOMEM;
1852
1853 dest->size = sizeof(*dest); /* size of latest abi version */
1854 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1855 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1856 dest->pcm_id = src_v4->pcm_id;
1857 dest->dai_id = src_v4->dai_id;
1858 dest->playback = src_v4->playback;
1859 dest->capture = src_v4->capture;
1860 dest->compress = src_v4->compress;
1861 dest->num_streams = src_v4->num_streams;
1862 for (i = 0; i < dest->num_streams; i++)
1863 memcpy(&dest->stream[i], &src_v4->stream[i],
1864 sizeof(struct snd_soc_tplg_stream));
1865
1866 for (i = 0; i < 2; i++)
1867 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1868
1869 *pcm = dest;
1870 return 0;
1871}
1872
Mengdong Lin64527e82016-01-15 16:13:28 +08001873static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001874 struct snd_soc_tplg_hdr *hdr)
1875{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001876 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001877 int count = hdr->count;
Vinod Koulfd340452016-12-08 23:01:27 +05301878 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001879 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001880
1881 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1882 return 0;
1883
Mengdong Lin55726dc2016-11-03 01:00:16 +08001884 /* check the element size and count */
1885 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1886 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1887 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1888 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1889 pcm->size);
1890 return -EINVAL;
1891 }
1892
Liam Girdwood8a978232015-05-29 19:06:14 +01001893 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08001894 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001895 hdr->payload_size, "PCM DAI")) {
1896 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1897 count);
1898 return -EINVAL;
1899 }
1900
Mengdong Lin64527e82016-01-15 16:13:28 +08001901 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001902 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1903
1904 /* check ABI version by size, create a new version of pcm
1905 * if abi not match.
1906 */
1907 if (pcm->size == sizeof(*pcm)) {
1908 abi_match = true;
1909 _pcm = pcm;
1910 } else {
1911 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05301912 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001913 }
1914
Mengdong Lin55726dc2016-11-03 01:00:16 +08001915 /* create the FE DAIs and DAI links */
1916 soc_tplg_pcm_create(tplg, _pcm);
1917
Mengdong Lin717a8e72016-11-03 01:03:34 +08001918 /* offset by version-specific struct size and
1919 * real priv data size
1920 */
1921 tplg->pos += pcm->size + _pcm->priv.size;
1922
Mengdong Lin55726dc2016-11-03 01:00:16 +08001923 if (!abi_match)
1924 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08001925 }
1926
Liam Girdwood8a978232015-05-29 19:06:14 +01001927 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001928
Liam Girdwood8a978232015-05-29 19:06:14 +01001929 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001930}
1931
Mengdong Lin593d9e52016-11-03 01:04:27 +08001932/**
1933 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00001934 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08001935 * @cfg: physical link configs.
1936 *
1937 * Topology context contains a list of supported HW formats (configs) and
1938 * a default format ID for the physical link. This function will use this
1939 * default ID to choose the HW format to set the link's DAI format for init.
1940 */
1941static void set_link_hw_format(struct snd_soc_dai_link *link,
1942 struct snd_soc_tplg_link_config *cfg)
1943{
1944 struct snd_soc_tplg_hw_config *hw_config;
1945 unsigned char bclk_master, fsync_master;
1946 unsigned char invert_bclk, invert_fsync;
1947 int i;
1948
1949 for (i = 0; i < cfg->num_hw_configs; i++) {
1950 hw_config = &cfg->hw_config[i];
1951 if (hw_config->id != cfg->default_hw_config_id)
1952 continue;
1953
1954 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
1955
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001956 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02001957 switch (hw_config->clock_gated) {
1958 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001959 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02001960 break;
1961
1962 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001963 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02001964 break;
1965
1966 default:
1967 /* ignore the value */
1968 break;
1969 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001970
Mengdong Lin593d9e52016-11-03 01:04:27 +08001971 /* clock signal polarity */
1972 invert_bclk = hw_config->invert_bclk;
1973 invert_fsync = hw_config->invert_fsync;
1974 if (!invert_bclk && !invert_fsync)
1975 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1976 else if (!invert_bclk && invert_fsync)
1977 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1978 else if (invert_bclk && !invert_fsync)
1979 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1980 else
1981 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1982
1983 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02001984 bclk_master = (hw_config->bclk_master ==
1985 SND_SOC_TPLG_BCLK_CM);
1986 fsync_master = (hw_config->fsync_master ==
1987 SND_SOC_TPLG_FSYNC_CM);
1988 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08001989 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08001990 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02001991 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1992 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08001993 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1994 else
1995 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1996 }
1997}
1998
1999/**
2000 * link_new_ver - Create a new physical link config from the old
2001 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002002 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002003 * @src: old version of phyical link config as a source
2004 * @link: latest version of physical link config created from the source
2005 *
2006 * Support from vesion 4. User need free the returned link config manually.
2007 */
2008static int link_new_ver(struct soc_tplg *tplg,
2009 struct snd_soc_tplg_link_config *src,
2010 struct snd_soc_tplg_link_config **link)
2011{
2012 struct snd_soc_tplg_link_config *dest;
2013 struct snd_soc_tplg_link_config_v4 *src_v4;
2014 int i;
2015
2016 *link = NULL;
2017
2018 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2019 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2020 return -EINVAL;
2021 }
2022
2023 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2024
2025 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2026 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2027 if (!dest)
2028 return -ENOMEM;
2029
2030 dest->size = sizeof(*dest);
2031 dest->id = src_v4->id;
2032 dest->num_streams = src_v4->num_streams;
2033 for (i = 0; i < dest->num_streams; i++)
2034 memcpy(&dest->stream[i], &src_v4->stream[i],
2035 sizeof(struct snd_soc_tplg_stream));
2036
2037 *link = dest;
2038 return 0;
2039}
2040
2041/* Find and configure an existing physical DAI link */
2042static int soc_tplg_link_config(struct soc_tplg *tplg,
2043 struct snd_soc_tplg_link_config *cfg)
2044{
2045 struct snd_soc_dai_link *link;
2046 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002047 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002048 int ret;
2049
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002050 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2051 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2052 return -EINVAL;
2053 else if (len)
2054 name = cfg->name;
2055 else
2056 name = NULL;
2057
2058 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2059 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2060 return -EINVAL;
2061 else if (len)
2062 stream_name = cfg->stream_name;
2063 else
2064 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002065
2066 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2067 name, stream_name);
2068 if (!link) {
2069 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2070 name, cfg->id);
2071 return -EINVAL;
2072 }
2073
2074 /* hw format */
2075 if (cfg->num_hw_configs)
2076 set_link_hw_format(link, cfg);
2077
2078 /* flags */
2079 if (cfg->flag_mask)
2080 set_link_flags(link, cfg->flag_mask, cfg->flags);
2081
2082 /* pass control to component driver for optional further init */
Mark Brown24ada0352018-04-18 15:40:41 +01002083 ret = soc_tplg_dai_link_load(tplg, link);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002084 if (ret < 0) {
2085 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2086 return ret;
2087 }
2088
2089 return 0;
2090}
2091
2092
2093/* Load physical link config elements from the topology context */
2094static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2095 struct snd_soc_tplg_hdr *hdr)
2096{
2097 struct snd_soc_tplg_link_config *link, *_link;
2098 int count = hdr->count;
2099 int i, ret;
2100 bool abi_match;
2101
2102 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2103 tplg->pos += hdr->size + hdr->payload_size;
2104 return 0;
2105 };
2106
2107 /* check the element size and count */
2108 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2109 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2110 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2111 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2112 link->size);
2113 return -EINVAL;
2114 }
2115
2116 if (soc_tplg_check_elem_count(tplg,
2117 link->size, count,
2118 hdr->payload_size, "physical link config")) {
2119 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2120 count);
2121 return -EINVAL;
2122 }
2123
2124 /* config physical DAI links */
2125 for (i = 0; i < count; i++) {
2126 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2127 if (link->size == sizeof(*link)) {
2128 abi_match = true;
2129 _link = link;
2130 } else {
2131 abi_match = false;
2132 ret = link_new_ver(tplg, link, &_link);
2133 if (ret < 0)
2134 return ret;
2135 }
2136
2137 ret = soc_tplg_link_config(tplg, _link);
2138 if (ret < 0)
2139 return ret;
2140
2141 /* offset by version-specific struct size and
2142 * real priv data size
2143 */
2144 tplg->pos += link->size + _link->priv.size;
2145
2146 if (!abi_match)
2147 kfree(_link); /* free the duplicated one */
2148 }
2149
2150 return 0;
2151}
2152
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002153/**
2154 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002155 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002156 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002157 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002158 * The physical dai should already be registered by the platform driver.
2159 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002160 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002161static int soc_tplg_dai_config(struct soc_tplg *tplg,
2162 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002163{
2164 struct snd_soc_dai_link_component dai_component = {0};
2165 struct snd_soc_dai *dai;
2166 struct snd_soc_dai_driver *dai_drv;
2167 struct snd_soc_pcm_stream *stream;
2168 struct snd_soc_tplg_stream_caps *caps;
2169 int ret;
2170
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002171 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002172 dai = snd_soc_find_dai(&dai_component);
2173 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002174 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2175 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002176 return -EINVAL;
2177 }
2178
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002179 if (d->dai_id != dai->id) {
2180 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2181 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002182 return -EINVAL;
2183 }
2184
2185 dai_drv = dai->driver;
2186 if (!dai_drv)
2187 return -EINVAL;
2188
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002189 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002190 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002191 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002192 set_stream_info(stream, caps);
2193 }
2194
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002195 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002196 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002197 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002198 set_stream_info(stream, caps);
2199 }
2200
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002201 if (d->flag_mask)
2202 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002203
2204 /* pass control to component driver for optional further init */
Mark Brown24ada0352018-04-18 15:40:41 +01002205 ret = soc_tplg_dai_load(tplg, dai_drv);
Mengdong Lin0038be92016-07-26 14:32:37 +08002206 if (ret < 0) {
2207 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2208 return ret;
2209 }
2210
2211 return 0;
2212}
2213
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002214/* load physical DAI elements */
2215static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2216 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002217{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002218 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002219 int count = hdr->count;
2220 int i;
2221
2222 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2223 return 0;
2224
2225 /* config the existing BE DAIs */
2226 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002227 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2228 if (dai->size != sizeof(*dai)) {
2229 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002230 return -EINVAL;
2231 }
2232
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002233 soc_tplg_dai_config(tplg, dai);
2234 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002235 }
2236
2237 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2238 return 0;
2239}
2240
Mengdong Lin583958f2016-10-11 14:36:42 +08002241/**
2242 * manifest_new_ver - Create a new version of manifest from the old version
2243 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002244 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002245 * @src: old version of manifest as a source
2246 * @manifest: latest version of manifest created from the source
2247 *
2248 * Support from vesion 4. Users need free the returned manifest manually.
2249 */
2250static int manifest_new_ver(struct soc_tplg *tplg,
2251 struct snd_soc_tplg_manifest *src,
2252 struct snd_soc_tplg_manifest **manifest)
2253{
2254 struct snd_soc_tplg_manifest *dest;
2255 struct snd_soc_tplg_manifest_v4 *src_v4;
2256
2257 *manifest = NULL;
2258
2259 if (src->size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002260 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2261 src->size);
2262 if (src->size)
2263 return -EINVAL;
2264 src->size = sizeof(*src_v4);
Mengdong Lin583958f2016-10-11 14:36:42 +08002265 }
2266
2267 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2268
2269 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2270 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2271 if (!dest)
2272 return -ENOMEM;
2273
2274 dest->size = sizeof(*dest); /* size of latest abi version */
2275 dest->control_elems = src_v4->control_elems;
2276 dest->widget_elems = src_v4->widget_elems;
2277 dest->graph_elems = src_v4->graph_elems;
2278 dest->pcm_elems = src_v4->pcm_elems;
2279 dest->dai_link_elems = src_v4->dai_link_elems;
2280 dest->priv.size = src_v4->priv.size;
2281 if (dest->priv.size)
2282 memcpy(dest->priv.data, src_v4->priv.data,
2283 src_v4->priv.size);
2284
2285 *manifest = dest;
2286 return 0;
2287}
Mengdong Lin0038be92016-07-26 14:32:37 +08002288
Liam Girdwood8a978232015-05-29 19:06:14 +01002289static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002290 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002291{
Mengdong Lin583958f2016-10-11 14:36:42 +08002292 struct snd_soc_tplg_manifest *manifest, *_manifest;
2293 bool abi_match;
2294 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002295
2296 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2297 return 0;
2298
2299 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002300
2301 /* check ABI version by size, create a new manifest if abi not match */
2302 if (manifest->size == sizeof(*manifest)) {
2303 abi_match = true;
2304 _manifest = manifest;
2305 } else {
2306 abi_match = false;
2307 err = manifest_new_ver(tplg, manifest, &_manifest);
2308 if (err < 0)
2309 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002310 }
2311
Mengdong Lin583958f2016-10-11 14:36:42 +08002312 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002313 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Mark Brown24ada0352018-04-18 15:40:41 +01002314 return tplg->ops->manifest(tplg->comp, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002315
Mengdong Lin583958f2016-10-11 14:36:42 +08002316 if (!abi_match) /* free the duplicated one */
2317 kfree(_manifest);
2318
Liam Girdwood8a978232015-05-29 19:06:14 +01002319 return 0;
2320}
2321
2322/* validate header magic, size and type */
2323static int soc_valid_header(struct soc_tplg *tplg,
2324 struct snd_soc_tplg_hdr *hdr)
2325{
2326 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2327 return 0;
2328
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002329 if (hdr->size != sizeof(*hdr)) {
2330 dev_err(tplg->dev,
2331 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2332 hdr->type, soc_tplg_get_hdr_offset(tplg),
2333 tplg->fw->size);
2334 return -EINVAL;
2335 }
2336
Liam Girdwood8a978232015-05-29 19:06:14 +01002337 /* big endian firmware objects not supported atm */
2338 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2339 dev_err(tplg->dev,
2340 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2341 tplg->pass, hdr->magic,
2342 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2343 return -EINVAL;
2344 }
2345
2346 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2347 dev_err(tplg->dev,
2348 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2349 tplg->pass, hdr->magic,
2350 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2351 return -EINVAL;
2352 }
2353
Mengdong Lin288b8da2016-11-03 01:03:17 +08002354 /* Support ABI from version 4 */
2355 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2356 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002357 dev_err(tplg->dev,
2358 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2359 tplg->pass, hdr->abi,
2360 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2361 tplg->fw->size);
2362 return -EINVAL;
2363 }
2364
2365 if (hdr->payload_size == 0) {
2366 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2367 soc_tplg_get_hdr_offset(tplg));
2368 return -EINVAL;
2369 }
2370
2371 if (tplg->pass == hdr->type)
2372 dev_dbg(tplg->dev,
2373 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2374 hdr->payload_size, hdr->type, hdr->version,
2375 hdr->vendor_type, tplg->pass);
2376
2377 return 1;
2378}
2379
2380/* check header type and call appropriate handler */
2381static int soc_tplg_load_header(struct soc_tplg *tplg,
2382 struct snd_soc_tplg_hdr *hdr)
2383{
2384 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2385
2386 /* check for matching ID */
2387 if (hdr->index != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002388 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002389 return 0;
2390
2391 tplg->index = hdr->index;
2392
2393 switch (hdr->type) {
2394 case SND_SOC_TPLG_TYPE_MIXER:
2395 case SND_SOC_TPLG_TYPE_ENUM:
2396 case SND_SOC_TPLG_TYPE_BYTES:
2397 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2398 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2399 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2400 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2401 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2402 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002403 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002404 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002405 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002406 case SND_SOC_TPLG_TYPE_DAI_LINK:
2407 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2408 /* physical link configurations */
2409 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002410 case SND_SOC_TPLG_TYPE_MANIFEST:
2411 return soc_tplg_manifest_load(tplg, hdr);
2412 default:
2413 /* bespoke vendor data object */
2414 return soc_tplg_vendor_load(tplg, hdr);
2415 }
2416
2417 return 0;
2418}
2419
2420/* process the topology file headers */
2421static int soc_tplg_process_headers(struct soc_tplg *tplg)
2422{
2423 struct snd_soc_tplg_hdr *hdr;
2424 int ret;
2425
2426 tplg->pass = SOC_TPLG_PASS_START;
2427
2428 /* process the header types from start to end */
2429 while (tplg->pass <= SOC_TPLG_PASS_END) {
2430
2431 tplg->hdr_pos = tplg->fw->data;
2432 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2433
2434 while (!soc_tplg_is_eof(tplg)) {
2435
2436 /* make sure header is valid before loading */
2437 ret = soc_valid_header(tplg, hdr);
2438 if (ret < 0)
2439 return ret;
2440 else if (ret == 0)
2441 break;
2442
2443 /* load the header object */
2444 ret = soc_tplg_load_header(tplg, hdr);
2445 if (ret < 0)
2446 return ret;
2447
2448 /* goto next header */
2449 tplg->hdr_pos += hdr->payload_size +
2450 sizeof(struct snd_soc_tplg_hdr);
2451 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2452 }
2453
2454 /* next data type pass */
2455 tplg->pass++;
2456 }
2457
2458 /* signal DAPM we are complete */
2459 ret = soc_tplg_dapm_complete(tplg);
2460 if (ret < 0)
2461 dev_err(tplg->dev,
2462 "ASoC: failed to initialise DAPM from Firmware\n");
2463
2464 return ret;
2465}
2466
2467static int soc_tplg_load(struct soc_tplg *tplg)
2468{
2469 int ret;
2470
2471 ret = soc_tplg_process_headers(tplg);
2472 if (ret == 0)
2473 soc_tplg_complete(tplg);
2474
2475 return ret;
2476}
2477
2478/* load audio component topology from "firmware" file */
2479int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2480 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2481{
2482 struct soc_tplg tplg;
2483
2484 /* setup parsing context */
2485 memset(&tplg, 0, sizeof(tplg));
2486 tplg.fw = fw;
2487 tplg.dev = comp->dev;
2488 tplg.comp = comp;
2489 tplg.ops = ops;
2490 tplg.req_index = id;
2491 tplg.io_ops = ops->io_ops;
2492 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002493 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2494 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002495
2496 return soc_tplg_load(&tplg);
2497}
2498EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2499
2500/* remove this dynamic widget */
2501void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2502{
2503 /* make sure we are a widget */
2504 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2505 return;
2506
2507 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2510
2511/* remove all dynamic widgets from this DAPM context */
2512void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2513 u32 index)
2514{
2515 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002516
2517 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2518
2519 /* make sure we are a widget with correct context */
2520 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2521 continue;
2522
2523 /* match ID */
2524 if (w->dobj.index != index &&
2525 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2526 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002527 /* check and free and dynamic widget kcontrols */
2528 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002529 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002530 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002531 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002532}
2533EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2534
2535/* remove dynamic controls from the component driver */
2536int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2537{
2538 struct snd_soc_dobj *dobj, *next_dobj;
2539 int pass = SOC_TPLG_PASS_END;
2540
2541 /* process the header types from end to start */
2542 while (pass >= SOC_TPLG_PASS_START) {
2543
2544 /* remove mixer controls */
2545 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2546 list) {
2547
2548 /* match index */
2549 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002550 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002551 continue;
2552
2553 switch (dobj->type) {
2554 case SND_SOC_DOBJ_MIXER:
2555 remove_mixer(comp, dobj, pass);
2556 break;
2557 case SND_SOC_DOBJ_ENUM:
2558 remove_enum(comp, dobj, pass);
2559 break;
2560 case SND_SOC_DOBJ_BYTES:
2561 remove_bytes(comp, dobj, pass);
2562 break;
2563 case SND_SOC_DOBJ_WIDGET:
2564 remove_widget(comp, dobj, pass);
2565 break;
2566 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002567 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002568 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002569 case SND_SOC_DOBJ_DAI_LINK:
2570 remove_link(comp, dobj, pass);
2571 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002572 default:
2573 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2574 dobj->type);
2575 break;
2576 }
2577 }
2578 pass--;
2579 }
2580
2581 /* let caller know if FW can be freed when no objects are left */
2582 return !list_empty(&comp->dobj_list);
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);