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