blob: 63e1a50f216195e876c2872b8eabb28ea53ec48e [file] [log] [blame]
Liam Girdwood8a978232015-05-29 19:06:14 +01001/*
2 * soc-topology.c -- ALSA SoC Topology
3 *
4 * Copyright (C) 2012 Texas Instruments Inc.
5 * Copyright (C) 2015 Intel Corporation.
6 *
7 * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
8 * K, Mythri P <mythri.p.k@intel.com>
9 * Prusty, Subhransu S <subhransu.s.prusty@intel.com>
10 * B, Jayachandran <jayachandran.b@intel.com>
11 * Abdullah, Omair M <omair.m.abdullah@intel.com>
12 * Jin, Yao <yao.jin@intel.com>
13 * Lin, Mengdong <mengdong.lin@intel.com>
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Add support to read audio firmware topology alongside firmware text. The
21 * topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
22 * equalizers, firmware, coefficients etc.
23 *
24 * This file only manages the core ALSA and ASoC components, all other bespoke
25 * firmware topology data is passed to component drivers for bespoke handling.
26 */
27
28#include <linux/kernel.h>
29#include <linux/export.h>
30#include <linux/list.h>
31#include <linux/firmware.h>
32#include <linux/slab.h>
33#include <sound/soc.h>
34#include <sound/soc-dapm.h>
35#include <sound/soc-topology.h>
Mengdong Lin28a87ee2015-08-05 14:41:13 +010036#include <sound/tlv.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010037
38/*
39 * We make several passes over the data (since it wont necessarily be ordered)
40 * and process objects in the following order. This guarantees the component
41 * drivers will be ready with any vendor data before the mixers and DAPM objects
42 * are loaded (that may make use of the vendor data).
43 */
44#define SOC_TPLG_PASS_MANIFEST 0
45#define SOC_TPLG_PASS_VENDOR 1
46#define SOC_TPLG_PASS_MIXER 2
47#define SOC_TPLG_PASS_WIDGET 3
Mengdong Lin1a8e7fa2015-08-10 22:48:30 +080048#define SOC_TPLG_PASS_PCM_DAI 4
49#define SOC_TPLG_PASS_GRAPH 5
50#define SOC_TPLG_PASS_PINS 6
Mengdong Lin0038be92016-07-26 14:32:37 +080051#define SOC_TPLG_PASS_BE_DAI 7
Mengdong Lin593d9e52016-11-03 01:04:27 +080052#define SOC_TPLG_PASS_LINK 8
Liam Girdwood8a978232015-05-29 19:06:14 +010053
54#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
Mengdong Lin593d9e52016-11-03 01:04:27 +080055#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
Liam Girdwood8a978232015-05-29 19:06:14 +010056
Mengdong Lin583958f2016-10-11 14:36:42 +080057/*
58 * Old version of ABI structs, supported for backward compatibility.
59 */
60
61/* Manifest v4 */
62struct snd_soc_tplg_manifest_v4 {
63 __le32 size; /* in bytes of this structure */
64 __le32 control_elems; /* number of control elements */
65 __le32 widget_elems; /* number of widget elements */
66 __le32 graph_elems; /* number of graph elements */
67 __le32 pcm_elems; /* number of PCM elements */
68 __le32 dai_link_elems; /* number of DAI link elements */
69 struct snd_soc_tplg_private priv;
70} __packed;
71
Mengdong Lin55726dc2016-11-03 01:00:16 +080072/* Stream Capabilities v4 */
73struct snd_soc_tplg_stream_caps_v4 {
74 __le32 size; /* in bytes of this structure */
75 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
76 __le64 formats; /* supported formats SNDRV_PCM_FMTBIT_* */
77 __le32 rates; /* supported rates SNDRV_PCM_RATE_* */
78 __le32 rate_min; /* min rate */
79 __le32 rate_max; /* max rate */
80 __le32 channels_min; /* min channels */
81 __le32 channels_max; /* max channels */
82 __le32 periods_min; /* min number of periods */
83 __le32 periods_max; /* max number of periods */
84 __le32 period_size_min; /* min period size bytes */
85 __le32 period_size_max; /* max period size bytes */
86 __le32 buffer_size_min; /* min buffer size bytes */
87 __le32 buffer_size_max; /* max buffer size bytes */
88} __packed;
89
90/* PCM v4 */
91struct snd_soc_tplg_pcm_v4 {
92 __le32 size; /* in bytes of this structure */
93 char pcm_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
94 char dai_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
95 __le32 pcm_id; /* unique ID - used to match with DAI link */
96 __le32 dai_id; /* unique ID - used to match */
97 __le32 playback; /* supports playback mode */
98 __le32 capture; /* supports capture mode */
99 __le32 compress; /* 1 = compressed; 0 = PCM */
100 struct snd_soc_tplg_stream stream[SND_SOC_TPLG_STREAM_CONFIG_MAX]; /* for DAI link */
101 __le32 num_streams; /* number of streams */
102 struct snd_soc_tplg_stream_caps_v4 caps[2]; /* playback and capture for DAI */
103} __packed;
104
Mengdong Lin593d9e52016-11-03 01:04:27 +0800105/* Physical link config v4 */
106struct snd_soc_tplg_link_config_v4 {
107 __le32 size; /* in bytes of this structure */
108 __le32 id; /* unique ID - used to match */
109 struct snd_soc_tplg_stream stream[SND_SOC_TPLG_STREAM_CONFIG_MAX]; /* supported configs playback and captrure */
110 __le32 num_streams; /* number of streams */
111} __packed;
112
Mengdong Lin583958f2016-10-11 14:36:42 +0800113/* topology context */
Liam Girdwood8a978232015-05-29 19:06:14 +0100114struct soc_tplg {
115 const struct firmware *fw;
116
117 /* runtime FW parsing */
118 const u8 *pos; /* read postion */
119 const u8 *hdr_pos; /* header position */
120 unsigned int pass; /* pass number */
121
122 /* component caller */
123 struct device *dev;
124 struct snd_soc_component *comp;
125 u32 index; /* current block index */
126 u32 req_index; /* required index, only loaded/free matching blocks */
127
Mengdong Lin88a17d82015-08-18 18:11:51 +0800128 /* vendor specific kcontrol operations */
Liam Girdwood8a978232015-05-29 19:06:14 +0100129 const struct snd_soc_tplg_kcontrol_ops *io_ops;
130 int io_ops_count;
131
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800132 /* vendor specific bytes ext handlers, for TLV bytes controls */
133 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
134 int bytes_ext_ops_count;
135
Liam Girdwood8a978232015-05-29 19:06:14 +0100136 /* optional fw loading callbacks to component drivers */
137 struct snd_soc_tplg_ops *ops;
138};
139
140static int soc_tplg_process_headers(struct soc_tplg *tplg);
141static void soc_tplg_complete(struct soc_tplg *tplg);
142struct snd_soc_dapm_widget *
143snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
144 const struct snd_soc_dapm_widget *widget);
145struct snd_soc_dapm_widget *
146snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
147 const struct snd_soc_dapm_widget *widget);
148
149/* check we dont overflow the data for this control chunk */
150static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
151 unsigned int count, size_t bytes, const char *elem_type)
152{
153 const u8 *end = tplg->pos + elem_size * count;
154
155 if (end > tplg->fw->data + tplg->fw->size) {
156 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
157 elem_type);
158 return -EINVAL;
159 }
160
161 /* check there is enough room in chunk for control.
162 extra bytes at the end of control are for vendor data here */
163 if (elem_size * count > bytes) {
164 dev_err(tplg->dev,
165 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
166 elem_type, count, elem_size, bytes);
167 return -EINVAL;
168 }
169
170 return 0;
171}
172
173static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
174{
175 const u8 *end = tplg->hdr_pos;
176
177 if (end >= tplg->fw->data + tplg->fw->size)
178 return 1;
179 return 0;
180}
181
182static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
183{
184 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
185}
186
187static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
188{
189 return (unsigned long)(tplg->pos - tplg->fw->data);
190}
191
192/* mapping of Kcontrol types and associated operations. */
193static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
194 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
195 snd_soc_put_volsw, snd_soc_info_volsw},
196 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
197 snd_soc_put_volsw_sx, NULL},
198 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
199 snd_soc_put_enum_double, snd_soc_info_enum_double},
200 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
201 snd_soc_put_enum_double, NULL},
202 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
203 snd_soc_bytes_put, snd_soc_bytes_info},
204 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
205 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
206 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
207 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
208 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
209 snd_soc_put_strobe, NULL},
210 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530211 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100212 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
213 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
214 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
215 snd_soc_dapm_put_enum_double, NULL},
216 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
217 snd_soc_dapm_put_enum_double, NULL},
218 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
219 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
220};
221
222struct soc_tplg_map {
223 int uid;
224 int kid;
225};
226
227/* mapping of widget types from UAPI IDs to kernel IDs */
228static const struct soc_tplg_map dapm_map[] = {
229 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
230 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
231 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
232 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
233 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
234 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
235 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
236 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
237 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
238 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
239 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
240 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
241 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
242 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
243 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
244 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
245};
246
247static int tplc_chan_get_reg(struct soc_tplg *tplg,
248 struct snd_soc_tplg_channel *chan, int map)
249{
250 int i;
251
252 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
253 if (chan[i].id == map)
254 return chan[i].reg;
255 }
256
257 return -EINVAL;
258}
259
260static int tplc_chan_get_shift(struct soc_tplg *tplg,
261 struct snd_soc_tplg_channel *chan, int map)
262{
263 int i;
264
265 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
266 if (chan[i].id == map)
267 return chan[i].shift;
268 }
269
270 return -EINVAL;
271}
272
273static int get_widget_id(int tplg_type)
274{
275 int i;
276
277 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
278 if (tplg_type == dapm_map[i].uid)
279 return dapm_map[i].kid;
280 }
281
282 return -EINVAL;
283}
284
Liam Girdwood8a978232015-05-29 19:06:14 +0100285static inline void soc_bind_err(struct soc_tplg *tplg,
286 struct snd_soc_tplg_ctl_hdr *hdr, int index)
287{
288 dev_err(tplg->dev,
289 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
290 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
291 soc_tplg_get_offset(tplg));
292}
293
294static inline void soc_control_err(struct soc_tplg *tplg,
295 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
296{
297 dev_err(tplg->dev,
298 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
299 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
300 soc_tplg_get_offset(tplg));
301}
302
303/* pass vendor data to component driver for processing */
304static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
305 struct snd_soc_tplg_hdr *hdr)
306{
307 int ret = 0;
308
309 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
310 ret = tplg->ops->vendor_load(tplg->comp, hdr);
311 else {
312 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
313 hdr->vendor_type);
314 return -EINVAL;
315 }
316
317 if (ret < 0)
318 dev_err(tplg->dev,
319 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
320 soc_tplg_get_hdr_offset(tplg),
321 soc_tplg_get_hdr_offset(tplg),
322 hdr->type, hdr->vendor_type);
323 return ret;
324}
325
326/* pass vendor data to component driver for processing */
327static int soc_tplg_vendor_load(struct soc_tplg *tplg,
328 struct snd_soc_tplg_hdr *hdr)
329{
330 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
331 return 0;
332
333 return soc_tplg_vendor_load_(tplg, hdr);
334}
335
336/* optionally pass new dynamic widget to component driver. This is mainly for
337 * external widgets where we can assign private data/ops */
338static int soc_tplg_widget_load(struct soc_tplg *tplg,
339 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
340{
341 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
342 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
343
344 return 0;
345}
346
Mengdong Lin64527e82016-01-15 16:13:28 +0800347/* pass DAI configurations to component driver for extra intialization */
348static int soc_tplg_dai_load(struct soc_tplg *tplg,
349 struct snd_soc_dai_driver *dai_drv)
Liam Girdwood8a978232015-05-29 19:06:14 +0100350{
Mengdong Lin64527e82016-01-15 16:13:28 +0800351 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
352 return tplg->ops->dai_load(tplg->comp, dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100353
354 return 0;
355}
356
Mengdong Linacfc7d42016-01-15 16:13:37 +0800357/* pass link configurations to component driver for extra intialization */
358static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
359 struct snd_soc_dai_link *link)
360{
361 if (tplg->comp && tplg->ops && tplg->ops->link_load)
362 return tplg->ops->link_load(tplg->comp, link);
363
364 return 0;
365}
366
Liam Girdwood8a978232015-05-29 19:06:14 +0100367/* tell the component driver that all firmware has been loaded in this request */
368static void soc_tplg_complete(struct soc_tplg *tplg)
369{
370 if (tplg->comp && tplg->ops && tplg->ops->complete)
371 tplg->ops->complete(tplg->comp);
372}
373
374/* add a dynamic kcontrol */
375static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
376 const struct snd_kcontrol_new *control_new, const char *prefix,
377 void *data, struct snd_kcontrol **kcontrol)
378{
379 int err;
380
381 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
382 if (*kcontrol == NULL) {
383 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
384 control_new->name);
385 return -ENOMEM;
386 }
387
388 err = snd_ctl_add(card, *kcontrol);
389 if (err < 0) {
390 dev_err(dev, "ASoC: Failed to add %s: %d\n",
391 control_new->name, err);
392 return err;
393 }
394
395 return 0;
396}
397
398/* add a dynamic kcontrol for component driver */
399static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
400 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
401{
402 struct snd_soc_component *comp = tplg->comp;
403
404 return soc_tplg_add_dcontrol(comp->card->snd_card,
405 comp->dev, k, NULL, comp, kcontrol);
406}
407
408/* remove a mixer kcontrol */
409static void remove_mixer(struct snd_soc_component *comp,
410 struct snd_soc_dobj *dobj, int pass)
411{
412 struct snd_card *card = comp->card->snd_card;
413 struct soc_mixer_control *sm =
414 container_of(dobj, struct soc_mixer_control, dobj);
415 const unsigned int *p = NULL;
416
417 if (pass != SOC_TPLG_PASS_MIXER)
418 return;
419
420 if (dobj->ops && dobj->ops->control_unload)
421 dobj->ops->control_unload(comp, dobj);
422
423 if (sm->dobj.control.kcontrol->tlv.p)
424 p = sm->dobj.control.kcontrol->tlv.p;
425 snd_ctl_remove(card, sm->dobj.control.kcontrol);
426 list_del(&sm->dobj.list);
427 kfree(sm);
428 kfree(p);
429}
430
431/* remove an enum kcontrol */
432static void remove_enum(struct snd_soc_component *comp,
433 struct snd_soc_dobj *dobj, int pass)
434{
435 struct snd_card *card = comp->card->snd_card;
436 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
437 int i;
438
439 if (pass != SOC_TPLG_PASS_MIXER)
440 return;
441
442 if (dobj->ops && dobj->ops->control_unload)
443 dobj->ops->control_unload(comp, dobj);
444
445 snd_ctl_remove(card, se->dobj.control.kcontrol);
446 list_del(&se->dobj.list);
447
448 kfree(se->dobj.control.dvalues);
449 for (i = 0; i < se->items; i++)
450 kfree(se->dobj.control.dtexts[i]);
451 kfree(se);
452}
453
454/* remove a byte kcontrol */
455static void remove_bytes(struct snd_soc_component *comp,
456 struct snd_soc_dobj *dobj, int pass)
457{
458 struct snd_card *card = comp->card->snd_card;
459 struct soc_bytes_ext *sb =
460 container_of(dobj, struct soc_bytes_ext, dobj);
461
462 if (pass != SOC_TPLG_PASS_MIXER)
463 return;
464
465 if (dobj->ops && dobj->ops->control_unload)
466 dobj->ops->control_unload(comp, dobj);
467
468 snd_ctl_remove(card, sb->dobj.control.kcontrol);
469 list_del(&sb->dobj.list);
470 kfree(sb);
471}
472
473/* remove a widget and it's kcontrols - routes must be removed first */
474static void remove_widget(struct snd_soc_component *comp,
475 struct snd_soc_dobj *dobj, int pass)
476{
477 struct snd_card *card = comp->card->snd_card;
478 struct snd_soc_dapm_widget *w =
479 container_of(dobj, struct snd_soc_dapm_widget, dobj);
480 int i;
481
482 if (pass != SOC_TPLG_PASS_WIDGET)
483 return;
484
485 if (dobj->ops && dobj->ops->widget_unload)
486 dobj->ops->widget_unload(comp, dobj);
487
488 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800489 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100490 * The enum may either have an array of values or strings.
491 */
492 if (dobj->widget.kcontrol_enum) {
493 /* enumerated widget mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800494 for (i = 0; i < w->num_kcontrols; i++) {
495 struct snd_kcontrol *kcontrol = w->kcontrols[i];
496 struct soc_enum *se =
497 (struct soc_enum *)kcontrol->private_value;
Liam Girdwood8a978232015-05-29 19:06:14 +0100498
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800499 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100500
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800501 kfree(se->dobj.control.dvalues);
502 for (i = 0; i < se->items; i++)
503 kfree(se->dobj.control.dtexts[i]);
Liam Girdwood8a978232015-05-29 19:06:14 +0100504
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800505 kfree(se);
506 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100507 kfree(w->kcontrol_news);
508 } else {
509 /* non enumerated widget mixer */
510 for (i = 0; i < w->num_kcontrols; i++) {
511 struct snd_kcontrol *kcontrol = w->kcontrols[i];
512 struct soc_mixer_control *sm =
513 (struct soc_mixer_control *) kcontrol->private_value;
514
515 kfree(w->kcontrols[i]->tlv.p);
516
517 snd_ctl_remove(card, w->kcontrols[i]);
518 kfree(sm);
519 }
520 kfree(w->kcontrol_news);
521 }
522 /* widget w is freed by soc-dapm.c */
523}
524
Mengdong Lin64527e82016-01-15 16:13:28 +0800525/* remove DAI configurations */
526static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100527 struct snd_soc_dobj *dobj, int pass)
528{
Mengdong Lin64527e82016-01-15 16:13:28 +0800529 struct snd_soc_dai_driver *dai_drv =
530 container_of(dobj, struct snd_soc_dai_driver, dobj);
531
Liam Girdwood8a978232015-05-29 19:06:14 +0100532 if (pass != SOC_TPLG_PASS_PCM_DAI)
533 return;
534
Mengdong Lin64527e82016-01-15 16:13:28 +0800535 if (dobj->ops && dobj->ops->dai_unload)
536 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100537
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800538 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100539 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800540 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100541}
542
Mengdong Linacfc7d42016-01-15 16:13:37 +0800543/* remove link configurations */
544static void remove_link(struct snd_soc_component *comp,
545 struct snd_soc_dobj *dobj, int pass)
546{
547 struct snd_soc_dai_link *link =
548 container_of(dobj, struct snd_soc_dai_link, dobj);
549
550 if (pass != SOC_TPLG_PASS_PCM_DAI)
551 return;
552
553 if (dobj->ops && dobj->ops->link_unload)
554 dobj->ops->link_unload(comp, dobj);
555
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800556 kfree(link->name);
557 kfree(link->stream_name);
558 kfree(link->cpu_dai_name);
559
Mengdong Linacfc7d42016-01-15 16:13:37 +0800560 list_del(&dobj->list);
561 snd_soc_remove_dai_link(comp->card, link);
562 kfree(link);
563}
564
Liam Girdwood8a978232015-05-29 19:06:14 +0100565/* bind a kcontrol to it's IO handlers */
566static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
567 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800568 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100569{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800570 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800571 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800572 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100573
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800574 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
575 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
576 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
577 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
578 struct soc_bytes_ext *sbe;
579 struct snd_soc_tplg_bytes_control *be;
580
581 sbe = (struct soc_bytes_ext *)k->private_value;
582 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
583
584 /* TLV bytes controls need standard kcontrol info handler,
585 * TLV callback and extended put/get handlers.
586 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530587 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800588 k->tlv.c = snd_soc_bytes_tlv_callback;
589
590 ext_ops = tplg->bytes_ext_ops;
591 num_ops = tplg->bytes_ext_ops_count;
592 for (i = 0; i < num_ops; i++) {
593 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
594 sbe->put = ext_ops[i].put;
595 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
596 sbe->get = ext_ops[i].get;
597 }
598
599 if (sbe->put && sbe->get)
600 return 0;
601 else
602 return -EINVAL;
603 }
604
Mengdong Lin88a17d82015-08-18 18:11:51 +0800605 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800606 ops = tplg->io_ops;
607 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100608 for (i = 0; i < num_ops; i++) {
609
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800610 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100611 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800612 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100613 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800614 if (k->info == NULL && ops[i].id == hdr->ops.info)
615 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100616 }
617
Mengdong Lin88a17d82015-08-18 18:11:51 +0800618 /* vendor specific handlers found ? */
619 if (k->put && k->get && k->info)
620 return 0;
621
622 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800623 ops = io_ops;
624 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800625 for (i = 0; i < num_ops; i++) {
626
627 if (k->put == NULL && ops[i].id == hdr->ops.put)
628 k->put = ops[i].put;
629 if (k->get == NULL && ops[i].id == hdr->ops.get)
630 k->get = ops[i].get;
631 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100632 k->info = ops[i].info;
633 }
634
635 /* standard handlers found ? */
636 if (k->put && k->get && k->info)
637 return 0;
638
Liam Girdwood8a978232015-05-29 19:06:14 +0100639 /* nothing to bind */
640 return -EINVAL;
641}
642
643/* bind a widgets to it's evnt handlers */
644int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
645 const struct snd_soc_tplg_widget_events *events,
646 int num_events, u16 event_type)
647{
648 int i;
649
650 w->event = NULL;
651
652 for (i = 0; i < num_events; i++) {
653 if (event_type == events[i].type) {
654
655 /* found - so assign event */
656 w->event = events[i].event_handler;
657 return 0;
658 }
659 }
660
661 /* not found */
662 return -EINVAL;
663}
664EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
665
666/* optionally pass new dynamic kcontrol to component driver. */
667static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
668 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
669{
670 if (tplg->comp && tplg->ops && tplg->ops->control_load)
671 return tplg->ops->control_load(tplg->comp, k, hdr);
672
673 return 0;
674}
675
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100676
677static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
678 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100679{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100680 unsigned int item_len = 2 * sizeof(unsigned int);
681 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100682
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100683 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
684 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100685 return -ENOMEM;
686
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100687 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
688 p[1] = item_len;
689 p[2] = scale->min;
690 p[3] = (scale->step & TLV_DB_SCALE_MASK)
691 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100692
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100693 kc->tlv.p = (void *)p;
694 return 0;
695}
696
697static int soc_tplg_create_tlv(struct soc_tplg *tplg,
698 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
699{
700 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
701
702 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
703 return 0;
704
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800705 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100706 tplg_tlv = &tc->tlv;
707 switch (tplg_tlv->type) {
708 case SNDRV_CTL_TLVT_DB_SCALE:
709 return soc_tplg_create_tlv_db_scale(tplg, kc,
710 &tplg_tlv->scale);
711
712 /* TODO: add support for other TLV types */
713 default:
714 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
715 tplg_tlv->type);
716 return -EINVAL;
717 }
718 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100719
720 return 0;
721}
722
723static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
724 struct snd_kcontrol_new *kc)
725{
726 kfree(kc->tlv.p);
727}
728
729static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
730 size_t size)
731{
732 struct snd_soc_tplg_bytes_control *be;
733 struct soc_bytes_ext *sbe;
734 struct snd_kcontrol_new kc;
735 int i, err;
736
737 if (soc_tplg_check_elem_count(tplg,
738 sizeof(struct snd_soc_tplg_bytes_control), count,
739 size, "mixer bytes")) {
740 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
741 count);
742 return -EINVAL;
743 }
744
745 for (i = 0; i < count; i++) {
746 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
747
748 /* validate kcontrol */
749 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
750 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
751 return -EINVAL;
752
753 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
754 if (sbe == NULL)
755 return -ENOMEM;
756
757 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
758 be->priv.size);
759
760 dev_dbg(tplg->dev,
761 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
762 be->hdr.name, be->hdr.access);
763
764 memset(&kc, 0, sizeof(kc));
765 kc.name = be->hdr.name;
766 kc.private_value = (long)sbe;
767 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
768 kc.access = be->hdr.access;
769
770 sbe->max = be->max;
771 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
772 sbe->dobj.ops = tplg->ops;
773 INIT_LIST_HEAD(&sbe->dobj.list);
774
775 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800776 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100777 if (err) {
778 soc_control_err(tplg, &be->hdr, be->hdr.name);
779 kfree(sbe);
780 continue;
781 }
782
783 /* pass control to driver for optional further init */
784 err = soc_tplg_init_kcontrol(tplg, &kc,
785 (struct snd_soc_tplg_ctl_hdr *)be);
786 if (err < 0) {
787 dev_err(tplg->dev, "ASoC: failed to init %s\n",
788 be->hdr.name);
789 kfree(sbe);
790 continue;
791 }
792
793 /* register control here */
794 err = soc_tplg_add_kcontrol(tplg, &kc,
795 &sbe->dobj.control.kcontrol);
796 if (err < 0) {
797 dev_err(tplg->dev, "ASoC: failed to add %s\n",
798 be->hdr.name);
799 kfree(sbe);
800 continue;
801 }
802
803 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
804 }
805 return 0;
806
807}
808
809static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
810 size_t size)
811{
812 struct snd_soc_tplg_mixer_control *mc;
813 struct soc_mixer_control *sm;
814 struct snd_kcontrol_new kc;
815 int i, err;
816
817 if (soc_tplg_check_elem_count(tplg,
818 sizeof(struct snd_soc_tplg_mixer_control),
819 count, size, "mixers")) {
820
821 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
822 count);
823 return -EINVAL;
824 }
825
826 for (i = 0; i < count; i++) {
827 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
828
829 /* validate kcontrol */
830 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
831 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
832 return -EINVAL;
833
834 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
835 if (sm == NULL)
836 return -ENOMEM;
837 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
838 mc->priv.size);
839
840 dev_dbg(tplg->dev,
841 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
842 mc->hdr.name, mc->hdr.access);
843
844 memset(&kc, 0, sizeof(kc));
845 kc.name = mc->hdr.name;
846 kc.private_value = (long)sm;
847 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
848 kc.access = mc->hdr.access;
849
850 /* we only support FL/FR channel mapping atm */
851 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
852 SNDRV_CHMAP_FL);
853 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
854 SNDRV_CHMAP_FR);
855 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
856 SNDRV_CHMAP_FL);
857 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
858 SNDRV_CHMAP_FR);
859
860 sm->max = mc->max;
861 sm->min = mc->min;
862 sm->invert = mc->invert;
863 sm->platform_max = mc->platform_max;
864 sm->dobj.index = tplg->index;
865 sm->dobj.ops = tplg->ops;
866 sm->dobj.type = SND_SOC_DOBJ_MIXER;
867 INIT_LIST_HEAD(&sm->dobj.list);
868
869 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800870 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100871 if (err) {
872 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
873 kfree(sm);
874 continue;
875 }
876
877 /* pass control to driver for optional further init */
878 err = soc_tplg_init_kcontrol(tplg, &kc,
879 (struct snd_soc_tplg_ctl_hdr *) mc);
880 if (err < 0) {
881 dev_err(tplg->dev, "ASoC: failed to init %s\n",
882 mc->hdr.name);
883 kfree(sm);
884 continue;
885 }
886
887 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100888 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100889
890 /* register control here */
891 err = soc_tplg_add_kcontrol(tplg, &kc,
892 &sm->dobj.control.kcontrol);
893 if (err < 0) {
894 dev_err(tplg->dev, "ASoC: failed to add %s\n",
895 mc->hdr.name);
896 soc_tplg_free_tlv(tplg, &kc);
897 kfree(sm);
898 continue;
899 }
900
901 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
902 }
903
904 return 0;
905}
906
907static int soc_tplg_denum_create_texts(struct soc_enum *se,
908 struct snd_soc_tplg_enum_control *ec)
909{
910 int i, ret;
911
912 se->dobj.control.dtexts =
913 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
914 if (se->dobj.control.dtexts == NULL)
915 return -ENOMEM;
916
917 for (i = 0; i < ec->items; i++) {
918
919 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
920 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
921 ret = -EINVAL;
922 goto err;
923 }
924
925 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
926 if (!se->dobj.control.dtexts[i]) {
927 ret = -ENOMEM;
928 goto err;
929 }
930 }
931
932 return 0;
933
934err:
935 for (--i; i >= 0; i--)
936 kfree(se->dobj.control.dtexts[i]);
937 kfree(se->dobj.control.dtexts);
938 return ret;
939}
940
941static int soc_tplg_denum_create_values(struct soc_enum *se,
942 struct snd_soc_tplg_enum_control *ec)
943{
944 if (ec->items > sizeof(*ec->values))
945 return -EINVAL;
946
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200947 se->dobj.control.dvalues = kmemdup(ec->values,
948 ec->items * sizeof(u32),
949 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100950 if (!se->dobj.control.dvalues)
951 return -ENOMEM;
952
Liam Girdwood8a978232015-05-29 19:06:14 +0100953 return 0;
954}
955
956static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
957 size_t size)
958{
959 struct snd_soc_tplg_enum_control *ec;
960 struct soc_enum *se;
961 struct snd_kcontrol_new kc;
962 int i, ret, err;
963
964 if (soc_tplg_check_elem_count(tplg,
965 sizeof(struct snd_soc_tplg_enum_control),
966 count, size, "enums")) {
967
968 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
969 count);
970 return -EINVAL;
971 }
972
973 for (i = 0; i < count; i++) {
974 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
975 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
976 ec->priv.size);
977
978 /* validate kcontrol */
979 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
980 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
981 return -EINVAL;
982
983 se = kzalloc((sizeof(*se)), GFP_KERNEL);
984 if (se == NULL)
985 return -ENOMEM;
986
987 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
988 ec->hdr.name, ec->items);
989
990 memset(&kc, 0, sizeof(kc));
991 kc.name = ec->hdr.name;
992 kc.private_value = (long)se;
993 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
994 kc.access = ec->hdr.access;
995
996 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
997 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
998 SNDRV_CHMAP_FL);
999 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1000 SNDRV_CHMAP_FL);
1001
1002 se->items = ec->items;
1003 se->mask = ec->mask;
1004 se->dobj.index = tplg->index;
1005 se->dobj.type = SND_SOC_DOBJ_ENUM;
1006 se->dobj.ops = tplg->ops;
1007 INIT_LIST_HEAD(&se->dobj.list);
1008
1009 switch (ec->hdr.ops.info) {
1010 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1011 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1012 err = soc_tplg_denum_create_values(se, ec);
1013 if (err < 0) {
1014 dev_err(tplg->dev,
1015 "ASoC: could not create values for %s\n",
1016 ec->hdr.name);
1017 kfree(se);
1018 continue;
1019 }
1020 /* fall through and create texts */
1021 case SND_SOC_TPLG_CTL_ENUM:
1022 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1023 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1024 err = soc_tplg_denum_create_texts(se, ec);
1025 if (err < 0) {
1026 dev_err(tplg->dev,
1027 "ASoC: could not create texts for %s\n",
1028 ec->hdr.name);
1029 kfree(se);
1030 continue;
1031 }
1032 break;
1033 default:
1034 dev_err(tplg->dev,
1035 "ASoC: invalid enum control type %d for %s\n",
1036 ec->hdr.ops.info, ec->hdr.name);
1037 kfree(se);
1038 continue;
1039 }
1040
1041 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001042 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001043 if (err) {
1044 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1045 kfree(se);
1046 continue;
1047 }
1048
1049 /* pass control to driver for optional further init */
1050 err = soc_tplg_init_kcontrol(tplg, &kc,
1051 (struct snd_soc_tplg_ctl_hdr *) ec);
1052 if (err < 0) {
1053 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1054 ec->hdr.name);
1055 kfree(se);
1056 continue;
1057 }
1058
1059 /* register control here */
1060 ret = soc_tplg_add_kcontrol(tplg,
1061 &kc, &se->dobj.control.kcontrol);
1062 if (ret < 0) {
1063 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1064 ec->hdr.name);
1065 kfree(se);
1066 continue;
1067 }
1068
1069 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1070 }
1071
1072 return 0;
1073}
1074
1075static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1076 struct snd_soc_tplg_hdr *hdr)
1077{
1078 struct snd_soc_tplg_ctl_hdr *control_hdr;
1079 int i;
1080
1081 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1082 tplg->pos += hdr->size + hdr->payload_size;
1083 return 0;
1084 }
1085
1086 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1087 soc_tplg_get_offset(tplg));
1088
1089 for (i = 0; i < hdr->count; i++) {
1090
1091 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1092
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001093 if (control_hdr->size != sizeof(*control_hdr)) {
1094 dev_err(tplg->dev, "ASoC: invalid control size\n");
1095 return -EINVAL;
1096 }
1097
Liam Girdwood8a978232015-05-29 19:06:14 +01001098 switch (control_hdr->ops.info) {
1099 case SND_SOC_TPLG_CTL_VOLSW:
1100 case SND_SOC_TPLG_CTL_STROBE:
1101 case SND_SOC_TPLG_CTL_VOLSW_SX:
1102 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1103 case SND_SOC_TPLG_CTL_RANGE:
1104 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1105 case SND_SOC_TPLG_DAPM_CTL_PIN:
1106 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1107 break;
1108 case SND_SOC_TPLG_CTL_ENUM:
1109 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1110 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1111 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1112 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1113 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1114 break;
1115 case SND_SOC_TPLG_CTL_BYTES:
1116 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1117 break;
1118 default:
1119 soc_bind_err(tplg, control_hdr, i);
1120 return -EINVAL;
1121 }
1122 }
1123
1124 return 0;
1125}
1126
1127static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1128 struct snd_soc_tplg_hdr *hdr)
1129{
1130 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1131 struct snd_soc_dapm_route route;
1132 struct snd_soc_tplg_dapm_graph_elem *elem;
1133 int count = hdr->count, i;
1134
1135 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1136 tplg->pos += hdr->size + hdr->payload_size;
1137 return 0;
1138 }
1139
1140 if (soc_tplg_check_elem_count(tplg,
1141 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1142 count, hdr->payload_size, "graph")) {
1143
1144 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1145 count);
1146 return -EINVAL;
1147 }
1148
1149 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1150
1151 for (i = 0; i < count; i++) {
1152 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1153 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1154
1155 /* validate routes */
1156 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1157 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1158 return -EINVAL;
1159 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1160 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1161 return -EINVAL;
1162 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1163 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1164 return -EINVAL;
1165
1166 route.source = elem->source;
1167 route.sink = elem->sink;
1168 route.connected = NULL; /* set to NULL atm for tplg users */
1169 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1170 route.control = NULL;
1171 else
1172 route.control = elem->control;
1173
1174 /* add route, but keep going if some fail */
1175 snd_soc_dapm_add_routes(dapm, &route, 1);
1176 }
1177
1178 return 0;
1179}
1180
1181static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1182 struct soc_tplg *tplg, int num_kcontrols)
1183{
1184 struct snd_kcontrol_new *kc;
1185 struct soc_mixer_control *sm;
1186 struct snd_soc_tplg_mixer_control *mc;
1187 int i, err;
1188
Axel Lin4ca7deb2015-08-05 22:34:22 +08001189 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001190 if (kc == NULL)
1191 return NULL;
1192
1193 for (i = 0; i < num_kcontrols; i++) {
1194 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1195 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1196 if (sm == NULL)
1197 goto err;
1198
1199 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1200 mc->priv.size);
1201
1202 /* validate kcontrol */
1203 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1204 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1205 goto err_str;
1206
1207 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1208 mc->hdr.name, i);
1209
1210 kc[i].name = mc->hdr.name;
1211 kc[i].private_value = (long)sm;
1212 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1213 kc[i].access = mc->hdr.access;
1214
1215 /* we only support FL/FR channel mapping atm */
1216 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1217 SNDRV_CHMAP_FL);
1218 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1219 SNDRV_CHMAP_FR);
1220 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1221 SNDRV_CHMAP_FL);
1222 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1223 SNDRV_CHMAP_FR);
1224
1225 sm->max = mc->max;
1226 sm->min = mc->min;
1227 sm->invert = mc->invert;
1228 sm->platform_max = mc->platform_max;
1229 sm->dobj.index = tplg->index;
1230 INIT_LIST_HEAD(&sm->dobj.list);
1231
1232 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001233 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001234 if (err) {
1235 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1236 kfree(sm);
1237 continue;
1238 }
1239
1240 /* pass control to driver for optional further init */
1241 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1242 (struct snd_soc_tplg_ctl_hdr *)mc);
1243 if (err < 0) {
1244 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1245 mc->hdr.name);
1246 kfree(sm);
1247 continue;
1248 }
1249 }
1250 return kc;
1251
1252err_str:
1253 kfree(sm);
1254err:
1255 for (--i; i >= 0; i--)
1256 kfree((void *)kc[i].private_value);
1257 kfree(kc);
1258 return NULL;
1259}
1260
1261static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001262 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001263{
1264 struct snd_kcontrol_new *kc;
1265 struct snd_soc_tplg_enum_control *ec;
1266 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001267 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001268
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001269 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001270 if (kc == NULL)
1271 return NULL;
1272
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001273 for (i = 0; i < num_kcontrols; i++) {
1274 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1275 /* validate kcontrol */
1276 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1277 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1278 return NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001279
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001280 se = kzalloc(sizeof(*se), GFP_KERNEL);
1281 if (se == NULL)
1282 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001283
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001284 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001285 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001286
1287 kc[i].name = ec->hdr.name;
1288 kc[i].private_value = (long)se;
1289 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1290 kc[i].access = ec->hdr.access;
1291
1292 /* we only support FL/FR channel mapping atm */
1293 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1294 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1295 SNDRV_CHMAP_FL);
1296 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1297 SNDRV_CHMAP_FR);
1298
1299 se->items = ec->items;
1300 se->mask = ec->mask;
1301 se->dobj.index = tplg->index;
1302
1303 switch (ec->hdr.ops.info) {
1304 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1305 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1306 err = soc_tplg_denum_create_values(se, ec);
1307 if (err < 0) {
1308 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1309 ec->hdr.name);
1310 goto err_se;
1311 }
1312 /* fall through to create texts */
1313 case SND_SOC_TPLG_CTL_ENUM:
1314 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1315 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1316 err = soc_tplg_denum_create_texts(se, ec);
1317 if (err < 0) {
1318 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1319 ec->hdr.name);
1320 goto err_se;
1321 }
1322 break;
1323 default:
1324 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1325 ec->hdr.ops.info, ec->hdr.name);
1326 goto err_se;
1327 }
1328
1329 /* map io handlers */
1330 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1331 if (err) {
1332 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1333 goto err_se;
1334 }
1335
1336 /* pass control to driver for optional further init */
1337 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1338 (struct snd_soc_tplg_ctl_hdr *)ec);
1339 if (err < 0) {
1340 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1341 ec->hdr.name);
1342 goto err_se;
1343 }
1344
1345 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1346 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001347 }
1348
1349 return kc;
1350
1351err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001352 for (; i >= 0; i--) {
1353 /* free values and texts */
1354 se = (struct soc_enum *)kc[i].private_value;
1355 kfree(se->dobj.control.dvalues);
1356 for (j = 0; j < ec->items; j++)
1357 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001358
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001359 kfree(se);
1360 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001361err:
1362 kfree(kc);
1363
1364 return NULL;
1365}
1366
1367static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1368 struct soc_tplg *tplg, int count)
1369{
1370 struct snd_soc_tplg_bytes_control *be;
1371 struct soc_bytes_ext *sbe;
1372 struct snd_kcontrol_new *kc;
1373 int i, err;
1374
Axel Lin4ca7deb2015-08-05 22:34:22 +08001375 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001376 if (!kc)
1377 return NULL;
1378
1379 for (i = 0; i < count; i++) {
1380 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1381
1382 /* validate kcontrol */
1383 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1384 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1385 goto err;
1386
1387 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1388 if (sbe == NULL)
1389 goto err;
1390
1391 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1392 be->priv.size);
1393
1394 dev_dbg(tplg->dev,
1395 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1396 be->hdr.name, be->hdr.access);
1397
Liam Girdwood8a978232015-05-29 19:06:14 +01001398 kc[i].name = be->hdr.name;
1399 kc[i].private_value = (long)sbe;
1400 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1401 kc[i].access = be->hdr.access;
1402
1403 sbe->max = be->max;
1404 INIT_LIST_HEAD(&sbe->dobj.list);
1405
1406 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001407 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001408 if (err) {
1409 soc_control_err(tplg, &be->hdr, be->hdr.name);
1410 kfree(sbe);
1411 continue;
1412 }
1413
1414 /* pass control to driver for optional further init */
1415 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1416 (struct snd_soc_tplg_ctl_hdr *)be);
1417 if (err < 0) {
1418 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1419 be->hdr.name);
1420 kfree(sbe);
1421 continue;
1422 }
1423 }
1424
1425 return kc;
1426
1427err:
1428 for (--i; i >= 0; i--)
1429 kfree((void *)kc[i].private_value);
1430
1431 kfree(kc);
1432 return NULL;
1433}
1434
1435static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1436 struct snd_soc_tplg_dapm_widget *w)
1437{
1438 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1439 struct snd_soc_dapm_widget template, *widget;
1440 struct snd_soc_tplg_ctl_hdr *control_hdr;
1441 struct snd_soc_card *card = tplg->comp->card;
1442 int ret = 0;
1443
1444 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1445 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1446 return -EINVAL;
1447 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1448 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1449 return -EINVAL;
1450
1451 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1452 w->name, w->id);
1453
1454 memset(&template, 0, sizeof(template));
1455
1456 /* map user to kernel widget ID */
1457 template.id = get_widget_id(w->id);
1458 if (template.id < 0)
1459 return template.id;
1460
1461 template.name = kstrdup(w->name, GFP_KERNEL);
1462 if (!template.name)
1463 return -ENOMEM;
1464 template.sname = kstrdup(w->sname, GFP_KERNEL);
1465 if (!template.sname) {
1466 ret = -ENOMEM;
1467 goto err;
1468 }
1469 template.reg = w->reg;
1470 template.shift = w->shift;
1471 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001472 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001473 template.on_val = w->invert ? 0 : 1;
1474 template.off_val = w->invert ? 1 : 0;
1475 template.ignore_suspend = w->ignore_suspend;
1476 template.event_flags = w->event_flags;
1477 template.dobj.index = tplg->index;
1478
1479 tplg->pos +=
1480 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1481 if (w->num_kcontrols == 0) {
1482 template.num_kcontrols = 0;
1483 goto widget;
1484 }
1485
1486 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1487 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1488 w->name, w->num_kcontrols, control_hdr->type);
1489
1490 switch (control_hdr->ops.info) {
1491 case SND_SOC_TPLG_CTL_VOLSW:
1492 case SND_SOC_TPLG_CTL_STROBE:
1493 case SND_SOC_TPLG_CTL_VOLSW_SX:
1494 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1495 case SND_SOC_TPLG_CTL_RANGE:
1496 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1497 template.num_kcontrols = w->num_kcontrols;
1498 template.kcontrol_news =
1499 soc_tplg_dapm_widget_dmixer_create(tplg,
1500 template.num_kcontrols);
1501 if (!template.kcontrol_news) {
1502 ret = -ENOMEM;
1503 goto hdr_err;
1504 }
1505 break;
1506 case SND_SOC_TPLG_CTL_ENUM:
1507 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1508 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1509 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1510 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1511 template.dobj.widget.kcontrol_enum = 1;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001512 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001513 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001514 soc_tplg_dapm_widget_denum_create(tplg,
1515 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001516 if (!template.kcontrol_news) {
1517 ret = -ENOMEM;
1518 goto hdr_err;
1519 }
1520 break;
1521 case SND_SOC_TPLG_CTL_BYTES:
1522 template.num_kcontrols = w->num_kcontrols;
1523 template.kcontrol_news =
1524 soc_tplg_dapm_widget_dbytes_create(tplg,
1525 template.num_kcontrols);
1526 if (!template.kcontrol_news) {
1527 ret = -ENOMEM;
1528 goto hdr_err;
1529 }
1530 break;
1531 default:
1532 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1533 control_hdr->ops.get, control_hdr->ops.put,
1534 control_hdr->ops.info);
1535 ret = -EINVAL;
1536 goto hdr_err;
1537 }
1538
1539widget:
1540 ret = soc_tplg_widget_load(tplg, &template, w);
1541 if (ret < 0)
1542 goto hdr_err;
1543
1544 /* card dapm mutex is held by the core if we are loading topology
1545 * data during sound card init. */
1546 if (card->instantiated)
1547 widget = snd_soc_dapm_new_control(dapm, &template);
1548 else
1549 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1550 if (widget == NULL) {
1551 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1552 w->name);
Wei Yongjun8ae3ea42016-08-10 13:43:12 +00001553 ret = -ENOMEM;
Liam Girdwood8a978232015-05-29 19:06:14 +01001554 goto hdr_err;
1555 }
1556
1557 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1558 widget->dobj.ops = tplg->ops;
1559 widget->dobj.index = tplg->index;
Jeeja KP8ea41672016-05-05 11:19:18 +05301560 kfree(template.sname);
1561 kfree(template.name);
Liam Girdwood8a978232015-05-29 19:06:14 +01001562 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1563 return 0;
1564
1565hdr_err:
1566 kfree(template.sname);
1567err:
1568 kfree(template.name);
1569 return ret;
1570}
1571
1572static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1573 struct snd_soc_tplg_hdr *hdr)
1574{
1575 struct snd_soc_tplg_dapm_widget *widget;
1576 int ret, count = hdr->count, i;
1577
1578 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1579 return 0;
1580
1581 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1582
1583 for (i = 0; i < count; i++) {
1584 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001585 if (widget->size != sizeof(*widget)) {
1586 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1587 return -EINVAL;
1588 }
1589
Liam Girdwood8a978232015-05-29 19:06:14 +01001590 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001591 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001592 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1593 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001594 return ret;
1595 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001596 }
1597
1598 return 0;
1599}
1600
1601static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1602{
1603 struct snd_soc_card *card = tplg->comp->card;
1604 int ret;
1605
1606 /* Card might not have been registered at this point.
1607 * If so, just return success.
1608 */
1609 if (!card || !card->instantiated) {
1610 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1611 "Do not add new widgets now\n");
1612 return 0;
1613 }
1614
1615 ret = snd_soc_dapm_new_widgets(card);
1616 if (ret < 0)
1617 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1618 ret);
1619
1620 return 0;
1621}
1622
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001623static void set_stream_info(struct snd_soc_pcm_stream *stream,
1624 struct snd_soc_tplg_stream_caps *caps)
1625{
1626 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1627 stream->channels_min = caps->channels_min;
1628 stream->channels_max = caps->channels_max;
1629 stream->rates = caps->rates;
1630 stream->rate_min = caps->rate_min;
1631 stream->rate_max = caps->rate_max;
1632 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001633 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001634}
1635
Mengdong Lin0038be92016-07-26 14:32:37 +08001636static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1637 unsigned int flag_mask, unsigned int flags)
1638{
1639 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1640 dai_drv->symmetric_rates =
1641 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1642
1643 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1644 dai_drv->symmetric_channels =
1645 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1646 1 : 0;
1647
1648 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1649 dai_drv->symmetric_samplebits =
1650 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1651 1 : 0;
1652}
1653
Mengdong Lin64527e82016-01-15 16:13:28 +08001654static int soc_tplg_dai_create(struct soc_tplg *tplg,
1655 struct snd_soc_tplg_pcm *pcm)
1656{
1657 struct snd_soc_dai_driver *dai_drv;
1658 struct snd_soc_pcm_stream *stream;
1659 struct snd_soc_tplg_stream_caps *caps;
1660 int ret;
1661
1662 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1663 if (dai_drv == NULL)
1664 return -ENOMEM;
1665
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001666 if (strlen(pcm->dai_name))
1667 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001668 dai_drv->id = pcm->dai_id;
1669
1670 if (pcm->playback) {
1671 stream = &dai_drv->playback;
1672 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001673 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001674 }
1675
1676 if (pcm->capture) {
1677 stream = &dai_drv->capture;
1678 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001679 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001680 }
1681
1682 /* pass control to component driver for optional further init */
1683 ret = soc_tplg_dai_load(tplg, dai_drv);
1684 if (ret < 0) {
1685 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1686 kfree(dai_drv);
1687 return ret;
1688 }
1689
1690 dai_drv->dobj.index = tplg->index;
1691 dai_drv->dobj.ops = tplg->ops;
1692 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1693 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1694
1695 /* register the DAI to the component */
1696 return snd_soc_register_dai(tplg->comp, dai_drv);
1697}
1698
Mengdong Lin717a8e72016-11-03 01:03:34 +08001699static void set_link_flags(struct snd_soc_dai_link *link,
1700 unsigned int flag_mask, unsigned int flags)
1701{
1702 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1703 link->symmetric_rates =
1704 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1705
1706 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1707 link->symmetric_channels =
1708 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1709 1 : 0;
1710
1711 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1712 link->symmetric_samplebits =
1713 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1714 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001715
1716 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1717 link->ignore_suspend =
1718 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1719 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001720}
1721
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001722/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001723static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001724 struct snd_soc_tplg_pcm *pcm)
1725{
1726 struct snd_soc_dai_link *link;
1727 int ret;
1728
1729 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1730 if (link == NULL)
1731 return -ENOMEM;
1732
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001733 if (strlen(pcm->pcm_name)) {
1734 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1735 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1736 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001737 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001738
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001739 if (strlen(pcm->dai_name))
1740 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1741
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001742 link->codec_name = "snd-soc-dummy";
1743 link->codec_dai_name = "snd-soc-dummy-dai";
1744
1745 /* enable DPCM */
1746 link->dynamic = 1;
1747 link->dpcm_playback = pcm->playback;
1748 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001749 if (pcm->flag_mask)
1750 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001751
Mengdong Linacfc7d42016-01-15 16:13:37 +08001752 /* pass control to component driver for optional further init */
1753 ret = soc_tplg_dai_link_load(tplg, link);
1754 if (ret < 0) {
1755 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1756 kfree(link);
1757 return ret;
1758 }
1759
1760 link->dobj.index = tplg->index;
1761 link->dobj.ops = tplg->ops;
1762 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1763 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1764
1765 snd_soc_add_dai_link(tplg->comp->card, link);
1766 return 0;
1767}
1768
1769/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001770static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1771 struct snd_soc_tplg_pcm *pcm)
1772{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001773 int ret;
1774
1775 ret = soc_tplg_dai_create(tplg, pcm);
1776 if (ret < 0)
1777 return ret;
1778
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001779 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001780}
1781
Mengdong Lin55726dc2016-11-03 01:00:16 +08001782/* copy stream caps from the old version 4 of source */
1783static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1784 struct snd_soc_tplg_stream_caps_v4 *src)
1785{
1786 dest->size = sizeof(*dest);
1787 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1788 dest->formats = src->formats;
1789 dest->rates = src->rates;
1790 dest->rate_min = src->rate_min;
1791 dest->rate_max = src->rate_max;
1792 dest->channels_min = src->channels_min;
1793 dest->channels_max = src->channels_max;
1794 dest->periods_min = src->periods_min;
1795 dest->periods_max = src->periods_max;
1796 dest->period_size_min = src->period_size_min;
1797 dest->period_size_max = src->period_size_max;
1798 dest->buffer_size_min = src->buffer_size_min;
1799 dest->buffer_size_max = src->buffer_size_max;
1800}
1801
1802/**
1803 * pcm_new_ver - Create the new version of PCM from the old version.
1804 * @tplg: topology context
1805 * @src: older version of pcm as a source
1806 * @pcm: latest version of pcm created from the source
1807 *
1808 * Support from vesion 4. User should free the returned pcm manually.
1809 */
1810static int pcm_new_ver(struct soc_tplg *tplg,
1811 struct snd_soc_tplg_pcm *src,
1812 struct snd_soc_tplg_pcm **pcm)
1813{
1814 struct snd_soc_tplg_pcm *dest;
1815 struct snd_soc_tplg_pcm_v4 *src_v4;
1816 int i;
1817
1818 *pcm = NULL;
1819
1820 if (src->size != sizeof(*src_v4)) {
1821 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1822 return -EINVAL;
1823 }
1824
1825 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1826 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1827 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1828 if (!dest)
1829 return -ENOMEM;
1830
1831 dest->size = sizeof(*dest); /* size of latest abi version */
1832 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1833 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1834 dest->pcm_id = src_v4->pcm_id;
1835 dest->dai_id = src_v4->dai_id;
1836 dest->playback = src_v4->playback;
1837 dest->capture = src_v4->capture;
1838 dest->compress = src_v4->compress;
1839 dest->num_streams = src_v4->num_streams;
1840 for (i = 0; i < dest->num_streams; i++)
1841 memcpy(&dest->stream[i], &src_v4->stream[i],
1842 sizeof(struct snd_soc_tplg_stream));
1843
1844 for (i = 0; i < 2; i++)
1845 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1846
1847 *pcm = dest;
1848 return 0;
1849}
1850
Mengdong Lin64527e82016-01-15 16:13:28 +08001851static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001852 struct snd_soc_tplg_hdr *hdr)
1853{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001854 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001855 int count = hdr->count;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001856 int i, err;
1857 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001858
1859 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1860 return 0;
1861
Mengdong Lin55726dc2016-11-03 01:00:16 +08001862 /* check the element size and count */
1863 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1864 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1865 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1866 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1867 pcm->size);
1868 return -EINVAL;
1869 }
1870
Liam Girdwood8a978232015-05-29 19:06:14 +01001871 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08001872 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001873 hdr->payload_size, "PCM DAI")) {
1874 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1875 count);
1876 return -EINVAL;
1877 }
1878
Mengdong Lin64527e82016-01-15 16:13:28 +08001879 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001880 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1881
1882 /* check ABI version by size, create a new version of pcm
1883 * if abi not match.
1884 */
1885 if (pcm->size == sizeof(*pcm)) {
1886 abi_match = true;
1887 _pcm = pcm;
1888 } else {
1889 abi_match = false;
1890 err = pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001891 }
1892
Mengdong Lin55726dc2016-11-03 01:00:16 +08001893 /* create the FE DAIs and DAI links */
1894 soc_tplg_pcm_create(tplg, _pcm);
1895
Mengdong Lin717a8e72016-11-03 01:03:34 +08001896 /* offset by version-specific struct size and
1897 * real priv data size
1898 */
1899 tplg->pos += pcm->size + _pcm->priv.size;
1900
Mengdong Lin55726dc2016-11-03 01:00:16 +08001901 if (!abi_match)
1902 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08001903 }
1904
Liam Girdwood8a978232015-05-29 19:06:14 +01001905 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001906
Liam Girdwood8a978232015-05-29 19:06:14 +01001907 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001908}
1909
Mengdong Lin593d9e52016-11-03 01:04:27 +08001910/**
1911 * set_link_hw_format - Set the HW audio format of the physical DAI link.
1912 * @tplg: topology context
1913 * @cfg: physical link configs.
1914 *
1915 * Topology context contains a list of supported HW formats (configs) and
1916 * a default format ID for the physical link. This function will use this
1917 * default ID to choose the HW format to set the link's DAI format for init.
1918 */
1919static void set_link_hw_format(struct snd_soc_dai_link *link,
1920 struct snd_soc_tplg_link_config *cfg)
1921{
1922 struct snd_soc_tplg_hw_config *hw_config;
1923 unsigned char bclk_master, fsync_master;
1924 unsigned char invert_bclk, invert_fsync;
1925 int i;
1926
1927 for (i = 0; i < cfg->num_hw_configs; i++) {
1928 hw_config = &cfg->hw_config[i];
1929 if (hw_config->id != cfg->default_hw_config_id)
1930 continue;
1931
1932 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
1933
1934 /* clock signal polarity */
1935 invert_bclk = hw_config->invert_bclk;
1936 invert_fsync = hw_config->invert_fsync;
1937 if (!invert_bclk && !invert_fsync)
1938 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1939 else if (!invert_bclk && invert_fsync)
1940 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1941 else if (invert_bclk && !invert_fsync)
1942 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1943 else
1944 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1945
1946 /* clock masters */
1947 bclk_master = hw_config->bclk_master;
1948 fsync_master = hw_config->fsync_master;
1949 if (!bclk_master && !fsync_master)
1950 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1951 else if (bclk_master && !fsync_master)
1952 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1953 else if (!bclk_master && fsync_master)
1954 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1955 else
1956 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1957 }
1958}
1959
1960/**
1961 * link_new_ver - Create a new physical link config from the old
1962 * version of source.
1963 * @toplogy: topology context
1964 * @src: old version of phyical link config as a source
1965 * @link: latest version of physical link config created from the source
1966 *
1967 * Support from vesion 4. User need free the returned link config manually.
1968 */
1969static int link_new_ver(struct soc_tplg *tplg,
1970 struct snd_soc_tplg_link_config *src,
1971 struct snd_soc_tplg_link_config **link)
1972{
1973 struct snd_soc_tplg_link_config *dest;
1974 struct snd_soc_tplg_link_config_v4 *src_v4;
1975 int i;
1976
1977 *link = NULL;
1978
1979 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
1980 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
1981 return -EINVAL;
1982 }
1983
1984 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
1985
1986 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
1987 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1988 if (!dest)
1989 return -ENOMEM;
1990
1991 dest->size = sizeof(*dest);
1992 dest->id = src_v4->id;
1993 dest->num_streams = src_v4->num_streams;
1994 for (i = 0; i < dest->num_streams; i++)
1995 memcpy(&dest->stream[i], &src_v4->stream[i],
1996 sizeof(struct snd_soc_tplg_stream));
1997
1998 *link = dest;
1999 return 0;
2000}
2001
2002/* Find and configure an existing physical DAI link */
2003static int soc_tplg_link_config(struct soc_tplg *tplg,
2004 struct snd_soc_tplg_link_config *cfg)
2005{
2006 struct snd_soc_dai_link *link;
2007 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002008 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002009 int ret;
2010
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002011 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2012 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2013 return -EINVAL;
2014 else if (len)
2015 name = cfg->name;
2016 else
2017 name = NULL;
2018
2019 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2020 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2021 return -EINVAL;
2022 else if (len)
2023 stream_name = cfg->stream_name;
2024 else
2025 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002026
2027 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2028 name, stream_name);
2029 if (!link) {
2030 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2031 name, cfg->id);
2032 return -EINVAL;
2033 }
2034
2035 /* hw format */
2036 if (cfg->num_hw_configs)
2037 set_link_hw_format(link, cfg);
2038
2039 /* flags */
2040 if (cfg->flag_mask)
2041 set_link_flags(link, cfg->flag_mask, cfg->flags);
2042
2043 /* pass control to component driver for optional further init */
2044 ret = soc_tplg_dai_link_load(tplg, link);
2045 if (ret < 0) {
2046 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2047 return ret;
2048 }
2049
2050 return 0;
2051}
2052
2053
2054/* Load physical link config elements from the topology context */
2055static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2056 struct snd_soc_tplg_hdr *hdr)
2057{
2058 struct snd_soc_tplg_link_config *link, *_link;
2059 int count = hdr->count;
2060 int i, ret;
2061 bool abi_match;
2062
2063 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2064 tplg->pos += hdr->size + hdr->payload_size;
2065 return 0;
2066 };
2067
2068 /* check the element size and count */
2069 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2070 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2071 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2072 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2073 link->size);
2074 return -EINVAL;
2075 }
2076
2077 if (soc_tplg_check_elem_count(tplg,
2078 link->size, count,
2079 hdr->payload_size, "physical link config")) {
2080 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2081 count);
2082 return -EINVAL;
2083 }
2084
2085 /* config physical DAI links */
2086 for (i = 0; i < count; i++) {
2087 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2088 if (link->size == sizeof(*link)) {
2089 abi_match = true;
2090 _link = link;
2091 } else {
2092 abi_match = false;
2093 ret = link_new_ver(tplg, link, &_link);
2094 if (ret < 0)
2095 return ret;
2096 }
2097
2098 ret = soc_tplg_link_config(tplg, _link);
2099 if (ret < 0)
2100 return ret;
2101
2102 /* offset by version-specific struct size and
2103 * real priv data size
2104 */
2105 tplg->pos += link->size + _link->priv.size;
2106
2107 if (!abi_match)
2108 kfree(_link); /* free the duplicated one */
2109 }
2110
2111 return 0;
2112}
2113
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002114/**
2115 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002116 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002117 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002118 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002119 * The physical dai should already be registered by the platform driver.
2120 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002121 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002122static int soc_tplg_dai_config(struct soc_tplg *tplg,
2123 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002124{
2125 struct snd_soc_dai_link_component dai_component = {0};
2126 struct snd_soc_dai *dai;
2127 struct snd_soc_dai_driver *dai_drv;
2128 struct snd_soc_pcm_stream *stream;
2129 struct snd_soc_tplg_stream_caps *caps;
2130 int ret;
2131
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002132 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002133 dai = snd_soc_find_dai(&dai_component);
2134 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002135 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2136 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002137 return -EINVAL;
2138 }
2139
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002140 if (d->dai_id != dai->id) {
2141 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2142 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002143 return -EINVAL;
2144 }
2145
2146 dai_drv = dai->driver;
2147 if (!dai_drv)
2148 return -EINVAL;
2149
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002150 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002151 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002152 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002153 set_stream_info(stream, caps);
2154 }
2155
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002156 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002157 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002158 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002159 set_stream_info(stream, caps);
2160 }
2161
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002162 if (d->flag_mask)
2163 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002164
2165 /* pass control to component driver for optional further init */
2166 ret = soc_tplg_dai_load(tplg, dai_drv);
2167 if (ret < 0) {
2168 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2169 return ret;
2170 }
2171
2172 return 0;
2173}
2174
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002175/* load physical DAI elements */
2176static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2177 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002178{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002179 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002180 int count = hdr->count;
2181 int i;
2182
2183 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2184 return 0;
2185
2186 /* config the existing BE DAIs */
2187 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002188 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2189 if (dai->size != sizeof(*dai)) {
2190 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002191 return -EINVAL;
2192 }
2193
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002194 soc_tplg_dai_config(tplg, dai);
2195 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002196 }
2197
2198 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2199 return 0;
2200}
2201
Mengdong Lin583958f2016-10-11 14:36:42 +08002202/**
2203 * manifest_new_ver - Create a new version of manifest from the old version
2204 * of source.
2205 * @toplogy: topology context
2206 * @src: old version of manifest as a source
2207 * @manifest: latest version of manifest created from the source
2208 *
2209 * Support from vesion 4. Users need free the returned manifest manually.
2210 */
2211static int manifest_new_ver(struct soc_tplg *tplg,
2212 struct snd_soc_tplg_manifest *src,
2213 struct snd_soc_tplg_manifest **manifest)
2214{
2215 struct snd_soc_tplg_manifest *dest;
2216 struct snd_soc_tplg_manifest_v4 *src_v4;
2217
2218 *manifest = NULL;
2219
2220 if (src->size != sizeof(*src_v4)) {
2221 dev_err(tplg->dev, "ASoC: invalid manifest size\n");
2222 return -EINVAL;
2223 }
2224
2225 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2226
2227 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2228 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2229 if (!dest)
2230 return -ENOMEM;
2231
2232 dest->size = sizeof(*dest); /* size of latest abi version */
2233 dest->control_elems = src_v4->control_elems;
2234 dest->widget_elems = src_v4->widget_elems;
2235 dest->graph_elems = src_v4->graph_elems;
2236 dest->pcm_elems = src_v4->pcm_elems;
2237 dest->dai_link_elems = src_v4->dai_link_elems;
2238 dest->priv.size = src_v4->priv.size;
2239 if (dest->priv.size)
2240 memcpy(dest->priv.data, src_v4->priv.data,
2241 src_v4->priv.size);
2242
2243 *manifest = dest;
2244 return 0;
2245}
Mengdong Lin0038be92016-07-26 14:32:37 +08002246
Liam Girdwood8a978232015-05-29 19:06:14 +01002247static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002248 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002249{
Mengdong Lin583958f2016-10-11 14:36:42 +08002250 struct snd_soc_tplg_manifest *manifest, *_manifest;
2251 bool abi_match;
2252 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002253
2254 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2255 return 0;
2256
2257 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002258
2259 /* check ABI version by size, create a new manifest if abi not match */
2260 if (manifest->size == sizeof(*manifest)) {
2261 abi_match = true;
2262 _manifest = manifest;
2263 } else {
2264 abi_match = false;
2265 err = manifest_new_ver(tplg, manifest, &_manifest);
2266 if (err < 0)
2267 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002268 }
2269
Mengdong Lin583958f2016-10-11 14:36:42 +08002270 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002271 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Mengdong Lin583958f2016-10-11 14:36:42 +08002272 return tplg->ops->manifest(tplg->comp, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002273
Mengdong Lin583958f2016-10-11 14:36:42 +08002274 if (!abi_match) /* free the duplicated one */
2275 kfree(_manifest);
2276
Liam Girdwood8a978232015-05-29 19:06:14 +01002277 return 0;
2278}
2279
2280/* validate header magic, size and type */
2281static int soc_valid_header(struct soc_tplg *tplg,
2282 struct snd_soc_tplg_hdr *hdr)
2283{
2284 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2285 return 0;
2286
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002287 if (hdr->size != sizeof(*hdr)) {
2288 dev_err(tplg->dev,
2289 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2290 hdr->type, soc_tplg_get_hdr_offset(tplg),
2291 tplg->fw->size);
2292 return -EINVAL;
2293 }
2294
Liam Girdwood8a978232015-05-29 19:06:14 +01002295 /* big endian firmware objects not supported atm */
2296 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2297 dev_err(tplg->dev,
2298 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2299 tplg->pass, hdr->magic,
2300 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2301 return -EINVAL;
2302 }
2303
2304 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2305 dev_err(tplg->dev,
2306 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2307 tplg->pass, hdr->magic,
2308 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2309 return -EINVAL;
2310 }
2311
Mengdong Lin288b8da2016-11-03 01:03:17 +08002312 /* Support ABI from version 4 */
2313 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2314 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002315 dev_err(tplg->dev,
2316 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2317 tplg->pass, hdr->abi,
2318 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2319 tplg->fw->size);
2320 return -EINVAL;
2321 }
2322
2323 if (hdr->payload_size == 0) {
2324 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2325 soc_tplg_get_hdr_offset(tplg));
2326 return -EINVAL;
2327 }
2328
2329 if (tplg->pass == hdr->type)
2330 dev_dbg(tplg->dev,
2331 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2332 hdr->payload_size, hdr->type, hdr->version,
2333 hdr->vendor_type, tplg->pass);
2334
2335 return 1;
2336}
2337
2338/* check header type and call appropriate handler */
2339static int soc_tplg_load_header(struct soc_tplg *tplg,
2340 struct snd_soc_tplg_hdr *hdr)
2341{
2342 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2343
2344 /* check for matching ID */
2345 if (hdr->index != tplg->req_index &&
2346 hdr->index != SND_SOC_TPLG_INDEX_ALL)
2347 return 0;
2348
2349 tplg->index = hdr->index;
2350
2351 switch (hdr->type) {
2352 case SND_SOC_TPLG_TYPE_MIXER:
2353 case SND_SOC_TPLG_TYPE_ENUM:
2354 case SND_SOC_TPLG_TYPE_BYTES:
2355 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2356 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2357 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2358 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2359 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2360 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002361 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002362 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002363 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002364 case SND_SOC_TPLG_TYPE_DAI_LINK:
2365 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2366 /* physical link configurations */
2367 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002368 case SND_SOC_TPLG_TYPE_MANIFEST:
2369 return soc_tplg_manifest_load(tplg, hdr);
2370 default:
2371 /* bespoke vendor data object */
2372 return soc_tplg_vendor_load(tplg, hdr);
2373 }
2374
2375 return 0;
2376}
2377
2378/* process the topology file headers */
2379static int soc_tplg_process_headers(struct soc_tplg *tplg)
2380{
2381 struct snd_soc_tplg_hdr *hdr;
2382 int ret;
2383
2384 tplg->pass = SOC_TPLG_PASS_START;
2385
2386 /* process the header types from start to end */
2387 while (tplg->pass <= SOC_TPLG_PASS_END) {
2388
2389 tplg->hdr_pos = tplg->fw->data;
2390 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2391
2392 while (!soc_tplg_is_eof(tplg)) {
2393
2394 /* make sure header is valid before loading */
2395 ret = soc_valid_header(tplg, hdr);
2396 if (ret < 0)
2397 return ret;
2398 else if (ret == 0)
2399 break;
2400
2401 /* load the header object */
2402 ret = soc_tplg_load_header(tplg, hdr);
2403 if (ret < 0)
2404 return ret;
2405
2406 /* goto next header */
2407 tplg->hdr_pos += hdr->payload_size +
2408 sizeof(struct snd_soc_tplg_hdr);
2409 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2410 }
2411
2412 /* next data type pass */
2413 tplg->pass++;
2414 }
2415
2416 /* signal DAPM we are complete */
2417 ret = soc_tplg_dapm_complete(tplg);
2418 if (ret < 0)
2419 dev_err(tplg->dev,
2420 "ASoC: failed to initialise DAPM from Firmware\n");
2421
2422 return ret;
2423}
2424
2425static int soc_tplg_load(struct soc_tplg *tplg)
2426{
2427 int ret;
2428
2429 ret = soc_tplg_process_headers(tplg);
2430 if (ret == 0)
2431 soc_tplg_complete(tplg);
2432
2433 return ret;
2434}
2435
2436/* load audio component topology from "firmware" file */
2437int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2438 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2439{
2440 struct soc_tplg tplg;
2441
2442 /* setup parsing context */
2443 memset(&tplg, 0, sizeof(tplg));
2444 tplg.fw = fw;
2445 tplg.dev = comp->dev;
2446 tplg.comp = comp;
2447 tplg.ops = ops;
2448 tplg.req_index = id;
2449 tplg.io_ops = ops->io_ops;
2450 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002451 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2452 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002453
2454 return soc_tplg_load(&tplg);
2455}
2456EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2457
2458/* remove this dynamic widget */
2459void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2460{
2461 /* make sure we are a widget */
2462 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2463 return;
2464
2465 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2466}
2467EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2468
2469/* remove all dynamic widgets from this DAPM context */
2470void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2471 u32 index)
2472{
2473 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002474
2475 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2476
2477 /* make sure we are a widget with correct context */
2478 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2479 continue;
2480
2481 /* match ID */
2482 if (w->dobj.index != index &&
2483 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2484 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002485 /* check and free and dynamic widget kcontrols */
2486 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002487 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002488 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002489 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002490}
2491EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2492
2493/* remove dynamic controls from the component driver */
2494int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2495{
2496 struct snd_soc_dobj *dobj, *next_dobj;
2497 int pass = SOC_TPLG_PASS_END;
2498
2499 /* process the header types from end to start */
2500 while (pass >= SOC_TPLG_PASS_START) {
2501
2502 /* remove mixer controls */
2503 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2504 list) {
2505
2506 /* match index */
2507 if (dobj->index != index &&
2508 dobj->index != SND_SOC_TPLG_INDEX_ALL)
2509 continue;
2510
2511 switch (dobj->type) {
2512 case SND_SOC_DOBJ_MIXER:
2513 remove_mixer(comp, dobj, pass);
2514 break;
2515 case SND_SOC_DOBJ_ENUM:
2516 remove_enum(comp, dobj, pass);
2517 break;
2518 case SND_SOC_DOBJ_BYTES:
2519 remove_bytes(comp, dobj, pass);
2520 break;
2521 case SND_SOC_DOBJ_WIDGET:
2522 remove_widget(comp, dobj, pass);
2523 break;
2524 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002525 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002526 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002527 case SND_SOC_DOBJ_DAI_LINK:
2528 remove_link(comp, dobj, pass);
2529 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002530 default:
2531 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2532 dobj->type);
2533 break;
2534 }
2535 }
2536 pass--;
2537 }
2538
2539 /* let caller know if FW can be freed when no objects are left */
2540 return !list_empty(&comp->dobj_list);
2541}
2542EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);