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