blob: 05d177d689e264e9d4e62a5f7076743ab87e642f [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)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100262 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, 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)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100294 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
295 tplg_w);
Liam Girdwood8a978232015-05-29 19:06:14 +0100296
297 return 0;
298}
299
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100300/* optionally pass new dynamic widget to component driver. This is mainly for
301 * external widgets where we can assign private data/ops */
302static int soc_tplg_widget_ready(struct soc_tplg *tplg,
303 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
304{
305 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100306 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
307 tplg_w);
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100308
309 return 0;
310}
311
Masahiro Yamada183b8022017-02-27 14:29:20 -0800312/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800313static int soc_tplg_dai_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100314 struct snd_soc_dai_driver *dai_drv,
315 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
Liam Girdwood8a978232015-05-29 19:06:14 +0100316{
Mengdong Lin64527e82016-01-15 16:13:28 +0800317 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100318 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
319 pcm, dai);
Liam Girdwood8a978232015-05-29 19:06:14 +0100320
321 return 0;
322}
323
Masahiro Yamada183b8022017-02-27 14:29:20 -0800324/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800325static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100326 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
Mengdong Linacfc7d42016-01-15 16:13:37 +0800327{
328 if (tplg->comp && tplg->ops && tplg->ops->link_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100329 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
Mengdong Linacfc7d42016-01-15 16:13:37 +0800330
331 return 0;
332}
333
Liam Girdwood8a978232015-05-29 19:06:14 +0100334/* tell the component driver that all firmware has been loaded in this request */
335static void soc_tplg_complete(struct soc_tplg *tplg)
336{
337 if (tplg->comp && tplg->ops && tplg->ops->complete)
338 tplg->ops->complete(tplg->comp);
339}
340
341/* add a dynamic kcontrol */
342static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
343 const struct snd_kcontrol_new *control_new, const char *prefix,
344 void *data, struct snd_kcontrol **kcontrol)
345{
346 int err;
347
348 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
349 if (*kcontrol == NULL) {
350 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
351 control_new->name);
352 return -ENOMEM;
353 }
354
355 err = snd_ctl_add(card, *kcontrol);
356 if (err < 0) {
357 dev_err(dev, "ASoC: Failed to add %s: %d\n",
358 control_new->name, err);
359 return err;
360 }
361
362 return 0;
363}
364
365/* add a dynamic kcontrol for component driver */
366static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
367 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
368{
369 struct snd_soc_component *comp = tplg->comp;
370
371 return soc_tplg_add_dcontrol(comp->card->snd_card,
372 comp->dev, k, NULL, comp, kcontrol);
373}
374
375/* remove a mixer kcontrol */
376static void remove_mixer(struct snd_soc_component *comp,
377 struct snd_soc_dobj *dobj, int pass)
378{
379 struct snd_card *card = comp->card->snd_card;
380 struct soc_mixer_control *sm =
381 container_of(dobj, struct soc_mixer_control, dobj);
382 const unsigned int *p = NULL;
383
384 if (pass != SOC_TPLG_PASS_MIXER)
385 return;
386
387 if (dobj->ops && dobj->ops->control_unload)
388 dobj->ops->control_unload(comp, dobj);
389
390 if (sm->dobj.control.kcontrol->tlv.p)
391 p = sm->dobj.control.kcontrol->tlv.p;
392 snd_ctl_remove(card, sm->dobj.control.kcontrol);
393 list_del(&sm->dobj.list);
394 kfree(sm);
395 kfree(p);
396}
397
398/* remove an enum kcontrol */
399static void remove_enum(struct snd_soc_component *comp,
400 struct snd_soc_dobj *dobj, int pass)
401{
402 struct snd_card *card = comp->card->snd_card;
403 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
404 int i;
405
406 if (pass != SOC_TPLG_PASS_MIXER)
407 return;
408
409 if (dobj->ops && dobj->ops->control_unload)
410 dobj->ops->control_unload(comp, dobj);
411
412 snd_ctl_remove(card, se->dobj.control.kcontrol);
413 list_del(&se->dobj.list);
414
415 kfree(se->dobj.control.dvalues);
416 for (i = 0; i < se->items; i++)
417 kfree(se->dobj.control.dtexts[i]);
418 kfree(se);
419}
420
421/* remove a byte kcontrol */
422static void remove_bytes(struct snd_soc_component *comp,
423 struct snd_soc_dobj *dobj, int pass)
424{
425 struct snd_card *card = comp->card->snd_card;
426 struct soc_bytes_ext *sb =
427 container_of(dobj, struct soc_bytes_ext, dobj);
428
429 if (pass != SOC_TPLG_PASS_MIXER)
430 return;
431
432 if (dobj->ops && dobj->ops->control_unload)
433 dobj->ops->control_unload(comp, dobj);
434
435 snd_ctl_remove(card, sb->dobj.control.kcontrol);
436 list_del(&sb->dobj.list);
437 kfree(sb);
438}
439
440/* remove a widget and it's kcontrols - routes must be removed first */
441static void remove_widget(struct snd_soc_component *comp,
442 struct snd_soc_dobj *dobj, int pass)
443{
444 struct snd_card *card = comp->card->snd_card;
445 struct snd_soc_dapm_widget *w =
446 container_of(dobj, struct snd_soc_dapm_widget, dobj);
447 int i;
448
449 if (pass != SOC_TPLG_PASS_WIDGET)
450 return;
451
452 if (dobj->ops && dobj->ops->widget_unload)
453 dobj->ops->widget_unload(comp, dobj);
454
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000455 if (!w->kcontrols)
456 goto free_news;
457
Liam Girdwood8a978232015-05-29 19:06:14 +0100458 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800459 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100460 * The enum may either have an array of values or strings.
461 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800462 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100463 /* enumerated widget mixer */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100464 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800465 struct snd_kcontrol *kcontrol = w->kcontrols[i];
466 struct soc_enum *se =
467 (struct soc_enum *)kcontrol->private_value;
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100468 int j;
Liam Girdwood8a978232015-05-29 19:06:14 +0100469
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800470 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100471
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800472 kfree(se->dobj.control.dvalues);
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100473 for (j = 0; j < se->items; j++)
474 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +0100475
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800476 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100477 kfree(w->kcontrol_news[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800478 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100479 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800480 /* volume mixer or bytes controls */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100481 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100482 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100483
Mengdong Lineea3dd42016-11-25 16:09:17 +0800484 if (dobj->widget.kcontrol_type
485 == SND_SOC_TPLG_TYPE_MIXER)
486 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100487
Mengdong Lineea3dd42016-11-25 16:09:17 +0800488 /* Private value is used as struct soc_mixer_control
489 * for volume mixers or soc_bytes_ext for bytes
490 * controls.
491 */
492 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000493 snd_ctl_remove(card, kcontrol);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100494 kfree(w->kcontrol_news[i].name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100495 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100496 }
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000497
498free_news:
499 kfree(w->kcontrol_news);
500
Liam Girdwood8a978232015-05-29 19:06:14 +0100501 /* widget w is freed by soc-dapm.c */
502}
503
Mengdong Lin64527e82016-01-15 16:13:28 +0800504/* remove DAI configurations */
505static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100506 struct snd_soc_dobj *dobj, int pass)
507{
Mengdong Lin64527e82016-01-15 16:13:28 +0800508 struct snd_soc_dai_driver *dai_drv =
509 container_of(dobj, struct snd_soc_dai_driver, dobj);
510
Liam Girdwood8a978232015-05-29 19:06:14 +0100511 if (pass != SOC_TPLG_PASS_PCM_DAI)
512 return;
513
Mengdong Lin64527e82016-01-15 16:13:28 +0800514 if (dobj->ops && dobj->ops->dai_unload)
515 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100516
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800517 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100518 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800519 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100520}
521
Mengdong Linacfc7d42016-01-15 16:13:37 +0800522/* remove link configurations */
523static void remove_link(struct snd_soc_component *comp,
524 struct snd_soc_dobj *dobj, int pass)
525{
526 struct snd_soc_dai_link *link =
527 container_of(dobj, struct snd_soc_dai_link, dobj);
528
529 if (pass != SOC_TPLG_PASS_PCM_DAI)
530 return;
531
532 if (dobj->ops && dobj->ops->link_unload)
533 dobj->ops->link_unload(comp, dobj);
534
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800535 kfree(link->name);
536 kfree(link->stream_name);
537 kfree(link->cpu_dai_name);
538
Mengdong Linacfc7d42016-01-15 16:13:37 +0800539 list_del(&dobj->list);
540 snd_soc_remove_dai_link(comp->card, link);
541 kfree(link);
542}
543
Liam Girdwood8a978232015-05-29 19:06:14 +0100544/* bind a kcontrol to it's IO handlers */
545static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
546 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800547 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100548{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800549 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800550 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800551 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100552
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800553 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
554 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
555 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
556 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
557 struct soc_bytes_ext *sbe;
558 struct snd_soc_tplg_bytes_control *be;
559
560 sbe = (struct soc_bytes_ext *)k->private_value;
561 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
562
563 /* TLV bytes controls need standard kcontrol info handler,
564 * TLV callback and extended put/get handlers.
565 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530566 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800567 k->tlv.c = snd_soc_bytes_tlv_callback;
568
569 ext_ops = tplg->bytes_ext_ops;
570 num_ops = tplg->bytes_ext_ops_count;
571 for (i = 0; i < num_ops; i++) {
572 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
573 sbe->put = ext_ops[i].put;
574 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
575 sbe->get = ext_ops[i].get;
576 }
577
578 if (sbe->put && sbe->get)
579 return 0;
580 else
581 return -EINVAL;
582 }
583
Mengdong Lin88a17d82015-08-18 18:11:51 +0800584 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800585 ops = tplg->io_ops;
586 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100587 for (i = 0; i < num_ops; i++) {
588
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800589 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100590 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800591 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100592 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800593 if (k->info == NULL && ops[i].id == hdr->ops.info)
594 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100595 }
596
Mengdong Lin88a17d82015-08-18 18:11:51 +0800597 /* vendor specific handlers found ? */
598 if (k->put && k->get && k->info)
599 return 0;
600
601 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800602 ops = io_ops;
603 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800604 for (i = 0; i < num_ops; i++) {
605
606 if (k->put == NULL && ops[i].id == hdr->ops.put)
607 k->put = ops[i].put;
608 if (k->get == NULL && ops[i].id == hdr->ops.get)
609 k->get = ops[i].get;
610 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100611 k->info = ops[i].info;
612 }
613
614 /* standard handlers found ? */
615 if (k->put && k->get && k->info)
616 return 0;
617
Liam Girdwood8a978232015-05-29 19:06:14 +0100618 /* nothing to bind */
619 return -EINVAL;
620}
621
622/* bind a widgets to it's evnt handlers */
623int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
624 const struct snd_soc_tplg_widget_events *events,
625 int num_events, u16 event_type)
626{
627 int i;
628
629 w->event = NULL;
630
631 for (i = 0; i < num_events; i++) {
632 if (event_type == events[i].type) {
633
634 /* found - so assign event */
635 w->event = events[i].event_handler;
636 return 0;
637 }
638 }
639
640 /* not found */
641 return -EINVAL;
642}
643EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
644
645/* optionally pass new dynamic kcontrol to component driver. */
646static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
647 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
648{
649 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100650 return tplg->ops->control_load(tplg->comp, tplg->index, k,
651 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100652
653 return 0;
654}
655
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100656
657static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
658 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100659{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100660 unsigned int item_len = 2 * sizeof(unsigned int);
661 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100662
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100663 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
664 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100665 return -ENOMEM;
666
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100667 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
668 p[1] = item_len;
669 p[2] = scale->min;
670 p[3] = (scale->step & TLV_DB_SCALE_MASK)
671 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100672
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100673 kc->tlv.p = (void *)p;
674 return 0;
675}
676
677static int soc_tplg_create_tlv(struct soc_tplg *tplg,
678 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
679{
680 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
681
682 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
683 return 0;
684
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800685 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100686 tplg_tlv = &tc->tlv;
687 switch (tplg_tlv->type) {
688 case SNDRV_CTL_TLVT_DB_SCALE:
689 return soc_tplg_create_tlv_db_scale(tplg, kc,
690 &tplg_tlv->scale);
691
692 /* TODO: add support for other TLV types */
693 default:
694 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
695 tplg_tlv->type);
696 return -EINVAL;
697 }
698 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100699
700 return 0;
701}
702
703static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
704 struct snd_kcontrol_new *kc)
705{
706 kfree(kc->tlv.p);
707}
708
709static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
710 size_t size)
711{
712 struct snd_soc_tplg_bytes_control *be;
713 struct soc_bytes_ext *sbe;
714 struct snd_kcontrol_new kc;
715 int i, err;
716
717 if (soc_tplg_check_elem_count(tplg,
718 sizeof(struct snd_soc_tplg_bytes_control), count,
719 size, "mixer bytes")) {
720 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
721 count);
722 return -EINVAL;
723 }
724
725 for (i = 0; i < count; i++) {
726 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
727
728 /* validate kcontrol */
729 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
730 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
731 return -EINVAL;
732
733 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
734 if (sbe == NULL)
735 return -ENOMEM;
736
737 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
738 be->priv.size);
739
740 dev_dbg(tplg->dev,
741 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
742 be->hdr.name, be->hdr.access);
743
744 memset(&kc, 0, sizeof(kc));
745 kc.name = be->hdr.name;
746 kc.private_value = (long)sbe;
747 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
748 kc.access = be->hdr.access;
749
750 sbe->max = be->max;
751 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
752 sbe->dobj.ops = tplg->ops;
753 INIT_LIST_HEAD(&sbe->dobj.list);
754
755 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800756 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100757 if (err) {
758 soc_control_err(tplg, &be->hdr, be->hdr.name);
759 kfree(sbe);
760 continue;
761 }
762
763 /* pass control to driver for optional further init */
764 err = soc_tplg_init_kcontrol(tplg, &kc,
765 (struct snd_soc_tplg_ctl_hdr *)be);
766 if (err < 0) {
767 dev_err(tplg->dev, "ASoC: failed to init %s\n",
768 be->hdr.name);
769 kfree(sbe);
770 continue;
771 }
772
773 /* register control here */
774 err = soc_tplg_add_kcontrol(tplg, &kc,
775 &sbe->dobj.control.kcontrol);
776 if (err < 0) {
777 dev_err(tplg->dev, "ASoC: failed to add %s\n",
778 be->hdr.name);
779 kfree(sbe);
780 continue;
781 }
782
783 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
784 }
785 return 0;
786
787}
788
789static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
790 size_t size)
791{
792 struct snd_soc_tplg_mixer_control *mc;
793 struct soc_mixer_control *sm;
794 struct snd_kcontrol_new kc;
795 int i, err;
796
797 if (soc_tplg_check_elem_count(tplg,
798 sizeof(struct snd_soc_tplg_mixer_control),
799 count, size, "mixers")) {
800
801 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
802 count);
803 return -EINVAL;
804 }
805
806 for (i = 0; i < count; i++) {
807 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
808
809 /* validate kcontrol */
810 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
811 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
812 return -EINVAL;
813
814 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
815 if (sm == NULL)
816 return -ENOMEM;
817 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
818 mc->priv.size);
819
820 dev_dbg(tplg->dev,
821 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
822 mc->hdr.name, mc->hdr.access);
823
824 memset(&kc, 0, sizeof(kc));
825 kc.name = mc->hdr.name;
826 kc.private_value = (long)sm;
827 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
828 kc.access = mc->hdr.access;
829
830 /* we only support FL/FR channel mapping atm */
831 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
832 SNDRV_CHMAP_FL);
833 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
834 SNDRV_CHMAP_FR);
835 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
836 SNDRV_CHMAP_FL);
837 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
838 SNDRV_CHMAP_FR);
839
840 sm->max = mc->max;
841 sm->min = mc->min;
842 sm->invert = mc->invert;
843 sm->platform_max = mc->platform_max;
844 sm->dobj.index = tplg->index;
845 sm->dobj.ops = tplg->ops;
846 sm->dobj.type = SND_SOC_DOBJ_MIXER;
847 INIT_LIST_HEAD(&sm->dobj.list);
848
849 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800850 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100851 if (err) {
852 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
853 kfree(sm);
854 continue;
855 }
856
857 /* pass control to driver for optional further init */
858 err = soc_tplg_init_kcontrol(tplg, &kc,
859 (struct snd_soc_tplg_ctl_hdr *) mc);
860 if (err < 0) {
861 dev_err(tplg->dev, "ASoC: failed to init %s\n",
862 mc->hdr.name);
863 kfree(sm);
864 continue;
865 }
866
867 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100868 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100869
870 /* register control here */
871 err = soc_tplg_add_kcontrol(tplg, &kc,
872 &sm->dobj.control.kcontrol);
873 if (err < 0) {
874 dev_err(tplg->dev, "ASoC: failed to add %s\n",
875 mc->hdr.name);
876 soc_tplg_free_tlv(tplg, &kc);
877 kfree(sm);
878 continue;
879 }
880
881 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
882 }
883
884 return 0;
885}
886
887static int soc_tplg_denum_create_texts(struct soc_enum *se,
888 struct snd_soc_tplg_enum_control *ec)
889{
890 int i, ret;
891
892 se->dobj.control.dtexts =
Kees Cook6396bb22018-06-12 14:03:40 -0700893 kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100894 if (se->dobj.control.dtexts == NULL)
895 return -ENOMEM;
896
897 for (i = 0; i < ec->items; i++) {
898
899 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
900 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
901 ret = -EINVAL;
902 goto err;
903 }
904
905 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
906 if (!se->dobj.control.dtexts[i]) {
907 ret = -ENOMEM;
908 goto err;
909 }
910 }
911
Mousumi Janab6e38b22017-04-11 13:06:22 +0530912 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100913 return 0;
914
915err:
916 for (--i; i >= 0; i--)
917 kfree(se->dobj.control.dtexts[i]);
918 kfree(se->dobj.control.dtexts);
919 return ret;
920}
921
922static int soc_tplg_denum_create_values(struct soc_enum *se,
923 struct snd_soc_tplg_enum_control *ec)
924{
925 if (ec->items > sizeof(*ec->values))
926 return -EINVAL;
927
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200928 se->dobj.control.dvalues = kmemdup(ec->values,
929 ec->items * sizeof(u32),
930 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100931 if (!se->dobj.control.dvalues)
932 return -ENOMEM;
933
Liam Girdwood8a978232015-05-29 19:06:14 +0100934 return 0;
935}
936
937static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
938 size_t size)
939{
940 struct snd_soc_tplg_enum_control *ec;
941 struct soc_enum *se;
942 struct snd_kcontrol_new kc;
943 int i, ret, err;
944
945 if (soc_tplg_check_elem_count(tplg,
946 sizeof(struct snd_soc_tplg_enum_control),
947 count, size, "enums")) {
948
949 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
950 count);
951 return -EINVAL;
952 }
953
954 for (i = 0; i < count; i++) {
955 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
956 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
957 ec->priv.size);
958
959 /* validate kcontrol */
960 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
961 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
962 return -EINVAL;
963
964 se = kzalloc((sizeof(*se)), GFP_KERNEL);
965 if (se == NULL)
966 return -ENOMEM;
967
968 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
969 ec->hdr.name, ec->items);
970
971 memset(&kc, 0, sizeof(kc));
972 kc.name = ec->hdr.name;
973 kc.private_value = (long)se;
974 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
975 kc.access = ec->hdr.access;
976
977 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
978 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
979 SNDRV_CHMAP_FL);
980 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
981 SNDRV_CHMAP_FL);
982
983 se->items = ec->items;
984 se->mask = ec->mask;
985 se->dobj.index = tplg->index;
986 se->dobj.type = SND_SOC_DOBJ_ENUM;
987 se->dobj.ops = tplg->ops;
988 INIT_LIST_HEAD(&se->dobj.list);
989
990 switch (ec->hdr.ops.info) {
991 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
992 case SND_SOC_TPLG_CTL_ENUM_VALUE:
993 err = soc_tplg_denum_create_values(se, ec);
994 if (err < 0) {
995 dev_err(tplg->dev,
996 "ASoC: could not create values for %s\n",
997 ec->hdr.name);
998 kfree(se);
999 continue;
1000 }
1001 /* fall through and create texts */
1002 case SND_SOC_TPLG_CTL_ENUM:
1003 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1004 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1005 err = soc_tplg_denum_create_texts(se, ec);
1006 if (err < 0) {
1007 dev_err(tplg->dev,
1008 "ASoC: could not create texts for %s\n",
1009 ec->hdr.name);
1010 kfree(se);
1011 continue;
1012 }
1013 break;
1014 default:
1015 dev_err(tplg->dev,
1016 "ASoC: invalid enum control type %d for %s\n",
1017 ec->hdr.ops.info, ec->hdr.name);
1018 kfree(se);
1019 continue;
1020 }
1021
1022 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001023 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001024 if (err) {
1025 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1026 kfree(se);
1027 continue;
1028 }
1029
1030 /* pass control to driver for optional further init */
1031 err = soc_tplg_init_kcontrol(tplg, &kc,
1032 (struct snd_soc_tplg_ctl_hdr *) ec);
1033 if (err < 0) {
1034 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1035 ec->hdr.name);
1036 kfree(se);
1037 continue;
1038 }
1039
1040 /* register control here */
1041 ret = soc_tplg_add_kcontrol(tplg,
1042 &kc, &se->dobj.control.kcontrol);
1043 if (ret < 0) {
1044 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1045 ec->hdr.name);
1046 kfree(se);
1047 continue;
1048 }
1049
1050 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1051 }
1052
1053 return 0;
1054}
1055
1056static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1057 struct snd_soc_tplg_hdr *hdr)
1058{
1059 struct snd_soc_tplg_ctl_hdr *control_hdr;
1060 int i;
1061
1062 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1063 tplg->pos += hdr->size + hdr->payload_size;
1064 return 0;
1065 }
1066
1067 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1068 soc_tplg_get_offset(tplg));
1069
1070 for (i = 0; i < hdr->count; i++) {
1071
1072 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1073
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001074 if (control_hdr->size != sizeof(*control_hdr)) {
1075 dev_err(tplg->dev, "ASoC: invalid control size\n");
1076 return -EINVAL;
1077 }
1078
Liam Girdwood8a978232015-05-29 19:06:14 +01001079 switch (control_hdr->ops.info) {
1080 case SND_SOC_TPLG_CTL_VOLSW:
1081 case SND_SOC_TPLG_CTL_STROBE:
1082 case SND_SOC_TPLG_CTL_VOLSW_SX:
1083 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1084 case SND_SOC_TPLG_CTL_RANGE:
1085 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1086 case SND_SOC_TPLG_DAPM_CTL_PIN:
1087 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1088 break;
1089 case SND_SOC_TPLG_CTL_ENUM:
1090 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1091 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1092 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1093 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1094 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1095 break;
1096 case SND_SOC_TPLG_CTL_BYTES:
1097 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1098 break;
1099 default:
1100 soc_bind_err(tplg, control_hdr, i);
1101 return -EINVAL;
1102 }
1103 }
1104
1105 return 0;
1106}
1107
Liam Girdwood503e79b2018-06-14 20:53:59 +01001108/* optionally pass new dynamic kcontrol to component driver. */
1109static int soc_tplg_add_route(struct soc_tplg *tplg,
1110 struct snd_soc_dapm_route *route)
1111{
1112 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1113 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1114 route);
1115
1116 return 0;
1117}
1118
Liam Girdwood8a978232015-05-29 19:06:14 +01001119static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1120 struct snd_soc_tplg_hdr *hdr)
1121{
1122 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1123 struct snd_soc_dapm_route route;
1124 struct snd_soc_tplg_dapm_graph_elem *elem;
1125 int count = hdr->count, i;
1126
1127 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1128 tplg->pos += hdr->size + hdr->payload_size;
1129 return 0;
1130 }
1131
1132 if (soc_tplg_check_elem_count(tplg,
1133 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1134 count, hdr->payload_size, "graph")) {
1135
1136 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1137 count);
1138 return -EINVAL;
1139 }
1140
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001141 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1142 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001143
1144 for (i = 0; i < count; i++) {
1145 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1146 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1147
1148 /* validate routes */
1149 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1150 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1151 return -EINVAL;
1152 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1153 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1154 return -EINVAL;
1155 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1156 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1157 return -EINVAL;
1158
1159 route.source = elem->source;
1160 route.sink = elem->sink;
1161 route.connected = NULL; /* set to NULL atm for tplg users */
1162 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1163 route.control = NULL;
1164 else
1165 route.control = elem->control;
1166
Liam Girdwood503e79b2018-06-14 20:53:59 +01001167 soc_tplg_add_route(tplg, &route);
1168
Liam Girdwood8a978232015-05-29 19:06:14 +01001169 /* add route, but keep going if some fail */
1170 snd_soc_dapm_add_routes(dapm, &route, 1);
1171 }
1172
1173 return 0;
1174}
1175
1176static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1177 struct soc_tplg *tplg, int num_kcontrols)
1178{
1179 struct snd_kcontrol_new *kc;
1180 struct soc_mixer_control *sm;
1181 struct snd_soc_tplg_mixer_control *mc;
1182 int i, err;
1183
Axel Lin4ca7deb2015-08-05 22:34:22 +08001184 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001185 if (kc == NULL)
1186 return NULL;
1187
1188 for (i = 0; i < num_kcontrols; i++) {
1189 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1190 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1191 if (sm == NULL)
1192 goto err;
1193
1194 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1195 mc->priv.size);
1196
1197 /* validate kcontrol */
1198 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1199 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1200 goto err_str;
1201
1202 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1203 mc->hdr.name, i);
1204
Liam Girdwood267e2c62018-03-27 12:04:04 +01001205 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1206 if (kc[i].name == NULL)
1207 goto err_str;
Liam Girdwood8a978232015-05-29 19:06:14 +01001208 kc[i].private_value = (long)sm;
1209 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1210 kc[i].access = mc->hdr.access;
1211
1212 /* we only support FL/FR channel mapping atm */
1213 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1214 SNDRV_CHMAP_FL);
1215 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1216 SNDRV_CHMAP_FR);
1217 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1218 SNDRV_CHMAP_FL);
1219 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1220 SNDRV_CHMAP_FR);
1221
1222 sm->max = mc->max;
1223 sm->min = mc->min;
1224 sm->invert = mc->invert;
1225 sm->platform_max = mc->platform_max;
1226 sm->dobj.index = tplg->index;
1227 INIT_LIST_HEAD(&sm->dobj.list);
1228
1229 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001230 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001231 if (err) {
1232 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1233 kfree(sm);
1234 continue;
1235 }
1236
1237 /* pass control to driver for optional further init */
1238 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1239 (struct snd_soc_tplg_ctl_hdr *)mc);
1240 if (err < 0) {
1241 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1242 mc->hdr.name);
1243 kfree(sm);
1244 continue;
1245 }
Ranjani Sridharanbde8b382018-03-09 11:11:17 -08001246
1247 /* create any TLV data */
1248 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01001249 }
1250 return kc;
1251
1252err_str:
1253 kfree(sm);
1254err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001255 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001256 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001257 kfree(kc[i].name);
1258 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001259 kfree(kc);
1260 return NULL;
1261}
1262
1263static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001264 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001265{
1266 struct snd_kcontrol_new *kc;
1267 struct snd_soc_tplg_enum_control *ec;
1268 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001269 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001270
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001271 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001272 if (kc == NULL)
1273 return NULL;
1274
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001275 for (i = 0; i < num_kcontrols; i++) {
1276 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1277 /* validate kcontrol */
1278 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1279 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Christophe JAILLETf3ee9092017-09-14 22:44:24 +02001280 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001281
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001282 se = kzalloc(sizeof(*se), GFP_KERNEL);
1283 if (se == NULL)
1284 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001285
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001286 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001287 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001288
Liam Girdwood267e2c62018-03-27 12:04:04 +01001289 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001290 if (kc[i].name == NULL) {
1291 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001292 goto err_se;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001293 }
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001294 kc[i].private_value = (long)se;
1295 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1296 kc[i].access = ec->hdr.access;
1297
1298 /* we only support FL/FR channel mapping atm */
1299 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1300 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1301 SNDRV_CHMAP_FL);
1302 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1303 SNDRV_CHMAP_FR);
1304
1305 se->items = ec->items;
1306 se->mask = ec->mask;
1307 se->dobj.index = tplg->index;
1308
1309 switch (ec->hdr.ops.info) {
1310 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1311 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1312 err = soc_tplg_denum_create_values(se, ec);
1313 if (err < 0) {
1314 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1315 ec->hdr.name);
1316 goto err_se;
1317 }
1318 /* fall through to create texts */
1319 case SND_SOC_TPLG_CTL_ENUM:
1320 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1321 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1322 err = soc_tplg_denum_create_texts(se, ec);
1323 if (err < 0) {
1324 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1325 ec->hdr.name);
1326 goto err_se;
1327 }
1328 break;
1329 default:
1330 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1331 ec->hdr.ops.info, ec->hdr.name);
1332 goto err_se;
1333 }
1334
1335 /* map io handlers */
1336 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1337 if (err) {
1338 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1339 goto err_se;
1340 }
1341
1342 /* pass control to driver for optional further init */
1343 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1344 (struct snd_soc_tplg_ctl_hdr *)ec);
1345 if (err < 0) {
1346 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1347 ec->hdr.name);
1348 goto err_se;
1349 }
1350
1351 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1352 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001353 }
1354
1355 return kc;
1356
1357err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001358 for (; i >= 0; i--) {
1359 /* free values and texts */
1360 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001361 if (!se)
1362 continue;
1363
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001364 kfree(se->dobj.control.dvalues);
1365 for (j = 0; j < ec->items; j++)
1366 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001367
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001368 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001369 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001370 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001371err:
1372 kfree(kc);
1373
1374 return NULL;
1375}
1376
1377static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1378 struct soc_tplg *tplg, int count)
1379{
1380 struct snd_soc_tplg_bytes_control *be;
1381 struct soc_bytes_ext *sbe;
1382 struct snd_kcontrol_new *kc;
1383 int i, err;
1384
Axel Lin4ca7deb2015-08-05 22:34:22 +08001385 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001386 if (!kc)
1387 return NULL;
1388
1389 for (i = 0; i < count; i++) {
1390 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1391
1392 /* validate kcontrol */
1393 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1394 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1395 goto err;
1396
1397 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1398 if (sbe == NULL)
1399 goto err;
1400
1401 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1402 be->priv.size);
1403
1404 dev_dbg(tplg->dev,
1405 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1406 be->hdr.name, be->hdr.access);
1407
Liam Girdwood267e2c62018-03-27 12:04:04 +01001408 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001409 if (kc[i].name == NULL) {
1410 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001411 goto err;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001412 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001413 kc[i].private_value = (long)sbe;
1414 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1415 kc[i].access = be->hdr.access;
1416
1417 sbe->max = be->max;
1418 INIT_LIST_HEAD(&sbe->dobj.list);
1419
1420 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001421 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001422 if (err) {
1423 soc_control_err(tplg, &be->hdr, be->hdr.name);
1424 kfree(sbe);
1425 continue;
1426 }
1427
1428 /* pass control to driver for optional further init */
1429 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1430 (struct snd_soc_tplg_ctl_hdr *)be);
1431 if (err < 0) {
1432 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1433 be->hdr.name);
1434 kfree(sbe);
1435 continue;
1436 }
1437 }
1438
1439 return kc;
1440
1441err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001442 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001443 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001444 kfree(kc[i].name);
1445 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001446
1447 kfree(kc);
1448 return NULL;
1449}
1450
1451static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1452 struct snd_soc_tplg_dapm_widget *w)
1453{
1454 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1455 struct snd_soc_dapm_widget template, *widget;
1456 struct snd_soc_tplg_ctl_hdr *control_hdr;
1457 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001458 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001459 int ret = 0;
1460
1461 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1462 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1463 return -EINVAL;
1464 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1465 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1466 return -EINVAL;
1467
1468 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1469 w->name, w->id);
1470
1471 memset(&template, 0, sizeof(template));
1472
1473 /* map user to kernel widget ID */
1474 template.id = get_widget_id(w->id);
1475 if (template.id < 0)
1476 return template.id;
1477
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001478 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001479 template.name = kstrdup(w->name, GFP_KERNEL);
1480 if (!template.name)
1481 return -ENOMEM;
1482 template.sname = kstrdup(w->sname, GFP_KERNEL);
1483 if (!template.sname) {
1484 ret = -ENOMEM;
1485 goto err;
1486 }
1487 template.reg = w->reg;
1488 template.shift = w->shift;
1489 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001490 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001491 template.on_val = w->invert ? 0 : 1;
1492 template.off_val = w->invert ? 1 : 0;
1493 template.ignore_suspend = w->ignore_suspend;
1494 template.event_flags = w->event_flags;
1495 template.dobj.index = tplg->index;
1496
1497 tplg->pos +=
1498 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1499 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001500 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001501 template.num_kcontrols = 0;
1502 goto widget;
1503 }
1504
1505 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1506 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1507 w->name, w->num_kcontrols, control_hdr->type);
1508
1509 switch (control_hdr->ops.info) {
1510 case SND_SOC_TPLG_CTL_VOLSW:
1511 case SND_SOC_TPLG_CTL_STROBE:
1512 case SND_SOC_TPLG_CTL_VOLSW_SX:
1513 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1514 case SND_SOC_TPLG_CTL_RANGE:
1515 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001516 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Liam Girdwood8a978232015-05-29 19:06:14 +01001517 template.num_kcontrols = w->num_kcontrols;
1518 template.kcontrol_news =
1519 soc_tplg_dapm_widget_dmixer_create(tplg,
1520 template.num_kcontrols);
1521 if (!template.kcontrol_news) {
1522 ret = -ENOMEM;
1523 goto hdr_err;
1524 }
1525 break;
1526 case SND_SOC_TPLG_CTL_ENUM:
1527 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1528 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1529 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1530 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001531 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001532 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001533 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001534 soc_tplg_dapm_widget_denum_create(tplg,
1535 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001536 if (!template.kcontrol_news) {
1537 ret = -ENOMEM;
1538 goto hdr_err;
1539 }
1540 break;
1541 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001542 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Liam Girdwood8a978232015-05-29 19:06:14 +01001543 template.num_kcontrols = w->num_kcontrols;
1544 template.kcontrol_news =
1545 soc_tplg_dapm_widget_dbytes_create(tplg,
1546 template.num_kcontrols);
1547 if (!template.kcontrol_news) {
1548 ret = -ENOMEM;
1549 goto hdr_err;
1550 }
1551 break;
1552 default:
1553 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1554 control_hdr->ops.get, control_hdr->ops.put,
1555 control_hdr->ops.info);
1556 ret = -EINVAL;
1557 goto hdr_err;
1558 }
1559
1560widget:
1561 ret = soc_tplg_widget_load(tplg, &template, w);
1562 if (ret < 0)
1563 goto hdr_err;
1564
1565 /* card dapm mutex is held by the core if we are loading topology
1566 * data during sound card init. */
1567 if (card->instantiated)
1568 widget = snd_soc_dapm_new_control(dapm, &template);
1569 else
1570 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001571 if (IS_ERR(widget)) {
1572 ret = PTR_ERR(widget);
1573 /* Do not nag about probe deferrals */
1574 if (ret != -EPROBE_DEFER)
1575 dev_err(tplg->dev,
1576 "ASoC: failed to create widget %s controls (%d)\n",
1577 w->name, ret);
1578 goto hdr_err;
1579 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001580 if (widget == NULL) {
1581 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1582 w->name);
Wei Yongjun8ae3ea42016-08-10 13:43:12 +00001583 ret = -ENOMEM;
Liam Girdwood8a978232015-05-29 19:06:14 +01001584 goto hdr_err;
1585 }
1586
1587 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001588 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001589 widget->dobj.ops = tplg->ops;
1590 widget->dobj.index = tplg->index;
1591 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001592
1593 ret = soc_tplg_widget_ready(tplg, widget, w);
1594 if (ret < 0)
1595 goto ready_err;
1596
Liam Girdwood8a978232015-05-29 19:06:14 +01001597 return 0;
1598
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001599ready_err:
1600 snd_soc_tplg_widget_remove(widget);
1601 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001602hdr_err:
1603 kfree(template.sname);
1604err:
1605 kfree(template.name);
1606 return ret;
1607}
1608
1609static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1610 struct snd_soc_tplg_hdr *hdr)
1611{
1612 struct snd_soc_tplg_dapm_widget *widget;
1613 int ret, count = hdr->count, i;
1614
1615 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1616 return 0;
1617
1618 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1619
1620 for (i = 0; i < count; i++) {
1621 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001622 if (widget->size != sizeof(*widget)) {
1623 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1624 return -EINVAL;
1625 }
1626
Liam Girdwood8a978232015-05-29 19:06:14 +01001627 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001628 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001629 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1630 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001631 return ret;
1632 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001633 }
1634
1635 return 0;
1636}
1637
1638static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1639{
1640 struct snd_soc_card *card = tplg->comp->card;
1641 int ret;
1642
1643 /* Card might not have been registered at this point.
1644 * If so, just return success.
1645 */
1646 if (!card || !card->instantiated) {
1647 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001648 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001649 return 0;
1650 }
1651
1652 ret = snd_soc_dapm_new_widgets(card);
1653 if (ret < 0)
1654 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1655 ret);
1656
1657 return 0;
1658}
1659
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001660static void set_stream_info(struct snd_soc_pcm_stream *stream,
1661 struct snd_soc_tplg_stream_caps *caps)
1662{
1663 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1664 stream->channels_min = caps->channels_min;
1665 stream->channels_max = caps->channels_max;
1666 stream->rates = caps->rates;
1667 stream->rate_min = caps->rate_min;
1668 stream->rate_max = caps->rate_max;
1669 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001670 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001671}
1672
Mengdong Lin0038be92016-07-26 14:32:37 +08001673static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1674 unsigned int flag_mask, unsigned int flags)
1675{
1676 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1677 dai_drv->symmetric_rates =
1678 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1679
1680 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1681 dai_drv->symmetric_channels =
1682 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1683 1 : 0;
1684
1685 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1686 dai_drv->symmetric_samplebits =
1687 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1688 1 : 0;
1689}
1690
Mengdong Lin64527e82016-01-15 16:13:28 +08001691static int soc_tplg_dai_create(struct soc_tplg *tplg,
1692 struct snd_soc_tplg_pcm *pcm)
1693{
1694 struct snd_soc_dai_driver *dai_drv;
1695 struct snd_soc_pcm_stream *stream;
1696 struct snd_soc_tplg_stream_caps *caps;
1697 int ret;
1698
1699 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1700 if (dai_drv == NULL)
1701 return -ENOMEM;
1702
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001703 if (strlen(pcm->dai_name))
1704 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001705 dai_drv->id = pcm->dai_id;
1706
1707 if (pcm->playback) {
1708 stream = &dai_drv->playback;
1709 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001710 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001711 }
1712
1713 if (pcm->capture) {
1714 stream = &dai_drv->capture;
1715 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001716 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001717 }
1718
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001719 if (pcm->compress)
1720 dai_drv->compress_new = snd_soc_new_compress;
1721
Mengdong Lin64527e82016-01-15 16:13:28 +08001722 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001723 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001724 if (ret < 0) {
1725 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1726 kfree(dai_drv);
1727 return ret;
1728 }
1729
1730 dai_drv->dobj.index = tplg->index;
1731 dai_drv->dobj.ops = tplg->ops;
1732 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1733 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1734
1735 /* register the DAI to the component */
1736 return snd_soc_register_dai(tplg->comp, dai_drv);
1737}
1738
Mengdong Lin717a8e72016-11-03 01:03:34 +08001739static void set_link_flags(struct snd_soc_dai_link *link,
1740 unsigned int flag_mask, unsigned int flags)
1741{
1742 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1743 link->symmetric_rates =
1744 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1745
1746 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1747 link->symmetric_channels =
1748 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1749 1 : 0;
1750
1751 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1752 link->symmetric_samplebits =
1753 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1754 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001755
1756 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1757 link->ignore_suspend =
1758 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1759 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001760}
1761
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001762/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001763static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001764 struct snd_soc_tplg_pcm *pcm)
1765{
1766 struct snd_soc_dai_link *link;
1767 int ret;
1768
1769 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1770 if (link == NULL)
1771 return -ENOMEM;
1772
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001773 if (strlen(pcm->pcm_name)) {
1774 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1775 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1776 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001777 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001778
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001779 if (strlen(pcm->dai_name))
1780 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1781
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001782 link->codec_name = "snd-soc-dummy";
1783 link->codec_dai_name = "snd-soc-dummy-dai";
1784
1785 /* enable DPCM */
1786 link->dynamic = 1;
1787 link->dpcm_playback = pcm->playback;
1788 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001789 if (pcm->flag_mask)
1790 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001791
Mengdong Linacfc7d42016-01-15 16:13:37 +08001792 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001793 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001794 if (ret < 0) {
1795 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1796 kfree(link);
1797 return ret;
1798 }
1799
1800 link->dobj.index = tplg->index;
1801 link->dobj.ops = tplg->ops;
1802 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1803 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1804
1805 snd_soc_add_dai_link(tplg->comp->card, link);
1806 return 0;
1807}
1808
1809/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001810static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1811 struct snd_soc_tplg_pcm *pcm)
1812{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001813 int ret;
1814
1815 ret = soc_tplg_dai_create(tplg, pcm);
1816 if (ret < 0)
1817 return ret;
1818
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001819 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001820}
1821
Mengdong Lin55726dc2016-11-03 01:00:16 +08001822/* copy stream caps from the old version 4 of source */
1823static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1824 struct snd_soc_tplg_stream_caps_v4 *src)
1825{
1826 dest->size = sizeof(*dest);
1827 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1828 dest->formats = src->formats;
1829 dest->rates = src->rates;
1830 dest->rate_min = src->rate_min;
1831 dest->rate_max = src->rate_max;
1832 dest->channels_min = src->channels_min;
1833 dest->channels_max = src->channels_max;
1834 dest->periods_min = src->periods_min;
1835 dest->periods_max = src->periods_max;
1836 dest->period_size_min = src->period_size_min;
1837 dest->period_size_max = src->period_size_max;
1838 dest->buffer_size_min = src->buffer_size_min;
1839 dest->buffer_size_max = src->buffer_size_max;
1840}
1841
1842/**
1843 * pcm_new_ver - Create the new version of PCM from the old version.
1844 * @tplg: topology context
1845 * @src: older version of pcm as a source
1846 * @pcm: latest version of pcm created from the source
1847 *
1848 * Support from vesion 4. User should free the returned pcm manually.
1849 */
1850static int pcm_new_ver(struct soc_tplg *tplg,
1851 struct snd_soc_tplg_pcm *src,
1852 struct snd_soc_tplg_pcm **pcm)
1853{
1854 struct snd_soc_tplg_pcm *dest;
1855 struct snd_soc_tplg_pcm_v4 *src_v4;
1856 int i;
1857
1858 *pcm = NULL;
1859
1860 if (src->size != sizeof(*src_v4)) {
1861 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1862 return -EINVAL;
1863 }
1864
1865 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1866 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1867 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1868 if (!dest)
1869 return -ENOMEM;
1870
1871 dest->size = sizeof(*dest); /* size of latest abi version */
1872 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1873 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1874 dest->pcm_id = src_v4->pcm_id;
1875 dest->dai_id = src_v4->dai_id;
1876 dest->playback = src_v4->playback;
1877 dest->capture = src_v4->capture;
1878 dest->compress = src_v4->compress;
1879 dest->num_streams = src_v4->num_streams;
1880 for (i = 0; i < dest->num_streams; i++)
1881 memcpy(&dest->stream[i], &src_v4->stream[i],
1882 sizeof(struct snd_soc_tplg_stream));
1883
1884 for (i = 0; i < 2; i++)
1885 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1886
1887 *pcm = dest;
1888 return 0;
1889}
1890
Mengdong Lin64527e82016-01-15 16:13:28 +08001891static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001892 struct snd_soc_tplg_hdr *hdr)
1893{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001894 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001895 int count = hdr->count;
Vinod Koulfd340452016-12-08 23:01:27 +05301896 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001897 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001898
1899 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1900 return 0;
1901
Mengdong Lin55726dc2016-11-03 01:00:16 +08001902 /* check the element size and count */
1903 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1904 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1905 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1906 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1907 pcm->size);
1908 return -EINVAL;
1909 }
1910
Liam Girdwood8a978232015-05-29 19:06:14 +01001911 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08001912 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001913 hdr->payload_size, "PCM DAI")) {
1914 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1915 count);
1916 return -EINVAL;
1917 }
1918
Mengdong Lin64527e82016-01-15 16:13:28 +08001919 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001920 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1921
1922 /* check ABI version by size, create a new version of pcm
1923 * if abi not match.
1924 */
1925 if (pcm->size == sizeof(*pcm)) {
1926 abi_match = true;
1927 _pcm = pcm;
1928 } else {
1929 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05301930 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001931 }
1932
Mengdong Lin55726dc2016-11-03 01:00:16 +08001933 /* create the FE DAIs and DAI links */
1934 soc_tplg_pcm_create(tplg, _pcm);
1935
Mengdong Lin717a8e72016-11-03 01:03:34 +08001936 /* offset by version-specific struct size and
1937 * real priv data size
1938 */
1939 tplg->pos += pcm->size + _pcm->priv.size;
1940
Mengdong Lin55726dc2016-11-03 01:00:16 +08001941 if (!abi_match)
1942 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08001943 }
1944
Liam Girdwood8a978232015-05-29 19:06:14 +01001945 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001946
Liam Girdwood8a978232015-05-29 19:06:14 +01001947 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001948}
1949
Mengdong Lin593d9e52016-11-03 01:04:27 +08001950/**
1951 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00001952 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08001953 * @cfg: physical link configs.
1954 *
1955 * Topology context contains a list of supported HW formats (configs) and
1956 * a default format ID for the physical link. This function will use this
1957 * default ID to choose the HW format to set the link's DAI format for init.
1958 */
1959static void set_link_hw_format(struct snd_soc_dai_link *link,
1960 struct snd_soc_tplg_link_config *cfg)
1961{
1962 struct snd_soc_tplg_hw_config *hw_config;
1963 unsigned char bclk_master, fsync_master;
1964 unsigned char invert_bclk, invert_fsync;
1965 int i;
1966
1967 for (i = 0; i < cfg->num_hw_configs; i++) {
1968 hw_config = &cfg->hw_config[i];
1969 if (hw_config->id != cfg->default_hw_config_id)
1970 continue;
1971
1972 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
1973
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001974 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02001975 switch (hw_config->clock_gated) {
1976 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001977 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02001978 break;
1979
1980 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001981 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02001982 break;
1983
1984 default:
1985 /* ignore the value */
1986 break;
1987 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02001988
Mengdong Lin593d9e52016-11-03 01:04:27 +08001989 /* clock signal polarity */
1990 invert_bclk = hw_config->invert_bclk;
1991 invert_fsync = hw_config->invert_fsync;
1992 if (!invert_bclk && !invert_fsync)
1993 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1994 else if (!invert_bclk && invert_fsync)
1995 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1996 else if (invert_bclk && !invert_fsync)
1997 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1998 else
1999 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2000
2001 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002002 bclk_master = (hw_config->bclk_master ==
2003 SND_SOC_TPLG_BCLK_CM);
2004 fsync_master = (hw_config->fsync_master ==
2005 SND_SOC_TPLG_FSYNC_CM);
2006 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002007 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002008 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002009 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2010 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002011 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2012 else
2013 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2014 }
2015}
2016
2017/**
2018 * link_new_ver - Create a new physical link config from the old
2019 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002020 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002021 * @src: old version of phyical link config as a source
2022 * @link: latest version of physical link config created from the source
2023 *
2024 * Support from vesion 4. User need free the returned link config manually.
2025 */
2026static int link_new_ver(struct soc_tplg *tplg,
2027 struct snd_soc_tplg_link_config *src,
2028 struct snd_soc_tplg_link_config **link)
2029{
2030 struct snd_soc_tplg_link_config *dest;
2031 struct snd_soc_tplg_link_config_v4 *src_v4;
2032 int i;
2033
2034 *link = NULL;
2035
2036 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2037 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2038 return -EINVAL;
2039 }
2040
2041 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2042
2043 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2044 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2045 if (!dest)
2046 return -ENOMEM;
2047
2048 dest->size = sizeof(*dest);
2049 dest->id = src_v4->id;
2050 dest->num_streams = src_v4->num_streams;
2051 for (i = 0; i < dest->num_streams; i++)
2052 memcpy(&dest->stream[i], &src_v4->stream[i],
2053 sizeof(struct snd_soc_tplg_stream));
2054
2055 *link = dest;
2056 return 0;
2057}
2058
2059/* Find and configure an existing physical DAI link */
2060static int soc_tplg_link_config(struct soc_tplg *tplg,
2061 struct snd_soc_tplg_link_config *cfg)
2062{
2063 struct snd_soc_dai_link *link;
2064 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002065 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002066 int ret;
2067
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002068 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2069 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2070 return -EINVAL;
2071 else if (len)
2072 name = cfg->name;
2073 else
2074 name = NULL;
2075
2076 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2077 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2078 return -EINVAL;
2079 else if (len)
2080 stream_name = cfg->stream_name;
2081 else
2082 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002083
2084 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2085 name, stream_name);
2086 if (!link) {
2087 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2088 name, cfg->id);
2089 return -EINVAL;
2090 }
2091
2092 /* hw format */
2093 if (cfg->num_hw_configs)
2094 set_link_hw_format(link, cfg);
2095
2096 /* flags */
2097 if (cfg->flag_mask)
2098 set_link_flags(link, cfg->flag_mask, cfg->flags);
2099
2100 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002101 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002102 if (ret < 0) {
2103 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2104 return ret;
2105 }
2106
2107 return 0;
2108}
2109
2110
2111/* Load physical link config elements from the topology context */
2112static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2113 struct snd_soc_tplg_hdr *hdr)
2114{
2115 struct snd_soc_tplg_link_config *link, *_link;
2116 int count = hdr->count;
2117 int i, ret;
2118 bool abi_match;
2119
2120 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2121 tplg->pos += hdr->size + hdr->payload_size;
2122 return 0;
2123 };
2124
2125 /* check the element size and count */
2126 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2127 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2128 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2129 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2130 link->size);
2131 return -EINVAL;
2132 }
2133
2134 if (soc_tplg_check_elem_count(tplg,
2135 link->size, count,
2136 hdr->payload_size, "physical link config")) {
2137 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2138 count);
2139 return -EINVAL;
2140 }
2141
2142 /* config physical DAI links */
2143 for (i = 0; i < count; i++) {
2144 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2145 if (link->size == sizeof(*link)) {
2146 abi_match = true;
2147 _link = link;
2148 } else {
2149 abi_match = false;
2150 ret = link_new_ver(tplg, link, &_link);
2151 if (ret < 0)
2152 return ret;
2153 }
2154
2155 ret = soc_tplg_link_config(tplg, _link);
2156 if (ret < 0)
2157 return ret;
2158
2159 /* offset by version-specific struct size and
2160 * real priv data size
2161 */
2162 tplg->pos += link->size + _link->priv.size;
2163
2164 if (!abi_match)
2165 kfree(_link); /* free the duplicated one */
2166 }
2167
2168 return 0;
2169}
2170
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002171/**
2172 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002173 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002174 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002175 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002176 * The physical dai should already be registered by the platform driver.
2177 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002178 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002179static int soc_tplg_dai_config(struct soc_tplg *tplg,
2180 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002181{
2182 struct snd_soc_dai_link_component dai_component = {0};
2183 struct snd_soc_dai *dai;
2184 struct snd_soc_dai_driver *dai_drv;
2185 struct snd_soc_pcm_stream *stream;
2186 struct snd_soc_tplg_stream_caps *caps;
2187 int ret;
2188
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002189 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002190 dai = snd_soc_find_dai(&dai_component);
2191 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002192 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2193 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002194 return -EINVAL;
2195 }
2196
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002197 if (d->dai_id != dai->id) {
2198 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2199 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002200 return -EINVAL;
2201 }
2202
2203 dai_drv = dai->driver;
2204 if (!dai_drv)
2205 return -EINVAL;
2206
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002207 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002208 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002209 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002210 set_stream_info(stream, caps);
2211 }
2212
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002213 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002214 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002215 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002216 set_stream_info(stream, caps);
2217 }
2218
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002219 if (d->flag_mask)
2220 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002221
2222 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002223 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002224 if (ret < 0) {
2225 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2226 return ret;
2227 }
2228
2229 return 0;
2230}
2231
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002232/* load physical DAI elements */
2233static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2234 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002235{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002236 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002237 int count = hdr->count;
2238 int i;
2239
2240 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2241 return 0;
2242
2243 /* config the existing BE DAIs */
2244 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002245 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2246 if (dai->size != sizeof(*dai)) {
2247 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002248 return -EINVAL;
2249 }
2250
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002251 soc_tplg_dai_config(tplg, dai);
2252 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002253 }
2254
2255 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2256 return 0;
2257}
2258
Mengdong Lin583958f2016-10-11 14:36:42 +08002259/**
2260 * manifest_new_ver - Create a new version of manifest from the old version
2261 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002262 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002263 * @src: old version of manifest as a source
2264 * @manifest: latest version of manifest created from the source
2265 *
2266 * Support from vesion 4. Users need free the returned manifest manually.
2267 */
2268static int manifest_new_ver(struct soc_tplg *tplg,
2269 struct snd_soc_tplg_manifest *src,
2270 struct snd_soc_tplg_manifest **manifest)
2271{
2272 struct snd_soc_tplg_manifest *dest;
2273 struct snd_soc_tplg_manifest_v4 *src_v4;
2274
2275 *manifest = NULL;
2276
2277 if (src->size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002278 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2279 src->size);
2280 if (src->size)
2281 return -EINVAL;
2282 src->size = sizeof(*src_v4);
Mengdong Lin583958f2016-10-11 14:36:42 +08002283 }
2284
2285 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2286
2287 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2288 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2289 if (!dest)
2290 return -ENOMEM;
2291
2292 dest->size = sizeof(*dest); /* size of latest abi version */
2293 dest->control_elems = src_v4->control_elems;
2294 dest->widget_elems = src_v4->widget_elems;
2295 dest->graph_elems = src_v4->graph_elems;
2296 dest->pcm_elems = src_v4->pcm_elems;
2297 dest->dai_link_elems = src_v4->dai_link_elems;
2298 dest->priv.size = src_v4->priv.size;
2299 if (dest->priv.size)
2300 memcpy(dest->priv.data, src_v4->priv.data,
2301 src_v4->priv.size);
2302
2303 *manifest = dest;
2304 return 0;
2305}
Mengdong Lin0038be92016-07-26 14:32:37 +08002306
Liam Girdwood8a978232015-05-29 19:06:14 +01002307static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002308 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002309{
Mengdong Lin583958f2016-10-11 14:36:42 +08002310 struct snd_soc_tplg_manifest *manifest, *_manifest;
2311 bool abi_match;
2312 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002313
2314 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2315 return 0;
2316
2317 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002318
2319 /* check ABI version by size, create a new manifest if abi not match */
2320 if (manifest->size == sizeof(*manifest)) {
2321 abi_match = true;
2322 _manifest = manifest;
2323 } else {
2324 abi_match = false;
2325 err = manifest_new_ver(tplg, manifest, &_manifest);
2326 if (err < 0)
2327 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002328 }
2329
Mengdong Lin583958f2016-10-11 14:36:42 +08002330 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002331 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002332 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002333
Mengdong Lin583958f2016-10-11 14:36:42 +08002334 if (!abi_match) /* free the duplicated one */
2335 kfree(_manifest);
2336
Liam Girdwood8a978232015-05-29 19:06:14 +01002337 return 0;
2338}
2339
2340/* validate header magic, size and type */
2341static int soc_valid_header(struct soc_tplg *tplg,
2342 struct snd_soc_tplg_hdr *hdr)
2343{
2344 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2345 return 0;
2346
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002347 if (hdr->size != sizeof(*hdr)) {
2348 dev_err(tplg->dev,
2349 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2350 hdr->type, soc_tplg_get_hdr_offset(tplg),
2351 tplg->fw->size);
2352 return -EINVAL;
2353 }
2354
Liam Girdwood8a978232015-05-29 19:06:14 +01002355 /* big endian firmware objects not supported atm */
2356 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2357 dev_err(tplg->dev,
2358 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2359 tplg->pass, hdr->magic,
2360 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2361 return -EINVAL;
2362 }
2363
2364 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2365 dev_err(tplg->dev,
2366 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2367 tplg->pass, hdr->magic,
2368 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2369 return -EINVAL;
2370 }
2371
Mengdong Lin288b8da2016-11-03 01:03:17 +08002372 /* Support ABI from version 4 */
2373 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2374 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002375 dev_err(tplg->dev,
2376 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2377 tplg->pass, hdr->abi,
2378 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2379 tplg->fw->size);
2380 return -EINVAL;
2381 }
2382
2383 if (hdr->payload_size == 0) {
2384 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2385 soc_tplg_get_hdr_offset(tplg));
2386 return -EINVAL;
2387 }
2388
2389 if (tplg->pass == hdr->type)
2390 dev_dbg(tplg->dev,
2391 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2392 hdr->payload_size, hdr->type, hdr->version,
2393 hdr->vendor_type, tplg->pass);
2394
2395 return 1;
2396}
2397
2398/* check header type and call appropriate handler */
2399static int soc_tplg_load_header(struct soc_tplg *tplg,
2400 struct snd_soc_tplg_hdr *hdr)
2401{
2402 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2403
2404 /* check for matching ID */
2405 if (hdr->index != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002406 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002407 return 0;
2408
2409 tplg->index = hdr->index;
2410
2411 switch (hdr->type) {
2412 case SND_SOC_TPLG_TYPE_MIXER:
2413 case SND_SOC_TPLG_TYPE_ENUM:
2414 case SND_SOC_TPLG_TYPE_BYTES:
2415 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2416 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2417 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2418 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2419 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2420 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002421 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002422 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002423 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002424 case SND_SOC_TPLG_TYPE_DAI_LINK:
2425 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2426 /* physical link configurations */
2427 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002428 case SND_SOC_TPLG_TYPE_MANIFEST:
2429 return soc_tplg_manifest_load(tplg, hdr);
2430 default:
2431 /* bespoke vendor data object */
2432 return soc_tplg_vendor_load(tplg, hdr);
2433 }
2434
2435 return 0;
2436}
2437
2438/* process the topology file headers */
2439static int soc_tplg_process_headers(struct soc_tplg *tplg)
2440{
2441 struct snd_soc_tplg_hdr *hdr;
2442 int ret;
2443
2444 tplg->pass = SOC_TPLG_PASS_START;
2445
2446 /* process the header types from start to end */
2447 while (tplg->pass <= SOC_TPLG_PASS_END) {
2448
2449 tplg->hdr_pos = tplg->fw->data;
2450 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2451
2452 while (!soc_tplg_is_eof(tplg)) {
2453
2454 /* make sure header is valid before loading */
2455 ret = soc_valid_header(tplg, hdr);
2456 if (ret < 0)
2457 return ret;
2458 else if (ret == 0)
2459 break;
2460
2461 /* load the header object */
2462 ret = soc_tplg_load_header(tplg, hdr);
2463 if (ret < 0)
2464 return ret;
2465
2466 /* goto next header */
2467 tplg->hdr_pos += hdr->payload_size +
2468 sizeof(struct snd_soc_tplg_hdr);
2469 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2470 }
2471
2472 /* next data type pass */
2473 tplg->pass++;
2474 }
2475
2476 /* signal DAPM we are complete */
2477 ret = soc_tplg_dapm_complete(tplg);
2478 if (ret < 0)
2479 dev_err(tplg->dev,
2480 "ASoC: failed to initialise DAPM from Firmware\n");
2481
2482 return ret;
2483}
2484
2485static int soc_tplg_load(struct soc_tplg *tplg)
2486{
2487 int ret;
2488
2489 ret = soc_tplg_process_headers(tplg);
2490 if (ret == 0)
2491 soc_tplg_complete(tplg);
2492
2493 return ret;
2494}
2495
2496/* load audio component topology from "firmware" file */
2497int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2498 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2499{
2500 struct soc_tplg tplg;
2501
2502 /* setup parsing context */
2503 memset(&tplg, 0, sizeof(tplg));
2504 tplg.fw = fw;
2505 tplg.dev = comp->dev;
2506 tplg.comp = comp;
2507 tplg.ops = ops;
2508 tplg.req_index = id;
2509 tplg.io_ops = ops->io_ops;
2510 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002511 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2512 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002513
2514 return soc_tplg_load(&tplg);
2515}
2516EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2517
2518/* remove this dynamic widget */
2519void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2520{
2521 /* make sure we are a widget */
2522 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2523 return;
2524
2525 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2526}
2527EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2528
2529/* remove all dynamic widgets from this DAPM context */
2530void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2531 u32 index)
2532{
2533 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002534
2535 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2536
2537 /* make sure we are a widget with correct context */
2538 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2539 continue;
2540
2541 /* match ID */
2542 if (w->dobj.index != index &&
2543 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2544 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002545 /* check and free and dynamic widget kcontrols */
2546 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002547 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002548 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002549 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002550}
2551EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2552
2553/* remove dynamic controls from the component driver */
2554int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2555{
2556 struct snd_soc_dobj *dobj, *next_dobj;
2557 int pass = SOC_TPLG_PASS_END;
2558
2559 /* process the header types from end to start */
2560 while (pass >= SOC_TPLG_PASS_START) {
2561
2562 /* remove mixer controls */
2563 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2564 list) {
2565
2566 /* match index */
2567 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002568 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002569 continue;
2570
2571 switch (dobj->type) {
2572 case SND_SOC_DOBJ_MIXER:
2573 remove_mixer(comp, dobj, pass);
2574 break;
2575 case SND_SOC_DOBJ_ENUM:
2576 remove_enum(comp, dobj, pass);
2577 break;
2578 case SND_SOC_DOBJ_BYTES:
2579 remove_bytes(comp, dobj, pass);
2580 break;
2581 case SND_SOC_DOBJ_WIDGET:
2582 remove_widget(comp, dobj, pass);
2583 break;
2584 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002585 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002586 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002587 case SND_SOC_DOBJ_DAI_LINK:
2588 remove_link(comp, dobj, pass);
2589 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002590 default:
2591 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2592 dobj->type);
2593 break;
2594 }
2595 }
2596 pass--;
2597 }
2598
2599 /* let caller know if FW can be freed when no objects are left */
2600 return !list_empty(&comp->dobj_list);
2601}
2602EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);