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