blob: 0eb01e8155f1abe4c5ebd00550d5921d28c31a01 [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
Liam Girdwood8a978232015-05-29 19:06:14 +010051
52#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
Mengdong Lin1a8e7fa2015-08-10 22:48:30 +080053#define SOC_TPLG_PASS_END SOC_TPLG_PASS_PINS
Liam Girdwood8a978232015-05-29 19:06:14 +010054
55struct soc_tplg {
56 const struct firmware *fw;
57
58 /* runtime FW parsing */
59 const u8 *pos; /* read postion */
60 const u8 *hdr_pos; /* header position */
61 unsigned int pass; /* pass number */
62
63 /* component caller */
64 struct device *dev;
65 struct snd_soc_component *comp;
66 u32 index; /* current block index */
67 u32 req_index; /* required index, only loaded/free matching blocks */
68
Mengdong Lin88a17d82015-08-18 18:11:51 +080069 /* vendor specific kcontrol operations */
Liam Girdwood8a978232015-05-29 19:06:14 +010070 const struct snd_soc_tplg_kcontrol_ops *io_ops;
71 int io_ops_count;
72
Mengdong Lin1a3232d2015-08-18 18:12:20 +080073 /* vendor specific bytes ext handlers, for TLV bytes controls */
74 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
75 int bytes_ext_ops_count;
76
Liam Girdwood8a978232015-05-29 19:06:14 +010077 /* optional fw loading callbacks to component drivers */
78 struct snd_soc_tplg_ops *ops;
79};
80
81static int soc_tplg_process_headers(struct soc_tplg *tplg);
82static void soc_tplg_complete(struct soc_tplg *tplg);
83struct snd_soc_dapm_widget *
84snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
85 const struct snd_soc_dapm_widget *widget);
86struct snd_soc_dapm_widget *
87snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
88 const struct snd_soc_dapm_widget *widget);
89
90/* check we dont overflow the data for this control chunk */
91static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
92 unsigned int count, size_t bytes, const char *elem_type)
93{
94 const u8 *end = tplg->pos + elem_size * count;
95
96 if (end > tplg->fw->data + tplg->fw->size) {
97 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
98 elem_type);
99 return -EINVAL;
100 }
101
102 /* check there is enough room in chunk for control.
103 extra bytes at the end of control are for vendor data here */
104 if (elem_size * count > bytes) {
105 dev_err(tplg->dev,
106 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
107 elem_type, count, elem_size, bytes);
108 return -EINVAL;
109 }
110
111 return 0;
112}
113
114static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
115{
116 const u8 *end = tplg->hdr_pos;
117
118 if (end >= tplg->fw->data + tplg->fw->size)
119 return 1;
120 return 0;
121}
122
123static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
124{
125 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
126}
127
128static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
129{
130 return (unsigned long)(tplg->pos - tplg->fw->data);
131}
132
133/* mapping of Kcontrol types and associated operations. */
134static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
135 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
136 snd_soc_put_volsw, snd_soc_info_volsw},
137 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
138 snd_soc_put_volsw_sx, NULL},
139 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
140 snd_soc_put_enum_double, snd_soc_info_enum_double},
141 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
142 snd_soc_put_enum_double, NULL},
143 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
144 snd_soc_bytes_put, snd_soc_bytes_info},
145 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
146 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
147 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
148 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
149 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
150 snd_soc_put_strobe, NULL},
151 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530152 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100153 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
154 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
155 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
156 snd_soc_dapm_put_enum_double, NULL},
157 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
158 snd_soc_dapm_put_enum_double, NULL},
159 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
160 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
161};
162
163struct soc_tplg_map {
164 int uid;
165 int kid;
166};
167
168/* mapping of widget types from UAPI IDs to kernel IDs */
169static const struct soc_tplg_map dapm_map[] = {
170 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
171 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
172 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
173 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
174 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
175 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
176 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
177 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
178 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
179 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
180 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
181 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
182 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
183 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
184 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
185 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
186};
187
188static int tplc_chan_get_reg(struct soc_tplg *tplg,
189 struct snd_soc_tplg_channel *chan, int map)
190{
191 int i;
192
193 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
194 if (chan[i].id == map)
195 return chan[i].reg;
196 }
197
198 return -EINVAL;
199}
200
201static int tplc_chan_get_shift(struct soc_tplg *tplg,
202 struct snd_soc_tplg_channel *chan, int map)
203{
204 int i;
205
206 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
207 if (chan[i].id == map)
208 return chan[i].shift;
209 }
210
211 return -EINVAL;
212}
213
214static int get_widget_id(int tplg_type)
215{
216 int i;
217
218 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
219 if (tplg_type == dapm_map[i].uid)
220 return dapm_map[i].kid;
221 }
222
223 return -EINVAL;
224}
225
226static enum snd_soc_dobj_type get_dobj_mixer_type(
227 struct snd_soc_tplg_ctl_hdr *control_hdr)
228{
229 if (control_hdr == NULL)
230 return SND_SOC_DOBJ_NONE;
231
232 switch (control_hdr->ops.info) {
233 case SND_SOC_TPLG_CTL_VOLSW:
234 case SND_SOC_TPLG_CTL_VOLSW_SX:
235 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
236 case SND_SOC_TPLG_CTL_RANGE:
237 case SND_SOC_TPLG_CTL_STROBE:
238 return SND_SOC_DOBJ_MIXER;
239 case SND_SOC_TPLG_CTL_ENUM:
240 case SND_SOC_TPLG_CTL_ENUM_VALUE:
241 return SND_SOC_DOBJ_ENUM;
242 case SND_SOC_TPLG_CTL_BYTES:
243 return SND_SOC_DOBJ_BYTES;
244 default:
245 return SND_SOC_DOBJ_NONE;
246 }
247}
248
249static enum snd_soc_dobj_type get_dobj_type(struct snd_soc_tplg_hdr *hdr,
250 struct snd_soc_tplg_ctl_hdr *control_hdr)
251{
252 switch (hdr->type) {
253 case SND_SOC_TPLG_TYPE_MIXER:
254 return get_dobj_mixer_type(control_hdr);
255 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
256 case SND_SOC_TPLG_TYPE_MANIFEST:
257 return SND_SOC_DOBJ_NONE;
258 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
259 return SND_SOC_DOBJ_WIDGET;
260 case SND_SOC_TPLG_TYPE_DAI_LINK:
261 return SND_SOC_DOBJ_DAI_LINK;
262 case SND_SOC_TPLG_TYPE_PCM:
263 return SND_SOC_DOBJ_PCM;
264 case SND_SOC_TPLG_TYPE_CODEC_LINK:
265 return SND_SOC_DOBJ_CODEC_LINK;
266 default:
267 return SND_SOC_DOBJ_NONE;
268 }
269}
270
271static inline void soc_bind_err(struct soc_tplg *tplg,
272 struct snd_soc_tplg_ctl_hdr *hdr, int index)
273{
274 dev_err(tplg->dev,
275 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
276 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
277 soc_tplg_get_offset(tplg));
278}
279
280static inline void soc_control_err(struct soc_tplg *tplg,
281 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
282{
283 dev_err(tplg->dev,
284 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
285 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
286 soc_tplg_get_offset(tplg));
287}
288
289/* pass vendor data to component driver for processing */
290static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
291 struct snd_soc_tplg_hdr *hdr)
292{
293 int ret = 0;
294
295 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
296 ret = tplg->ops->vendor_load(tplg->comp, hdr);
297 else {
298 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
299 hdr->vendor_type);
300 return -EINVAL;
301 }
302
303 if (ret < 0)
304 dev_err(tplg->dev,
305 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
306 soc_tplg_get_hdr_offset(tplg),
307 soc_tplg_get_hdr_offset(tplg),
308 hdr->type, hdr->vendor_type);
309 return ret;
310}
311
312/* pass vendor data to component driver for processing */
313static int soc_tplg_vendor_load(struct soc_tplg *tplg,
314 struct snd_soc_tplg_hdr *hdr)
315{
316 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
317 return 0;
318
319 return soc_tplg_vendor_load_(tplg, hdr);
320}
321
322/* optionally pass new dynamic widget to component driver. This is mainly for
323 * external widgets where we can assign private data/ops */
324static int soc_tplg_widget_load(struct soc_tplg *tplg,
325 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
326{
327 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
328 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
329
330 return 0;
331}
332
Mengdong Lin64527e82016-01-15 16:13:28 +0800333/* pass DAI configurations to component driver for extra intialization */
334static int soc_tplg_dai_load(struct soc_tplg *tplg,
335 struct snd_soc_dai_driver *dai_drv)
Liam Girdwood8a978232015-05-29 19:06:14 +0100336{
Mengdong Lin64527e82016-01-15 16:13:28 +0800337 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
338 return tplg->ops->dai_load(tplg->comp, dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100339
340 return 0;
341}
342
Mengdong Linacfc7d42016-01-15 16:13:37 +0800343/* pass link configurations to component driver for extra intialization */
344static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
345 struct snd_soc_dai_link *link)
346{
347 if (tplg->comp && tplg->ops && tplg->ops->link_load)
348 return tplg->ops->link_load(tplg->comp, link);
349
350 return 0;
351}
352
Liam Girdwood8a978232015-05-29 19:06:14 +0100353/* tell the component driver that all firmware has been loaded in this request */
354static void soc_tplg_complete(struct soc_tplg *tplg)
355{
356 if (tplg->comp && tplg->ops && tplg->ops->complete)
357 tplg->ops->complete(tplg->comp);
358}
359
360/* add a dynamic kcontrol */
361static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
362 const struct snd_kcontrol_new *control_new, const char *prefix,
363 void *data, struct snd_kcontrol **kcontrol)
364{
365 int err;
366
367 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
368 if (*kcontrol == NULL) {
369 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
370 control_new->name);
371 return -ENOMEM;
372 }
373
374 err = snd_ctl_add(card, *kcontrol);
375 if (err < 0) {
376 dev_err(dev, "ASoC: Failed to add %s: %d\n",
377 control_new->name, err);
378 return err;
379 }
380
381 return 0;
382}
383
384/* add a dynamic kcontrol for component driver */
385static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
386 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
387{
388 struct snd_soc_component *comp = tplg->comp;
389
390 return soc_tplg_add_dcontrol(comp->card->snd_card,
391 comp->dev, k, NULL, comp, kcontrol);
392}
393
394/* remove a mixer kcontrol */
395static void remove_mixer(struct snd_soc_component *comp,
396 struct snd_soc_dobj *dobj, int pass)
397{
398 struct snd_card *card = comp->card->snd_card;
399 struct soc_mixer_control *sm =
400 container_of(dobj, struct soc_mixer_control, dobj);
401 const unsigned int *p = NULL;
402
403 if (pass != SOC_TPLG_PASS_MIXER)
404 return;
405
406 if (dobj->ops && dobj->ops->control_unload)
407 dobj->ops->control_unload(comp, dobj);
408
409 if (sm->dobj.control.kcontrol->tlv.p)
410 p = sm->dobj.control.kcontrol->tlv.p;
411 snd_ctl_remove(card, sm->dobj.control.kcontrol);
412 list_del(&sm->dobj.list);
413 kfree(sm);
414 kfree(p);
415}
416
417/* remove an enum kcontrol */
418static void remove_enum(struct snd_soc_component *comp,
419 struct snd_soc_dobj *dobj, int pass)
420{
421 struct snd_card *card = comp->card->snd_card;
422 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
423 int i;
424
425 if (pass != SOC_TPLG_PASS_MIXER)
426 return;
427
428 if (dobj->ops && dobj->ops->control_unload)
429 dobj->ops->control_unload(comp, dobj);
430
431 snd_ctl_remove(card, se->dobj.control.kcontrol);
432 list_del(&se->dobj.list);
433
434 kfree(se->dobj.control.dvalues);
435 for (i = 0; i < se->items; i++)
436 kfree(se->dobj.control.dtexts[i]);
437 kfree(se);
438}
439
440/* remove a byte kcontrol */
441static void remove_bytes(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 soc_bytes_ext *sb =
446 container_of(dobj, struct soc_bytes_ext, dobj);
447
448 if (pass != SOC_TPLG_PASS_MIXER)
449 return;
450
451 if (dobj->ops && dobj->ops->control_unload)
452 dobj->ops->control_unload(comp, dobj);
453
454 snd_ctl_remove(card, sb->dobj.control.kcontrol);
455 list_del(&sb->dobj.list);
456 kfree(sb);
457}
458
459/* remove a widget and it's kcontrols - routes must be removed first */
460static void remove_widget(struct snd_soc_component *comp,
461 struct snd_soc_dobj *dobj, int pass)
462{
463 struct snd_card *card = comp->card->snd_card;
464 struct snd_soc_dapm_widget *w =
465 container_of(dobj, struct snd_soc_dapm_widget, dobj);
466 int i;
467
468 if (pass != SOC_TPLG_PASS_WIDGET)
469 return;
470
471 if (dobj->ops && dobj->ops->widget_unload)
472 dobj->ops->widget_unload(comp, dobj);
473
474 /*
475 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
476 * The enum may either have an array of values or strings.
477 */
478 if (dobj->widget.kcontrol_enum) {
479 /* enumerated widget mixer */
480 struct soc_enum *se =
481 (struct soc_enum *)w->kcontrols[0]->private_value;
482
483 snd_ctl_remove(card, w->kcontrols[0]);
484
485 kfree(se->dobj.control.dvalues);
486 for (i = 0; i < se->items; i++)
487 kfree(se->dobj.control.dtexts[i]);
488
489 kfree(se);
490 kfree(w->kcontrol_news);
491 } else {
492 /* non enumerated widget mixer */
493 for (i = 0; i < w->num_kcontrols; i++) {
494 struct snd_kcontrol *kcontrol = w->kcontrols[i];
495 struct soc_mixer_control *sm =
496 (struct soc_mixer_control *) kcontrol->private_value;
497
498 kfree(w->kcontrols[i]->tlv.p);
499
500 snd_ctl_remove(card, w->kcontrols[i]);
501 kfree(sm);
502 }
503 kfree(w->kcontrol_news);
504 }
505 /* widget w is freed by soc-dapm.c */
506}
507
Mengdong Lin64527e82016-01-15 16:13:28 +0800508/* remove DAI configurations */
509static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100510 struct snd_soc_dobj *dobj, int pass)
511{
Mengdong Lin64527e82016-01-15 16:13:28 +0800512 struct snd_soc_dai_driver *dai_drv =
513 container_of(dobj, struct snd_soc_dai_driver, dobj);
514
Liam Girdwood8a978232015-05-29 19:06:14 +0100515 if (pass != SOC_TPLG_PASS_PCM_DAI)
516 return;
517
Mengdong Lin64527e82016-01-15 16:13:28 +0800518 if (dobj->ops && dobj->ops->dai_unload)
519 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100520
521 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800522 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100523}
524
Mengdong Linacfc7d42016-01-15 16:13:37 +0800525/* remove link configurations */
526static void remove_link(struct snd_soc_component *comp,
527 struct snd_soc_dobj *dobj, int pass)
528{
529 struct snd_soc_dai_link *link =
530 container_of(dobj, struct snd_soc_dai_link, dobj);
531
532 if (pass != SOC_TPLG_PASS_PCM_DAI)
533 return;
534
535 if (dobj->ops && dobj->ops->link_unload)
536 dobj->ops->link_unload(comp, dobj);
537
538 list_del(&dobj->list);
539 snd_soc_remove_dai_link(comp->card, link);
540 kfree(link);
541}
542
Liam Girdwood8a978232015-05-29 19:06:14 +0100543/* bind a kcontrol to it's IO handlers */
544static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
545 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800546 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100547{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800548 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800549 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800550 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100551
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800552 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
553 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
554 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
555 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
556 struct soc_bytes_ext *sbe;
557 struct snd_soc_tplg_bytes_control *be;
558
559 sbe = (struct soc_bytes_ext *)k->private_value;
560 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
561
562 /* TLV bytes controls need standard kcontrol info handler,
563 * TLV callback and extended put/get handlers.
564 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530565 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800566 k->tlv.c = snd_soc_bytes_tlv_callback;
567
568 ext_ops = tplg->bytes_ext_ops;
569 num_ops = tplg->bytes_ext_ops_count;
570 for (i = 0; i < num_ops; i++) {
571 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
572 sbe->put = ext_ops[i].put;
573 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
574 sbe->get = ext_ops[i].get;
575 }
576
577 if (sbe->put && sbe->get)
578 return 0;
579 else
580 return -EINVAL;
581 }
582
Mengdong Lin88a17d82015-08-18 18:11:51 +0800583 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800584 ops = tplg->io_ops;
585 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100586 for (i = 0; i < num_ops; i++) {
587
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800588 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100589 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800590 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100591 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800592 if (k->info == NULL && ops[i].id == hdr->ops.info)
593 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100594 }
595
Mengdong Lin88a17d82015-08-18 18:11:51 +0800596 /* vendor specific handlers found ? */
597 if (k->put && k->get && k->info)
598 return 0;
599
600 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800601 ops = io_ops;
602 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800603 for (i = 0; i < num_ops; i++) {
604
605 if (k->put == NULL && ops[i].id == hdr->ops.put)
606 k->put = ops[i].put;
607 if (k->get == NULL && ops[i].id == hdr->ops.get)
608 k->get = ops[i].get;
609 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100610 k->info = ops[i].info;
611 }
612
613 /* standard handlers found ? */
614 if (k->put && k->get && k->info)
615 return 0;
616
Liam Girdwood8a978232015-05-29 19:06:14 +0100617 /* nothing to bind */
618 return -EINVAL;
619}
620
621/* bind a widgets to it's evnt handlers */
622int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
623 const struct snd_soc_tplg_widget_events *events,
624 int num_events, u16 event_type)
625{
626 int i;
627
628 w->event = NULL;
629
630 for (i = 0; i < num_events; i++) {
631 if (event_type == events[i].type) {
632
633 /* found - so assign event */
634 w->event = events[i].event_handler;
635 return 0;
636 }
637 }
638
639 /* not found */
640 return -EINVAL;
641}
642EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
643
644/* optionally pass new dynamic kcontrol to component driver. */
645static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
646 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
647{
648 if (tplg->comp && tplg->ops && tplg->ops->control_load)
649 return tplg->ops->control_load(tplg->comp, k, hdr);
650
651 return 0;
652}
653
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100654
655static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
656 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100657{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100658 unsigned int item_len = 2 * sizeof(unsigned int);
659 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100660
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100661 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
662 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100663 return -ENOMEM;
664
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100665 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
666 p[1] = item_len;
667 p[2] = scale->min;
668 p[3] = (scale->step & TLV_DB_SCALE_MASK)
669 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100670
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100671 kc->tlv.p = (void *)p;
672 return 0;
673}
674
675static int soc_tplg_create_tlv(struct soc_tplg *tplg,
676 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
677{
678 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
679
680 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
681 return 0;
682
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800683 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100684 tplg_tlv = &tc->tlv;
685 switch (tplg_tlv->type) {
686 case SNDRV_CTL_TLVT_DB_SCALE:
687 return soc_tplg_create_tlv_db_scale(tplg, kc,
688 &tplg_tlv->scale);
689
690 /* TODO: add support for other TLV types */
691 default:
692 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
693 tplg_tlv->type);
694 return -EINVAL;
695 }
696 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100697
698 return 0;
699}
700
701static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
702 struct snd_kcontrol_new *kc)
703{
704 kfree(kc->tlv.p);
705}
706
707static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
708 size_t size)
709{
710 struct snd_soc_tplg_bytes_control *be;
711 struct soc_bytes_ext *sbe;
712 struct snd_kcontrol_new kc;
713 int i, err;
714
715 if (soc_tplg_check_elem_count(tplg,
716 sizeof(struct snd_soc_tplg_bytes_control), count,
717 size, "mixer bytes")) {
718 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
719 count);
720 return -EINVAL;
721 }
722
723 for (i = 0; i < count; i++) {
724 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
725
726 /* validate kcontrol */
727 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
728 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
729 return -EINVAL;
730
731 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
732 if (sbe == NULL)
733 return -ENOMEM;
734
735 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
736 be->priv.size);
737
738 dev_dbg(tplg->dev,
739 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
740 be->hdr.name, be->hdr.access);
741
742 memset(&kc, 0, sizeof(kc));
743 kc.name = be->hdr.name;
744 kc.private_value = (long)sbe;
745 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
746 kc.access = be->hdr.access;
747
748 sbe->max = be->max;
749 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
750 sbe->dobj.ops = tplg->ops;
751 INIT_LIST_HEAD(&sbe->dobj.list);
752
753 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800754 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100755 if (err) {
756 soc_control_err(tplg, &be->hdr, be->hdr.name);
757 kfree(sbe);
758 continue;
759 }
760
761 /* pass control to driver for optional further init */
762 err = soc_tplg_init_kcontrol(tplg, &kc,
763 (struct snd_soc_tplg_ctl_hdr *)be);
764 if (err < 0) {
765 dev_err(tplg->dev, "ASoC: failed to init %s\n",
766 be->hdr.name);
767 kfree(sbe);
768 continue;
769 }
770
771 /* register control here */
772 err = soc_tplg_add_kcontrol(tplg, &kc,
773 &sbe->dobj.control.kcontrol);
774 if (err < 0) {
775 dev_err(tplg->dev, "ASoC: failed to add %s\n",
776 be->hdr.name);
777 kfree(sbe);
778 continue;
779 }
780
781 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
782 }
783 return 0;
784
785}
786
787static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
788 size_t size)
789{
790 struct snd_soc_tplg_mixer_control *mc;
791 struct soc_mixer_control *sm;
792 struct snd_kcontrol_new kc;
793 int i, err;
794
795 if (soc_tplg_check_elem_count(tplg,
796 sizeof(struct snd_soc_tplg_mixer_control),
797 count, size, "mixers")) {
798
799 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
800 count);
801 return -EINVAL;
802 }
803
804 for (i = 0; i < count; i++) {
805 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
806
807 /* validate kcontrol */
808 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
809 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
810 return -EINVAL;
811
812 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
813 if (sm == NULL)
814 return -ENOMEM;
815 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
816 mc->priv.size);
817
818 dev_dbg(tplg->dev,
819 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
820 mc->hdr.name, mc->hdr.access);
821
822 memset(&kc, 0, sizeof(kc));
823 kc.name = mc->hdr.name;
824 kc.private_value = (long)sm;
825 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
826 kc.access = mc->hdr.access;
827
828 /* we only support FL/FR channel mapping atm */
829 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
830 SNDRV_CHMAP_FL);
831 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
832 SNDRV_CHMAP_FR);
833 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
834 SNDRV_CHMAP_FL);
835 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
836 SNDRV_CHMAP_FR);
837
838 sm->max = mc->max;
839 sm->min = mc->min;
840 sm->invert = mc->invert;
841 sm->platform_max = mc->platform_max;
842 sm->dobj.index = tplg->index;
843 sm->dobj.ops = tplg->ops;
844 sm->dobj.type = SND_SOC_DOBJ_MIXER;
845 INIT_LIST_HEAD(&sm->dobj.list);
846
847 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800848 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100849 if (err) {
850 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
851 kfree(sm);
852 continue;
853 }
854
855 /* pass control to driver for optional further init */
856 err = soc_tplg_init_kcontrol(tplg, &kc,
857 (struct snd_soc_tplg_ctl_hdr *) mc);
858 if (err < 0) {
859 dev_err(tplg->dev, "ASoC: failed to init %s\n",
860 mc->hdr.name);
861 kfree(sm);
862 continue;
863 }
864
865 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100866 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100867
868 /* register control here */
869 err = soc_tplg_add_kcontrol(tplg, &kc,
870 &sm->dobj.control.kcontrol);
871 if (err < 0) {
872 dev_err(tplg->dev, "ASoC: failed to add %s\n",
873 mc->hdr.name);
874 soc_tplg_free_tlv(tplg, &kc);
875 kfree(sm);
876 continue;
877 }
878
879 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
880 }
881
882 return 0;
883}
884
885static int soc_tplg_denum_create_texts(struct soc_enum *se,
886 struct snd_soc_tplg_enum_control *ec)
887{
888 int i, ret;
889
890 se->dobj.control.dtexts =
891 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
892 if (se->dobj.control.dtexts == NULL)
893 return -ENOMEM;
894
895 for (i = 0; i < ec->items; i++) {
896
897 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
898 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
899 ret = -EINVAL;
900 goto err;
901 }
902
903 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
904 if (!se->dobj.control.dtexts[i]) {
905 ret = -ENOMEM;
906 goto err;
907 }
908 }
909
910 return 0;
911
912err:
913 for (--i; i >= 0; i--)
914 kfree(se->dobj.control.dtexts[i]);
915 kfree(se->dobj.control.dtexts);
916 return ret;
917}
918
919static int soc_tplg_denum_create_values(struct soc_enum *se,
920 struct snd_soc_tplg_enum_control *ec)
921{
922 if (ec->items > sizeof(*ec->values))
923 return -EINVAL;
924
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200925 se->dobj.control.dvalues = kmemdup(ec->values,
926 ec->items * sizeof(u32),
927 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100928 if (!se->dobj.control.dvalues)
929 return -ENOMEM;
930
Liam Girdwood8a978232015-05-29 19:06:14 +0100931 return 0;
932}
933
934static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
935 size_t size)
936{
937 struct snd_soc_tplg_enum_control *ec;
938 struct soc_enum *se;
939 struct snd_kcontrol_new kc;
940 int i, ret, err;
941
942 if (soc_tplg_check_elem_count(tplg,
943 sizeof(struct snd_soc_tplg_enum_control),
944 count, size, "enums")) {
945
946 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
947 count);
948 return -EINVAL;
949 }
950
951 for (i = 0; i < count; i++) {
952 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
953 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
954 ec->priv.size);
955
956 /* validate kcontrol */
957 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
958 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
959 return -EINVAL;
960
961 se = kzalloc((sizeof(*se)), GFP_KERNEL);
962 if (se == NULL)
963 return -ENOMEM;
964
965 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
966 ec->hdr.name, ec->items);
967
968 memset(&kc, 0, sizeof(kc));
969 kc.name = ec->hdr.name;
970 kc.private_value = (long)se;
971 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
972 kc.access = ec->hdr.access;
973
974 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
975 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
976 SNDRV_CHMAP_FL);
977 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
978 SNDRV_CHMAP_FL);
979
980 se->items = ec->items;
981 se->mask = ec->mask;
982 se->dobj.index = tplg->index;
983 se->dobj.type = SND_SOC_DOBJ_ENUM;
984 se->dobj.ops = tplg->ops;
985 INIT_LIST_HEAD(&se->dobj.list);
986
987 switch (ec->hdr.ops.info) {
988 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
989 case SND_SOC_TPLG_CTL_ENUM_VALUE:
990 err = soc_tplg_denum_create_values(se, ec);
991 if (err < 0) {
992 dev_err(tplg->dev,
993 "ASoC: could not create values for %s\n",
994 ec->hdr.name);
995 kfree(se);
996 continue;
997 }
998 /* fall through and create texts */
999 case SND_SOC_TPLG_CTL_ENUM:
1000 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1001 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1002 err = soc_tplg_denum_create_texts(se, ec);
1003 if (err < 0) {
1004 dev_err(tplg->dev,
1005 "ASoC: could not create texts for %s\n",
1006 ec->hdr.name);
1007 kfree(se);
1008 continue;
1009 }
1010 break;
1011 default:
1012 dev_err(tplg->dev,
1013 "ASoC: invalid enum control type %d for %s\n",
1014 ec->hdr.ops.info, ec->hdr.name);
1015 kfree(se);
1016 continue;
1017 }
1018
1019 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001020 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001021 if (err) {
1022 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1023 kfree(se);
1024 continue;
1025 }
1026
1027 /* pass control to driver for optional further init */
1028 err = soc_tplg_init_kcontrol(tplg, &kc,
1029 (struct snd_soc_tplg_ctl_hdr *) ec);
1030 if (err < 0) {
1031 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1032 ec->hdr.name);
1033 kfree(se);
1034 continue;
1035 }
1036
1037 /* register control here */
1038 ret = soc_tplg_add_kcontrol(tplg,
1039 &kc, &se->dobj.control.kcontrol);
1040 if (ret < 0) {
1041 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1042 ec->hdr.name);
1043 kfree(se);
1044 continue;
1045 }
1046
1047 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1048 }
1049
1050 return 0;
1051}
1052
1053static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1054 struct snd_soc_tplg_hdr *hdr)
1055{
1056 struct snd_soc_tplg_ctl_hdr *control_hdr;
1057 int i;
1058
1059 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1060 tplg->pos += hdr->size + hdr->payload_size;
1061 return 0;
1062 }
1063
1064 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1065 soc_tplg_get_offset(tplg));
1066
1067 for (i = 0; i < hdr->count; i++) {
1068
1069 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1070
1071 switch (control_hdr->ops.info) {
1072 case SND_SOC_TPLG_CTL_VOLSW:
1073 case SND_SOC_TPLG_CTL_STROBE:
1074 case SND_SOC_TPLG_CTL_VOLSW_SX:
1075 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1076 case SND_SOC_TPLG_CTL_RANGE:
1077 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1078 case SND_SOC_TPLG_DAPM_CTL_PIN:
1079 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1080 break;
1081 case SND_SOC_TPLG_CTL_ENUM:
1082 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1083 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1084 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1085 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1086 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1087 break;
1088 case SND_SOC_TPLG_CTL_BYTES:
1089 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1090 break;
1091 default:
1092 soc_bind_err(tplg, control_hdr, i);
1093 return -EINVAL;
1094 }
1095 }
1096
1097 return 0;
1098}
1099
1100static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1101 struct snd_soc_tplg_hdr *hdr)
1102{
1103 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1104 struct snd_soc_dapm_route route;
1105 struct snd_soc_tplg_dapm_graph_elem *elem;
1106 int count = hdr->count, i;
1107
1108 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1109 tplg->pos += hdr->size + hdr->payload_size;
1110 return 0;
1111 }
1112
1113 if (soc_tplg_check_elem_count(tplg,
1114 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1115 count, hdr->payload_size, "graph")) {
1116
1117 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1118 count);
1119 return -EINVAL;
1120 }
1121
1122 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1123
1124 for (i = 0; i < count; i++) {
1125 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1126 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1127
1128 /* validate routes */
1129 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1130 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1131 return -EINVAL;
1132 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1133 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1134 return -EINVAL;
1135 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1136 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1137 return -EINVAL;
1138
1139 route.source = elem->source;
1140 route.sink = elem->sink;
1141 route.connected = NULL; /* set to NULL atm for tplg users */
1142 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1143 route.control = NULL;
1144 else
1145 route.control = elem->control;
1146
1147 /* add route, but keep going if some fail */
1148 snd_soc_dapm_add_routes(dapm, &route, 1);
1149 }
1150
1151 return 0;
1152}
1153
1154static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1155 struct soc_tplg *tplg, int num_kcontrols)
1156{
1157 struct snd_kcontrol_new *kc;
1158 struct soc_mixer_control *sm;
1159 struct snd_soc_tplg_mixer_control *mc;
1160 int i, err;
1161
Axel Lin4ca7deb2015-08-05 22:34:22 +08001162 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001163 if (kc == NULL)
1164 return NULL;
1165
1166 for (i = 0; i < num_kcontrols; i++) {
1167 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1168 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1169 if (sm == NULL)
1170 goto err;
1171
1172 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1173 mc->priv.size);
1174
1175 /* validate kcontrol */
1176 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1177 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1178 goto err_str;
1179
1180 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1181 mc->hdr.name, i);
1182
1183 kc[i].name = mc->hdr.name;
1184 kc[i].private_value = (long)sm;
1185 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1186 kc[i].access = mc->hdr.access;
1187
1188 /* we only support FL/FR channel mapping atm */
1189 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1190 SNDRV_CHMAP_FL);
1191 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1192 SNDRV_CHMAP_FR);
1193 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1194 SNDRV_CHMAP_FL);
1195 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1196 SNDRV_CHMAP_FR);
1197
1198 sm->max = mc->max;
1199 sm->min = mc->min;
1200 sm->invert = mc->invert;
1201 sm->platform_max = mc->platform_max;
1202 sm->dobj.index = tplg->index;
1203 INIT_LIST_HEAD(&sm->dobj.list);
1204
1205 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001206 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001207 if (err) {
1208 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1209 kfree(sm);
1210 continue;
1211 }
1212
1213 /* pass control to driver for optional further init */
1214 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1215 (struct snd_soc_tplg_ctl_hdr *)mc);
1216 if (err < 0) {
1217 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1218 mc->hdr.name);
1219 kfree(sm);
1220 continue;
1221 }
1222 }
1223 return kc;
1224
1225err_str:
1226 kfree(sm);
1227err:
1228 for (--i; i >= 0; i--)
1229 kfree((void *)kc[i].private_value);
1230 kfree(kc);
1231 return NULL;
1232}
1233
1234static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1235 struct soc_tplg *tplg)
1236{
1237 struct snd_kcontrol_new *kc;
1238 struct snd_soc_tplg_enum_control *ec;
1239 struct soc_enum *se;
1240 int i, err;
1241
1242 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1243 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1244 ec->priv.size);
1245
1246 /* validate kcontrol */
1247 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1248 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1249 return NULL;
1250
1251 kc = kzalloc(sizeof(*kc), GFP_KERNEL);
1252 if (kc == NULL)
1253 return NULL;
1254
1255 se = kzalloc(sizeof(*se), GFP_KERNEL);
1256 if (se == NULL)
1257 goto err;
1258
1259 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1260 ec->hdr.name);
1261
1262 kc->name = ec->hdr.name;
1263 kc->private_value = (long)se;
1264 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1265 kc->access = ec->hdr.access;
1266
1267 /* we only support FL/FR channel mapping atm */
1268 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1269 se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
1270 se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
1271
1272 se->items = ec->items;
1273 se->mask = ec->mask;
1274 se->dobj.index = tplg->index;
1275
1276 switch (ec->hdr.ops.info) {
1277 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1278 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1279 err = soc_tplg_denum_create_values(se, ec);
1280 if (err < 0) {
1281 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1282 ec->hdr.name);
1283 goto err_se;
1284 }
1285 /* fall through to create texts */
1286 case SND_SOC_TPLG_CTL_ENUM:
1287 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1288 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1289 err = soc_tplg_denum_create_texts(se, ec);
1290 if (err < 0) {
1291 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1292 ec->hdr.name);
1293 goto err_se;
1294 }
1295 break;
1296 default:
1297 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1298 ec->hdr.ops.info, ec->hdr.name);
1299 goto err_se;
1300 }
1301
1302 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001303 err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001304 if (err) {
1305 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1306 goto err_se;
1307 }
1308
1309 /* pass control to driver for optional further init */
1310 err = soc_tplg_init_kcontrol(tplg, kc,
1311 (struct snd_soc_tplg_ctl_hdr *)ec);
1312 if (err < 0) {
1313 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1314 ec->hdr.name);
1315 goto err_se;
1316 }
1317
1318 return kc;
1319
1320err_se:
1321 /* free values and texts */
1322 kfree(se->dobj.control.dvalues);
1323 for (i = 0; i < ec->items; i++)
1324 kfree(se->dobj.control.dtexts[i]);
1325
1326 kfree(se);
1327err:
1328 kfree(kc);
1329
1330 return NULL;
1331}
1332
1333static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1334 struct soc_tplg *tplg, int count)
1335{
1336 struct snd_soc_tplg_bytes_control *be;
1337 struct soc_bytes_ext *sbe;
1338 struct snd_kcontrol_new *kc;
1339 int i, err;
1340
Axel Lin4ca7deb2015-08-05 22:34:22 +08001341 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001342 if (!kc)
1343 return NULL;
1344
1345 for (i = 0; i < count; i++) {
1346 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1347
1348 /* validate kcontrol */
1349 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1350 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1351 goto err;
1352
1353 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1354 if (sbe == NULL)
1355 goto err;
1356
1357 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1358 be->priv.size);
1359
1360 dev_dbg(tplg->dev,
1361 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1362 be->hdr.name, be->hdr.access);
1363
Liam Girdwood8a978232015-05-29 19:06:14 +01001364 kc[i].name = be->hdr.name;
1365 kc[i].private_value = (long)sbe;
1366 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1367 kc[i].access = be->hdr.access;
1368
1369 sbe->max = be->max;
1370 INIT_LIST_HEAD(&sbe->dobj.list);
1371
1372 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001373 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001374 if (err) {
1375 soc_control_err(tplg, &be->hdr, be->hdr.name);
1376 kfree(sbe);
1377 continue;
1378 }
1379
1380 /* pass control to driver for optional further init */
1381 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1382 (struct snd_soc_tplg_ctl_hdr *)be);
1383 if (err < 0) {
1384 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1385 be->hdr.name);
1386 kfree(sbe);
1387 continue;
1388 }
1389 }
1390
1391 return kc;
1392
1393err:
1394 for (--i; i >= 0; i--)
1395 kfree((void *)kc[i].private_value);
1396
1397 kfree(kc);
1398 return NULL;
1399}
1400
1401static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1402 struct snd_soc_tplg_dapm_widget *w)
1403{
1404 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1405 struct snd_soc_dapm_widget template, *widget;
1406 struct snd_soc_tplg_ctl_hdr *control_hdr;
1407 struct snd_soc_card *card = tplg->comp->card;
1408 int ret = 0;
1409
1410 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1411 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1412 return -EINVAL;
1413 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1414 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1415 return -EINVAL;
1416
1417 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1418 w->name, w->id);
1419
1420 memset(&template, 0, sizeof(template));
1421
1422 /* map user to kernel widget ID */
1423 template.id = get_widget_id(w->id);
1424 if (template.id < 0)
1425 return template.id;
1426
1427 template.name = kstrdup(w->name, GFP_KERNEL);
1428 if (!template.name)
1429 return -ENOMEM;
1430 template.sname = kstrdup(w->sname, GFP_KERNEL);
1431 if (!template.sname) {
1432 ret = -ENOMEM;
1433 goto err;
1434 }
1435 template.reg = w->reg;
1436 template.shift = w->shift;
1437 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001438 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001439 template.on_val = w->invert ? 0 : 1;
1440 template.off_val = w->invert ? 1 : 0;
1441 template.ignore_suspend = w->ignore_suspend;
1442 template.event_flags = w->event_flags;
1443 template.dobj.index = tplg->index;
1444
1445 tplg->pos +=
1446 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1447 if (w->num_kcontrols == 0) {
1448 template.num_kcontrols = 0;
1449 goto widget;
1450 }
1451
1452 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1453 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1454 w->name, w->num_kcontrols, control_hdr->type);
1455
1456 switch (control_hdr->ops.info) {
1457 case SND_SOC_TPLG_CTL_VOLSW:
1458 case SND_SOC_TPLG_CTL_STROBE:
1459 case SND_SOC_TPLG_CTL_VOLSW_SX:
1460 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1461 case SND_SOC_TPLG_CTL_RANGE:
1462 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1463 template.num_kcontrols = w->num_kcontrols;
1464 template.kcontrol_news =
1465 soc_tplg_dapm_widget_dmixer_create(tplg,
1466 template.num_kcontrols);
1467 if (!template.kcontrol_news) {
1468 ret = -ENOMEM;
1469 goto hdr_err;
1470 }
1471 break;
1472 case SND_SOC_TPLG_CTL_ENUM:
1473 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1474 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1475 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1476 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1477 template.dobj.widget.kcontrol_enum = 1;
1478 template.num_kcontrols = 1;
1479 template.kcontrol_news =
1480 soc_tplg_dapm_widget_denum_create(tplg);
1481 if (!template.kcontrol_news) {
1482 ret = -ENOMEM;
1483 goto hdr_err;
1484 }
1485 break;
1486 case SND_SOC_TPLG_CTL_BYTES:
1487 template.num_kcontrols = w->num_kcontrols;
1488 template.kcontrol_news =
1489 soc_tplg_dapm_widget_dbytes_create(tplg,
1490 template.num_kcontrols);
1491 if (!template.kcontrol_news) {
1492 ret = -ENOMEM;
1493 goto hdr_err;
1494 }
1495 break;
1496 default:
1497 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1498 control_hdr->ops.get, control_hdr->ops.put,
1499 control_hdr->ops.info);
1500 ret = -EINVAL;
1501 goto hdr_err;
1502 }
1503
1504widget:
1505 ret = soc_tplg_widget_load(tplg, &template, w);
1506 if (ret < 0)
1507 goto hdr_err;
1508
1509 /* card dapm mutex is held by the core if we are loading topology
1510 * data during sound card init. */
1511 if (card->instantiated)
1512 widget = snd_soc_dapm_new_control(dapm, &template);
1513 else
1514 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1515 if (widget == NULL) {
1516 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1517 w->name);
1518 goto hdr_err;
1519 }
1520
1521 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1522 widget->dobj.ops = tplg->ops;
1523 widget->dobj.index = tplg->index;
1524 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1525 return 0;
1526
1527hdr_err:
1528 kfree(template.sname);
1529err:
1530 kfree(template.name);
1531 return ret;
1532}
1533
1534static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1535 struct snd_soc_tplg_hdr *hdr)
1536{
1537 struct snd_soc_tplg_dapm_widget *widget;
1538 int ret, count = hdr->count, i;
1539
1540 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1541 return 0;
1542
1543 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1544
1545 for (i = 0; i < count; i++) {
1546 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1547 ret = soc_tplg_dapm_widget_create(tplg, widget);
1548 if (ret < 0)
1549 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1550 widget->name);
1551 }
1552
1553 return 0;
1554}
1555
1556static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1557{
1558 struct snd_soc_card *card = tplg->comp->card;
1559 int ret;
1560
1561 /* Card might not have been registered at this point.
1562 * If so, just return success.
1563 */
1564 if (!card || !card->instantiated) {
1565 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1566 "Do not add new widgets now\n");
1567 return 0;
1568 }
1569
1570 ret = snd_soc_dapm_new_widgets(card);
1571 if (ret < 0)
1572 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1573 ret);
1574
1575 return 0;
1576}
1577
Mengdong Lin64527e82016-01-15 16:13:28 +08001578static int soc_tplg_dai_create(struct soc_tplg *tplg,
1579 struct snd_soc_tplg_pcm *pcm)
1580{
1581 struct snd_soc_dai_driver *dai_drv;
1582 struct snd_soc_pcm_stream *stream;
1583 struct snd_soc_tplg_stream_caps *caps;
1584 int ret;
1585
1586 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1587 if (dai_drv == NULL)
1588 return -ENOMEM;
1589
1590 dai_drv->name = pcm->dai_name;
1591 dai_drv->id = pcm->dai_id;
1592
1593 if (pcm->playback) {
1594 stream = &dai_drv->playback;
1595 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1596
1597 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1598 stream->channels_min = caps->channels_min;
1599 stream->channels_max = caps->channels_max;
1600 stream->rates = snd_pcm_rate_range_to_bits(caps->rate_min,
1601 caps->rate_max);
1602 stream->formats = caps->formats;
1603 }
1604
1605 if (pcm->capture) {
1606 stream = &dai_drv->capture;
1607 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1608
1609 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1610 stream->channels_min = caps->channels_min;
1611 stream->channels_max = caps->channels_max;
1612 stream->rates = snd_pcm_rate_range_to_bits(caps->rate_min,
1613 caps->rate_max);
1614 stream->formats = caps->formats;
1615 }
1616
1617 /* pass control to component driver for optional further init */
1618 ret = soc_tplg_dai_load(tplg, dai_drv);
1619 if (ret < 0) {
1620 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1621 kfree(dai_drv);
1622 return ret;
1623 }
1624
1625 dai_drv->dobj.index = tplg->index;
1626 dai_drv->dobj.ops = tplg->ops;
1627 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1628 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1629
1630 /* register the DAI to the component */
1631 return snd_soc_register_dai(tplg->comp, dai_drv);
1632}
1633
Mengdong Linacfc7d42016-01-15 16:13:37 +08001634static int soc_tplg_link_create(struct soc_tplg *tplg,
1635 struct snd_soc_tplg_pcm *pcm)
1636{
1637 struct snd_soc_dai_link *link;
1638 int ret;
1639
1640 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1641 if (link == NULL)
1642 return -ENOMEM;
1643
1644 link->name = pcm->pcm_name;
1645 link->stream_name = pcm->pcm_name;
1646
1647 /* pass control to component driver for optional further init */
1648 ret = soc_tplg_dai_link_load(tplg, link);
1649 if (ret < 0) {
1650 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1651 kfree(link);
1652 return ret;
1653 }
1654
1655 link->dobj.index = tplg->index;
1656 link->dobj.ops = tplg->ops;
1657 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1658 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1659
1660 snd_soc_add_dai_link(tplg->comp->card, link);
1661 return 0;
1662}
1663
1664/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001665static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1666 struct snd_soc_tplg_pcm *pcm)
1667{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001668 int ret;
1669
1670 ret = soc_tplg_dai_create(tplg, pcm);
1671 if (ret < 0)
1672 return ret;
1673
1674 return soc_tplg_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001675}
1676
1677static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001678 struct snd_soc_tplg_hdr *hdr)
1679{
Mengdong Lin64527e82016-01-15 16:13:28 +08001680 struct snd_soc_tplg_pcm *pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001681 int count = hdr->count;
Mengdong Lin64527e82016-01-15 16:13:28 +08001682 int i;
Liam Girdwood8a978232015-05-29 19:06:14 +01001683
1684 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1685 return 0;
1686
Mengdong Lin64527e82016-01-15 16:13:28 +08001687 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Liam Girdwood8a978232015-05-29 19:06:14 +01001688
1689 if (soc_tplg_check_elem_count(tplg,
Vedang Patel5b2688a2015-09-30 17:28:47 +08001690 sizeof(struct snd_soc_tplg_pcm), count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001691 hdr->payload_size, "PCM DAI")) {
1692 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1693 count);
1694 return -EINVAL;
1695 }
1696
Mengdong Lin64527e82016-01-15 16:13:28 +08001697 /* create the FE DAIs and DAI links */
1698 for (i = 0; i < count; i++) {
1699 soc_tplg_pcm_create(tplg, pcm);
1700 pcm++;
1701 }
1702
Liam Girdwood8a978232015-05-29 19:06:14 +01001703 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Vedang Patel5b2688a2015-09-30 17:28:47 +08001704 tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count;
Liam Girdwood8a978232015-05-29 19:06:14 +01001705
Liam Girdwood8a978232015-05-29 19:06:14 +01001706 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001707}
1708
1709static int soc_tplg_manifest_load(struct soc_tplg *tplg,
1710 struct snd_soc_tplg_hdr *hdr)
1711{
1712 struct snd_soc_tplg_manifest *manifest;
1713
1714 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
1715 return 0;
1716
1717 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
1718 tplg->pos += sizeof(struct snd_soc_tplg_manifest);
1719
1720 if (tplg->comp && tplg->ops && tplg->ops->manifest)
1721 return tplg->ops->manifest(tplg->comp, manifest);
1722
1723 dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n");
1724 return 0;
1725}
1726
1727/* validate header magic, size and type */
1728static int soc_valid_header(struct soc_tplg *tplg,
1729 struct snd_soc_tplg_hdr *hdr)
1730{
1731 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
1732 return 0;
1733
1734 /* big endian firmware objects not supported atm */
1735 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
1736 dev_err(tplg->dev,
1737 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1738 tplg->pass, hdr->magic,
1739 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1740 return -EINVAL;
1741 }
1742
1743 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
1744 dev_err(tplg->dev,
1745 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1746 tplg->pass, hdr->magic,
1747 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1748 return -EINVAL;
1749 }
1750
1751 if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
1752 dev_err(tplg->dev,
1753 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1754 tplg->pass, hdr->abi,
1755 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
1756 tplg->fw->size);
1757 return -EINVAL;
1758 }
1759
1760 if (hdr->payload_size == 0) {
1761 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
1762 soc_tplg_get_hdr_offset(tplg));
1763 return -EINVAL;
1764 }
1765
1766 if (tplg->pass == hdr->type)
1767 dev_dbg(tplg->dev,
1768 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
1769 hdr->payload_size, hdr->type, hdr->version,
1770 hdr->vendor_type, tplg->pass);
1771
1772 return 1;
1773}
1774
1775/* check header type and call appropriate handler */
1776static int soc_tplg_load_header(struct soc_tplg *tplg,
1777 struct snd_soc_tplg_hdr *hdr)
1778{
1779 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
1780
1781 /* check for matching ID */
1782 if (hdr->index != tplg->req_index &&
1783 hdr->index != SND_SOC_TPLG_INDEX_ALL)
1784 return 0;
1785
1786 tplg->index = hdr->index;
1787
1788 switch (hdr->type) {
1789 case SND_SOC_TPLG_TYPE_MIXER:
1790 case SND_SOC_TPLG_TYPE_ENUM:
1791 case SND_SOC_TPLG_TYPE_BYTES:
1792 return soc_tplg_kcontrol_elems_load(tplg, hdr);
1793 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
1794 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
1795 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
1796 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
1797 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08001798 return soc_tplg_pcm_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01001799 case SND_SOC_TPLG_TYPE_MANIFEST:
1800 return soc_tplg_manifest_load(tplg, hdr);
1801 default:
1802 /* bespoke vendor data object */
1803 return soc_tplg_vendor_load(tplg, hdr);
1804 }
1805
1806 return 0;
1807}
1808
1809/* process the topology file headers */
1810static int soc_tplg_process_headers(struct soc_tplg *tplg)
1811{
1812 struct snd_soc_tplg_hdr *hdr;
1813 int ret;
1814
1815 tplg->pass = SOC_TPLG_PASS_START;
1816
1817 /* process the header types from start to end */
1818 while (tplg->pass <= SOC_TPLG_PASS_END) {
1819
1820 tplg->hdr_pos = tplg->fw->data;
1821 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1822
1823 while (!soc_tplg_is_eof(tplg)) {
1824
1825 /* make sure header is valid before loading */
1826 ret = soc_valid_header(tplg, hdr);
1827 if (ret < 0)
1828 return ret;
1829 else if (ret == 0)
1830 break;
1831
1832 /* load the header object */
1833 ret = soc_tplg_load_header(tplg, hdr);
1834 if (ret < 0)
1835 return ret;
1836
1837 /* goto next header */
1838 tplg->hdr_pos += hdr->payload_size +
1839 sizeof(struct snd_soc_tplg_hdr);
1840 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1841 }
1842
1843 /* next data type pass */
1844 tplg->pass++;
1845 }
1846
1847 /* signal DAPM we are complete */
1848 ret = soc_tplg_dapm_complete(tplg);
1849 if (ret < 0)
1850 dev_err(tplg->dev,
1851 "ASoC: failed to initialise DAPM from Firmware\n");
1852
1853 return ret;
1854}
1855
1856static int soc_tplg_load(struct soc_tplg *tplg)
1857{
1858 int ret;
1859
1860 ret = soc_tplg_process_headers(tplg);
1861 if (ret == 0)
1862 soc_tplg_complete(tplg);
1863
1864 return ret;
1865}
1866
1867/* load audio component topology from "firmware" file */
1868int snd_soc_tplg_component_load(struct snd_soc_component *comp,
1869 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
1870{
1871 struct soc_tplg tplg;
1872
1873 /* setup parsing context */
1874 memset(&tplg, 0, sizeof(tplg));
1875 tplg.fw = fw;
1876 tplg.dev = comp->dev;
1877 tplg.comp = comp;
1878 tplg.ops = ops;
1879 tplg.req_index = id;
1880 tplg.io_ops = ops->io_ops;
1881 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08001882 tplg.bytes_ext_ops = ops->bytes_ext_ops;
1883 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01001884
1885 return soc_tplg_load(&tplg);
1886}
1887EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
1888
1889/* remove this dynamic widget */
1890void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
1891{
1892 /* make sure we are a widget */
1893 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
1894 return;
1895
1896 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
1897}
1898EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
1899
1900/* remove all dynamic widgets from this DAPM context */
1901void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
1902 u32 index)
1903{
1904 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01001905
1906 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1907
1908 /* make sure we are a widget with correct context */
1909 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
1910 continue;
1911
1912 /* match ID */
1913 if (w->dobj.index != index &&
1914 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
1915 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01001916 /* check and free and dynamic widget kcontrols */
1917 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02001918 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01001919 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02001920 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01001921}
1922EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
1923
1924/* remove dynamic controls from the component driver */
1925int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
1926{
1927 struct snd_soc_dobj *dobj, *next_dobj;
1928 int pass = SOC_TPLG_PASS_END;
1929
1930 /* process the header types from end to start */
1931 while (pass >= SOC_TPLG_PASS_START) {
1932
1933 /* remove mixer controls */
1934 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
1935 list) {
1936
1937 /* match index */
1938 if (dobj->index != index &&
1939 dobj->index != SND_SOC_TPLG_INDEX_ALL)
1940 continue;
1941
1942 switch (dobj->type) {
1943 case SND_SOC_DOBJ_MIXER:
1944 remove_mixer(comp, dobj, pass);
1945 break;
1946 case SND_SOC_DOBJ_ENUM:
1947 remove_enum(comp, dobj, pass);
1948 break;
1949 case SND_SOC_DOBJ_BYTES:
1950 remove_bytes(comp, dobj, pass);
1951 break;
1952 case SND_SOC_DOBJ_WIDGET:
1953 remove_widget(comp, dobj, pass);
1954 break;
1955 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08001956 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01001957 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001958 case SND_SOC_DOBJ_DAI_LINK:
1959 remove_link(comp, dobj, pass);
1960 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01001961 default:
1962 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
1963 dobj->type);
1964 break;
1965 }
1966 }
1967 pass--;
1968 }
1969
1970 /* let caller know if FW can be freed when no objects are left */
1971 return !list_empty(&comp->dobj_list);
1972}
1973EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);