blob: ee7f15aa46fca30400a07d8b8cf4f44717f0a89f [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
Liam Girdwood8a978232015-05-29 19:06:14 +0100226static inline void soc_bind_err(struct soc_tplg *tplg,
227 struct snd_soc_tplg_ctl_hdr *hdr, int index)
228{
229 dev_err(tplg->dev,
230 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
231 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
232 soc_tplg_get_offset(tplg));
233}
234
235static inline void soc_control_err(struct soc_tplg *tplg,
236 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
237{
238 dev_err(tplg->dev,
239 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
240 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
241 soc_tplg_get_offset(tplg));
242}
243
244/* pass vendor data to component driver for processing */
245static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
246 struct snd_soc_tplg_hdr *hdr)
247{
248 int ret = 0;
249
250 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
251 ret = tplg->ops->vendor_load(tplg->comp, hdr);
252 else {
253 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
254 hdr->vendor_type);
255 return -EINVAL;
256 }
257
258 if (ret < 0)
259 dev_err(tplg->dev,
260 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
261 soc_tplg_get_hdr_offset(tplg),
262 soc_tplg_get_hdr_offset(tplg),
263 hdr->type, hdr->vendor_type);
264 return ret;
265}
266
267/* pass vendor data to component driver for processing */
268static int soc_tplg_vendor_load(struct soc_tplg *tplg,
269 struct snd_soc_tplg_hdr *hdr)
270{
271 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
272 return 0;
273
274 return soc_tplg_vendor_load_(tplg, hdr);
275}
276
277/* optionally pass new dynamic widget to component driver. This is mainly for
278 * external widgets where we can assign private data/ops */
279static int soc_tplg_widget_load(struct soc_tplg *tplg,
280 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
281{
282 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
283 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
284
285 return 0;
286}
287
Mengdong Lin64527e82016-01-15 16:13:28 +0800288/* pass DAI configurations to component driver for extra intialization */
289static int soc_tplg_dai_load(struct soc_tplg *tplg,
290 struct snd_soc_dai_driver *dai_drv)
Liam Girdwood8a978232015-05-29 19:06:14 +0100291{
Mengdong Lin64527e82016-01-15 16:13:28 +0800292 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
293 return tplg->ops->dai_load(tplg->comp, dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100294
295 return 0;
296}
297
Mengdong Linacfc7d42016-01-15 16:13:37 +0800298/* pass link configurations to component driver for extra intialization */
299static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
300 struct snd_soc_dai_link *link)
301{
302 if (tplg->comp && tplg->ops && tplg->ops->link_load)
303 return tplg->ops->link_load(tplg->comp, link);
304
305 return 0;
306}
307
Liam Girdwood8a978232015-05-29 19:06:14 +0100308/* tell the component driver that all firmware has been loaded in this request */
309static void soc_tplg_complete(struct soc_tplg *tplg)
310{
311 if (tplg->comp && tplg->ops && tplg->ops->complete)
312 tplg->ops->complete(tplg->comp);
313}
314
315/* add a dynamic kcontrol */
316static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
317 const struct snd_kcontrol_new *control_new, const char *prefix,
318 void *data, struct snd_kcontrol **kcontrol)
319{
320 int err;
321
322 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
323 if (*kcontrol == NULL) {
324 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
325 control_new->name);
326 return -ENOMEM;
327 }
328
329 err = snd_ctl_add(card, *kcontrol);
330 if (err < 0) {
331 dev_err(dev, "ASoC: Failed to add %s: %d\n",
332 control_new->name, err);
333 return err;
334 }
335
336 return 0;
337}
338
339/* add a dynamic kcontrol for component driver */
340static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
341 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
342{
343 struct snd_soc_component *comp = tplg->comp;
344
345 return soc_tplg_add_dcontrol(comp->card->snd_card,
346 comp->dev, k, NULL, comp, kcontrol);
347}
348
349/* remove a mixer kcontrol */
350static void remove_mixer(struct snd_soc_component *comp,
351 struct snd_soc_dobj *dobj, int pass)
352{
353 struct snd_card *card = comp->card->snd_card;
354 struct soc_mixer_control *sm =
355 container_of(dobj, struct soc_mixer_control, dobj);
356 const unsigned int *p = NULL;
357
358 if (pass != SOC_TPLG_PASS_MIXER)
359 return;
360
361 if (dobj->ops && dobj->ops->control_unload)
362 dobj->ops->control_unload(comp, dobj);
363
364 if (sm->dobj.control.kcontrol->tlv.p)
365 p = sm->dobj.control.kcontrol->tlv.p;
366 snd_ctl_remove(card, sm->dobj.control.kcontrol);
367 list_del(&sm->dobj.list);
368 kfree(sm);
369 kfree(p);
370}
371
372/* remove an enum kcontrol */
373static void remove_enum(struct snd_soc_component *comp,
374 struct snd_soc_dobj *dobj, int pass)
375{
376 struct snd_card *card = comp->card->snd_card;
377 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
378 int i;
379
380 if (pass != SOC_TPLG_PASS_MIXER)
381 return;
382
383 if (dobj->ops && dobj->ops->control_unload)
384 dobj->ops->control_unload(comp, dobj);
385
386 snd_ctl_remove(card, se->dobj.control.kcontrol);
387 list_del(&se->dobj.list);
388
389 kfree(se->dobj.control.dvalues);
390 for (i = 0; i < se->items; i++)
391 kfree(se->dobj.control.dtexts[i]);
392 kfree(se);
393}
394
395/* remove a byte kcontrol */
396static void remove_bytes(struct snd_soc_component *comp,
397 struct snd_soc_dobj *dobj, int pass)
398{
399 struct snd_card *card = comp->card->snd_card;
400 struct soc_bytes_ext *sb =
401 container_of(dobj, struct soc_bytes_ext, dobj);
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 snd_ctl_remove(card, sb->dobj.control.kcontrol);
410 list_del(&sb->dobj.list);
411 kfree(sb);
412}
413
414/* remove a widget and it's kcontrols - routes must be removed first */
415static void remove_widget(struct snd_soc_component *comp,
416 struct snd_soc_dobj *dobj, int pass)
417{
418 struct snd_card *card = comp->card->snd_card;
419 struct snd_soc_dapm_widget *w =
420 container_of(dobj, struct snd_soc_dapm_widget, dobj);
421 int i;
422
423 if (pass != SOC_TPLG_PASS_WIDGET)
424 return;
425
426 if (dobj->ops && dobj->ops->widget_unload)
427 dobj->ops->widget_unload(comp, dobj);
428
429 /*
430 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
431 * The enum may either have an array of values or strings.
432 */
433 if (dobj->widget.kcontrol_enum) {
434 /* enumerated widget mixer */
435 struct soc_enum *se =
436 (struct soc_enum *)w->kcontrols[0]->private_value;
437
438 snd_ctl_remove(card, w->kcontrols[0]);
439
440 kfree(se->dobj.control.dvalues);
441 for (i = 0; i < se->items; i++)
442 kfree(se->dobj.control.dtexts[i]);
443
444 kfree(se);
445 kfree(w->kcontrol_news);
446 } else {
447 /* non enumerated widget mixer */
448 for (i = 0; i < w->num_kcontrols; i++) {
449 struct snd_kcontrol *kcontrol = w->kcontrols[i];
450 struct soc_mixer_control *sm =
451 (struct soc_mixer_control *) kcontrol->private_value;
452
453 kfree(w->kcontrols[i]->tlv.p);
454
455 snd_ctl_remove(card, w->kcontrols[i]);
456 kfree(sm);
457 }
458 kfree(w->kcontrol_news);
459 }
460 /* widget w is freed by soc-dapm.c */
461}
462
Mengdong Lin64527e82016-01-15 16:13:28 +0800463/* remove DAI configurations */
464static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100465 struct snd_soc_dobj *dobj, int pass)
466{
Mengdong Lin64527e82016-01-15 16:13:28 +0800467 struct snd_soc_dai_driver *dai_drv =
468 container_of(dobj, struct snd_soc_dai_driver, dobj);
469
Liam Girdwood8a978232015-05-29 19:06:14 +0100470 if (pass != SOC_TPLG_PASS_PCM_DAI)
471 return;
472
Mengdong Lin64527e82016-01-15 16:13:28 +0800473 if (dobj->ops && dobj->ops->dai_unload)
474 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100475
476 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800477 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100478}
479
Mengdong Linacfc7d42016-01-15 16:13:37 +0800480/* remove link configurations */
481static void remove_link(struct snd_soc_component *comp,
482 struct snd_soc_dobj *dobj, int pass)
483{
484 struct snd_soc_dai_link *link =
485 container_of(dobj, struct snd_soc_dai_link, dobj);
486
487 if (pass != SOC_TPLG_PASS_PCM_DAI)
488 return;
489
490 if (dobj->ops && dobj->ops->link_unload)
491 dobj->ops->link_unload(comp, dobj);
492
493 list_del(&dobj->list);
494 snd_soc_remove_dai_link(comp->card, link);
495 kfree(link);
496}
497
Liam Girdwood8a978232015-05-29 19:06:14 +0100498/* bind a kcontrol to it's IO handlers */
499static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
500 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800501 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100502{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800503 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800504 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800505 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100506
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800507 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
508 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
509 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
510 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
511 struct soc_bytes_ext *sbe;
512 struct snd_soc_tplg_bytes_control *be;
513
514 sbe = (struct soc_bytes_ext *)k->private_value;
515 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
516
517 /* TLV bytes controls need standard kcontrol info handler,
518 * TLV callback and extended put/get handlers.
519 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530520 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800521 k->tlv.c = snd_soc_bytes_tlv_callback;
522
523 ext_ops = tplg->bytes_ext_ops;
524 num_ops = tplg->bytes_ext_ops_count;
525 for (i = 0; i < num_ops; i++) {
526 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
527 sbe->put = ext_ops[i].put;
528 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
529 sbe->get = ext_ops[i].get;
530 }
531
532 if (sbe->put && sbe->get)
533 return 0;
534 else
535 return -EINVAL;
536 }
537
Mengdong Lin88a17d82015-08-18 18:11:51 +0800538 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800539 ops = tplg->io_ops;
540 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100541 for (i = 0; i < num_ops; i++) {
542
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800543 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100544 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800545 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100546 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800547 if (k->info == NULL && ops[i].id == hdr->ops.info)
548 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100549 }
550
Mengdong Lin88a17d82015-08-18 18:11:51 +0800551 /* vendor specific handlers found ? */
552 if (k->put && k->get && k->info)
553 return 0;
554
555 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800556 ops = io_ops;
557 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800558 for (i = 0; i < num_ops; i++) {
559
560 if (k->put == NULL && ops[i].id == hdr->ops.put)
561 k->put = ops[i].put;
562 if (k->get == NULL && ops[i].id == hdr->ops.get)
563 k->get = ops[i].get;
564 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100565 k->info = ops[i].info;
566 }
567
568 /* standard handlers found ? */
569 if (k->put && k->get && k->info)
570 return 0;
571
Liam Girdwood8a978232015-05-29 19:06:14 +0100572 /* nothing to bind */
573 return -EINVAL;
574}
575
576/* bind a widgets to it's evnt handlers */
577int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
578 const struct snd_soc_tplg_widget_events *events,
579 int num_events, u16 event_type)
580{
581 int i;
582
583 w->event = NULL;
584
585 for (i = 0; i < num_events; i++) {
586 if (event_type == events[i].type) {
587
588 /* found - so assign event */
589 w->event = events[i].event_handler;
590 return 0;
591 }
592 }
593
594 /* not found */
595 return -EINVAL;
596}
597EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
598
599/* optionally pass new dynamic kcontrol to component driver. */
600static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
601 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
602{
603 if (tplg->comp && tplg->ops && tplg->ops->control_load)
604 return tplg->ops->control_load(tplg->comp, k, hdr);
605
606 return 0;
607}
608
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100609
610static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
611 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100612{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100613 unsigned int item_len = 2 * sizeof(unsigned int);
614 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100615
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100616 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
617 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100618 return -ENOMEM;
619
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100620 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
621 p[1] = item_len;
622 p[2] = scale->min;
623 p[3] = (scale->step & TLV_DB_SCALE_MASK)
624 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100625
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100626 kc->tlv.p = (void *)p;
627 return 0;
628}
629
630static int soc_tplg_create_tlv(struct soc_tplg *tplg,
631 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
632{
633 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
634
635 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
636 return 0;
637
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800638 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100639 tplg_tlv = &tc->tlv;
640 switch (tplg_tlv->type) {
641 case SNDRV_CTL_TLVT_DB_SCALE:
642 return soc_tplg_create_tlv_db_scale(tplg, kc,
643 &tplg_tlv->scale);
644
645 /* TODO: add support for other TLV types */
646 default:
647 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
648 tplg_tlv->type);
649 return -EINVAL;
650 }
651 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100652
653 return 0;
654}
655
656static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
657 struct snd_kcontrol_new *kc)
658{
659 kfree(kc->tlv.p);
660}
661
662static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
663 size_t size)
664{
665 struct snd_soc_tplg_bytes_control *be;
666 struct soc_bytes_ext *sbe;
667 struct snd_kcontrol_new kc;
668 int i, err;
669
670 if (soc_tplg_check_elem_count(tplg,
671 sizeof(struct snd_soc_tplg_bytes_control), count,
672 size, "mixer bytes")) {
673 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
674 count);
675 return -EINVAL;
676 }
677
678 for (i = 0; i < count; i++) {
679 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
680
681 /* validate kcontrol */
682 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
683 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
684 return -EINVAL;
685
686 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
687 if (sbe == NULL)
688 return -ENOMEM;
689
690 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
691 be->priv.size);
692
693 dev_dbg(tplg->dev,
694 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
695 be->hdr.name, be->hdr.access);
696
697 memset(&kc, 0, sizeof(kc));
698 kc.name = be->hdr.name;
699 kc.private_value = (long)sbe;
700 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
701 kc.access = be->hdr.access;
702
703 sbe->max = be->max;
704 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
705 sbe->dobj.ops = tplg->ops;
706 INIT_LIST_HEAD(&sbe->dobj.list);
707
708 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800709 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100710 if (err) {
711 soc_control_err(tplg, &be->hdr, be->hdr.name);
712 kfree(sbe);
713 continue;
714 }
715
716 /* pass control to driver for optional further init */
717 err = soc_tplg_init_kcontrol(tplg, &kc,
718 (struct snd_soc_tplg_ctl_hdr *)be);
719 if (err < 0) {
720 dev_err(tplg->dev, "ASoC: failed to init %s\n",
721 be->hdr.name);
722 kfree(sbe);
723 continue;
724 }
725
726 /* register control here */
727 err = soc_tplg_add_kcontrol(tplg, &kc,
728 &sbe->dobj.control.kcontrol);
729 if (err < 0) {
730 dev_err(tplg->dev, "ASoC: failed to add %s\n",
731 be->hdr.name);
732 kfree(sbe);
733 continue;
734 }
735
736 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
737 }
738 return 0;
739
740}
741
742static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
743 size_t size)
744{
745 struct snd_soc_tplg_mixer_control *mc;
746 struct soc_mixer_control *sm;
747 struct snd_kcontrol_new kc;
748 int i, err;
749
750 if (soc_tplg_check_elem_count(tplg,
751 sizeof(struct snd_soc_tplg_mixer_control),
752 count, size, "mixers")) {
753
754 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
755 count);
756 return -EINVAL;
757 }
758
759 for (i = 0; i < count; i++) {
760 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
761
762 /* validate kcontrol */
763 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
764 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
765 return -EINVAL;
766
767 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
768 if (sm == NULL)
769 return -ENOMEM;
770 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
771 mc->priv.size);
772
773 dev_dbg(tplg->dev,
774 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
775 mc->hdr.name, mc->hdr.access);
776
777 memset(&kc, 0, sizeof(kc));
778 kc.name = mc->hdr.name;
779 kc.private_value = (long)sm;
780 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
781 kc.access = mc->hdr.access;
782
783 /* we only support FL/FR channel mapping atm */
784 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
785 SNDRV_CHMAP_FL);
786 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
787 SNDRV_CHMAP_FR);
788 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
789 SNDRV_CHMAP_FL);
790 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
791 SNDRV_CHMAP_FR);
792
793 sm->max = mc->max;
794 sm->min = mc->min;
795 sm->invert = mc->invert;
796 sm->platform_max = mc->platform_max;
797 sm->dobj.index = tplg->index;
798 sm->dobj.ops = tplg->ops;
799 sm->dobj.type = SND_SOC_DOBJ_MIXER;
800 INIT_LIST_HEAD(&sm->dobj.list);
801
802 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800803 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100804 if (err) {
805 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
806 kfree(sm);
807 continue;
808 }
809
810 /* pass control to driver for optional further init */
811 err = soc_tplg_init_kcontrol(tplg, &kc,
812 (struct snd_soc_tplg_ctl_hdr *) mc);
813 if (err < 0) {
814 dev_err(tplg->dev, "ASoC: failed to init %s\n",
815 mc->hdr.name);
816 kfree(sm);
817 continue;
818 }
819
820 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100821 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100822
823 /* register control here */
824 err = soc_tplg_add_kcontrol(tplg, &kc,
825 &sm->dobj.control.kcontrol);
826 if (err < 0) {
827 dev_err(tplg->dev, "ASoC: failed to add %s\n",
828 mc->hdr.name);
829 soc_tplg_free_tlv(tplg, &kc);
830 kfree(sm);
831 continue;
832 }
833
834 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
835 }
836
837 return 0;
838}
839
840static int soc_tplg_denum_create_texts(struct soc_enum *se,
841 struct snd_soc_tplg_enum_control *ec)
842{
843 int i, ret;
844
845 se->dobj.control.dtexts =
846 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
847 if (se->dobj.control.dtexts == NULL)
848 return -ENOMEM;
849
850 for (i = 0; i < ec->items; i++) {
851
852 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
853 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
854 ret = -EINVAL;
855 goto err;
856 }
857
858 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
859 if (!se->dobj.control.dtexts[i]) {
860 ret = -ENOMEM;
861 goto err;
862 }
863 }
864
865 return 0;
866
867err:
868 for (--i; i >= 0; i--)
869 kfree(se->dobj.control.dtexts[i]);
870 kfree(se->dobj.control.dtexts);
871 return ret;
872}
873
874static int soc_tplg_denum_create_values(struct soc_enum *se,
875 struct snd_soc_tplg_enum_control *ec)
876{
877 if (ec->items > sizeof(*ec->values))
878 return -EINVAL;
879
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200880 se->dobj.control.dvalues = kmemdup(ec->values,
881 ec->items * sizeof(u32),
882 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100883 if (!se->dobj.control.dvalues)
884 return -ENOMEM;
885
Liam Girdwood8a978232015-05-29 19:06:14 +0100886 return 0;
887}
888
889static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
890 size_t size)
891{
892 struct snd_soc_tplg_enum_control *ec;
893 struct soc_enum *se;
894 struct snd_kcontrol_new kc;
895 int i, ret, err;
896
897 if (soc_tplg_check_elem_count(tplg,
898 sizeof(struct snd_soc_tplg_enum_control),
899 count, size, "enums")) {
900
901 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
902 count);
903 return -EINVAL;
904 }
905
906 for (i = 0; i < count; i++) {
907 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
908 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
909 ec->priv.size);
910
911 /* validate kcontrol */
912 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
913 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
914 return -EINVAL;
915
916 se = kzalloc((sizeof(*se)), GFP_KERNEL);
917 if (se == NULL)
918 return -ENOMEM;
919
920 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
921 ec->hdr.name, ec->items);
922
923 memset(&kc, 0, sizeof(kc));
924 kc.name = ec->hdr.name;
925 kc.private_value = (long)se;
926 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
927 kc.access = ec->hdr.access;
928
929 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
930 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
931 SNDRV_CHMAP_FL);
932 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
933 SNDRV_CHMAP_FL);
934
935 se->items = ec->items;
936 se->mask = ec->mask;
937 se->dobj.index = tplg->index;
938 se->dobj.type = SND_SOC_DOBJ_ENUM;
939 se->dobj.ops = tplg->ops;
940 INIT_LIST_HEAD(&se->dobj.list);
941
942 switch (ec->hdr.ops.info) {
943 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
944 case SND_SOC_TPLG_CTL_ENUM_VALUE:
945 err = soc_tplg_denum_create_values(se, ec);
946 if (err < 0) {
947 dev_err(tplg->dev,
948 "ASoC: could not create values for %s\n",
949 ec->hdr.name);
950 kfree(se);
951 continue;
952 }
953 /* fall through and create texts */
954 case SND_SOC_TPLG_CTL_ENUM:
955 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
956 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
957 err = soc_tplg_denum_create_texts(se, ec);
958 if (err < 0) {
959 dev_err(tplg->dev,
960 "ASoC: could not create texts for %s\n",
961 ec->hdr.name);
962 kfree(se);
963 continue;
964 }
965 break;
966 default:
967 dev_err(tplg->dev,
968 "ASoC: invalid enum control type %d for %s\n",
969 ec->hdr.ops.info, ec->hdr.name);
970 kfree(se);
971 continue;
972 }
973
974 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800975 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100976 if (err) {
977 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
978 kfree(se);
979 continue;
980 }
981
982 /* pass control to driver for optional further init */
983 err = soc_tplg_init_kcontrol(tplg, &kc,
984 (struct snd_soc_tplg_ctl_hdr *) ec);
985 if (err < 0) {
986 dev_err(tplg->dev, "ASoC: failed to init %s\n",
987 ec->hdr.name);
988 kfree(se);
989 continue;
990 }
991
992 /* register control here */
993 ret = soc_tplg_add_kcontrol(tplg,
994 &kc, &se->dobj.control.kcontrol);
995 if (ret < 0) {
996 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
997 ec->hdr.name);
998 kfree(se);
999 continue;
1000 }
1001
1002 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1003 }
1004
1005 return 0;
1006}
1007
1008static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1009 struct snd_soc_tplg_hdr *hdr)
1010{
1011 struct snd_soc_tplg_ctl_hdr *control_hdr;
1012 int i;
1013
1014 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1015 tplg->pos += hdr->size + hdr->payload_size;
1016 return 0;
1017 }
1018
1019 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1020 soc_tplg_get_offset(tplg));
1021
1022 for (i = 0; i < hdr->count; i++) {
1023
1024 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1025
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001026 if (control_hdr->size != sizeof(*control_hdr)) {
1027 dev_err(tplg->dev, "ASoC: invalid control size\n");
1028 return -EINVAL;
1029 }
1030
Liam Girdwood8a978232015-05-29 19:06:14 +01001031 switch (control_hdr->ops.info) {
1032 case SND_SOC_TPLG_CTL_VOLSW:
1033 case SND_SOC_TPLG_CTL_STROBE:
1034 case SND_SOC_TPLG_CTL_VOLSW_SX:
1035 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1036 case SND_SOC_TPLG_CTL_RANGE:
1037 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1038 case SND_SOC_TPLG_DAPM_CTL_PIN:
1039 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1040 break;
1041 case SND_SOC_TPLG_CTL_ENUM:
1042 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1043 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1044 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1045 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1046 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1047 break;
1048 case SND_SOC_TPLG_CTL_BYTES:
1049 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1050 break;
1051 default:
1052 soc_bind_err(tplg, control_hdr, i);
1053 return -EINVAL;
1054 }
1055 }
1056
1057 return 0;
1058}
1059
1060static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1061 struct snd_soc_tplg_hdr *hdr)
1062{
1063 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1064 struct snd_soc_dapm_route route;
1065 struct snd_soc_tplg_dapm_graph_elem *elem;
1066 int count = hdr->count, i;
1067
1068 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1069 tplg->pos += hdr->size + hdr->payload_size;
1070 return 0;
1071 }
1072
1073 if (soc_tplg_check_elem_count(tplg,
1074 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1075 count, hdr->payload_size, "graph")) {
1076
1077 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1078 count);
1079 return -EINVAL;
1080 }
1081
1082 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1083
1084 for (i = 0; i < count; i++) {
1085 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1086 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1087
1088 /* validate routes */
1089 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1090 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1091 return -EINVAL;
1092 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1093 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1094 return -EINVAL;
1095 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1096 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1097 return -EINVAL;
1098
1099 route.source = elem->source;
1100 route.sink = elem->sink;
1101 route.connected = NULL; /* set to NULL atm for tplg users */
1102 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1103 route.control = NULL;
1104 else
1105 route.control = elem->control;
1106
1107 /* add route, but keep going if some fail */
1108 snd_soc_dapm_add_routes(dapm, &route, 1);
1109 }
1110
1111 return 0;
1112}
1113
1114static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1115 struct soc_tplg *tplg, int num_kcontrols)
1116{
1117 struct snd_kcontrol_new *kc;
1118 struct soc_mixer_control *sm;
1119 struct snd_soc_tplg_mixer_control *mc;
1120 int i, err;
1121
Axel Lin4ca7deb2015-08-05 22:34:22 +08001122 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001123 if (kc == NULL)
1124 return NULL;
1125
1126 for (i = 0; i < num_kcontrols; i++) {
1127 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1128 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1129 if (sm == NULL)
1130 goto err;
1131
1132 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1133 mc->priv.size);
1134
1135 /* validate kcontrol */
1136 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1137 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1138 goto err_str;
1139
1140 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1141 mc->hdr.name, i);
1142
1143 kc[i].name = mc->hdr.name;
1144 kc[i].private_value = (long)sm;
1145 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1146 kc[i].access = mc->hdr.access;
1147
1148 /* we only support FL/FR channel mapping atm */
1149 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1150 SNDRV_CHMAP_FL);
1151 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1152 SNDRV_CHMAP_FR);
1153 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1154 SNDRV_CHMAP_FL);
1155 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1156 SNDRV_CHMAP_FR);
1157
1158 sm->max = mc->max;
1159 sm->min = mc->min;
1160 sm->invert = mc->invert;
1161 sm->platform_max = mc->platform_max;
1162 sm->dobj.index = tplg->index;
1163 INIT_LIST_HEAD(&sm->dobj.list);
1164
1165 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001166 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001167 if (err) {
1168 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1169 kfree(sm);
1170 continue;
1171 }
1172
1173 /* pass control to driver for optional further init */
1174 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1175 (struct snd_soc_tplg_ctl_hdr *)mc);
1176 if (err < 0) {
1177 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1178 mc->hdr.name);
1179 kfree(sm);
1180 continue;
1181 }
1182 }
1183 return kc;
1184
1185err_str:
1186 kfree(sm);
1187err:
1188 for (--i; i >= 0; i--)
1189 kfree((void *)kc[i].private_value);
1190 kfree(kc);
1191 return NULL;
1192}
1193
1194static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1195 struct soc_tplg *tplg)
1196{
1197 struct snd_kcontrol_new *kc;
1198 struct snd_soc_tplg_enum_control *ec;
1199 struct soc_enum *se;
1200 int i, err;
1201
1202 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1203 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1204 ec->priv.size);
1205
1206 /* validate kcontrol */
1207 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1208 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1209 return NULL;
1210
1211 kc = kzalloc(sizeof(*kc), GFP_KERNEL);
1212 if (kc == NULL)
1213 return NULL;
1214
1215 se = kzalloc(sizeof(*se), GFP_KERNEL);
1216 if (se == NULL)
1217 goto err;
1218
1219 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1220 ec->hdr.name);
1221
1222 kc->name = ec->hdr.name;
1223 kc->private_value = (long)se;
1224 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1225 kc->access = ec->hdr.access;
1226
1227 /* we only support FL/FR channel mapping atm */
1228 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1229 se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
1230 se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
1231
1232 se->items = ec->items;
1233 se->mask = ec->mask;
1234 se->dobj.index = tplg->index;
1235
1236 switch (ec->hdr.ops.info) {
1237 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1238 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1239 err = soc_tplg_denum_create_values(se, ec);
1240 if (err < 0) {
1241 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1242 ec->hdr.name);
1243 goto err_se;
1244 }
1245 /* fall through to create texts */
1246 case SND_SOC_TPLG_CTL_ENUM:
1247 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1248 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1249 err = soc_tplg_denum_create_texts(se, ec);
1250 if (err < 0) {
1251 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1252 ec->hdr.name);
1253 goto err_se;
1254 }
1255 break;
1256 default:
1257 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1258 ec->hdr.ops.info, ec->hdr.name);
1259 goto err_se;
1260 }
1261
1262 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001263 err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001264 if (err) {
1265 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1266 goto err_se;
1267 }
1268
1269 /* pass control to driver for optional further init */
1270 err = soc_tplg_init_kcontrol(tplg, kc,
1271 (struct snd_soc_tplg_ctl_hdr *)ec);
1272 if (err < 0) {
1273 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1274 ec->hdr.name);
1275 goto err_se;
1276 }
1277
1278 return kc;
1279
1280err_se:
1281 /* free values and texts */
1282 kfree(se->dobj.control.dvalues);
1283 for (i = 0; i < ec->items; i++)
1284 kfree(se->dobj.control.dtexts[i]);
1285
1286 kfree(se);
1287err:
1288 kfree(kc);
1289
1290 return NULL;
1291}
1292
1293static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1294 struct soc_tplg *tplg, int count)
1295{
1296 struct snd_soc_tplg_bytes_control *be;
1297 struct soc_bytes_ext *sbe;
1298 struct snd_kcontrol_new *kc;
1299 int i, err;
1300
Axel Lin4ca7deb2015-08-05 22:34:22 +08001301 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001302 if (!kc)
1303 return NULL;
1304
1305 for (i = 0; i < count; i++) {
1306 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1307
1308 /* validate kcontrol */
1309 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1310 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1311 goto err;
1312
1313 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1314 if (sbe == NULL)
1315 goto err;
1316
1317 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1318 be->priv.size);
1319
1320 dev_dbg(tplg->dev,
1321 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1322 be->hdr.name, be->hdr.access);
1323
Liam Girdwood8a978232015-05-29 19:06:14 +01001324 kc[i].name = be->hdr.name;
1325 kc[i].private_value = (long)sbe;
1326 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1327 kc[i].access = be->hdr.access;
1328
1329 sbe->max = be->max;
1330 INIT_LIST_HEAD(&sbe->dobj.list);
1331
1332 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001333 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001334 if (err) {
1335 soc_control_err(tplg, &be->hdr, be->hdr.name);
1336 kfree(sbe);
1337 continue;
1338 }
1339
1340 /* pass control to driver for optional further init */
1341 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1342 (struct snd_soc_tplg_ctl_hdr *)be);
1343 if (err < 0) {
1344 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1345 be->hdr.name);
1346 kfree(sbe);
1347 continue;
1348 }
1349 }
1350
1351 return kc;
1352
1353err:
1354 for (--i; i >= 0; i--)
1355 kfree((void *)kc[i].private_value);
1356
1357 kfree(kc);
1358 return NULL;
1359}
1360
1361static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1362 struct snd_soc_tplg_dapm_widget *w)
1363{
1364 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1365 struct snd_soc_dapm_widget template, *widget;
1366 struct snd_soc_tplg_ctl_hdr *control_hdr;
1367 struct snd_soc_card *card = tplg->comp->card;
1368 int ret = 0;
1369
1370 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1371 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1372 return -EINVAL;
1373 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1374 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1375 return -EINVAL;
1376
1377 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1378 w->name, w->id);
1379
1380 memset(&template, 0, sizeof(template));
1381
1382 /* map user to kernel widget ID */
1383 template.id = get_widget_id(w->id);
1384 if (template.id < 0)
1385 return template.id;
1386
1387 template.name = kstrdup(w->name, GFP_KERNEL);
1388 if (!template.name)
1389 return -ENOMEM;
1390 template.sname = kstrdup(w->sname, GFP_KERNEL);
1391 if (!template.sname) {
1392 ret = -ENOMEM;
1393 goto err;
1394 }
1395 template.reg = w->reg;
1396 template.shift = w->shift;
1397 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001398 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001399 template.on_val = w->invert ? 0 : 1;
1400 template.off_val = w->invert ? 1 : 0;
1401 template.ignore_suspend = w->ignore_suspend;
1402 template.event_flags = w->event_flags;
1403 template.dobj.index = tplg->index;
1404
1405 tplg->pos +=
1406 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1407 if (w->num_kcontrols == 0) {
1408 template.num_kcontrols = 0;
1409 goto widget;
1410 }
1411
1412 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1413 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1414 w->name, w->num_kcontrols, control_hdr->type);
1415
1416 switch (control_hdr->ops.info) {
1417 case SND_SOC_TPLG_CTL_VOLSW:
1418 case SND_SOC_TPLG_CTL_STROBE:
1419 case SND_SOC_TPLG_CTL_VOLSW_SX:
1420 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1421 case SND_SOC_TPLG_CTL_RANGE:
1422 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1423 template.num_kcontrols = w->num_kcontrols;
1424 template.kcontrol_news =
1425 soc_tplg_dapm_widget_dmixer_create(tplg,
1426 template.num_kcontrols);
1427 if (!template.kcontrol_news) {
1428 ret = -ENOMEM;
1429 goto hdr_err;
1430 }
1431 break;
1432 case SND_SOC_TPLG_CTL_ENUM:
1433 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1434 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1435 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1436 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1437 template.dobj.widget.kcontrol_enum = 1;
1438 template.num_kcontrols = 1;
1439 template.kcontrol_news =
1440 soc_tplg_dapm_widget_denum_create(tplg);
1441 if (!template.kcontrol_news) {
1442 ret = -ENOMEM;
1443 goto hdr_err;
1444 }
1445 break;
1446 case SND_SOC_TPLG_CTL_BYTES:
1447 template.num_kcontrols = w->num_kcontrols;
1448 template.kcontrol_news =
1449 soc_tplg_dapm_widget_dbytes_create(tplg,
1450 template.num_kcontrols);
1451 if (!template.kcontrol_news) {
1452 ret = -ENOMEM;
1453 goto hdr_err;
1454 }
1455 break;
1456 default:
1457 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1458 control_hdr->ops.get, control_hdr->ops.put,
1459 control_hdr->ops.info);
1460 ret = -EINVAL;
1461 goto hdr_err;
1462 }
1463
1464widget:
1465 ret = soc_tplg_widget_load(tplg, &template, w);
1466 if (ret < 0)
1467 goto hdr_err;
1468
1469 /* card dapm mutex is held by the core if we are loading topology
1470 * data during sound card init. */
1471 if (card->instantiated)
1472 widget = snd_soc_dapm_new_control(dapm, &template);
1473 else
1474 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1475 if (widget == NULL) {
1476 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1477 w->name);
1478 goto hdr_err;
1479 }
1480
1481 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1482 widget->dobj.ops = tplg->ops;
1483 widget->dobj.index = tplg->index;
Jeeja KP8ea41672016-05-05 11:19:18 +05301484 kfree(template.sname);
1485 kfree(template.name);
Liam Girdwood8a978232015-05-29 19:06:14 +01001486 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1487 return 0;
1488
1489hdr_err:
1490 kfree(template.sname);
1491err:
1492 kfree(template.name);
1493 return ret;
1494}
1495
1496static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1497 struct snd_soc_tplg_hdr *hdr)
1498{
1499 struct snd_soc_tplg_dapm_widget *widget;
1500 int ret, count = hdr->count, i;
1501
1502 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1503 return 0;
1504
1505 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1506
1507 for (i = 0; i < count; i++) {
1508 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001509 if (widget->size != sizeof(*widget)) {
1510 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1511 return -EINVAL;
1512 }
1513
Liam Girdwood8a978232015-05-29 19:06:14 +01001514 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001515 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001516 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1517 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001518 return ret;
1519 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001520 }
1521
1522 return 0;
1523}
1524
1525static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1526{
1527 struct snd_soc_card *card = tplg->comp->card;
1528 int ret;
1529
1530 /* Card might not have been registered at this point.
1531 * If so, just return success.
1532 */
1533 if (!card || !card->instantiated) {
1534 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1535 "Do not add new widgets now\n");
1536 return 0;
1537 }
1538
1539 ret = snd_soc_dapm_new_widgets(card);
1540 if (ret < 0)
1541 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1542 ret);
1543
1544 return 0;
1545}
1546
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001547static void set_stream_info(struct snd_soc_pcm_stream *stream,
1548 struct snd_soc_tplg_stream_caps *caps)
1549{
1550 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1551 stream->channels_min = caps->channels_min;
1552 stream->channels_max = caps->channels_max;
1553 stream->rates = caps->rates;
1554 stream->rate_min = caps->rate_min;
1555 stream->rate_max = caps->rate_max;
1556 stream->formats = caps->formats;
1557}
1558
Mengdong Lin64527e82016-01-15 16:13:28 +08001559static int soc_tplg_dai_create(struct soc_tplg *tplg,
1560 struct snd_soc_tplg_pcm *pcm)
1561{
1562 struct snd_soc_dai_driver *dai_drv;
1563 struct snd_soc_pcm_stream *stream;
1564 struct snd_soc_tplg_stream_caps *caps;
1565 int ret;
1566
1567 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1568 if (dai_drv == NULL)
1569 return -ENOMEM;
1570
1571 dai_drv->name = pcm->dai_name;
1572 dai_drv->id = pcm->dai_id;
1573
1574 if (pcm->playback) {
1575 stream = &dai_drv->playback;
1576 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001577 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001578 }
1579
1580 if (pcm->capture) {
1581 stream = &dai_drv->capture;
1582 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001583 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001584 }
1585
1586 /* pass control to component driver for optional further init */
1587 ret = soc_tplg_dai_load(tplg, dai_drv);
1588 if (ret < 0) {
1589 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1590 kfree(dai_drv);
1591 return ret;
1592 }
1593
1594 dai_drv->dobj.index = tplg->index;
1595 dai_drv->dobj.ops = tplg->ops;
1596 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1597 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1598
1599 /* register the DAI to the component */
1600 return snd_soc_register_dai(tplg->comp, dai_drv);
1601}
1602
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001603/* create the FE DAI link */
Mengdong Linacfc7d42016-01-15 16:13:37 +08001604static int soc_tplg_link_create(struct soc_tplg *tplg,
1605 struct snd_soc_tplg_pcm *pcm)
1606{
1607 struct snd_soc_dai_link *link;
1608 int ret;
1609
1610 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1611 if (link == NULL)
1612 return -ENOMEM;
1613
1614 link->name = pcm->pcm_name;
1615 link->stream_name = pcm->pcm_name;
Mengdong Linb84fff52016-04-19 13:12:43 +08001616 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001617
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001618 link->cpu_dai_name = pcm->dai_name;
1619 link->codec_name = "snd-soc-dummy";
1620 link->codec_dai_name = "snd-soc-dummy-dai";
1621
1622 /* enable DPCM */
1623 link->dynamic = 1;
1624 link->dpcm_playback = pcm->playback;
1625 link->dpcm_capture = pcm->capture;
1626
Mengdong Linacfc7d42016-01-15 16:13:37 +08001627 /* pass control to component driver for optional further init */
1628 ret = soc_tplg_dai_link_load(tplg, link);
1629 if (ret < 0) {
1630 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1631 kfree(link);
1632 return ret;
1633 }
1634
1635 link->dobj.index = tplg->index;
1636 link->dobj.ops = tplg->ops;
1637 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1638 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1639
1640 snd_soc_add_dai_link(tplg->comp->card, link);
1641 return 0;
1642}
1643
1644/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001645static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1646 struct snd_soc_tplg_pcm *pcm)
1647{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001648 int ret;
1649
1650 ret = soc_tplg_dai_create(tplg, pcm);
1651 if (ret < 0)
1652 return ret;
1653
1654 return soc_tplg_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001655}
1656
1657static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001658 struct snd_soc_tplg_hdr *hdr)
1659{
Mengdong Lin64527e82016-01-15 16:13:28 +08001660 struct snd_soc_tplg_pcm *pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001661 int count = hdr->count;
Mengdong Lin64527e82016-01-15 16:13:28 +08001662 int i;
Liam Girdwood8a978232015-05-29 19:06:14 +01001663
1664 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1665 return 0;
1666
Liam Girdwood8a978232015-05-29 19:06:14 +01001667 if (soc_tplg_check_elem_count(tplg,
Vedang Patel5b2688a2015-09-30 17:28:47 +08001668 sizeof(struct snd_soc_tplg_pcm), count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001669 hdr->payload_size, "PCM DAI")) {
1670 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1671 count);
1672 return -EINVAL;
1673 }
1674
Mengdong Lin64527e82016-01-15 16:13:28 +08001675 /* create the FE DAIs and DAI links */
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001676 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Mengdong Lin64527e82016-01-15 16:13:28 +08001677 for (i = 0; i < count; i++) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001678 if (pcm->size != sizeof(*pcm)) {
1679 dev_err(tplg->dev, "ASoC: invalid pcm size\n");
1680 return -EINVAL;
1681 }
1682
Mengdong Lin64527e82016-01-15 16:13:28 +08001683 soc_tplg_pcm_create(tplg, pcm);
1684 pcm++;
1685 }
1686
Liam Girdwood8a978232015-05-29 19:06:14 +01001687 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Vedang Patel5b2688a2015-09-30 17:28:47 +08001688 tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count;
Liam Girdwood8a978232015-05-29 19:06:14 +01001689
Liam Girdwood8a978232015-05-29 19:06:14 +01001690 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001691}
1692
1693static int soc_tplg_manifest_load(struct soc_tplg *tplg,
1694 struct snd_soc_tplg_hdr *hdr)
1695{
1696 struct snd_soc_tplg_manifest *manifest;
1697
1698 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
1699 return 0;
1700
1701 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001702 if (manifest->size != sizeof(*manifest)) {
1703 dev_err(tplg->dev, "ASoC: invalid manifest size\n");
1704 return -EINVAL;
1705 }
1706
Liam Girdwood8a978232015-05-29 19:06:14 +01001707 tplg->pos += sizeof(struct snd_soc_tplg_manifest);
1708
1709 if (tplg->comp && tplg->ops && tplg->ops->manifest)
1710 return tplg->ops->manifest(tplg->comp, manifest);
1711
1712 dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n");
1713 return 0;
1714}
1715
1716/* validate header magic, size and type */
1717static int soc_valid_header(struct soc_tplg *tplg,
1718 struct snd_soc_tplg_hdr *hdr)
1719{
1720 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
1721 return 0;
1722
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001723 if (hdr->size != sizeof(*hdr)) {
1724 dev_err(tplg->dev,
1725 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
1726 hdr->type, soc_tplg_get_hdr_offset(tplg),
1727 tplg->fw->size);
1728 return -EINVAL;
1729 }
1730
Liam Girdwood8a978232015-05-29 19:06:14 +01001731 /* big endian firmware objects not supported atm */
1732 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
1733 dev_err(tplg->dev,
1734 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1735 tplg->pass, hdr->magic,
1736 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1737 return -EINVAL;
1738 }
1739
1740 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
1741 dev_err(tplg->dev,
1742 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1743 tplg->pass, hdr->magic,
1744 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1745 return -EINVAL;
1746 }
1747
1748 if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
1749 dev_err(tplg->dev,
1750 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1751 tplg->pass, hdr->abi,
1752 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
1753 tplg->fw->size);
1754 return -EINVAL;
1755 }
1756
1757 if (hdr->payload_size == 0) {
1758 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
1759 soc_tplg_get_hdr_offset(tplg));
1760 return -EINVAL;
1761 }
1762
1763 if (tplg->pass == hdr->type)
1764 dev_dbg(tplg->dev,
1765 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
1766 hdr->payload_size, hdr->type, hdr->version,
1767 hdr->vendor_type, tplg->pass);
1768
1769 return 1;
1770}
1771
1772/* check header type and call appropriate handler */
1773static int soc_tplg_load_header(struct soc_tplg *tplg,
1774 struct snd_soc_tplg_hdr *hdr)
1775{
1776 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
1777
1778 /* check for matching ID */
1779 if (hdr->index != tplg->req_index &&
1780 hdr->index != SND_SOC_TPLG_INDEX_ALL)
1781 return 0;
1782
1783 tplg->index = hdr->index;
1784
1785 switch (hdr->type) {
1786 case SND_SOC_TPLG_TYPE_MIXER:
1787 case SND_SOC_TPLG_TYPE_ENUM:
1788 case SND_SOC_TPLG_TYPE_BYTES:
1789 return soc_tplg_kcontrol_elems_load(tplg, hdr);
1790 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
1791 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
1792 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
1793 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
1794 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08001795 return soc_tplg_pcm_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01001796 case SND_SOC_TPLG_TYPE_MANIFEST:
1797 return soc_tplg_manifest_load(tplg, hdr);
1798 default:
1799 /* bespoke vendor data object */
1800 return soc_tplg_vendor_load(tplg, hdr);
1801 }
1802
1803 return 0;
1804}
1805
1806/* process the topology file headers */
1807static int soc_tplg_process_headers(struct soc_tplg *tplg)
1808{
1809 struct snd_soc_tplg_hdr *hdr;
1810 int ret;
1811
1812 tplg->pass = SOC_TPLG_PASS_START;
1813
1814 /* process the header types from start to end */
1815 while (tplg->pass <= SOC_TPLG_PASS_END) {
1816
1817 tplg->hdr_pos = tplg->fw->data;
1818 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1819
1820 while (!soc_tplg_is_eof(tplg)) {
1821
1822 /* make sure header is valid before loading */
1823 ret = soc_valid_header(tplg, hdr);
1824 if (ret < 0)
1825 return ret;
1826 else if (ret == 0)
1827 break;
1828
1829 /* load the header object */
1830 ret = soc_tplg_load_header(tplg, hdr);
1831 if (ret < 0)
1832 return ret;
1833
1834 /* goto next header */
1835 tplg->hdr_pos += hdr->payload_size +
1836 sizeof(struct snd_soc_tplg_hdr);
1837 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1838 }
1839
1840 /* next data type pass */
1841 tplg->pass++;
1842 }
1843
1844 /* signal DAPM we are complete */
1845 ret = soc_tplg_dapm_complete(tplg);
1846 if (ret < 0)
1847 dev_err(tplg->dev,
1848 "ASoC: failed to initialise DAPM from Firmware\n");
1849
1850 return ret;
1851}
1852
1853static int soc_tplg_load(struct soc_tplg *tplg)
1854{
1855 int ret;
1856
1857 ret = soc_tplg_process_headers(tplg);
1858 if (ret == 0)
1859 soc_tplg_complete(tplg);
1860
1861 return ret;
1862}
1863
1864/* load audio component topology from "firmware" file */
1865int snd_soc_tplg_component_load(struct snd_soc_component *comp,
1866 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
1867{
1868 struct soc_tplg tplg;
1869
1870 /* setup parsing context */
1871 memset(&tplg, 0, sizeof(tplg));
1872 tplg.fw = fw;
1873 tplg.dev = comp->dev;
1874 tplg.comp = comp;
1875 tplg.ops = ops;
1876 tplg.req_index = id;
1877 tplg.io_ops = ops->io_ops;
1878 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08001879 tplg.bytes_ext_ops = ops->bytes_ext_ops;
1880 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01001881
1882 return soc_tplg_load(&tplg);
1883}
1884EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
1885
1886/* remove this dynamic widget */
1887void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
1888{
1889 /* make sure we are a widget */
1890 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
1891 return;
1892
1893 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
1894}
1895EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
1896
1897/* remove all dynamic widgets from this DAPM context */
1898void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
1899 u32 index)
1900{
1901 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01001902
1903 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1904
1905 /* make sure we are a widget with correct context */
1906 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
1907 continue;
1908
1909 /* match ID */
1910 if (w->dobj.index != index &&
1911 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
1912 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01001913 /* check and free and dynamic widget kcontrols */
1914 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02001915 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01001916 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02001917 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01001918}
1919EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
1920
1921/* remove dynamic controls from the component driver */
1922int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
1923{
1924 struct snd_soc_dobj *dobj, *next_dobj;
1925 int pass = SOC_TPLG_PASS_END;
1926
1927 /* process the header types from end to start */
1928 while (pass >= SOC_TPLG_PASS_START) {
1929
1930 /* remove mixer controls */
1931 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
1932 list) {
1933
1934 /* match index */
1935 if (dobj->index != index &&
1936 dobj->index != SND_SOC_TPLG_INDEX_ALL)
1937 continue;
1938
1939 switch (dobj->type) {
1940 case SND_SOC_DOBJ_MIXER:
1941 remove_mixer(comp, dobj, pass);
1942 break;
1943 case SND_SOC_DOBJ_ENUM:
1944 remove_enum(comp, dobj, pass);
1945 break;
1946 case SND_SOC_DOBJ_BYTES:
1947 remove_bytes(comp, dobj, pass);
1948 break;
1949 case SND_SOC_DOBJ_WIDGET:
1950 remove_widget(comp, dobj, pass);
1951 break;
1952 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08001953 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01001954 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001955 case SND_SOC_DOBJ_DAI_LINK:
1956 remove_link(comp, dobj, pass);
1957 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01001958 default:
1959 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
1960 dobj->type);
1961 break;
1962 }
1963 }
1964 pass--;
1965 }
1966
1967 /* let caller know if FW can be freed when no objects are left */
1968 return !list_empty(&comp->dobj_list);
1969}
1970EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);