OSDN Git Service

Merge branch 'for-5.5' of https://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[tomoyo/tomoyo-test1.git] / sound / soc / sof / topology.c
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10
11 #include <linux/firmware.h>
12 #include <sound/tlv.h>
13 #include <sound/pcm_params.h>
14 #include <uapi/sound/sof/tokens.h>
15 #include "sof-priv.h"
16 #include "sof-audio.h"
17 #include "ops.h"
18
19 #define COMP_ID_UNASSIGNED              0xffffffff
20 /*
21  * Constants used in the computation of linear volume gain
22  * from dB gain 20th root of 10 in Q1.16 fixed-point notation
23  */
24 #define VOL_TWENTIETH_ROOT_OF_TEN       73533
25 /* 40th root of 10 in Q1.16 fixed-point notation*/
26 #define VOL_FORTIETH_ROOT_OF_TEN        69419
27 /*
28  * Volume fractional word length define to 16 sets
29  * the volume linear gain value to use Qx.16 format
30  */
31 #define VOLUME_FWL      16
32 /* 0.5 dB step value in topology TLV */
33 #define VOL_HALF_DB_STEP        50
34 /* Full volume for default values */
35 #define VOL_ZERO_DB     BIT(VOLUME_FWL)
36
37 /* TLV data items */
38 #define TLV_ITEMS       3
39 #define TLV_MIN         0
40 #define TLV_STEP        1
41 #define TLV_MUTE        2
42
43 /* size of tplg abi in byte */
44 #define SOF_TPLG_ABI_SIZE 3
45
46 struct sof_widget_data {
47         int ctrl_type;
48         int ipc_cmd;
49         struct sof_abi_hdr *pdata;
50         struct snd_sof_control *control;
51 };
52
53 /* send pcm params ipc */
54 static int ipc_pcm_params(struct snd_sof_widget *swidget, int dir)
55 {
56         struct sof_ipc_pcm_params_reply ipc_params_reply;
57         struct snd_soc_component *scomp = swidget->scomp;
58         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
59         struct sof_ipc_pcm_params pcm;
60         struct snd_pcm_hw_params *params;
61         struct snd_sof_pcm *spcm;
62         int ret = 0;
63
64         memset(&pcm, 0, sizeof(pcm));
65
66         /* get runtime PCM params using widget's stream name */
67         spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
68         if (!spcm) {
69                 dev_err(scomp->dev, "error: cannot find PCM for %s\n",
70                         swidget->widget->name);
71                 return -EINVAL;
72         }
73
74         params = &spcm->params[dir];
75
76         /* set IPC PCM params */
77         pcm.hdr.size = sizeof(pcm);
78         pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
79         pcm.comp_id = swidget->comp_id;
80         pcm.params.hdr.size = sizeof(pcm.params);
81         pcm.params.direction = dir;
82         pcm.params.sample_valid_bytes = params_width(params) >> 3;
83         pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
84         pcm.params.rate = params_rate(params);
85         pcm.params.channels = params_channels(params);
86         pcm.params.host_period_bytes = params_period_bytes(params);
87
88         /* set format */
89         switch (params_format(params)) {
90         case SNDRV_PCM_FORMAT_S16:
91                 pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
92                 break;
93         case SNDRV_PCM_FORMAT_S24:
94                 pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
95                 break;
96         case SNDRV_PCM_FORMAT_S32:
97                 pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
98                 break;
99         default:
100                 return -EINVAL;
101         }
102
103         /* send IPC to the DSP */
104         ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
105                                  &ipc_params_reply, sizeof(ipc_params_reply));
106         if (ret < 0)
107                 dev_err(scomp->dev, "error: pcm params failed for %s\n",
108                         swidget->widget->name);
109
110         return ret;
111 }
112
113  /* send stream trigger ipc */
114 static int ipc_trigger(struct snd_sof_widget *swidget, int cmd)
115 {
116         struct snd_soc_component *scomp = swidget->scomp;
117         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
118         struct sof_ipc_stream stream;
119         struct sof_ipc_reply reply;
120         int ret = 0;
121
122         /* set IPC stream params */
123         stream.hdr.size = sizeof(stream);
124         stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd;
125         stream.comp_id = swidget->comp_id;
126
127         /* send IPC to the DSP */
128         ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
129                                  sizeof(stream), &reply, sizeof(reply));
130         if (ret < 0)
131                 dev_err(scomp->dev, "error: failed to trigger %s\n",
132                         swidget->widget->name);
133
134         return ret;
135 }
136
137 static int sof_keyword_dapm_event(struct snd_soc_dapm_widget *w,
138                                   struct snd_kcontrol *k, int event)
139 {
140         struct snd_sof_widget *swidget = w->dobj.private;
141         struct snd_soc_component *scomp;
142         int stream = SNDRV_PCM_STREAM_CAPTURE;
143         struct snd_sof_pcm *spcm;
144         int ret = 0;
145
146         if (!swidget)
147                 return 0;
148
149         scomp = swidget->scomp;
150
151         dev_dbg(scomp->dev, "received event %d for widget %s\n",
152                 event, w->name);
153
154         /* get runtime PCM params using widget's stream name */
155         spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
156         if (!spcm) {
157                 dev_err(scomp->dev, "error: cannot find PCM for %s\n",
158                         swidget->widget->name);
159                 return -EINVAL;
160         }
161
162         /* process events */
163         switch (event) {
164         case SND_SOC_DAPM_PRE_PMU:
165                 if (spcm->stream[stream].suspend_ignored) {
166                         dev_dbg(scomp->dev, "PRE_PMU event ignored, KWD pipeline is already RUNNING\n");
167                         return 0;
168                 }
169
170                 /* set pcm params */
171                 ret = ipc_pcm_params(swidget, stream);
172                 if (ret < 0) {
173                         dev_err(scomp->dev,
174                                 "error: failed to set pcm params for widget %s\n",
175                                 swidget->widget->name);
176                         break;
177                 }
178
179                 /* start trigger */
180                 ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_START);
181                 if (ret < 0)
182                         dev_err(scomp->dev,
183                                 "error: failed to trigger widget %s\n",
184                                 swidget->widget->name);
185                 break;
186         case SND_SOC_DAPM_POST_PMD:
187                 if (spcm->stream[stream].suspend_ignored) {
188                         dev_dbg(scomp->dev, "POST_PMD even ignored, KWD pipeline will remain RUNNING\n");
189                         return 0;
190                 }
191
192                 /* stop trigger */
193                 ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP);
194                 if (ret < 0)
195                         dev_err(scomp->dev,
196                                 "error: failed to trigger widget %s\n",
197                                 swidget->widget->name);
198
199                 /* pcm free */
200                 ret = ipc_trigger(swidget, SOF_IPC_STREAM_PCM_FREE);
201                 if (ret < 0)
202                         dev_err(scomp->dev,
203                                 "error: failed to trigger widget %s\n",
204                                 swidget->widget->name);
205                 break;
206         default:
207                 break;
208         }
209
210         return ret;
211 }
212
213 /* event handlers for keyword detect component */
214 static const struct snd_soc_tplg_widget_events sof_kwd_events[] = {
215         {SOF_KEYWORD_DETECT_DAPM_EVENT, sof_keyword_dapm_event},
216 };
217
218 static inline int get_tlv_data(const int *p, int tlv[TLV_ITEMS])
219 {
220         /* we only support dB scale TLV type at the moment */
221         if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
222                 return -EINVAL;
223
224         /* min value in topology tlv data is multiplied by 100 */
225         tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
226
227         /* volume steps */
228         tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
229                                 TLV_DB_SCALE_MASK);
230
231         /* mute ON/OFF */
232         if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
233                 TLV_DB_SCALE_MUTE) == 0)
234                 tlv[TLV_MUTE] = 0;
235         else
236                 tlv[TLV_MUTE] = 1;
237
238         return 0;
239 }
240
241 /*
242  * Function to truncate an unsigned 64-bit number
243  * by x bits and return 32-bit unsigned number. This
244  * function also takes care of rounding while truncating
245  */
246 static inline u32 vol_shift_64(u64 i, u32 x)
247 {
248         /* do not truncate more than 32 bits */
249         if (x > 32)
250                 x = 32;
251
252         if (x == 0)
253                 return (u32)i;
254
255         return (u32)(((i >> (x - 1)) + 1) >> 1);
256 }
257
258 /*
259  * Function to compute a ^ exp where,
260  * a is a fractional number represented by a fixed-point
261  * integer with a fractional world length of "fwl"
262  * exp is an integer
263  * fwl is the fractional word length
264  * Return value is a fractional number represented by a
265  * fixed-point integer with a fractional word length of "fwl"
266  */
267 static u32 vol_pow32(u32 a, int exp, u32 fwl)
268 {
269         int i, iter;
270         u32 power = 1 << fwl;
271         u64 numerator;
272
273         /* if exponent is 0, return 1 */
274         if (exp == 0)
275                 return power;
276
277         /* determine the number of iterations based on the exponent */
278         if (exp < 0)
279                 iter = exp * -1;
280         else
281                 iter = exp;
282
283         /* mutiply a "iter" times to compute power */
284         for (i = 0; i < iter; i++) {
285                 /*
286                  * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
287                  * Truncate product back to fwl fractional bits with rounding
288                  */
289                 power = vol_shift_64((u64)power * a, fwl);
290         }
291
292         if (exp > 0) {
293                 /* if exp is positive, return the result */
294                 return power;
295         }
296
297         /* if exp is negative, return the multiplicative inverse */
298         numerator = (u64)1 << (fwl << 1);
299         do_div(numerator, power);
300
301         return (u32)numerator;
302 }
303
304 /*
305  * Function to calculate volume gain from TLV data.
306  * This function can only handle gain steps that are multiples of 0.5 dB
307  */
308 static u32 vol_compute_gain(u32 value, int *tlv)
309 {
310         int dB_gain;
311         u32 linear_gain;
312         int f_step;
313
314         /* mute volume */
315         if (value == 0 && tlv[TLV_MUTE])
316                 return 0;
317
318         /*
319          * compute dB gain from tlv. tlv_step
320          * in topology is multiplied by 100
321          */
322         dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
323
324         /*
325          * compute linear gain represented by fixed-point
326          * int with VOLUME_FWL fractional bits
327          */
328         linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
329
330         /* extract the fractional part of volume step */
331         f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
332
333         /* if volume step is an odd multiple of 0.5 dB */
334         if (f_step == VOL_HALF_DB_STEP && (value & 1))
335                 linear_gain = vol_shift_64((u64)linear_gain *
336                                                   VOL_FORTIETH_ROOT_OF_TEN,
337                                                   VOLUME_FWL);
338
339         return linear_gain;
340 }
341
342 /*
343  * Set up volume table for kcontrols from tlv data
344  * "size" specifies the number of entries in the table
345  */
346 static int set_up_volume_table(struct snd_sof_control *scontrol,
347                                int tlv[TLV_ITEMS], int size)
348 {
349         int j;
350
351         /* init the volume table */
352         scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
353         if (!scontrol->volume_table)
354                 return -ENOMEM;
355
356         /* populate the volume table */
357         for (j = 0; j < size ; j++)
358                 scontrol->volume_table[j] = vol_compute_gain(j, tlv);
359
360         return 0;
361 }
362
363 struct sof_dai_types {
364         const char *name;
365         enum sof_ipc_dai_type type;
366 };
367
368 static const struct sof_dai_types sof_dais[] = {
369         {"SSP", SOF_DAI_INTEL_SSP},
370         {"HDA", SOF_DAI_INTEL_HDA},
371         {"DMIC", SOF_DAI_INTEL_DMIC},
372         {"ALH", SOF_DAI_INTEL_ALH},
373         {"SAI", SOF_DAI_IMX_SAI},
374         {"ESAI", SOF_DAI_IMX_ESAI},
375 };
376
377 static enum sof_ipc_dai_type find_dai(const char *name)
378 {
379         int i;
380
381         for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
382                 if (strcmp(name, sof_dais[i].name) == 0)
383                         return sof_dais[i].type;
384         }
385
386         return SOF_DAI_INTEL_NONE;
387 }
388
389 /*
390  * Supported Frame format types and lookup, add new ones to end of list.
391  */
392
393 struct sof_frame_types {
394         const char *name;
395         enum sof_ipc_frame frame;
396 };
397
398 static const struct sof_frame_types sof_frames[] = {
399         {"s16le", SOF_IPC_FRAME_S16_LE},
400         {"s24le", SOF_IPC_FRAME_S24_4LE},
401         {"s32le", SOF_IPC_FRAME_S32_LE},
402         {"float", SOF_IPC_FRAME_FLOAT},
403 };
404
405 static enum sof_ipc_frame find_format(const char *name)
406 {
407         int i;
408
409         for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
410                 if (strcmp(name, sof_frames[i].name) == 0)
411                         return sof_frames[i].frame;
412         }
413
414         /* use s32le if nothing is specified */
415         return SOF_IPC_FRAME_S32_LE;
416 }
417
418 struct sof_process_types {
419         const char *name;
420         enum sof_ipc_process_type type;
421         enum sof_comp_type comp_type;
422 };
423
424 static const struct sof_process_types sof_process[] = {
425         {"EQFIR", SOF_PROCESS_EQFIR, SOF_COMP_EQ_FIR},
426         {"EQIIR", SOF_PROCESS_EQIIR, SOF_COMP_EQ_IIR},
427         {"KEYWORD_DETECT", SOF_PROCESS_KEYWORD_DETECT, SOF_COMP_KEYWORD_DETECT},
428         {"KPB", SOF_PROCESS_KPB, SOF_COMP_KPB},
429         {"CHAN_SELECTOR", SOF_PROCESS_CHAN_SELECTOR, SOF_COMP_SELECTOR},
430         {"MUX", SOF_PROCESS_MUX, SOF_COMP_MUX},
431         {"DEMUX", SOF_PROCESS_DEMUX, SOF_COMP_DEMUX},
432 };
433
434 static enum sof_ipc_process_type find_process(const char *name)
435 {
436         int i;
437
438         for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
439                 if (strcmp(name, sof_process[i].name) == 0)
440                         return sof_process[i].type;
441         }
442
443         return SOF_PROCESS_NONE;
444 }
445
446 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type)
447 {
448         int i;
449
450         for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
451                 if (sof_process[i].type == type)
452                         return sof_process[i].comp_type;
453         }
454
455         return SOF_COMP_NONE;
456 }
457
458 /*
459  * Topology Token Parsing.
460  * New tokens should be added to headers and parsing tables below.
461  */
462
463 struct sof_topology_token {
464         u32 token;
465         u32 type;
466         int (*get_token)(void *elem, void *object, u32 offset, u32 size);
467         u32 offset;
468         u32 size;
469 };
470
471 static int get_token_u32(void *elem, void *object, u32 offset, u32 size)
472 {
473         struct snd_soc_tplg_vendor_value_elem *velem = elem;
474         u32 *val = (u32 *)((u8 *)object + offset);
475
476         *val = le32_to_cpu(velem->value);
477         return 0;
478 }
479
480 static int get_token_u16(void *elem, void *object, u32 offset, u32 size)
481 {
482         struct snd_soc_tplg_vendor_value_elem *velem = elem;
483         u16 *val = (u16 *)((u8 *)object + offset);
484
485         *val = (u16)le32_to_cpu(velem->value);
486         return 0;
487 }
488
489 static int get_token_comp_format(void *elem, void *object, u32 offset, u32 size)
490 {
491         struct snd_soc_tplg_vendor_string_elem *velem = elem;
492         u32 *val = (u32 *)((u8 *)object + offset);
493
494         *val = find_format(velem->string);
495         return 0;
496 }
497
498 static int get_token_dai_type(void *elem, void *object, u32 offset, u32 size)
499 {
500         struct snd_soc_tplg_vendor_string_elem *velem = elem;
501         u32 *val = (u32 *)((u8 *)object + offset);
502
503         *val = find_dai(velem->string);
504         return 0;
505 }
506
507 static int get_token_process_type(void *elem, void *object, u32 offset,
508                                   u32 size)
509 {
510         struct snd_soc_tplg_vendor_string_elem *velem = elem;
511         u32 *val = (u32 *)((u8 *)object + offset);
512
513         *val = find_process(velem->string);
514         return 0;
515 }
516
517 /* Buffers */
518 static const struct sof_topology_token buffer_tokens[] = {
519         {SOF_TKN_BUF_SIZE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
520                 offsetof(struct sof_ipc_buffer, size), 0},
521         {SOF_TKN_BUF_CAPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
522                 offsetof(struct sof_ipc_buffer, caps), 0},
523 };
524
525 /* DAI */
526 static const struct sof_topology_token dai_tokens[] = {
527         {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
528                 offsetof(struct sof_ipc_comp_dai, type), 0},
529         {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
530                 offsetof(struct sof_ipc_comp_dai, dai_index), 0},
531         {SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
532                 offsetof(struct sof_ipc_comp_dai, direction), 0},
533 };
534
535 /* BE DAI link */
536 static const struct sof_topology_token dai_link_tokens[] = {
537         {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
538                 offsetof(struct sof_ipc_dai_config, type), 0},
539         {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
540                 offsetof(struct sof_ipc_dai_config, dai_index), 0},
541 };
542
543 /* scheduling */
544 static const struct sof_topology_token sched_tokens[] = {
545         {SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
546                 offsetof(struct sof_ipc_pipe_new, period), 0},
547         {SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
548                 offsetof(struct sof_ipc_pipe_new, priority), 0},
549         {SOF_TKN_SCHED_MIPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
550                 offsetof(struct sof_ipc_pipe_new, period_mips), 0},
551         {SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
552                 offsetof(struct sof_ipc_pipe_new, core), 0},
553         {SOF_TKN_SCHED_FRAMES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
554                 offsetof(struct sof_ipc_pipe_new, frames_per_sched), 0},
555         {SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
556                 offsetof(struct sof_ipc_pipe_new, time_domain), 0},
557 };
558
559 /* volume */
560 static const struct sof_topology_token volume_tokens[] = {
561         {SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
562                 get_token_u32, offsetof(struct sof_ipc_comp_volume, ramp), 0},
563         {SOF_TKN_VOLUME_RAMP_STEP_MS,
564                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
565                 offsetof(struct sof_ipc_comp_volume, initial_ramp), 0},
566 };
567
568 /* SRC */
569 static const struct sof_topology_token src_tokens[] = {
570         {SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
571                 offsetof(struct sof_ipc_comp_src, source_rate), 0},
572         {SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
573                 offsetof(struct sof_ipc_comp_src, sink_rate), 0},
574 };
575
576 /* ASRC */
577 static const struct sof_topology_token asrc_tokens[] = {
578         {SOF_TKN_ASRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
579                 offsetof(struct sof_ipc_comp_asrc, source_rate), 0},
580         {SOF_TKN_ASRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
581                 offsetof(struct sof_ipc_comp_asrc, sink_rate), 0},
582         {SOF_TKN_ASRC_ASYNCHRONOUS_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
583                 get_token_u32,
584                 offsetof(struct sof_ipc_comp_asrc, asynchronous_mode), 0},
585         {SOF_TKN_ASRC_OPERATION_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
586                 get_token_u32,
587                 offsetof(struct sof_ipc_comp_asrc, operation_mode), 0},
588 };
589
590 /* Tone */
591 static const struct sof_topology_token tone_tokens[] = {
592 };
593
594 /* EFFECT */
595 static const struct sof_topology_token process_tokens[] = {
596         {SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING,
597                 get_token_process_type,
598                 offsetof(struct sof_ipc_comp_process, type), 0},
599 };
600
601 /* PCM */
602 static const struct sof_topology_token pcm_tokens[] = {
603         {SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
604                 offsetof(struct sof_ipc_comp_host, dmac_config), 0},
605 };
606
607 /* PCM */
608 static const struct sof_topology_token stream_tokens[] = {
609         {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3,
610                 SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
611                 offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible), 0},
612         {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3,
613                 SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
614                 offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible), 0},
615 };
616
617 /* Generic components */
618 static const struct sof_topology_token comp_tokens[] = {
619         {SOF_TKN_COMP_PERIOD_SINK_COUNT,
620                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
621                 offsetof(struct sof_ipc_comp_config, periods_sink), 0},
622         {SOF_TKN_COMP_PERIOD_SOURCE_COUNT,
623                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
624                 offsetof(struct sof_ipc_comp_config, periods_source), 0},
625         {SOF_TKN_COMP_FORMAT,
626                 SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
627                 offsetof(struct sof_ipc_comp_config, frame_fmt), 0},
628 };
629
630 /* SSP */
631 static const struct sof_topology_token ssp_tokens[] = {
632         {SOF_TKN_INTEL_SSP_CLKS_CONTROL,
633                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
634                 offsetof(struct sof_ipc_dai_ssp_params, clks_control), 0},
635         {SOF_TKN_INTEL_SSP_MCLK_ID,
636                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
637                 offsetof(struct sof_ipc_dai_ssp_params, mclk_id), 0},
638         {SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
639                 get_token_u32,
640                 offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits), 0},
641         {SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT,
642                 get_token_u16,
643                 offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width), 0},
644         {SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
645                 get_token_u32,
646                 offsetof(struct sof_ipc_dai_ssp_params, quirks), 0},
647         {SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL,
648                 get_token_u16,
649                 offsetof(struct sof_ipc_dai_ssp_params,
650                          tdm_per_slot_padding_flag), 0},
651         {SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD,
652                 get_token_u32,
653                 offsetof(struct sof_ipc_dai_ssp_params, bclk_delay), 0},
654
655 };
656
657 /* DMIC */
658 static const struct sof_topology_token dmic_tokens[] = {
659         {SOF_TKN_INTEL_DMIC_DRIVER_VERSION,
660                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
661                 offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version),
662                 0},
663         {SOF_TKN_INTEL_DMIC_CLK_MIN,
664                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
665                 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min), 0},
666         {SOF_TKN_INTEL_DMIC_CLK_MAX,
667                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
668                 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max), 0},
669         {SOF_TKN_INTEL_DMIC_SAMPLE_RATE,
670                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
671                 offsetof(struct sof_ipc_dai_dmic_params, fifo_fs), 0},
672         {SOF_TKN_INTEL_DMIC_DUTY_MIN,
673                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
674                 offsetof(struct sof_ipc_dai_dmic_params, duty_min), 0},
675         {SOF_TKN_INTEL_DMIC_DUTY_MAX,
676                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
677                 offsetof(struct sof_ipc_dai_dmic_params, duty_max), 0},
678         {SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
679                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
680                 offsetof(struct sof_ipc_dai_dmic_params,
681                          num_pdm_active), 0},
682         {SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH,
683                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
684                 offsetof(struct sof_ipc_dai_dmic_params, fifo_bits), 0},
685         {SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS,
686                 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
687                 offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time), 0},
688
689 };
690
691 /* ESAI */
692 static const struct sof_topology_token esai_tokens[] = {
693         {SOF_TKN_IMX_ESAI_MCLK_ID,
694                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
695                 offsetof(struct sof_ipc_dai_esai_params, mclk_id), 0},
696 };
697
698 /*
699  * DMIC PDM Tokens
700  * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token
701  * as it increments the index while parsing the array of pdm tokens
702  * and determines the correct offset
703  */
704 static const struct sof_topology_token dmic_pdm_tokens[] = {
705         {SOF_TKN_INTEL_DMIC_PDM_CTRL_ID,
706                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
707                 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id),
708                 0},
709         {SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable,
710                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
711                 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a),
712                 0},
713         {SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable,
714                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
715                 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b),
716                 0},
717         {SOF_TKN_INTEL_DMIC_PDM_POLARITY_A,
718                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
719                 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a),
720                 0},
721         {SOF_TKN_INTEL_DMIC_PDM_POLARITY_B,
722                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
723                 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b),
724                 0},
725         {SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE,
726                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
727                 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge),
728                 0},
729         {SOF_TKN_INTEL_DMIC_PDM_SKEW,
730                 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
731                 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew),
732                 0},
733 };
734
735 /* HDA */
736 static const struct sof_topology_token hda_tokens[] = {
737 };
738
739 /* Leds */
740 static const struct sof_topology_token led_tokens[] = {
741         {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
742          offsetof(struct snd_sof_led_control, use_led), 0},
743         {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD,
744          get_token_u32, offsetof(struct snd_sof_led_control, direction), 0},
745 };
746
747 static void sof_parse_uuid_tokens(struct snd_soc_component *scomp,
748                                   void *object,
749                                   const struct sof_topology_token *tokens,
750                                   int count,
751                                   struct snd_soc_tplg_vendor_array *array)
752 {
753         struct snd_soc_tplg_vendor_uuid_elem *elem;
754         int i, j;
755
756         /* parse element by element */
757         for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
758                 elem = &array->uuid[i];
759
760                 /* search for token */
761                 for (j = 0; j < count; j++) {
762                         /* match token type */
763                         if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
764                                 continue;
765
766                         /* match token id */
767                         if (tokens[j].token != le32_to_cpu(elem->token))
768                                 continue;
769
770                         /* matched - now load token */
771                         tokens[j].get_token(elem, object, tokens[j].offset,
772                                             tokens[j].size);
773                 }
774         }
775 }
776
777 static void sof_parse_string_tokens(struct snd_soc_component *scomp,
778                                     void *object,
779                                     const struct sof_topology_token *tokens,
780                                     int count,
781                                     struct snd_soc_tplg_vendor_array *array)
782 {
783         struct snd_soc_tplg_vendor_string_elem *elem;
784         int i, j;
785
786         /* parse element by element */
787         for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
788                 elem = &array->string[i];
789
790                 /* search for token */
791                 for (j = 0; j < count; j++) {
792                         /* match token type */
793                         if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
794                                 continue;
795
796                         /* match token id */
797                         if (tokens[j].token != le32_to_cpu(elem->token))
798                                 continue;
799
800                         /* matched - now load token */
801                         tokens[j].get_token(elem, object, tokens[j].offset,
802                                             tokens[j].size);
803                 }
804         }
805 }
806
807 static void sof_parse_word_tokens(struct snd_soc_component *scomp,
808                                   void *object,
809                                   const struct sof_topology_token *tokens,
810                                   int count,
811                                   struct snd_soc_tplg_vendor_array *array)
812 {
813         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
814         struct snd_soc_tplg_vendor_value_elem *elem;
815         size_t size = sizeof(struct sof_ipc_dai_dmic_pdm_ctrl);
816         int i, j;
817         u32 offset;
818         u32 *index = NULL;
819
820         /* parse element by element */
821         for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
822                 elem = &array->value[i];
823
824                 /* search for token */
825                 for (j = 0; j < count; j++) {
826                         /* match token type */
827                         if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
828                               tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
829                               tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
830                               tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
831                                 continue;
832
833                         /* match token id */
834                         if (tokens[j].token != le32_to_cpu(elem->token))
835                                 continue;
836
837                         /* pdm config array index */
838                         if (sdev->private)
839                                 index = sdev->private;
840
841                         /* matched - determine offset */
842                         switch (tokens[j].token) {
843                         case SOF_TKN_INTEL_DMIC_PDM_CTRL_ID:
844
845                                 /* inc number of pdm array index */
846                                 if (index)
847                                         (*index)++;
848                                 /* fallthrough */
849                         case SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable:
850                         case SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable:
851                         case SOF_TKN_INTEL_DMIC_PDM_POLARITY_A:
852                         case SOF_TKN_INTEL_DMIC_PDM_POLARITY_B:
853                         case SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE:
854                         case SOF_TKN_INTEL_DMIC_PDM_SKEW:
855
856                                 /* check if array index is valid */
857                                 if (!index || *index == 0) {
858                                         dev_err(scomp->dev,
859                                                 "error: invalid array offset\n");
860                                         continue;
861                                 } else {
862                                         /* offset within the pdm config array */
863                                         offset = size * (*index - 1);
864                                 }
865                                 break;
866                         default:
867                                 offset = 0;
868                                 break;
869                         }
870
871                         /* load token */
872                         tokens[j].get_token(elem, object,
873                                             offset + tokens[j].offset,
874                                             tokens[j].size);
875                 }
876         }
877 }
878
879 static int sof_parse_tokens(struct snd_soc_component *scomp,
880                             void *object,
881                             const struct sof_topology_token *tokens,
882                             int count,
883                             struct snd_soc_tplg_vendor_array *array,
884                             int priv_size)
885 {
886         int asize;
887
888         while (priv_size > 0) {
889                 asize = le32_to_cpu(array->size);
890
891                 /* validate asize */
892                 if (asize < 0) { /* FIXME: A zero-size array makes no sense */
893                         dev_err(scomp->dev, "error: invalid array size 0x%x\n",
894                                 asize);
895                         return -EINVAL;
896                 }
897
898                 /* make sure there is enough data before parsing */
899                 priv_size -= asize;
900                 if (priv_size < 0) {
901                         dev_err(scomp->dev, "error: invalid array size 0x%x\n",
902                                 asize);
903                         return -EINVAL;
904                 }
905
906                 /* call correct parser depending on type */
907                 switch (le32_to_cpu(array->type)) {
908                 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
909                         sof_parse_uuid_tokens(scomp, object, tokens, count,
910                                               array);
911                         break;
912                 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
913                         sof_parse_string_tokens(scomp, object, tokens, count,
914                                                 array);
915                         break;
916                 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
917                 case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
918                 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
919                 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
920                         sof_parse_word_tokens(scomp, object, tokens, count,
921                                               array);
922                         break;
923                 default:
924                         dev_err(scomp->dev, "error: unknown token type %d\n",
925                                 array->type);
926                         return -EINVAL;
927                 }
928
929                 /* next array */
930                 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
931                         + asize);
932         }
933         return 0;
934 }
935
936 static void sof_dbg_comp_config(struct snd_soc_component *scomp,
937                                 struct sof_ipc_comp_config *config)
938 {
939         dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n",
940                 config->periods_sink, config->periods_source,
941                 config->frame_fmt);
942 }
943
944 /*
945  * Standard Kcontrols.
946  */
947
948 static int sof_control_load_volume(struct snd_soc_component *scomp,
949                                    struct snd_sof_control *scontrol,
950                                    struct snd_kcontrol_new *kc,
951                                    struct snd_soc_tplg_ctl_hdr *hdr)
952 {
953         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
954         struct snd_soc_tplg_mixer_control *mc =
955                 container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
956         struct sof_ipc_ctrl_data *cdata;
957         int tlv[TLV_ITEMS];
958         unsigned int i;
959         int ret = 0;
960
961         /* validate topology data */
962         if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN) {
963                 ret = -EINVAL;
964                 goto out;
965         }
966
967         /* init the volume get/put data */
968         scontrol->size = struct_size(scontrol->control_data, chanv,
969                                      le32_to_cpu(mc->num_channels));
970         scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
971         if (!scontrol->control_data) {
972                 ret = -ENOMEM;
973                 goto out;
974         }
975
976         scontrol->comp_id = sdev->next_comp_id;
977         scontrol->min_volume_step = le32_to_cpu(mc->min);
978         scontrol->max_volume_step = le32_to_cpu(mc->max);
979         scontrol->num_channels = le32_to_cpu(mc->num_channels);
980
981         /* set cmd for mixer control */
982         if (le32_to_cpu(mc->max) == 1) {
983                 scontrol->cmd = SOF_CTRL_CMD_SWITCH;
984                 goto skip;
985         }
986
987         scontrol->cmd = SOF_CTRL_CMD_VOLUME;
988
989         /* extract tlv data */
990         if (get_tlv_data(kc->tlv.p, tlv) < 0) {
991                 dev_err(scomp->dev, "error: invalid TLV data\n");
992                 ret = -EINVAL;
993                 goto out_free;
994         }
995
996         /* set up volume table */
997         ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
998         if (ret < 0) {
999                 dev_err(scomp->dev, "error: setting up volume table\n");
1000                 goto out_free;
1001         }
1002
1003         /* set default volume values to 0dB in control */
1004         cdata = scontrol->control_data;
1005         for (i = 0; i < scontrol->num_channels; i++) {
1006                 cdata->chanv[i].channel = i;
1007                 cdata->chanv[i].value = VOL_ZERO_DB;
1008         }
1009
1010 skip:
1011         /* set up possible led control from mixer private data */
1012         ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
1013                                ARRAY_SIZE(led_tokens), mc->priv.array,
1014                                le32_to_cpu(mc->priv.size));
1015         if (ret != 0) {
1016                 dev_err(scomp->dev, "error: parse led tokens failed %d\n",
1017                         le32_to_cpu(mc->priv.size));
1018                 goto out_free_table;
1019         }
1020
1021         dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1022                 scontrol->comp_id, scontrol->num_channels);
1023
1024         return ret;
1025
1026 out_free_table:
1027         if (le32_to_cpu(mc->max) > 1)
1028                 kfree(scontrol->volume_table);
1029 out_free:
1030         kfree(scontrol->control_data);
1031 out:
1032         return ret;
1033 }
1034
1035 static int sof_control_load_enum(struct snd_soc_component *scomp,
1036                                  struct snd_sof_control *scontrol,
1037                                  struct snd_kcontrol_new *kc,
1038                                  struct snd_soc_tplg_ctl_hdr *hdr)
1039 {
1040         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1041         struct snd_soc_tplg_enum_control *ec =
1042                 container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
1043
1044         /* validate topology data */
1045         if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
1046                 return -EINVAL;
1047
1048         /* init the enum get/put data */
1049         scontrol->size = struct_size(scontrol->control_data, chanv,
1050                                      le32_to_cpu(ec->num_channels));
1051         scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1052         if (!scontrol->control_data)
1053                 return -ENOMEM;
1054
1055         scontrol->comp_id = sdev->next_comp_id;
1056         scontrol->num_channels = le32_to_cpu(ec->num_channels);
1057
1058         scontrol->cmd = SOF_CTRL_CMD_ENUM;
1059
1060         dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
1061                 scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
1062
1063         return 0;
1064 }
1065
1066 static int sof_control_load_bytes(struct snd_soc_component *scomp,
1067                                   struct snd_sof_control *scontrol,
1068                                   struct snd_kcontrol_new *kc,
1069                                   struct snd_soc_tplg_ctl_hdr *hdr)
1070 {
1071         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1072         struct sof_ipc_ctrl_data *cdata;
1073         struct snd_soc_tplg_bytes_control *control =
1074                 container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
1075         struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
1076         int max_size = sbe->max;
1077         int ret = 0;
1078
1079         /* init the get/put bytes data */
1080         scontrol->size = sizeof(struct sof_ipc_ctrl_data) +
1081                 le32_to_cpu(control->priv.size);
1082
1083         if (scontrol->size > max_size) {
1084                 dev_err(scomp->dev, "err: bytes data size %d exceeds max %d.\n",
1085                         scontrol->size, max_size);
1086                 ret = -EINVAL;
1087                 goto out;
1088         }
1089
1090         scontrol->control_data = kzalloc(max_size, GFP_KERNEL);
1091         cdata = scontrol->control_data;
1092         if (!scontrol->control_data) {
1093                 ret = -ENOMEM;
1094                 goto out;
1095         }
1096
1097         scontrol->comp_id = sdev->next_comp_id;
1098         scontrol->cmd = SOF_CTRL_CMD_BINARY;
1099
1100         dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1101                 scontrol->comp_id, scontrol->num_channels);
1102
1103         if (le32_to_cpu(control->priv.size) > 0) {
1104                 memcpy(cdata->data, control->priv.data,
1105                        le32_to_cpu(control->priv.size));
1106
1107                 if (cdata->data->magic != SOF_ABI_MAGIC) {
1108                         dev_err(scomp->dev, "error: Wrong ABI magic 0x%08x.\n",
1109                                 cdata->data->magic);
1110                         ret = -EINVAL;
1111                         goto out_free;
1112                 }
1113                 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION,
1114                                                  cdata->data->abi)) {
1115                         dev_err(scomp->dev,
1116                                 "error: Incompatible ABI version 0x%08x.\n",
1117                                 cdata->data->abi);
1118                         ret = -EINVAL;
1119                         goto out_free;
1120                 }
1121                 if (cdata->data->size + sizeof(const struct sof_abi_hdr) !=
1122                     le32_to_cpu(control->priv.size)) {
1123                         dev_err(scomp->dev,
1124                                 "error: Conflict in bytes vs. priv size.\n");
1125                         ret = -EINVAL;
1126                         goto out_free;
1127                 }
1128         }
1129
1130         return ret;
1131
1132 out_free:
1133         kfree(scontrol->control_data);
1134 out:
1135         return ret;
1136 }
1137
1138 /* external kcontrol init - used for any driver specific init */
1139 static int sof_control_load(struct snd_soc_component *scomp, int index,
1140                             struct snd_kcontrol_new *kc,
1141                             struct snd_soc_tplg_ctl_hdr *hdr)
1142 {
1143         struct soc_mixer_control *sm;
1144         struct soc_bytes_ext *sbe;
1145         struct soc_enum *se;
1146         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1147         struct snd_soc_dobj *dobj;
1148         struct snd_sof_control *scontrol;
1149         int ret = -EINVAL;
1150
1151         dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
1152                 hdr->type, hdr->name);
1153
1154         scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
1155         if (!scontrol)
1156                 return -ENOMEM;
1157
1158         scontrol->scomp = scomp;
1159
1160         switch (le32_to_cpu(hdr->ops.info)) {
1161         case SND_SOC_TPLG_CTL_VOLSW:
1162         case SND_SOC_TPLG_CTL_VOLSW_SX:
1163         case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1164                 sm = (struct soc_mixer_control *)kc->private_value;
1165                 dobj = &sm->dobj;
1166                 ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
1167                 break;
1168         case SND_SOC_TPLG_CTL_BYTES:
1169                 sbe = (struct soc_bytes_ext *)kc->private_value;
1170                 dobj = &sbe->dobj;
1171                 ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
1172                 break;
1173         case SND_SOC_TPLG_CTL_ENUM:
1174         case SND_SOC_TPLG_CTL_ENUM_VALUE:
1175                 se = (struct soc_enum *)kc->private_value;
1176                 dobj = &se->dobj;
1177                 ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
1178                 break;
1179         case SND_SOC_TPLG_CTL_RANGE:
1180         case SND_SOC_TPLG_CTL_STROBE:
1181         case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1182         case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1183         case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1184         case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1185         case SND_SOC_TPLG_DAPM_CTL_PIN:
1186         default:
1187                 dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
1188                          hdr->ops.get, hdr->ops.put, hdr->ops.info);
1189                 kfree(scontrol);
1190                 return 0;
1191         }
1192
1193         if (ret < 0) {
1194                 kfree(scontrol);
1195                 return ret;
1196         }
1197
1198         dobj->private = scontrol;
1199         list_add(&scontrol->list, &sdev->kcontrol_list);
1200         return ret;
1201 }
1202
1203 static int sof_control_unload(struct snd_soc_component *scomp,
1204                               struct snd_soc_dobj *dobj)
1205 {
1206         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1207         struct sof_ipc_free fcomp;
1208         struct snd_sof_control *scontrol = dobj->private;
1209
1210         dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scomp->name);
1211
1212         fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE;
1213         fcomp.hdr.size = sizeof(fcomp);
1214         fcomp.id = scontrol->comp_id;
1215
1216         kfree(scontrol->control_data);
1217         list_del(&scontrol->list);
1218         kfree(scontrol);
1219         /* send IPC to the DSP */
1220         return sof_ipc_tx_message(sdev->ipc,
1221                                   fcomp.hdr.cmd, &fcomp, sizeof(fcomp),
1222                                   NULL, 0);
1223 }
1224
1225 /*
1226  * DAI Topology
1227  */
1228
1229 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1230                                   struct snd_soc_dapm_widget *w,
1231                                   struct snd_soc_tplg_dapm_widget *tw,
1232                                   struct snd_sof_dai *dai)
1233 {
1234         struct snd_soc_card *card = scomp->card;
1235         struct snd_soc_pcm_runtime *rtd;
1236
1237         list_for_each_entry(rtd, &card->rtd_list, list) {
1238                 dev_vdbg(scomp->dev, "tplg: check widget: %s stream: %s dai stream: %s\n",
1239                          w->name,  w->sname, rtd->dai_link->stream_name);
1240
1241                 if (!w->sname || !rtd->dai_link->stream_name)
1242                         continue;
1243
1244                 /* does stream match DAI link ? */
1245                 if (strcmp(w->sname, rtd->dai_link->stream_name))
1246                         continue;
1247
1248                 switch (w->id) {
1249                 case snd_soc_dapm_dai_out:
1250                         rtd->cpu_dai->capture_widget = w;
1251                         dai->name = rtd->dai_link->name;
1252                         dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1253                                 w->name, rtd->dai_link->name);
1254                         break;
1255                 case snd_soc_dapm_dai_in:
1256                         rtd->cpu_dai->playback_widget = w;
1257                         dai->name = rtd->dai_link->name;
1258                         dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1259                                 w->name, rtd->dai_link->name);
1260                         break;
1261                 default:
1262                         break;
1263                 }
1264         }
1265
1266         /* check we have a connection */
1267         if (!dai->name) {
1268                 dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1269                         w->name, w->sname);
1270                 return -EINVAL;
1271         }
1272
1273         return 0;
1274 }
1275
1276 static int sof_widget_load_dai(struct snd_soc_component *scomp, int index,
1277                                struct snd_sof_widget *swidget,
1278                                struct snd_soc_tplg_dapm_widget *tw,
1279                                struct sof_ipc_comp_reply *r,
1280                                struct snd_sof_dai *dai)
1281 {
1282         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1283         struct snd_soc_tplg_private *private = &tw->priv;
1284         struct sof_ipc_comp_dai comp_dai;
1285         int ret;
1286
1287         /* configure dai IPC message */
1288         memset(&comp_dai, 0, sizeof(comp_dai));
1289         comp_dai.comp.hdr.size = sizeof(comp_dai);
1290         comp_dai.comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1291         comp_dai.comp.id = swidget->comp_id;
1292         comp_dai.comp.type = SOF_COMP_DAI;
1293         comp_dai.comp.pipeline_id = index;
1294         comp_dai.config.hdr.size = sizeof(comp_dai.config);
1295
1296         ret = sof_parse_tokens(scomp, &comp_dai, dai_tokens,
1297                                ARRAY_SIZE(dai_tokens), private->array,
1298                                le32_to_cpu(private->size));
1299         if (ret != 0) {
1300                 dev_err(scomp->dev, "error: parse dai tokens failed %d\n",
1301                         le32_to_cpu(private->size));
1302                 return ret;
1303         }
1304
1305         ret = sof_parse_tokens(scomp, &comp_dai.config, comp_tokens,
1306                                ARRAY_SIZE(comp_tokens), private->array,
1307                                le32_to_cpu(private->size));
1308         if (ret != 0) {
1309                 dev_err(scomp->dev, "error: parse dai.cfg tokens failed %d\n",
1310                         private->size);
1311                 return ret;
1312         }
1313
1314         dev_dbg(scomp->dev, "dai %s: type %d index %d\n",
1315                 swidget->widget->name, comp_dai.type, comp_dai.dai_index);
1316         sof_dbg_comp_config(scomp, &comp_dai.config);
1317
1318         ret = sof_ipc_tx_message(sdev->ipc, comp_dai.comp.hdr.cmd,
1319                                  &comp_dai, sizeof(comp_dai), r, sizeof(*r));
1320
1321         if (ret == 0 && dai) {
1322                 dai->scomp = scomp;
1323                 memcpy(&dai->comp_dai, &comp_dai, sizeof(comp_dai));
1324         }
1325
1326         return ret;
1327 }
1328
1329 /*
1330  * Buffer topology
1331  */
1332
1333 static int sof_widget_load_buffer(struct snd_soc_component *scomp, int index,
1334                                   struct snd_sof_widget *swidget,
1335                                   struct snd_soc_tplg_dapm_widget *tw,
1336                                   struct sof_ipc_comp_reply *r)
1337 {
1338         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1339         struct snd_soc_tplg_private *private = &tw->priv;
1340         struct sof_ipc_buffer *buffer;
1341         int ret;
1342
1343         buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1344         if (!buffer)
1345                 return -ENOMEM;
1346
1347         /* configure dai IPC message */
1348         buffer->comp.hdr.size = sizeof(*buffer);
1349         buffer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_BUFFER_NEW;
1350         buffer->comp.id = swidget->comp_id;
1351         buffer->comp.type = SOF_COMP_BUFFER;
1352         buffer->comp.pipeline_id = index;
1353
1354         ret = sof_parse_tokens(scomp, buffer, buffer_tokens,
1355                                ARRAY_SIZE(buffer_tokens), private->array,
1356                                le32_to_cpu(private->size));
1357         if (ret != 0) {
1358                 dev_err(scomp->dev, "error: parse buffer tokens failed %d\n",
1359                         private->size);
1360                 kfree(buffer);
1361                 return ret;
1362         }
1363
1364         dev_dbg(scomp->dev, "buffer %s: size %d caps 0x%x\n",
1365                 swidget->widget->name, buffer->size, buffer->caps);
1366
1367         swidget->private = buffer;
1368
1369         ret = sof_ipc_tx_message(sdev->ipc, buffer->comp.hdr.cmd, buffer,
1370                                  sizeof(*buffer), r, sizeof(*r));
1371         if (ret < 0) {
1372                 dev_err(scomp->dev, "error: buffer %s load failed\n",
1373                         swidget->widget->name);
1374                 kfree(buffer);
1375         }
1376
1377         return ret;
1378 }
1379
1380 /* bind PCM ID to host component ID */
1381 static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
1382                      int dir)
1383 {
1384         struct snd_sof_widget *host_widget;
1385
1386         host_widget = snd_sof_find_swidget_sname(scomp,
1387                                                  spcm->pcm.caps[dir].name,
1388                                                  dir);
1389         if (!host_widget) {
1390                 dev_err(scomp->dev, "can't find host comp to bind pcm\n");
1391                 return -EINVAL;
1392         }
1393
1394         spcm->stream[dir].comp_id = host_widget->comp_id;
1395
1396         return 0;
1397 }
1398
1399 /*
1400  * PCM Topology
1401  */
1402
1403 static int sof_widget_load_pcm(struct snd_soc_component *scomp, int index,
1404                                struct snd_sof_widget *swidget,
1405                                enum sof_ipc_stream_direction dir,
1406                                struct snd_soc_tplg_dapm_widget *tw,
1407                                struct sof_ipc_comp_reply *r)
1408 {
1409         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1410         struct snd_soc_tplg_private *private = &tw->priv;
1411         struct sof_ipc_comp_host *host;
1412         int ret;
1413
1414         host = kzalloc(sizeof(*host), GFP_KERNEL);
1415         if (!host)
1416                 return -ENOMEM;
1417
1418         /* configure host comp IPC message */
1419         host->comp.hdr.size = sizeof(*host);
1420         host->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1421         host->comp.id = swidget->comp_id;
1422         host->comp.type = SOF_COMP_HOST;
1423         host->comp.pipeline_id = index;
1424         host->direction = dir;
1425         host->config.hdr.size = sizeof(host->config);
1426
1427         ret = sof_parse_tokens(scomp, host, pcm_tokens,
1428                                ARRAY_SIZE(pcm_tokens), private->array,
1429                                le32_to_cpu(private->size));
1430         if (ret != 0) {
1431                 dev_err(scomp->dev, "error: parse host tokens failed %d\n",
1432                         private->size);
1433                 goto err;
1434         }
1435
1436         ret = sof_parse_tokens(scomp, &host->config, comp_tokens,
1437                                ARRAY_SIZE(comp_tokens), private->array,
1438                                le32_to_cpu(private->size));
1439         if (ret != 0) {
1440                 dev_err(scomp->dev, "error: parse host.cfg tokens failed %d\n",
1441                         le32_to_cpu(private->size));
1442                 goto err;
1443         }
1444
1445         dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name);
1446         sof_dbg_comp_config(scomp, &host->config);
1447
1448         swidget->private = host;
1449
1450         ret = sof_ipc_tx_message(sdev->ipc, host->comp.hdr.cmd, host,
1451                                  sizeof(*host), r, sizeof(*r));
1452         if (ret >= 0)
1453                 return ret;
1454 err:
1455         kfree(host);
1456         return ret;
1457 }
1458
1459 /*
1460  * Pipeline Topology
1461  */
1462 int sof_load_pipeline_ipc(struct device *dev,
1463                           struct sof_ipc_pipe_new *pipeline,
1464                           struct sof_ipc_comp_reply *r)
1465 {
1466         struct snd_sof_dev *sdev = dev_get_drvdata(dev);
1467         struct sof_ipc_pm_core_config pm_core_config;
1468         int ret;
1469
1470         ret = sof_ipc_tx_message(sdev->ipc, pipeline->hdr.cmd, pipeline,
1471                                  sizeof(*pipeline), r, sizeof(*r));
1472         if (ret < 0) {
1473                 dev_err(dev, "error: load pipeline ipc failure\n");
1474                 return ret;
1475         }
1476
1477         /* power up the core that this pipeline is scheduled on */
1478         ret = snd_sof_dsp_core_power_up(sdev, 1 << pipeline->core);
1479         if (ret < 0) {
1480                 dev_err(dev, "error: powering up pipeline schedule core %d\n",
1481                         pipeline->core);
1482                 return ret;
1483         }
1484
1485         /* update enabled cores mask */
1486         sdev->enabled_cores_mask |= 1 << pipeline->core;
1487
1488         /*
1489          * Now notify DSP that the core that this pipeline is scheduled on
1490          * has been powered up
1491          */
1492         memset(&pm_core_config, 0, sizeof(pm_core_config));
1493         pm_core_config.enable_mask = sdev->enabled_cores_mask;
1494
1495         /* configure CORE_ENABLE ipc message */
1496         pm_core_config.hdr.size = sizeof(pm_core_config);
1497         pm_core_config.hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CORE_ENABLE;
1498
1499         /* send ipc */
1500         ret = sof_ipc_tx_message(sdev->ipc, pm_core_config.hdr.cmd,
1501                                  &pm_core_config, sizeof(pm_core_config),
1502                                  &pm_core_config, sizeof(pm_core_config));
1503         if (ret < 0)
1504                 dev_err(dev, "error: core enable ipc failure\n");
1505
1506         return ret;
1507 }
1508
1509 static int sof_widget_load_pipeline(struct snd_soc_component *scomp,
1510                                     int index, struct snd_sof_widget *swidget,
1511                                     struct snd_soc_tplg_dapm_widget *tw,
1512                                     struct sof_ipc_comp_reply *r)
1513 {
1514         struct snd_soc_tplg_private *private = &tw->priv;
1515         struct sof_ipc_pipe_new *pipeline;
1516         struct snd_sof_widget *comp_swidget;
1517         int ret;
1518
1519         pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL);
1520         if (!pipeline)
1521                 return -ENOMEM;
1522
1523         /* configure dai IPC message */
1524         pipeline->hdr.size = sizeof(*pipeline);
1525         pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW;
1526         pipeline->pipeline_id = index;
1527         pipeline->comp_id = swidget->comp_id;
1528
1529         /* component at start of pipeline is our stream id */
1530         comp_swidget = snd_sof_find_swidget(scomp, tw->sname);
1531         if (!comp_swidget) {
1532                 dev_err(scomp->dev, "error: widget %s refers to non existent widget %s\n",
1533                         tw->name, tw->sname);
1534                 ret = -EINVAL;
1535                 goto err;
1536         }
1537
1538         pipeline->sched_id = comp_swidget->comp_id;
1539
1540         dev_dbg(scomp->dev, "tplg: pipeline id %d comp %d scheduling comp id %d\n",
1541                 pipeline->pipeline_id, pipeline->comp_id, pipeline->sched_id);
1542
1543         ret = sof_parse_tokens(scomp, pipeline, sched_tokens,
1544                                ARRAY_SIZE(sched_tokens), private->array,
1545                                le32_to_cpu(private->size));
1546         if (ret != 0) {
1547                 dev_err(scomp->dev, "error: parse pipeline tokens failed %d\n",
1548                         private->size);
1549                 goto err;
1550         }
1551
1552         dev_dbg(scomp->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d\n",
1553                 swidget->widget->name, pipeline->period, pipeline->priority,
1554                 pipeline->period_mips, pipeline->core, pipeline->frames_per_sched);
1555
1556         swidget->private = pipeline;
1557
1558         /* send ipc's to create pipeline comp and power up schedule core */
1559         ret = sof_load_pipeline_ipc(scomp->dev, pipeline, r);
1560         if (ret >= 0)
1561                 return ret;
1562 err:
1563         kfree(pipeline);
1564         return ret;
1565 }
1566
1567 /*
1568  * Mixer topology
1569  */
1570
1571 static int sof_widget_load_mixer(struct snd_soc_component *scomp, int index,
1572                                  struct snd_sof_widget *swidget,
1573                                  struct snd_soc_tplg_dapm_widget *tw,
1574                                  struct sof_ipc_comp_reply *r)
1575 {
1576         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1577         struct snd_soc_tplg_private *private = &tw->priv;
1578         struct sof_ipc_comp_mixer *mixer;
1579         int ret;
1580
1581         mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
1582         if (!mixer)
1583                 return -ENOMEM;
1584
1585         /* configure mixer IPC message */
1586         mixer->comp.hdr.size = sizeof(*mixer);
1587         mixer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1588         mixer->comp.id = swidget->comp_id;
1589         mixer->comp.type = SOF_COMP_MIXER;
1590         mixer->comp.pipeline_id = index;
1591         mixer->config.hdr.size = sizeof(mixer->config);
1592
1593         ret = sof_parse_tokens(scomp, &mixer->config, comp_tokens,
1594                                ARRAY_SIZE(comp_tokens), private->array,
1595                                le32_to_cpu(private->size));
1596         if (ret != 0) {
1597                 dev_err(scomp->dev, "error: parse mixer.cfg tokens failed %d\n",
1598                         private->size);
1599                 kfree(mixer);
1600                 return ret;
1601         }
1602
1603         sof_dbg_comp_config(scomp, &mixer->config);
1604
1605         swidget->private = mixer;
1606
1607         ret = sof_ipc_tx_message(sdev->ipc, mixer->comp.hdr.cmd, mixer,
1608                                  sizeof(*mixer), r, sizeof(*r));
1609         if (ret < 0)
1610                 kfree(mixer);
1611
1612         return ret;
1613 }
1614
1615 /*
1616  * Mux topology
1617  */
1618 static int sof_widget_load_mux(struct snd_soc_component *scomp, int index,
1619                                struct snd_sof_widget *swidget,
1620                                struct snd_soc_tplg_dapm_widget *tw,
1621                                struct sof_ipc_comp_reply *r)
1622 {
1623         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1624         struct snd_soc_tplg_private *private = &tw->priv;
1625         struct sof_ipc_comp_mux *mux;
1626         int ret;
1627
1628         mux = kzalloc(sizeof(*mux), GFP_KERNEL);
1629         if (!mux)
1630                 return -ENOMEM;
1631
1632         /* configure mux IPC message */
1633         mux->comp.hdr.size = sizeof(*mux);
1634         mux->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1635         mux->comp.id = swidget->comp_id;
1636         mux->comp.type = SOF_COMP_MUX;
1637         mux->comp.pipeline_id = index;
1638         mux->config.hdr.size = sizeof(mux->config);
1639
1640         ret = sof_parse_tokens(scomp, &mux->config, comp_tokens,
1641                                ARRAY_SIZE(comp_tokens), private->array,
1642                                le32_to_cpu(private->size));
1643         if (ret != 0) {
1644                 dev_err(scomp->dev, "error: parse mux.cfg tokens failed %d\n",
1645                         private->size);
1646                 kfree(mux);
1647                 return ret;
1648         }
1649
1650         sof_dbg_comp_config(scomp, &mux->config);
1651
1652         swidget->private = mux;
1653
1654         ret = sof_ipc_tx_message(sdev->ipc, mux->comp.hdr.cmd, mux,
1655                                  sizeof(*mux), r, sizeof(*r));
1656         if (ret < 0)
1657                 kfree(mux);
1658
1659         return ret;
1660 }
1661
1662 /*
1663  * PGA Topology
1664  */
1665
1666 static int sof_widget_load_pga(struct snd_soc_component *scomp, int index,
1667                                struct snd_sof_widget *swidget,
1668                                struct snd_soc_tplg_dapm_widget *tw,
1669                                struct sof_ipc_comp_reply *r)
1670 {
1671         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1672         struct snd_soc_tplg_private *private = &tw->priv;
1673         struct sof_ipc_comp_volume *volume;
1674         struct snd_sof_control *scontrol;
1675         int min_step;
1676         int max_step;
1677         int ret;
1678
1679         volume = kzalloc(sizeof(*volume), GFP_KERNEL);
1680         if (!volume)
1681                 return -ENOMEM;
1682
1683         if (!le32_to_cpu(tw->num_kcontrols)) {
1684                 dev_err(scomp->dev, "error: invalid kcontrol count %d for volume\n",
1685                         tw->num_kcontrols);
1686                 ret = -EINVAL;
1687                 goto err;
1688         }
1689
1690         /* configure volume IPC message */
1691         volume->comp.hdr.size = sizeof(*volume);
1692         volume->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1693         volume->comp.id = swidget->comp_id;
1694         volume->comp.type = SOF_COMP_VOLUME;
1695         volume->comp.pipeline_id = index;
1696         volume->config.hdr.size = sizeof(volume->config);
1697
1698         ret = sof_parse_tokens(scomp, volume, volume_tokens,
1699                                ARRAY_SIZE(volume_tokens), private->array,
1700                                le32_to_cpu(private->size));
1701         if (ret != 0) {
1702                 dev_err(scomp->dev, "error: parse volume tokens failed %d\n",
1703                         private->size);
1704                 goto err;
1705         }
1706         ret = sof_parse_tokens(scomp, &volume->config, comp_tokens,
1707                                ARRAY_SIZE(comp_tokens), private->array,
1708                                le32_to_cpu(private->size));
1709         if (ret != 0) {
1710                 dev_err(scomp->dev, "error: parse volume.cfg tokens failed %d\n",
1711                         le32_to_cpu(private->size));
1712                 goto err;
1713         }
1714
1715         sof_dbg_comp_config(scomp, &volume->config);
1716
1717         swidget->private = volume;
1718
1719         list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
1720                 if (scontrol->comp_id == swidget->comp_id &&
1721                     scontrol->volume_table) {
1722                         min_step = scontrol->min_volume_step;
1723                         max_step = scontrol->max_volume_step;
1724                         volume->min_value = scontrol->volume_table[min_step];
1725                         volume->max_value = scontrol->volume_table[max_step];
1726                         volume->channels = scontrol->num_channels;
1727                         break;
1728                 }
1729         }
1730
1731         ret = sof_ipc_tx_message(sdev->ipc, volume->comp.hdr.cmd, volume,
1732                                  sizeof(*volume), r, sizeof(*r));
1733         if (ret >= 0)
1734                 return ret;
1735 err:
1736         kfree(volume);
1737         return ret;
1738 }
1739
1740 /*
1741  * SRC Topology
1742  */
1743
1744 static int sof_widget_load_src(struct snd_soc_component *scomp, int index,
1745                                struct snd_sof_widget *swidget,
1746                                struct snd_soc_tplg_dapm_widget *tw,
1747                                struct sof_ipc_comp_reply *r)
1748 {
1749         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1750         struct snd_soc_tplg_private *private = &tw->priv;
1751         struct sof_ipc_comp_src *src;
1752         int ret;
1753
1754         src = kzalloc(sizeof(*src), GFP_KERNEL);
1755         if (!src)
1756                 return -ENOMEM;
1757
1758         /* configure src IPC message */
1759         src->comp.hdr.size = sizeof(*src);
1760         src->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1761         src->comp.id = swidget->comp_id;
1762         src->comp.type = SOF_COMP_SRC;
1763         src->comp.pipeline_id = index;
1764         src->config.hdr.size = sizeof(src->config);
1765
1766         ret = sof_parse_tokens(scomp, src, src_tokens,
1767                                ARRAY_SIZE(src_tokens), private->array,
1768                                le32_to_cpu(private->size));
1769         if (ret != 0) {
1770                 dev_err(scomp->dev, "error: parse src tokens failed %d\n",
1771                         private->size);
1772                 goto err;
1773         }
1774
1775         ret = sof_parse_tokens(scomp, &src->config, comp_tokens,
1776                                ARRAY_SIZE(comp_tokens), private->array,
1777                                le32_to_cpu(private->size));
1778         if (ret != 0) {
1779                 dev_err(scomp->dev, "error: parse src.cfg tokens failed %d\n",
1780                         le32_to_cpu(private->size));
1781                 goto err;
1782         }
1783
1784         dev_dbg(scomp->dev, "src %s: source rate %d sink rate %d\n",
1785                 swidget->widget->name, src->source_rate, src->sink_rate);
1786         sof_dbg_comp_config(scomp, &src->config);
1787
1788         swidget->private = src;
1789
1790         ret = sof_ipc_tx_message(sdev->ipc, src->comp.hdr.cmd, src,
1791                                  sizeof(*src), r, sizeof(*r));
1792         if (ret >= 0)
1793                 return ret;
1794 err:
1795         kfree(src);
1796         return ret;
1797 }
1798
1799 /*
1800  * ASRC Topology
1801  */
1802
1803 static int sof_widget_load_asrc(struct snd_soc_component *scomp, int index,
1804                                 struct snd_sof_widget *swidget,
1805                                 struct snd_soc_tplg_dapm_widget *tw,
1806                                 struct sof_ipc_comp_reply *r)
1807 {
1808         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1809         struct snd_soc_tplg_private *private = &tw->priv;
1810         struct sof_ipc_comp_asrc *asrc;
1811         int ret;
1812
1813         asrc = kzalloc(sizeof(*asrc), GFP_KERNEL);
1814         if (!asrc)
1815                 return -ENOMEM;
1816
1817         /* configure ASRC IPC message */
1818         asrc->comp.hdr.size = sizeof(*asrc);
1819         asrc->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1820         asrc->comp.id = swidget->comp_id;
1821         asrc->comp.type = SOF_COMP_ASRC;
1822         asrc->comp.pipeline_id = index;
1823         asrc->config.hdr.size = sizeof(asrc->config);
1824
1825         ret = sof_parse_tokens(scomp, asrc, asrc_tokens,
1826                                ARRAY_SIZE(asrc_tokens), private->array,
1827                                le32_to_cpu(private->size));
1828         if (ret != 0) {
1829                 dev_err(scomp->dev, "error: parse asrc tokens failed %d\n",
1830                         private->size);
1831                 goto err;
1832         }
1833
1834         ret = sof_parse_tokens(scomp, &asrc->config, comp_tokens,
1835                                ARRAY_SIZE(comp_tokens), private->array,
1836                                le32_to_cpu(private->size));
1837         if (ret != 0) {
1838                 dev_err(scomp->dev, "error: parse asrc.cfg tokens failed %d\n",
1839                         le32_to_cpu(private->size));
1840                 goto err;
1841         }
1842
1843         dev_dbg(scomp->dev, "asrc %s: source rate %d sink rate %d "
1844                 "asynch %d operation %d\n",
1845                 swidget->widget->name, asrc->source_rate, asrc->sink_rate,
1846                 asrc->asynchronous_mode, asrc->operation_mode);
1847         sof_dbg_comp_config(scomp, &asrc->config);
1848
1849         swidget->private = asrc;
1850
1851         ret = sof_ipc_tx_message(sdev->ipc, asrc->comp.hdr.cmd, asrc,
1852                                  sizeof(*asrc), r, sizeof(*r));
1853         if (ret >= 0)
1854                 return ret;
1855 err:
1856         kfree(asrc);
1857         return ret;
1858 }
1859
1860 /*
1861  * Signal Generator Topology
1862  */
1863
1864 static int sof_widget_load_siggen(struct snd_soc_component *scomp, int index,
1865                                   struct snd_sof_widget *swidget,
1866                                   struct snd_soc_tplg_dapm_widget *tw,
1867                                   struct sof_ipc_comp_reply *r)
1868 {
1869         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1870         struct snd_soc_tplg_private *private = &tw->priv;
1871         struct sof_ipc_comp_tone *tone;
1872         int ret;
1873
1874         tone = kzalloc(sizeof(*tone), GFP_KERNEL);
1875         if (!tone)
1876                 return -ENOMEM;
1877
1878         /* configure siggen IPC message */
1879         tone->comp.hdr.size = sizeof(*tone);
1880         tone->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1881         tone->comp.id = swidget->comp_id;
1882         tone->comp.type = SOF_COMP_TONE;
1883         tone->comp.pipeline_id = index;
1884         tone->config.hdr.size = sizeof(tone->config);
1885
1886         ret = sof_parse_tokens(scomp, tone, tone_tokens,
1887                                ARRAY_SIZE(tone_tokens), private->array,
1888                                le32_to_cpu(private->size));
1889         if (ret != 0) {
1890                 dev_err(scomp->dev, "error: parse tone tokens failed %d\n",
1891                         le32_to_cpu(private->size));
1892                 goto err;
1893         }
1894
1895         ret = sof_parse_tokens(scomp, &tone->config, comp_tokens,
1896                                ARRAY_SIZE(comp_tokens), private->array,
1897                                le32_to_cpu(private->size));
1898         if (ret != 0) {
1899                 dev_err(scomp->dev, "error: parse tone.cfg tokens failed %d\n",
1900                         le32_to_cpu(private->size));
1901                 goto err;
1902         }
1903
1904         dev_dbg(scomp->dev, "tone %s: frequency %d amplitude %d\n",
1905                 swidget->widget->name, tone->frequency, tone->amplitude);
1906         sof_dbg_comp_config(scomp, &tone->config);
1907
1908         swidget->private = tone;
1909
1910         ret = sof_ipc_tx_message(sdev->ipc, tone->comp.hdr.cmd, tone,
1911                                  sizeof(*tone), r, sizeof(*r));
1912         if (ret >= 0)
1913                 return ret;
1914 err:
1915         kfree(tone);
1916         return ret;
1917 }
1918
1919 static int sof_get_control_data(struct snd_soc_component *scomp,
1920                                 struct snd_soc_dapm_widget *widget,
1921                                 struct sof_widget_data *wdata,
1922                                 size_t *size)
1923 {
1924         const struct snd_kcontrol_new *kc;
1925         struct soc_mixer_control *sm;
1926         struct soc_bytes_ext *sbe;
1927         struct soc_enum *se;
1928         int i;
1929
1930         *size = 0;
1931
1932         for (i = 0; i < widget->num_kcontrols; i++) {
1933                 kc = &widget->kcontrol_news[i];
1934
1935                 switch (widget->dobj.widget.kcontrol_type) {
1936                 case SND_SOC_TPLG_TYPE_MIXER:
1937                         sm = (struct soc_mixer_control *)kc->private_value;
1938                         wdata[i].control = sm->dobj.private;
1939                         break;
1940                 case SND_SOC_TPLG_TYPE_BYTES:
1941                         sbe = (struct soc_bytes_ext *)kc->private_value;
1942                         wdata[i].control = sbe->dobj.private;
1943                         break;
1944                 case SND_SOC_TPLG_TYPE_ENUM:
1945                         se = (struct soc_enum *)kc->private_value;
1946                         wdata[i].control = se->dobj.private;
1947                         break;
1948                 default:
1949                         dev_err(scomp->dev, "error: unknown kcontrol type %d in widget %s\n",
1950                                 widget->dobj.widget.kcontrol_type,
1951                                 widget->name);
1952                         return -EINVAL;
1953                 }
1954
1955                 if (!wdata[i].control) {
1956                         dev_err(scomp->dev, "error: no scontrol for widget %s\n",
1957                                 widget->name);
1958                         return -EINVAL;
1959                 }
1960
1961                 wdata[i].pdata = wdata[i].control->control_data->data;
1962                 if (!wdata[i].pdata)
1963                         return -EINVAL;
1964
1965                 /* make sure data is valid - data can be updated at runtime */
1966                 if (wdata[i].pdata->magic != SOF_ABI_MAGIC)
1967                         return -EINVAL;
1968
1969                 *size += wdata[i].pdata->size;
1970
1971                 /* get data type */
1972                 switch (wdata[i].control->cmd) {
1973                 case SOF_CTRL_CMD_VOLUME:
1974                 case SOF_CTRL_CMD_ENUM:
1975                 case SOF_CTRL_CMD_SWITCH:
1976                         wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE;
1977                         wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
1978                         break;
1979                 case SOF_CTRL_CMD_BINARY:
1980                         wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA;
1981                         wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET;
1982                         break;
1983                 default:
1984                         break;
1985                 }
1986         }
1987
1988         return 0;
1989 }
1990
1991 static int sof_process_load(struct snd_soc_component *scomp, int index,
1992                             struct snd_sof_widget *swidget,
1993                             struct snd_soc_tplg_dapm_widget *tw,
1994                             struct sof_ipc_comp_reply *r,
1995                             int type)
1996 {
1997         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1998         struct snd_soc_dapm_widget *widget = swidget->widget;
1999         struct snd_soc_tplg_private *private = &tw->priv;
2000         struct sof_ipc_comp_process *process = NULL;
2001         struct sof_widget_data *wdata = NULL;
2002         size_t ipc_data_size = 0;
2003         size_t ipc_size;
2004         int offset = 0;
2005         int ret = 0;
2006         int i;
2007
2008         if (type == SOF_COMP_NONE) {
2009                 dev_err(scomp->dev, "error: invalid process comp type %d\n",
2010                         type);
2011                 return -EINVAL;
2012         }
2013
2014         /* allocate struct for widget control data sizes and types */
2015         if (widget->num_kcontrols) {
2016                 wdata = kcalloc(widget->num_kcontrols,
2017                                 sizeof(*wdata),
2018                                 GFP_KERNEL);
2019
2020                 if (!wdata)
2021                         return -ENOMEM;
2022
2023                 /* get possible component controls and get size of all pdata */
2024                 ret = sof_get_control_data(scomp, widget, wdata,
2025                                            &ipc_data_size);
2026
2027                 if (ret < 0)
2028                         goto out;
2029         }
2030
2031         ipc_size = sizeof(struct sof_ipc_comp_process) +
2032                 le32_to_cpu(private->size) +
2033                 ipc_data_size;
2034
2035         /* we are exceeding max ipc size, config needs to be sent separately */
2036         if (ipc_size > SOF_IPC_MSG_MAX_SIZE) {
2037                 ipc_size -= ipc_data_size;
2038                 ipc_data_size = 0;
2039         }
2040
2041         process = kzalloc(ipc_size, GFP_KERNEL);
2042         if (!process) {
2043                 ret = -ENOMEM;
2044                 goto out;
2045         }
2046
2047         /* configure iir IPC message */
2048         process->comp.hdr.size = ipc_size;
2049         process->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
2050         process->comp.id = swidget->comp_id;
2051         process->comp.type = type;
2052         process->comp.pipeline_id = index;
2053         process->config.hdr.size = sizeof(process->config);
2054
2055         ret = sof_parse_tokens(scomp, &process->config, comp_tokens,
2056                                ARRAY_SIZE(comp_tokens), private->array,
2057                                le32_to_cpu(private->size));
2058         if (ret != 0) {
2059                 dev_err(scomp->dev, "error: parse process.cfg tokens failed %d\n",
2060                         le32_to_cpu(private->size));
2061                 goto err;
2062         }
2063
2064         sof_dbg_comp_config(scomp, &process->config);
2065
2066         /*
2067          * found private data in control, so copy it.
2068          * get possible component controls - get size of all pdata,
2069          * then memcpy with headers
2070          */
2071         if (ipc_data_size) {
2072                 for (i = 0; i < widget->num_kcontrols; i++) {
2073                         memcpy(&process->data + offset,
2074                                wdata[i].pdata->data,
2075                                wdata[i].pdata->size);
2076                         offset += wdata[i].pdata->size;
2077                 }
2078         }
2079
2080         process->size = ipc_data_size;
2081         swidget->private = process;
2082
2083         ret = sof_ipc_tx_message(sdev->ipc, process->comp.hdr.cmd, process,
2084                                  ipc_size, r, sizeof(*r));
2085
2086         if (ret < 0) {
2087                 dev_err(scomp->dev, "error: create process failed\n");
2088                 goto err;
2089         }
2090
2091         /* we sent the data in single message so return */
2092         if (ipc_data_size)
2093                 goto out;
2094
2095         /* send control data with large message supported method */
2096         for (i = 0; i < widget->num_kcontrols; i++) {
2097                 wdata[i].control->readback_offset = 0;
2098                 ret = snd_sof_ipc_set_get_comp_data(wdata[i].control,
2099                                                     wdata[i].ipc_cmd,
2100                                                     wdata[i].ctrl_type,
2101                                                     wdata[i].control->cmd,
2102                                                     true);
2103                 if (ret != 0) {
2104                         dev_err(scomp->dev, "error: send control failed\n");
2105                         break;
2106                 }
2107         }
2108
2109 err:
2110         if (ret < 0)
2111                 kfree(process);
2112 out:
2113         kfree(wdata);
2114         return ret;
2115 }
2116
2117 /*
2118  * Processing Component Topology - can be "effect", "codec", or general
2119  * "processing".
2120  */
2121
2122 static int sof_widget_load_process(struct snd_soc_component *scomp, int index,
2123                                    struct snd_sof_widget *swidget,
2124                                    struct snd_soc_tplg_dapm_widget *tw,
2125                                    struct sof_ipc_comp_reply *r)
2126 {
2127         struct snd_soc_tplg_private *private = &tw->priv;
2128         struct sof_ipc_comp_process config;
2129         int ret;
2130
2131         /* check we have some tokens - we need at least process type */
2132         if (le32_to_cpu(private->size) == 0) {
2133                 dev_err(scomp->dev, "error: process tokens not found\n");
2134                 return -EINVAL;
2135         }
2136
2137         memset(&config, 0, sizeof(config));
2138
2139         /* get the process token */
2140         ret = sof_parse_tokens(scomp, &config, process_tokens,
2141                                ARRAY_SIZE(process_tokens), private->array,
2142                                le32_to_cpu(private->size));
2143         if (ret != 0) {
2144                 dev_err(scomp->dev, "error: parse process tokens failed %d\n",
2145                         le32_to_cpu(private->size));
2146                 return ret;
2147         }
2148
2149         /* now load process specific data and send IPC */
2150         ret = sof_process_load(scomp, index, swidget, tw, r,
2151                                find_process_comp_type(config.type));
2152         if (ret < 0) {
2153                 dev_err(scomp->dev, "error: process loading failed\n");
2154                 return ret;
2155         }
2156
2157         return 0;
2158 }
2159
2160 static int sof_widget_bind_event(struct snd_soc_component *scomp,
2161                                  struct snd_sof_widget *swidget,
2162                                  u16 event_type)
2163 {
2164         struct sof_ipc_comp *ipc_comp;
2165
2166         /* validate widget event type */
2167         switch (event_type) {
2168         case SOF_KEYWORD_DETECT_DAPM_EVENT:
2169                 /* only KEYWORD_DETECT comps should handle this */
2170                 if (swidget->id != snd_soc_dapm_effect)
2171                         break;
2172
2173                 ipc_comp = swidget->private;
2174                 if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
2175                         break;
2176
2177                 /* bind event to keyword detect comp */
2178                 return snd_soc_tplg_widget_bind_event(swidget->widget,
2179                                                       sof_kwd_events,
2180                                                       ARRAY_SIZE(sof_kwd_events),
2181                                                       event_type);
2182         default:
2183                 break;
2184         }
2185
2186         dev_err(scomp->dev,
2187                 "error: invalid event type %d for widget %s\n",
2188                 event_type, swidget->widget->name);
2189         return -EINVAL;
2190 }
2191
2192 /* external widget init - used for any driver specific init */
2193 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
2194                             struct snd_soc_dapm_widget *w,
2195                             struct snd_soc_tplg_dapm_widget *tw)
2196 {
2197         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2198         struct snd_sof_widget *swidget;
2199         struct snd_sof_dai *dai;
2200         struct sof_ipc_comp_reply reply;
2201         struct snd_sof_control *scontrol;
2202         int ret = 0;
2203
2204         swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
2205         if (!swidget)
2206                 return -ENOMEM;
2207
2208         swidget->scomp = scomp;
2209         swidget->widget = w;
2210         swidget->comp_id = sdev->next_comp_id++;
2211         swidget->complete = 0;
2212         swidget->id = w->id;
2213         swidget->pipeline_id = index;
2214         swidget->private = NULL;
2215         memset(&reply, 0, sizeof(reply));
2216
2217         dev_dbg(scomp->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
2218                 swidget->comp_id, index, swidget->id, tw->name,
2219                 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2220                         ? tw->sname : "none");
2221
2222         /* handle any special case widgets */
2223         switch (w->id) {
2224         case snd_soc_dapm_dai_in:
2225         case snd_soc_dapm_dai_out:
2226                 dai = kzalloc(sizeof(*dai), GFP_KERNEL);
2227                 if (!dai) {
2228                         kfree(swidget);
2229                         return -ENOMEM;
2230                 }
2231
2232                 ret = sof_widget_load_dai(scomp, index, swidget, tw, &reply,
2233                                           dai);
2234                 if (ret == 0) {
2235                         sof_connect_dai_widget(scomp, w, tw, dai);
2236                         list_add(&dai->list, &sdev->dai_list);
2237                         swidget->private = dai;
2238                 } else {
2239                         kfree(dai);
2240                 }
2241                 break;
2242         case snd_soc_dapm_mixer:
2243                 ret = sof_widget_load_mixer(scomp, index, swidget, tw, &reply);
2244                 break;
2245         case snd_soc_dapm_pga:
2246                 ret = sof_widget_load_pga(scomp, index, swidget, tw, &reply);
2247                 /* Find scontrol for this pga and set readback offset*/
2248                 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
2249                         if (scontrol->comp_id == swidget->comp_id) {
2250                                 scontrol->readback_offset = reply.offset;
2251                                 break;
2252                         }
2253                 }
2254                 break;
2255         case snd_soc_dapm_buffer:
2256                 ret = sof_widget_load_buffer(scomp, index, swidget, tw, &reply);
2257                 break;
2258         case snd_soc_dapm_scheduler:
2259                 ret = sof_widget_load_pipeline(scomp, index, swidget, tw,
2260                                                &reply);
2261                 break;
2262         case snd_soc_dapm_aif_out:
2263                 ret = sof_widget_load_pcm(scomp, index, swidget,
2264                                           SOF_IPC_STREAM_CAPTURE, tw, &reply);
2265                 break;
2266         case snd_soc_dapm_aif_in:
2267                 ret = sof_widget_load_pcm(scomp, index, swidget,
2268                                           SOF_IPC_STREAM_PLAYBACK, tw, &reply);
2269                 break;
2270         case snd_soc_dapm_src:
2271                 ret = sof_widget_load_src(scomp, index, swidget, tw, &reply);
2272                 break;
2273         case snd_soc_dapm_asrc:
2274                 ret = sof_widget_load_asrc(scomp, index, swidget, tw, &reply);
2275                 break;
2276         case snd_soc_dapm_siggen:
2277                 ret = sof_widget_load_siggen(scomp, index, swidget, tw, &reply);
2278                 break;
2279         case snd_soc_dapm_effect:
2280                 ret = sof_widget_load_process(scomp, index, swidget, tw,
2281                                               &reply);
2282                 break;
2283         case snd_soc_dapm_mux:
2284         case snd_soc_dapm_demux:
2285                 ret = sof_widget_load_mux(scomp, index, swidget, tw, &reply);
2286                 break;
2287         case snd_soc_dapm_switch:
2288         case snd_soc_dapm_dai_link:
2289         case snd_soc_dapm_kcontrol:
2290         default:
2291                 dev_warn(scomp->dev, "warning: widget type %d name %s not handled\n",
2292                          swidget->id, tw->name);
2293                 break;
2294         }
2295
2296         /* check IPC reply */
2297         if (ret < 0 || reply.rhdr.error < 0) {
2298                 dev_err(scomp->dev,
2299                         "error: DSP failed to add widget id %d type %d name : %s stream %s reply %d\n",
2300                         tw->shift, swidget->id, tw->name,
2301                         strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2302                                 ? tw->sname : "none", reply.rhdr.error);
2303                 kfree(swidget);
2304                 return ret;
2305         }
2306
2307         /* bind widget to external event */
2308         if (tw->event_type) {
2309                 ret = sof_widget_bind_event(scomp, swidget,
2310                                             le16_to_cpu(tw->event_type));
2311                 if (ret) {
2312                         dev_err(scomp->dev, "error: widget event binding failed\n");
2313                         kfree(swidget->private);
2314                         kfree(swidget);
2315                         return ret;
2316                 }
2317         }
2318
2319         w->dobj.private = swidget;
2320         list_add(&swidget->list, &sdev->widget_list);
2321         return ret;
2322 }
2323
2324 static int sof_route_unload(struct snd_soc_component *scomp,
2325                             struct snd_soc_dobj *dobj)
2326 {
2327         struct snd_sof_route *sroute;
2328
2329         sroute = dobj->private;
2330         if (!sroute)
2331                 return 0;
2332
2333         /* free sroute and its private data */
2334         kfree(sroute->private);
2335         list_del(&sroute->list);
2336         kfree(sroute);
2337
2338         return 0;
2339 }
2340
2341 static int sof_widget_unload(struct snd_soc_component *scomp,
2342                              struct snd_soc_dobj *dobj)
2343 {
2344         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2345         const struct snd_kcontrol_new *kc;
2346         struct snd_soc_dapm_widget *widget;
2347         struct sof_ipc_pipe_new *pipeline;
2348         struct snd_sof_control *scontrol;
2349         struct snd_sof_widget *swidget;
2350         struct soc_mixer_control *sm;
2351         struct soc_bytes_ext *sbe;
2352         struct snd_sof_dai *dai;
2353         struct soc_enum *se;
2354         int ret = 0;
2355         int i;
2356
2357         swidget = dobj->private;
2358         if (!swidget)
2359                 return 0;
2360
2361         widget = swidget->widget;
2362
2363         switch (swidget->id) {
2364         case snd_soc_dapm_dai_in:
2365         case snd_soc_dapm_dai_out:
2366                 dai = swidget->private;
2367
2368                 if (dai) {
2369                         /* free dai config */
2370                         kfree(dai->dai_config);
2371                         list_del(&dai->list);
2372                 }
2373                 break;
2374         case snd_soc_dapm_scheduler:
2375
2376                 /* power down the pipeline schedule core */
2377                 pipeline = swidget->private;
2378                 ret = snd_sof_dsp_core_power_down(sdev, 1 << pipeline->core);
2379                 if (ret < 0)
2380                         dev_err(scomp->dev, "error: powering down pipeline schedule core %d\n",
2381                                 pipeline->core);
2382
2383                 /* update enabled cores mask */
2384                 sdev->enabled_cores_mask &= ~(1 << pipeline->core);
2385
2386                 break;
2387         default:
2388                 break;
2389         }
2390         for (i = 0; i < widget->num_kcontrols; i++) {
2391                 kc = &widget->kcontrol_news[i];
2392                 switch (dobj->widget.kcontrol_type) {
2393                 case SND_SOC_TPLG_TYPE_MIXER:
2394                         sm = (struct soc_mixer_control *)kc->private_value;
2395                         scontrol = sm->dobj.private;
2396                         if (sm->max > 1)
2397                                 kfree(scontrol->volume_table);
2398                         break;
2399                 case SND_SOC_TPLG_TYPE_ENUM:
2400                         se = (struct soc_enum *)kc->private_value;
2401                         scontrol = se->dobj.private;
2402                         break;
2403                 case SND_SOC_TPLG_TYPE_BYTES:
2404                         sbe = (struct soc_bytes_ext *)kc->private_value;
2405                         scontrol = sbe->dobj.private;
2406                         break;
2407                 default:
2408                         dev_warn(scomp->dev, "unsupported kcontrol_type\n");
2409                         goto out;
2410                 }
2411                 kfree(scontrol->control_data);
2412                 list_del(&scontrol->list);
2413                 kfree(scontrol);
2414         }
2415
2416 out:
2417         /* free private value */
2418         kfree(swidget->private);
2419
2420         /* remove and free swidget object */
2421         list_del(&swidget->list);
2422         kfree(swidget);
2423
2424         return ret;
2425 }
2426
2427 /*
2428  * DAI HW configuration.
2429  */
2430
2431 /* FE DAI - used for any driver specific init */
2432 static int sof_dai_load(struct snd_soc_component *scomp, int index,
2433                         struct snd_soc_dai_driver *dai_drv,
2434                         struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
2435 {
2436         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2437         struct snd_soc_tplg_stream_caps *caps;
2438         struct snd_soc_tplg_private *private = &pcm->priv;
2439         struct snd_sof_pcm *spcm;
2440         int stream = SNDRV_PCM_STREAM_PLAYBACK;
2441         int ret = 0;
2442
2443         /* nothing to do for BEs atm */
2444         if (!pcm)
2445                 return 0;
2446
2447         spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
2448         if (!spcm)
2449                 return -ENOMEM;
2450
2451         spcm->scomp = scomp;
2452         spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].comp_id = COMP_ID_UNASSIGNED;
2453         spcm->stream[SNDRV_PCM_STREAM_CAPTURE].comp_id = COMP_ID_UNASSIGNED;
2454
2455         spcm->pcm = *pcm;
2456         dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
2457
2458         dai_drv->dobj.private = spcm;
2459         list_add(&spcm->list, &sdev->pcm_list);
2460
2461         ret = sof_parse_tokens(scomp, spcm, stream_tokens,
2462                                ARRAY_SIZE(stream_tokens), private->array,
2463                                le32_to_cpu(private->size));
2464         if (ret) {
2465                 dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
2466                         le32_to_cpu(private->size));
2467                 return ret;
2468         }
2469
2470         /* do we need to allocate playback PCM DMA pages */
2471         if (!spcm->pcm.playback)
2472                 goto capture;
2473
2474         dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: playback d0i3:%d\n",
2475                  spcm->pcm.pcm_name, spcm->stream[0].d0i3_compatible);
2476
2477         caps = &spcm->pcm.caps[stream];
2478
2479         /* allocate playback page table buffer */
2480         ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2481                                   PAGE_SIZE, &spcm->stream[stream].page_table);
2482         if (ret < 0) {
2483                 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2484                         caps->name, ret);
2485
2486                 return ret;
2487         }
2488
2489         /* bind pcm to host comp */
2490         ret = spcm_bind(scomp, spcm, stream);
2491         if (ret) {
2492                 dev_err(scomp->dev,
2493                         "error: can't bind pcm to host\n");
2494                 goto free_playback_tables;
2495         }
2496
2497 capture:
2498         stream = SNDRV_PCM_STREAM_CAPTURE;
2499
2500         /* do we need to allocate capture PCM DMA pages */
2501         if (!spcm->pcm.capture)
2502                 return ret;
2503
2504         dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: capture d0i3:%d\n",
2505                  spcm->pcm.pcm_name, spcm->stream[1].d0i3_compatible);
2506
2507         caps = &spcm->pcm.caps[stream];
2508
2509         /* allocate capture page table buffer */
2510         ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2511                                   PAGE_SIZE, &spcm->stream[stream].page_table);
2512         if (ret < 0) {
2513                 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2514                         caps->name, ret);
2515                 goto free_playback_tables;
2516         }
2517
2518         /* bind pcm to host comp */
2519         ret = spcm_bind(scomp, spcm, stream);
2520         if (ret) {
2521                 dev_err(scomp->dev,
2522                         "error: can't bind pcm to host\n");
2523                 snd_dma_free_pages(&spcm->stream[stream].page_table);
2524                 goto free_playback_tables;
2525         }
2526
2527         return ret;
2528
2529 free_playback_tables:
2530         if (spcm->pcm.playback)
2531                 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2532
2533         return ret;
2534 }
2535
2536 static int sof_dai_unload(struct snd_soc_component *scomp,
2537                           struct snd_soc_dobj *dobj)
2538 {
2539         struct snd_sof_pcm *spcm = dobj->private;
2540
2541         /* free PCM DMA pages */
2542         if (spcm->pcm.playback)
2543                 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2544
2545         if (spcm->pcm.capture)
2546                 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
2547
2548         /* remove from list and free spcm */
2549         list_del(&spcm->list);
2550         kfree(spcm);
2551
2552         return 0;
2553 }
2554
2555 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
2556                                struct sof_ipc_dai_config *config)
2557 {
2558         /* clock directions wrt codec */
2559         if (hw_config->bclk_master == SND_SOC_TPLG_BCLK_CM) {
2560                 /* codec is bclk master */
2561                 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM)
2562                         config->format |= SOF_DAI_FMT_CBM_CFM;
2563                 else
2564                         config->format |= SOF_DAI_FMT_CBM_CFS;
2565         } else {
2566                 /* codec is bclk slave */
2567                 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM)
2568                         config->format |= SOF_DAI_FMT_CBS_CFM;
2569                 else
2570                         config->format |= SOF_DAI_FMT_CBS_CFS;
2571         }
2572
2573         /* inverted clocks ? */
2574         if (hw_config->invert_bclk) {
2575                 if (hw_config->invert_fsync)
2576                         config->format |= SOF_DAI_FMT_IB_IF;
2577                 else
2578                         config->format |= SOF_DAI_FMT_IB_NF;
2579         } else {
2580                 if (hw_config->invert_fsync)
2581                         config->format |= SOF_DAI_FMT_NB_IF;
2582                 else
2583                         config->format |= SOF_DAI_FMT_NB_NF;
2584         }
2585 }
2586
2587 /* set config for all DAI's with name matching the link name */
2588 static int sof_set_dai_config(struct snd_sof_dev *sdev, u32 size,
2589                               struct snd_soc_dai_link *link,
2590                               struct sof_ipc_dai_config *config)
2591 {
2592         struct snd_sof_dai *dai;
2593         int found = 0;
2594
2595         list_for_each_entry(dai, &sdev->dai_list, list) {
2596                 if (!dai->name)
2597                         continue;
2598
2599                 if (strcmp(link->name, dai->name) == 0) {
2600                         dai->dai_config = kmemdup(config, size, GFP_KERNEL);
2601                         if (!dai->dai_config)
2602                                 return -ENOMEM;
2603
2604                         /* set cpu_dai_name */
2605                         dai->cpu_dai_name = link->cpus->dai_name;
2606
2607                         found = 1;
2608                 }
2609         }
2610
2611         /*
2612          * machine driver may define a dai link with playback and capture
2613          * dai enabled, but the dai link in topology would support both, one
2614          * or none of them. Here print a warning message to notify user
2615          */
2616         if (!found) {
2617                 dev_warn(sdev->dev, "warning: failed to find dai for dai link %s",
2618                          link->name);
2619         }
2620
2621         return 0;
2622 }
2623
2624 static int sof_link_ssp_load(struct snd_soc_component *scomp, int index,
2625                              struct snd_soc_dai_link *link,
2626                              struct snd_soc_tplg_link_config *cfg,
2627                              struct snd_soc_tplg_hw_config *hw_config,
2628                              struct sof_ipc_dai_config *config)
2629 {
2630         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2631         struct snd_soc_tplg_private *private = &cfg->priv;
2632         struct sof_ipc_reply reply;
2633         u32 size = sizeof(*config);
2634         int ret;
2635
2636         /* handle master/slave and inverted clocks */
2637         sof_dai_set_format(hw_config, config);
2638
2639         /* init IPC */
2640         memset(&config->ssp, 0, sizeof(struct sof_ipc_dai_ssp_params));
2641         config->hdr.size = size;
2642
2643         ret = sof_parse_tokens(scomp, &config->ssp, ssp_tokens,
2644                                ARRAY_SIZE(ssp_tokens), private->array,
2645                                le32_to_cpu(private->size));
2646         if (ret != 0) {
2647                 dev_err(scomp->dev, "error: parse ssp tokens failed %d\n",
2648                         le32_to_cpu(private->size));
2649                 return ret;
2650         }
2651
2652         config->ssp.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2653         config->ssp.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2654         config->ssp.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2655         config->ssp.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2656         config->ssp.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2657         config->ssp.mclk_direction = hw_config->mclk_direction;
2658         config->ssp.rx_slots = le32_to_cpu(hw_config->rx_slots);
2659         config->ssp.tx_slots = le32_to_cpu(hw_config->tx_slots);
2660
2661         dev_dbg(scomp->dev, "tplg: config SSP%d fmt 0x%x mclk %d bclk %d fclk %d width (%d)%d slots %d mclk id %d quirks %d\n",
2662                 config->dai_index, config->format,
2663                 config->ssp.mclk_rate, config->ssp.bclk_rate,
2664                 config->ssp.fsync_rate, config->ssp.sample_valid_bits,
2665                 config->ssp.tdm_slot_width, config->ssp.tdm_slots,
2666                 config->ssp.mclk_id, config->ssp.quirks);
2667
2668         /* validate SSP fsync rate and channel count */
2669         if (config->ssp.fsync_rate < 8000 || config->ssp.fsync_rate > 192000) {
2670                 dev_err(scomp->dev, "error: invalid fsync rate for SSP%d\n",
2671                         config->dai_index);
2672                 return -EINVAL;
2673         }
2674
2675         if (config->ssp.tdm_slots < 1 || config->ssp.tdm_slots > 8) {
2676                 dev_err(scomp->dev, "error: invalid channel count for SSP%d\n",
2677                         config->dai_index);
2678                 return -EINVAL;
2679         }
2680
2681         /* send message to DSP */
2682         ret = sof_ipc_tx_message(sdev->ipc,
2683                                  config->hdr.cmd, config, size, &reply,
2684                                  sizeof(reply));
2685
2686         if (ret < 0) {
2687                 dev_err(scomp->dev, "error: failed to set DAI config for SSP%d\n",
2688                         config->dai_index);
2689                 return ret;
2690         }
2691
2692         /* set config for all DAI's with name matching the link name */
2693         ret = sof_set_dai_config(sdev, size, link, config);
2694         if (ret < 0)
2695                 dev_err(scomp->dev, "error: failed to save DAI config for SSP%d\n",
2696                         config->dai_index);
2697
2698         return ret;
2699 }
2700
2701 static int sof_link_sai_load(struct snd_soc_component *scomp, int index,
2702                              struct snd_soc_dai_link *link,
2703                              struct snd_soc_tplg_link_config *cfg,
2704                              struct snd_soc_tplg_hw_config *hw_config,
2705                              struct sof_ipc_dai_config *config)
2706 {
2707         /*TODO: Add implementation */
2708         return 0;
2709 }
2710
2711 static int sof_link_esai_load(struct snd_soc_component *scomp, int index,
2712                               struct snd_soc_dai_link *link,
2713                               struct snd_soc_tplg_link_config *cfg,
2714                               struct snd_soc_tplg_hw_config *hw_config,
2715                               struct sof_ipc_dai_config *config)
2716 {
2717         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2718         struct snd_soc_tplg_private *private = &cfg->priv;
2719         struct sof_ipc_reply reply;
2720         u32 size = sizeof(*config);
2721         int ret;
2722
2723         /* handle master/slave and inverted clocks */
2724         sof_dai_set_format(hw_config, config);
2725
2726         /* init IPC */
2727         memset(&config->esai, 0, sizeof(struct sof_ipc_dai_esai_params));
2728         config->hdr.size = size;
2729
2730         ret = sof_parse_tokens(scomp, &config->esai, esai_tokens,
2731                                ARRAY_SIZE(esai_tokens), private->array,
2732                                le32_to_cpu(private->size));
2733         if (ret != 0) {
2734                 dev_err(scomp->dev, "error: parse esai tokens failed %d\n",
2735                         le32_to_cpu(private->size));
2736                 return ret;
2737         }
2738
2739         config->esai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2740         config->esai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2741         config->esai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2742         config->esai.mclk_direction = hw_config->mclk_direction;
2743         config->esai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2744         config->esai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2745         config->esai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2746         config->esai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2747
2748         dev_info(scomp->dev,
2749                  "tplg: config ESAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2750                 config->dai_index, config->format,
2751                 config->esai.mclk_rate, config->esai.tdm_slot_width,
2752                 config->esai.tdm_slots, config->esai.mclk_id);
2753
2754         if (config->esai.tdm_slots < 1 || config->esai.tdm_slots > 8) {
2755                 dev_err(scomp->dev, "error: invalid channel count for ESAI%d\n",
2756                         config->dai_index);
2757                 return -EINVAL;
2758         }
2759
2760         /* send message to DSP */
2761         ret = sof_ipc_tx_message(sdev->ipc,
2762                                  config->hdr.cmd, config, size, &reply,
2763                                  sizeof(reply));
2764         if (ret < 0) {
2765                 dev_err(scomp->dev, "error: failed to set DAI config for ESAI%d\n",
2766                         config->dai_index);
2767                 return ret;
2768         }
2769
2770         /* set config for all DAI's with name matching the link name */
2771         ret = sof_set_dai_config(sdev, size, link, config);
2772         if (ret < 0)
2773                 dev_err(scomp->dev, "error: failed to save DAI config for ESAI%d\n",
2774                         config->dai_index);
2775
2776         return ret;
2777 }
2778
2779 static int sof_link_dmic_load(struct snd_soc_component *scomp, int index,
2780                               struct snd_soc_dai_link *link,
2781                               struct snd_soc_tplg_link_config *cfg,
2782                               struct snd_soc_tplg_hw_config *hw_config,
2783                               struct sof_ipc_dai_config *config)
2784 {
2785         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2786         struct snd_soc_tplg_private *private = &cfg->priv;
2787         struct sof_ipc_dai_config *ipc_config;
2788         struct sof_ipc_reply reply;
2789         struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
2790         struct sof_ipc_fw_version *v = &ready->version;
2791         u32 size;
2792         int ret, j;
2793
2794         /*
2795          * config is only used for the common params in dmic_params structure
2796          * that does not include the PDM controller config array
2797          * Set the common params to 0.
2798          */
2799         memset(&config->dmic, 0, sizeof(struct sof_ipc_dai_dmic_params));
2800
2801         /* get DMIC tokens */
2802         ret = sof_parse_tokens(scomp, &config->dmic, dmic_tokens,
2803                                ARRAY_SIZE(dmic_tokens), private->array,
2804                                le32_to_cpu(private->size));
2805         if (ret != 0) {
2806                 dev_err(scomp->dev, "error: parse dmic tokens failed %d\n",
2807                         le32_to_cpu(private->size));
2808                 return ret;
2809         }
2810
2811         /*
2812          * allocate memory for dmic dai config accounting for the
2813          * variable number of active pdm controllers
2814          * This will be the ipc payload for setting dai config
2815          */
2816         size = sizeof(*config) + sizeof(struct sof_ipc_dai_dmic_pdm_ctrl) *
2817                                         config->dmic.num_pdm_active;
2818
2819         ipc_config = kzalloc(size, GFP_KERNEL);
2820         if (!ipc_config)
2821                 return -ENOMEM;
2822
2823         /* copy the common dai config and dmic params */
2824         memcpy(ipc_config, config, sizeof(*config));
2825
2826         /*
2827          * alloc memory for private member
2828          * Used to track the pdm config array index currently being parsed
2829          */
2830         sdev->private = kzalloc(sizeof(u32), GFP_KERNEL);
2831         if (!sdev->private) {
2832                 kfree(ipc_config);
2833                 return -ENOMEM;
2834         }
2835
2836         /* get DMIC PDM tokens */
2837         ret = sof_parse_tokens(scomp, &ipc_config->dmic.pdm[0], dmic_pdm_tokens,
2838                                ARRAY_SIZE(dmic_pdm_tokens), private->array,
2839                                le32_to_cpu(private->size));
2840         if (ret != 0) {
2841                 dev_err(scomp->dev, "error: parse dmic pdm tokens failed %d\n",
2842                         le32_to_cpu(private->size));
2843                 goto err;
2844         }
2845
2846         /* set IPC header size */
2847         ipc_config->hdr.size = size;
2848
2849         /* debug messages */
2850         dev_dbg(scomp->dev, "tplg: config DMIC%d driver version %d\n",
2851                 ipc_config->dai_index, ipc_config->dmic.driver_ipc_version);
2852         dev_dbg(scomp->dev, "pdmclk_min %d pdm_clkmax %d duty_min %hd\n",
2853                 ipc_config->dmic.pdmclk_min, ipc_config->dmic.pdmclk_max,
2854                 ipc_config->dmic.duty_min);
2855         dev_dbg(scomp->dev, "duty_max %hd fifo_fs %d num_pdms active %d\n",
2856                 ipc_config->dmic.duty_max, ipc_config->dmic.fifo_fs,
2857                 ipc_config->dmic.num_pdm_active);
2858         dev_dbg(scomp->dev, "fifo word length %hd\n",
2859                 ipc_config->dmic.fifo_bits);
2860
2861         for (j = 0; j < ipc_config->dmic.num_pdm_active; j++) {
2862                 dev_dbg(scomp->dev, "pdm %hd mic a %hd mic b %hd\n",
2863                         ipc_config->dmic.pdm[j].id,
2864                         ipc_config->dmic.pdm[j].enable_mic_a,
2865                         ipc_config->dmic.pdm[j].enable_mic_b);
2866                 dev_dbg(scomp->dev, "pdm %hd polarity a %hd polarity b %hd\n",
2867                         ipc_config->dmic.pdm[j].id,
2868                         ipc_config->dmic.pdm[j].polarity_mic_a,
2869                         ipc_config->dmic.pdm[j].polarity_mic_b);
2870                 dev_dbg(scomp->dev, "pdm %hd clk_edge %hd skew %hd\n",
2871                         ipc_config->dmic.pdm[j].id,
2872                         ipc_config->dmic.pdm[j].clk_edge,
2873                         ipc_config->dmic.pdm[j].skew);
2874         }
2875
2876         if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1)) {
2877                 /* this takes care of backwards compatible handling of fifo_bits_b */
2878                 ipc_config->dmic.reserved_2 = ipc_config->dmic.fifo_bits;
2879         }
2880
2881         /* send message to DSP */
2882         ret = sof_ipc_tx_message(sdev->ipc,
2883                                  ipc_config->hdr.cmd, ipc_config, size, &reply,
2884                                  sizeof(reply));
2885
2886         if (ret < 0) {
2887                 dev_err(scomp->dev,
2888                         "error: failed to set DAI config for DMIC%d\n",
2889                         config->dai_index);
2890                 goto err;
2891         }
2892
2893         /* set config for all DAI's with name matching the link name */
2894         ret = sof_set_dai_config(sdev, size, link, ipc_config);
2895         if (ret < 0)
2896                 dev_err(scomp->dev, "error: failed to save DAI config for DMIC%d\n",
2897                         config->dai_index);
2898
2899 err:
2900         kfree(sdev->private);
2901         kfree(ipc_config);
2902
2903         return ret;
2904 }
2905
2906 /*
2907  * for hda link, playback and capture are supported by different dai
2908  * in FW. Here get the dai_index, set dma channel of each dai
2909  * and send config to FW. In FW, each dai sets config by dai_index
2910  */
2911 static int sof_link_hda_process(struct snd_sof_dev *sdev,
2912                                 struct snd_soc_dai_link *link,
2913                                 struct sof_ipc_dai_config *config)
2914 {
2915         struct sof_ipc_reply reply;
2916         u32 size = sizeof(*config);
2917         struct snd_sof_dai *sof_dai;
2918         int found = 0;
2919         int ret;
2920
2921         list_for_each_entry(sof_dai, &sdev->dai_list, list) {
2922                 if (!sof_dai->name)
2923                         continue;
2924
2925                 if (strcmp(link->name, sof_dai->name) == 0) {
2926                         config->dai_index = sof_dai->comp_dai.dai_index;
2927                         found = 1;
2928
2929                         config->hda.link_dma_ch = DMA_CHAN_INVALID;
2930
2931                         /* save config in dai component */
2932                         sof_dai->dai_config = kmemdup(config, size, GFP_KERNEL);
2933                         if (!sof_dai->dai_config)
2934                                 return -ENOMEM;
2935
2936                         sof_dai->cpu_dai_name = link->cpus->dai_name;
2937
2938                         /* send message to DSP */
2939                         ret = sof_ipc_tx_message(sdev->ipc,
2940                                                  config->hdr.cmd, config, size,
2941                                                  &reply, sizeof(reply));
2942
2943                         if (ret < 0) {
2944                                 dev_err(sdev->dev, "error: failed to set DAI config for direction:%d of HDA dai %d\n",
2945                                         sof_dai->comp_dai.direction,
2946                                         config->dai_index);
2947
2948                                 return ret;
2949                         }
2950                 }
2951         }
2952
2953         /*
2954          * machine driver may define a dai link with playback and capture
2955          * dai enabled, but the dai link in topology would support both, one
2956          * or none of them. Here print a warning message to notify user
2957          */
2958         if (!found) {
2959                 dev_warn(sdev->dev, "warning: failed to find dai for dai link %s",
2960                          link->name);
2961         }
2962
2963         return 0;
2964 }
2965
2966 static int sof_link_hda_load(struct snd_soc_component *scomp, int index,
2967                              struct snd_soc_dai_link *link,
2968                              struct snd_soc_tplg_link_config *cfg,
2969                              struct snd_soc_tplg_hw_config *hw_config,
2970                              struct sof_ipc_dai_config *config)
2971 {
2972         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2973         struct snd_soc_tplg_private *private = &cfg->priv;
2974         struct snd_soc_dai *dai;
2975         u32 size = sizeof(*config);
2976         int ret;
2977
2978         /* init IPC */
2979         memset(&config->hda, 0, sizeof(struct sof_ipc_dai_hda_params));
2980         config->hdr.size = size;
2981
2982         /* get any bespoke DAI tokens */
2983         ret = sof_parse_tokens(scomp, config, hda_tokens,
2984                                ARRAY_SIZE(hda_tokens), private->array,
2985                                le32_to_cpu(private->size));
2986         if (ret != 0) {
2987                 dev_err(scomp->dev, "error: parse hda tokens failed %d\n",
2988                         le32_to_cpu(private->size));
2989                 return ret;
2990         }
2991
2992         dai = snd_soc_find_dai(link->cpus);
2993         if (!dai) {
2994                 dev_err(scomp->dev, "error: failed to find dai %s in %s",
2995                         link->cpus->dai_name, __func__);
2996                 return -EINVAL;
2997         }
2998
2999         ret = sof_link_hda_process(sdev, link, config);
3000         if (ret < 0)
3001                 dev_err(scomp->dev, "error: failed to process hda dai link %s",
3002                         link->name);
3003
3004         return ret;
3005 }
3006
3007 static int sof_link_alh_load(struct snd_soc_component *scomp, int index,
3008                              struct snd_soc_dai_link *link,
3009                              struct snd_soc_tplg_link_config *cfg,
3010                              struct snd_soc_tplg_hw_config *hw_config,
3011                              struct sof_ipc_dai_config *config)
3012 {
3013         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3014         struct sof_ipc_reply reply;
3015         u32 size = sizeof(*config);
3016         int ret;
3017
3018         /* init IPC */
3019         config->hdr.size = size;
3020
3021         /* send message to DSP */
3022         ret = sof_ipc_tx_message(sdev->ipc,
3023                                  config->hdr.cmd, config, size, &reply,
3024                                  sizeof(reply));
3025
3026         if (ret < 0) {
3027                 dev_err(scomp->dev, "error: failed to set DAI config for ALH %d\n",
3028                         config->dai_index);
3029                 return ret;
3030         }
3031
3032         /* set config for all DAI's with name matching the link name */
3033         ret = sof_set_dai_config(sdev, size, link, config);
3034         if (ret < 0)
3035                 dev_err(scomp->dev, "error: failed to save DAI config for ALH %d\n",
3036                         config->dai_index);
3037
3038         return ret;
3039 }
3040
3041 /* DAI link - used for any driver specific init */
3042 static int sof_link_load(struct snd_soc_component *scomp, int index,
3043                          struct snd_soc_dai_link *link,
3044                          struct snd_soc_tplg_link_config *cfg)
3045 {
3046         struct snd_soc_tplg_private *private = &cfg->priv;
3047         struct sof_ipc_dai_config config;
3048         struct snd_soc_tplg_hw_config *hw_config;
3049         int num_hw_configs;
3050         int ret;
3051         int i = 0;
3052
3053         if (!link->platforms) {
3054                 dev_err(scomp->dev, "error: no platforms\n");
3055                 return -EINVAL;
3056         }
3057         link->platforms->name = dev_name(scomp->dev);
3058
3059         /*
3060          * Set nonatomic property for FE dai links as their trigger action
3061          * involves IPC's.
3062          */
3063         if (!link->no_pcm) {
3064                 link->nonatomic = true;
3065
3066                 /* set trigger order */
3067                 link->trigger[0] = SND_SOC_DPCM_TRIGGER_POST;
3068                 link->trigger[1] = SND_SOC_DPCM_TRIGGER_POST;
3069
3070                 /* nothing more to do for FE dai links */
3071                 return 0;
3072         }
3073
3074         /* check we have some tokens - we need at least DAI type */
3075         if (le32_to_cpu(private->size) == 0) {
3076                 dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
3077                 return -EINVAL;
3078         }
3079
3080         /* Send BE DAI link configurations to DSP */
3081         memset(&config, 0, sizeof(config));
3082
3083         /* get any common DAI tokens */
3084         ret = sof_parse_tokens(scomp, &config, dai_link_tokens,
3085                                ARRAY_SIZE(dai_link_tokens), private->array,
3086                                le32_to_cpu(private->size));
3087         if (ret != 0) {
3088                 dev_err(scomp->dev, "error: parse link tokens failed %d\n",
3089                         le32_to_cpu(private->size));
3090                 return ret;
3091         }
3092
3093         /*
3094          * DAI links are expected to have at least 1 hw_config.
3095          * But some older topologies might have no hw_config for HDA dai links.
3096          */
3097         num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
3098         if (!num_hw_configs) {
3099                 if (config.type != SOF_DAI_INTEL_HDA) {
3100                         dev_err(scomp->dev, "error: unexpected DAI config count %d!\n",
3101                                 le32_to_cpu(cfg->num_hw_configs));
3102                         return -EINVAL;
3103                 }
3104         } else {
3105                 dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d!\n",
3106                         cfg->num_hw_configs, le32_to_cpu(cfg->default_hw_config_id));
3107
3108                 for (i = 0; i < num_hw_configs; i++) {
3109                         if (cfg->hw_config[i].id == cfg->default_hw_config_id)
3110                                 break;
3111                 }
3112
3113                 if (i == num_hw_configs) {
3114                         dev_err(scomp->dev, "error: default hw_config id: %d not found!\n",
3115                                 le32_to_cpu(cfg->default_hw_config_id));
3116                         return -EINVAL;
3117                 }
3118         }
3119
3120         /* configure dai IPC message */
3121         hw_config = &cfg->hw_config[i];
3122
3123         config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
3124         config.format = le32_to_cpu(hw_config->fmt);
3125
3126         /* now load DAI specific data and send IPC - type comes from token */
3127         switch (config.type) {
3128         case SOF_DAI_INTEL_SSP:
3129                 ret = sof_link_ssp_load(scomp, index, link, cfg, hw_config,
3130                                         &config);
3131                 break;
3132         case SOF_DAI_INTEL_DMIC:
3133                 ret = sof_link_dmic_load(scomp, index, link, cfg, hw_config,
3134                                          &config);
3135                 break;
3136         case SOF_DAI_INTEL_HDA:
3137                 ret = sof_link_hda_load(scomp, index, link, cfg, hw_config,
3138                                         &config);
3139                 break;
3140         case SOF_DAI_INTEL_ALH:
3141                 ret = sof_link_alh_load(scomp, index, link, cfg, hw_config,
3142                                         &config);
3143                 break;
3144         case SOF_DAI_IMX_SAI:
3145                 ret = sof_link_sai_load(scomp, index, link, cfg, hw_config,
3146                                         &config);
3147                 break;
3148         case SOF_DAI_IMX_ESAI:
3149                 ret = sof_link_esai_load(scomp, index, link, cfg, hw_config,
3150                                          &config);
3151                 break;
3152         default:
3153                 dev_err(scomp->dev, "error: invalid DAI type %d\n",
3154                         config.type);
3155                 ret = -EINVAL;
3156                 break;
3157         }
3158         if (ret < 0)
3159                 return ret;
3160
3161         return 0;
3162 }
3163
3164 static int sof_link_hda_unload(struct snd_sof_dev *sdev,
3165                                struct snd_soc_dai_link *link)
3166 {
3167         struct snd_soc_dai *dai;
3168         int ret = 0;
3169
3170         dai = snd_soc_find_dai(link->cpus);
3171         if (!dai) {
3172                 dev_err(sdev->dev, "error: failed to find dai %s in %s",
3173                         link->cpus->dai_name, __func__);
3174                 return -EINVAL;
3175         }
3176
3177         return ret;
3178 }
3179
3180 static int sof_link_unload(struct snd_soc_component *scomp,
3181                            struct snd_soc_dobj *dobj)
3182 {
3183         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3184         struct snd_soc_dai_link *link =
3185                 container_of(dobj, struct snd_soc_dai_link, dobj);
3186
3187         struct snd_sof_dai *sof_dai;
3188         int ret = 0;
3189
3190         /* only BE link is loaded by sof */
3191         if (!link->no_pcm)
3192                 return 0;
3193
3194         list_for_each_entry(sof_dai, &sdev->dai_list, list) {
3195                 if (!sof_dai->name)
3196                         continue;
3197
3198                 if (strcmp(link->name, sof_dai->name) == 0)
3199                         goto found;
3200         }
3201
3202         dev_err(scomp->dev, "error: failed to find dai %s in %s",
3203                 link->name, __func__);
3204         return -EINVAL;
3205 found:
3206
3207         switch (sof_dai->dai_config->type) {
3208         case SOF_DAI_INTEL_SSP:
3209         case SOF_DAI_INTEL_DMIC:
3210         case SOF_DAI_INTEL_ALH:
3211         case SOF_DAI_IMX_SAI:
3212         case SOF_DAI_IMX_ESAI:
3213                 /* no resource needs to be released for all cases above */
3214                 break;
3215         case SOF_DAI_INTEL_HDA:
3216                 ret = sof_link_hda_unload(sdev, link);
3217                 break;
3218         default:
3219                 dev_err(scomp->dev, "error: invalid DAI type %d\n",
3220                         sof_dai->dai_config->type);
3221                 ret = -EINVAL;
3222                 break;
3223         }
3224
3225         return ret;
3226 }
3227
3228 /* DAI link - used for any driver specific init */
3229 static int sof_route_load(struct snd_soc_component *scomp, int index,
3230                           struct snd_soc_dapm_route *route)
3231 {
3232         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3233         struct sof_ipc_pipe_comp_connect *connect;
3234         struct snd_sof_widget *source_swidget, *sink_swidget;
3235         struct snd_soc_dobj *dobj = &route->dobj;
3236         struct snd_sof_route *sroute;
3237         struct sof_ipc_reply reply;
3238         int ret = 0;
3239
3240         /* allocate memory for sroute and connect */
3241         sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
3242         if (!sroute)
3243                 return -ENOMEM;
3244
3245         sroute->scomp = scomp;
3246
3247         connect = kzalloc(sizeof(*connect), GFP_KERNEL);
3248         if (!connect) {
3249                 kfree(sroute);
3250                 return -ENOMEM;
3251         }
3252
3253         connect->hdr.size = sizeof(*connect);
3254         connect->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_CONNECT;
3255
3256         dev_dbg(scomp->dev, "sink %s control %s source %s\n",
3257                 route->sink, route->control ? route->control : "none",
3258                 route->source);
3259
3260         /* source component */
3261         source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
3262         if (!source_swidget) {
3263                 dev_err(scomp->dev, "error: source %s not found\n",
3264                         route->source);
3265                 ret = -EINVAL;
3266                 goto err;
3267         }
3268
3269         /*
3270          * Virtual widgets of type output/out_drv may be added in topology
3271          * for compatibility. These are not handled by the FW.
3272          * So, don't send routes whose source/sink widget is of such types
3273          * to the DSP.
3274          */
3275         if (source_swidget->id == snd_soc_dapm_out_drv ||
3276             source_swidget->id == snd_soc_dapm_output)
3277                 goto err;
3278
3279         connect->source_id = source_swidget->comp_id;
3280
3281         /* sink component */
3282         sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
3283         if (!sink_swidget) {
3284                 dev_err(scomp->dev, "error: sink %s not found\n",
3285                         route->sink);
3286                 ret = -EINVAL;
3287                 goto err;
3288         }
3289
3290         /*
3291          * Don't send routes whose sink widget is of type
3292          * output or out_drv to the DSP
3293          */
3294         if (sink_swidget->id == snd_soc_dapm_out_drv ||
3295             sink_swidget->id == snd_soc_dapm_output)
3296                 goto err;
3297
3298         connect->sink_id = sink_swidget->comp_id;
3299
3300         /*
3301          * For virtual routes, both sink and source are not
3302          * buffer. Since only buffer linked to component is supported by
3303          * FW, others are reported as error, add check in route function,
3304          * do not send it to FW when both source and sink are not buffer
3305          */
3306         if (source_swidget->id != snd_soc_dapm_buffer &&
3307             sink_swidget->id != snd_soc_dapm_buffer) {
3308                 dev_dbg(scomp->dev, "warning: neither Linked source component %s nor sink component %s is of buffer type, ignoring link\n",
3309                         route->source, route->sink);
3310                 ret = 0;
3311                 goto err;
3312         } else {
3313                 ret = sof_ipc_tx_message(sdev->ipc,
3314                                          connect->hdr.cmd,
3315                                          connect, sizeof(*connect),
3316                                          &reply, sizeof(reply));
3317
3318                 /* check IPC return value */
3319                 if (ret < 0) {
3320                         dev_err(scomp->dev, "error: failed to add route sink %s control %s source %s\n",
3321                                 route->sink,
3322                                 route->control ? route->control : "none",
3323                                 route->source);
3324                         goto err;
3325                 }
3326
3327                 /* check IPC reply */
3328                 if (reply.error < 0) {
3329                         dev_err(scomp->dev, "error: DSP failed to add route sink %s control %s source %s result %d\n",
3330                                 route->sink,
3331                                 route->control ? route->control : "none",
3332                                 route->source, reply.error);
3333                         ret = reply.error;
3334                         goto err;
3335                 }
3336
3337                 sroute->route = route;
3338                 dobj->private = sroute;
3339                 sroute->private = connect;
3340
3341                 /* add route to route list */
3342                 list_add(&sroute->list, &sdev->route_list);
3343
3344                 return ret;
3345         }
3346
3347 err:
3348         kfree(connect);
3349         kfree(sroute);
3350         return ret;
3351 }
3352
3353 /* Function to set the initial value of SOF kcontrols.
3354  * The value will be stored in scontrol->control_data
3355  */
3356 static int snd_sof_cache_kcontrol_val(struct snd_soc_component *scomp)
3357 {
3358         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3359         struct snd_sof_control *scontrol = NULL;
3360         int ipc_cmd, ctrl_type;
3361         int ret = 0;
3362
3363         list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
3364
3365                 /* notify DSP of kcontrol values */
3366                 switch (scontrol->cmd) {
3367                 case SOF_CTRL_CMD_VOLUME:
3368                 case SOF_CTRL_CMD_ENUM:
3369                 case SOF_CTRL_CMD_SWITCH:
3370                         ipc_cmd = SOF_IPC_COMP_GET_VALUE;
3371                         ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_GET;
3372                         break;
3373                 case SOF_CTRL_CMD_BINARY:
3374                         ipc_cmd = SOF_IPC_COMP_GET_DATA;
3375                         ctrl_type = SOF_CTRL_TYPE_DATA_GET;
3376                         break;
3377                 default:
3378                         dev_err(scomp->dev,
3379                                 "error: Invalid scontrol->cmd: %d\n",
3380                                 scontrol->cmd);
3381                         return -EINVAL;
3382                 }
3383                 ret = snd_sof_ipc_set_get_comp_data(scontrol,
3384                                                     ipc_cmd, ctrl_type,
3385                                                     scontrol->cmd,
3386                                                     false);
3387                 if (ret < 0) {
3388                         dev_warn(scomp->dev,
3389                                  "error: kcontrol value get for widget: %d\n",
3390                                  scontrol->comp_id);
3391                 }
3392         }
3393
3394         return ret;
3395 }
3396
3397 int snd_sof_complete_pipeline(struct device *dev,
3398                               struct snd_sof_widget *swidget)
3399 {
3400         struct snd_sof_dev *sdev = dev_get_drvdata(dev);
3401         struct sof_ipc_pipe_ready ready;
3402         struct sof_ipc_reply reply;
3403         int ret;
3404
3405         dev_dbg(dev, "tplg: complete pipeline %s id %d\n",
3406                 swidget->widget->name, swidget->comp_id);
3407
3408         memset(&ready, 0, sizeof(ready));
3409         ready.hdr.size = sizeof(ready);
3410         ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
3411         ready.comp_id = swidget->comp_id;
3412
3413         ret = sof_ipc_tx_message(sdev->ipc,
3414                                  ready.hdr.cmd, &ready, sizeof(ready), &reply,
3415                                  sizeof(reply));
3416         if (ret < 0)
3417                 return ret;
3418         return 1;
3419 }
3420
3421 /* completion - called at completion of firmware loading */
3422 static void sof_complete(struct snd_soc_component *scomp)
3423 {
3424         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3425         struct snd_sof_widget *swidget;
3426
3427         /* some widget types require completion notificattion */
3428         list_for_each_entry(swidget, &sdev->widget_list, list) {
3429                 if (swidget->complete)
3430                         continue;
3431
3432                 switch (swidget->id) {
3433                 case snd_soc_dapm_scheduler:
3434                         swidget->complete =
3435                                 snd_sof_complete_pipeline(scomp->dev, swidget);
3436                         break;
3437                 default:
3438                         break;
3439                 }
3440         }
3441         /*
3442          * cache initial values of SOF kcontrols by reading DSP value over
3443          * IPC. It may be overwritten by alsa-mixer after booting up
3444          */
3445         snd_sof_cache_kcontrol_val(scomp);
3446 }
3447
3448 /* manifest - optional to inform component of manifest */
3449 static int sof_manifest(struct snd_soc_component *scomp, int index,
3450                         struct snd_soc_tplg_manifest *man)
3451 {
3452         u32 size;
3453         u32 abi_version;
3454
3455         size = le32_to_cpu(man->priv.size);
3456
3457         /* backward compatible with tplg without ABI info */
3458         if (!size) {
3459                 dev_dbg(scomp->dev, "No topology ABI info\n");
3460                 return 0;
3461         }
3462
3463         if (size != SOF_TPLG_ABI_SIZE) {
3464                 dev_err(scomp->dev, "error: invalid topology ABI size\n");
3465                 return -EINVAL;
3466         }
3467
3468         dev_info(scomp->dev,
3469                  "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
3470                  man->priv.data[0], man->priv.data[1],
3471                  man->priv.data[2], SOF_ABI_MAJOR, SOF_ABI_MINOR,
3472                  SOF_ABI_PATCH);
3473
3474         abi_version = SOF_ABI_VER(man->priv.data[0],
3475                                   man->priv.data[1],
3476                                   man->priv.data[2]);
3477
3478         if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
3479                 dev_err(scomp->dev, "error: incompatible topology ABI version\n");
3480                 return -EINVAL;
3481         }
3482
3483         if (abi_version > SOF_ABI_VERSION) {
3484                 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
3485                         dev_warn(scomp->dev, "warn: topology ABI is more recent than kernel\n");
3486                 } else {
3487                         dev_err(scomp->dev, "error: topology ABI is more recent than kernel\n");
3488                         return -EINVAL;
3489                 }
3490         }
3491
3492         return 0;
3493 }
3494
3495 /* vendor specific kcontrol handlers available for binding */
3496 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
3497         {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
3498         {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
3499         {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
3500         {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
3501 };
3502
3503 /* vendor specific bytes ext handlers available for binding */
3504 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
3505         {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
3506 };
3507
3508 static struct snd_soc_tplg_ops sof_tplg_ops = {
3509         /* external kcontrol init - used for any driver specific init */
3510         .control_load   = sof_control_load,
3511         .control_unload = sof_control_unload,
3512
3513         /* external kcontrol init - used for any driver specific init */
3514         .dapm_route_load        = sof_route_load,
3515         .dapm_route_unload      = sof_route_unload,
3516
3517         /* external widget init - used for any driver specific init */
3518         /* .widget_load is not currently used */
3519         .widget_ready   = sof_widget_ready,
3520         .widget_unload  = sof_widget_unload,
3521
3522         /* FE DAI - used for any driver specific init */
3523         .dai_load       = sof_dai_load,
3524         .dai_unload     = sof_dai_unload,
3525
3526         /* DAI link - used for any driver specific init */
3527         .link_load      = sof_link_load,
3528         .link_unload    = sof_link_unload,
3529
3530         /* completion - called at completion of firmware loading */
3531         .complete       = sof_complete,
3532
3533         /* manifest - optional to inform component of manifest */
3534         .manifest       = sof_manifest,
3535
3536         /* vendor specific kcontrol handlers available for binding */
3537         .io_ops         = sof_io_ops,
3538         .io_ops_count   = ARRAY_SIZE(sof_io_ops),
3539
3540         /* vendor specific bytes ext handlers available for binding */
3541         .bytes_ext_ops  = sof_bytes_ext_ops,
3542         .bytes_ext_ops_count    = ARRAY_SIZE(sof_bytes_ext_ops),
3543 };
3544
3545 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
3546 {
3547         const struct firmware *fw;
3548         int ret;
3549
3550         dev_dbg(scomp->dev, "loading topology:%s\n", file);
3551
3552         ret = request_firmware(&fw, file, scomp->dev);
3553         if (ret < 0) {
3554                 dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
3555                         file, ret);
3556                 return ret;
3557         }
3558
3559         ret = snd_soc_tplg_component_load(scomp,
3560                                           &sof_tplg_ops, fw,
3561                                           SND_SOC_TPLG_INDEX_ALL);
3562         if (ret < 0) {
3563                 dev_err(scomp->dev, "error: tplg component load failed %d\n",
3564                         ret);
3565                 ret = -EINVAL;
3566         }
3567
3568         release_firmware(fw);
3569         return ret;
3570 }
3571 EXPORT_SYMBOL(snd_sof_load_topology);