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