xref: /openbmc/linux/sound/soc/codecs/wm_adsp.c (revision 0aaa8977)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * wm_adsp.c  --  Wolfson ADSP support
4  *
5  * Copyright 2012 Wolfson Microelectronics plc
6  *
7  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8  */
9 
10 #include <linux/ctype.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/firmware.h>
16 #include <linux/list.h>
17 #include <linux/pm.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
23 #include <linux/debugfs.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/jack.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
31 
32 #include "wm_adsp.h"
33 
34 #define adsp_crit(_dsp, fmt, ...) \
35 	dev_crit(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__)
36 #define adsp_err(_dsp, fmt, ...) \
37 	dev_err(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__)
38 #define adsp_warn(_dsp, fmt, ...) \
39 	dev_warn(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__)
40 #define adsp_info(_dsp, fmt, ...) \
41 	dev_info(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__)
42 #define adsp_dbg(_dsp, fmt, ...) \
43 	dev_dbg(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__)
44 
45 #define compr_err(_obj, fmt, ...) \
46 	adsp_err(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \
47 		 ##__VA_ARGS__)
48 #define compr_dbg(_obj, fmt, ...) \
49 	adsp_dbg(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \
50 		 ##__VA_ARGS__)
51 
52 #define ADSP_MAX_STD_CTRL_SIZE               512
53 
54 static const struct cs_dsp_client_ops wm_adsp1_client_ops;
55 static const struct cs_dsp_client_ops wm_adsp2_client_ops;
56 
57 #define WM_ADSP_FW_MBC_VSS  0
58 #define WM_ADSP_FW_HIFI     1
59 #define WM_ADSP_FW_TX       2
60 #define WM_ADSP_FW_TX_SPK   3
61 #define WM_ADSP_FW_RX       4
62 #define WM_ADSP_FW_RX_ANC   5
63 #define WM_ADSP_FW_CTRL     6
64 #define WM_ADSP_FW_ASR      7
65 #define WM_ADSP_FW_TRACE    8
66 #define WM_ADSP_FW_SPK_PROT 9
67 #define WM_ADSP_FW_SPK_CALI 10
68 #define WM_ADSP_FW_SPK_DIAG 11
69 #define WM_ADSP_FW_MISC     12
70 
71 #define WM_ADSP_NUM_FW      13
72 
73 static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = {
74 	[WM_ADSP_FW_MBC_VSS] =  "MBC/VSS",
75 	[WM_ADSP_FW_HIFI] =     "MasterHiFi",
76 	[WM_ADSP_FW_TX] =       "Tx",
77 	[WM_ADSP_FW_TX_SPK] =   "Tx Speaker",
78 	[WM_ADSP_FW_RX] =       "Rx",
79 	[WM_ADSP_FW_RX_ANC] =   "Rx ANC",
80 	[WM_ADSP_FW_CTRL] =     "Voice Ctrl",
81 	[WM_ADSP_FW_ASR] =      "ASR Assist",
82 	[WM_ADSP_FW_TRACE] =    "Dbg Trace",
83 	[WM_ADSP_FW_SPK_PROT] = "Protection",
84 	[WM_ADSP_FW_SPK_CALI] = "Calibration",
85 	[WM_ADSP_FW_SPK_DIAG] = "Diagnostic",
86 	[WM_ADSP_FW_MISC] =     "Misc",
87 };
88 
89 struct wm_adsp_system_config_xm_hdr {
90 	__be32 sys_enable;
91 	__be32 fw_id;
92 	__be32 fw_rev;
93 	__be32 boot_status;
94 	__be32 watchdog;
95 	__be32 dma_buffer_size;
96 	__be32 rdma[6];
97 	__be32 wdma[8];
98 	__be32 build_job_name[3];
99 	__be32 build_job_number;
100 };
101 
102 struct wm_halo_system_config_xm_hdr {
103 	__be32 halo_heartbeat;
104 	__be32 build_job_name[3];
105 	__be32 build_job_number;
106 };
107 
108 struct wm_adsp_alg_xm_struct {
109 	__be32 magic;
110 	__be32 smoothing;
111 	__be32 threshold;
112 	__be32 host_buf_ptr;
113 	__be32 start_seq;
114 	__be32 high_water_mark;
115 	__be32 low_water_mark;
116 	__be64 smoothed_power;
117 };
118 
119 struct wm_adsp_host_buf_coeff_v1 {
120 	__be32 host_buf_ptr;		/* Host buffer pointer */
121 	__be32 versions;		/* Version numbers */
122 	__be32 name[4];			/* The buffer name */
123 };
124 
125 struct wm_adsp_buffer {
126 	__be32 buf1_base;		/* Base addr of first buffer area */
127 	__be32 buf1_size;		/* Size of buf1 area in DSP words */
128 	__be32 buf2_base;		/* Base addr of 2nd buffer area */
129 	__be32 buf1_buf2_size;		/* Size of buf1+buf2 in DSP words */
130 	__be32 buf3_base;		/* Base addr of buf3 area */
131 	__be32 buf_total_size;		/* Size of buf1+buf2+buf3 in DSP words */
132 	__be32 high_water_mark;		/* Point at which IRQ is asserted */
133 	__be32 irq_count;		/* bits 1-31 count IRQ assertions */
134 	__be32 irq_ack;			/* acked IRQ count, bit 0 enables IRQ */
135 	__be32 next_write_index;	/* word index of next write */
136 	__be32 next_read_index;		/* word index of next read */
137 	__be32 error;			/* error if any */
138 	__be32 oldest_block_index;	/* word index of oldest surviving */
139 	__be32 requested_rewind;	/* how many blocks rewind was done */
140 	__be32 reserved_space;		/* internal */
141 	__be32 min_free;		/* min free space since stream start */
142 	__be32 blocks_written[2];	/* total blocks written (64 bit) */
143 	__be32 words_written[2];	/* total words written (64 bit) */
144 };
145 
146 struct wm_adsp_compr;
147 
148 struct wm_adsp_compr_buf {
149 	struct list_head list;
150 	struct wm_adsp *dsp;
151 	struct wm_adsp_compr *compr;
152 
153 	struct wm_adsp_buffer_region *regions;
154 	u32 host_buf_ptr;
155 
156 	u32 error;
157 	u32 irq_count;
158 	int read_index;
159 	int avail;
160 	int host_buf_mem_type;
161 
162 	char *name;
163 };
164 
165 struct wm_adsp_compr {
166 	struct list_head list;
167 	struct wm_adsp *dsp;
168 	struct wm_adsp_compr_buf *buf;
169 
170 	struct snd_compr_stream *stream;
171 	struct snd_compressed_buffer size;
172 
173 	u32 *raw_buf;
174 	unsigned int copied_total;
175 
176 	unsigned int sample_rate;
177 
178 	const char *name;
179 };
180 
181 #define WM_ADSP_MIN_FRAGMENTS          1
182 #define WM_ADSP_MAX_FRAGMENTS          256
183 #define WM_ADSP_MIN_FRAGMENT_SIZE      (64 * CS_DSP_DATA_WORD_SIZE)
184 #define WM_ADSP_MAX_FRAGMENT_SIZE      (4096 * CS_DSP_DATA_WORD_SIZE)
185 
186 #define WM_ADSP_ALG_XM_STRUCT_MAGIC    0x49aec7
187 
188 #define HOST_BUFFER_FIELD(field) \
189 	(offsetof(struct wm_adsp_buffer, field) / sizeof(__be32))
190 
191 #define ALG_XM_FIELD(field) \
192 	(offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32))
193 
194 #define HOST_BUF_COEFF_SUPPORTED_COMPAT_VER	1
195 
196 #define HOST_BUF_COEFF_COMPAT_VER_MASK		0xFF00
197 #define HOST_BUF_COEFF_COMPAT_VER_SHIFT		8
198 
199 static int wm_adsp_buffer_init(struct wm_adsp *dsp);
200 static int wm_adsp_buffer_free(struct wm_adsp *dsp);
201 
202 struct wm_adsp_buffer_region {
203 	unsigned int offset;
204 	unsigned int cumulative_size;
205 	unsigned int mem_type;
206 	unsigned int base_addr;
207 };
208 
209 struct wm_adsp_buffer_region_def {
210 	unsigned int mem_type;
211 	unsigned int base_offset;
212 	unsigned int size_offset;
213 };
214 
215 static const struct wm_adsp_buffer_region_def default_regions[] = {
216 	{
217 		.mem_type = WMFW_ADSP2_XM,
218 		.base_offset = HOST_BUFFER_FIELD(buf1_base),
219 		.size_offset = HOST_BUFFER_FIELD(buf1_size),
220 	},
221 	{
222 		.mem_type = WMFW_ADSP2_XM,
223 		.base_offset = HOST_BUFFER_FIELD(buf2_base),
224 		.size_offset = HOST_BUFFER_FIELD(buf1_buf2_size),
225 	},
226 	{
227 		.mem_type = WMFW_ADSP2_YM,
228 		.base_offset = HOST_BUFFER_FIELD(buf3_base),
229 		.size_offset = HOST_BUFFER_FIELD(buf_total_size),
230 	},
231 };
232 
233 struct wm_adsp_fw_caps {
234 	u32 id;
235 	struct snd_codec_desc desc;
236 	int num_regions;
237 	const struct wm_adsp_buffer_region_def *region_defs;
238 };
239 
240 static const struct wm_adsp_fw_caps ctrl_caps[] = {
241 	{
242 		.id = SND_AUDIOCODEC_BESPOKE,
243 		.desc = {
244 			.max_ch = 8,
245 			.sample_rates = { 16000 },
246 			.num_sample_rates = 1,
247 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
248 		},
249 		.num_regions = ARRAY_SIZE(default_regions),
250 		.region_defs = default_regions,
251 	},
252 };
253 
254 static const struct wm_adsp_fw_caps trace_caps[] = {
255 	{
256 		.id = SND_AUDIOCODEC_BESPOKE,
257 		.desc = {
258 			.max_ch = 8,
259 			.sample_rates = {
260 				4000, 8000, 11025, 12000, 16000, 22050,
261 				24000, 32000, 44100, 48000, 64000, 88200,
262 				96000, 176400, 192000
263 			},
264 			.num_sample_rates = 15,
265 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
266 		},
267 		.num_regions = ARRAY_SIZE(default_regions),
268 		.region_defs = default_regions,
269 	},
270 };
271 
272 static const struct {
273 	const char *file;
274 	int compr_direction;
275 	int num_caps;
276 	const struct wm_adsp_fw_caps *caps;
277 	bool voice_trigger;
278 } wm_adsp_fw[WM_ADSP_NUM_FW] = {
279 	[WM_ADSP_FW_MBC_VSS] =  { .file = "mbc-vss" },
280 	[WM_ADSP_FW_HIFI] =     { .file = "hifi" },
281 	[WM_ADSP_FW_TX] =       { .file = "tx" },
282 	[WM_ADSP_FW_TX_SPK] =   { .file = "tx-spk" },
283 	[WM_ADSP_FW_RX] =       { .file = "rx" },
284 	[WM_ADSP_FW_RX_ANC] =   { .file = "rx-anc" },
285 	[WM_ADSP_FW_CTRL] =     {
286 		.file = "ctrl",
287 		.compr_direction = SND_COMPRESS_CAPTURE,
288 		.num_caps = ARRAY_SIZE(ctrl_caps),
289 		.caps = ctrl_caps,
290 		.voice_trigger = true,
291 	},
292 	[WM_ADSP_FW_ASR] =      { .file = "asr" },
293 	[WM_ADSP_FW_TRACE] =    {
294 		.file = "trace",
295 		.compr_direction = SND_COMPRESS_CAPTURE,
296 		.num_caps = ARRAY_SIZE(trace_caps),
297 		.caps = trace_caps,
298 	},
299 	[WM_ADSP_FW_SPK_PROT] = { .file = "spk-prot" },
300 	[WM_ADSP_FW_SPK_CALI] = { .file = "spk-cali" },
301 	[WM_ADSP_FW_SPK_DIAG] = { .file = "spk-diag" },
302 	[WM_ADSP_FW_MISC] =     { .file = "misc" },
303 };
304 
305 struct wm_coeff_ctl {
306 	const char *name;
307 	struct cs_dsp_coeff_ctl *cs_ctl;
308 	struct soc_bytes_ext bytes_ext;
309 	struct work_struct work;
310 };
311 
312 int wm_adsp_fw_get(struct snd_kcontrol *kcontrol,
313 		   struct snd_ctl_elem_value *ucontrol)
314 {
315 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
316 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
317 	struct wm_adsp *dsp = snd_soc_component_get_drvdata(component);
318 
319 	ucontrol->value.enumerated.item[0] = dsp[e->shift_l].fw;
320 
321 	return 0;
322 }
323 EXPORT_SYMBOL_GPL(wm_adsp_fw_get);
324 
325 int wm_adsp_fw_put(struct snd_kcontrol *kcontrol,
326 		   struct snd_ctl_elem_value *ucontrol)
327 {
328 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
329 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
330 	struct wm_adsp *dsp = snd_soc_component_get_drvdata(component);
331 	int ret = 0;
332 
333 	if (ucontrol->value.enumerated.item[0] == dsp[e->shift_l].fw)
334 		return 0;
335 
336 	if (ucontrol->value.enumerated.item[0] >= WM_ADSP_NUM_FW)
337 		return -EINVAL;
338 
339 	mutex_lock(&dsp[e->shift_l].cs_dsp.pwr_lock);
340 
341 	if (dsp[e->shift_l].cs_dsp.booted || !list_empty(&dsp[e->shift_l].compr_list))
342 		ret = -EBUSY;
343 	else
344 		dsp[e->shift_l].fw = ucontrol->value.enumerated.item[0];
345 
346 	mutex_unlock(&dsp[e->shift_l].cs_dsp.pwr_lock);
347 
348 	return ret;
349 }
350 EXPORT_SYMBOL_GPL(wm_adsp_fw_put);
351 
352 const struct soc_enum wm_adsp_fw_enum[] = {
353 	SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
354 	SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
355 	SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
356 	SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
357 	SOC_ENUM_SINGLE(0, 4, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
358 	SOC_ENUM_SINGLE(0, 5, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
359 	SOC_ENUM_SINGLE(0, 6, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
360 };
361 EXPORT_SYMBOL_GPL(wm_adsp_fw_enum);
362 
363 static inline struct wm_coeff_ctl *bytes_ext_to_ctl(struct soc_bytes_ext *ext)
364 {
365 	return container_of(ext, struct wm_coeff_ctl, bytes_ext);
366 }
367 
368 static int wm_coeff_info(struct snd_kcontrol *kctl,
369 			 struct snd_ctl_elem_info *uinfo)
370 {
371 	struct soc_bytes_ext *bytes_ext =
372 		(struct soc_bytes_ext *)kctl->private_value;
373 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
374 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
375 
376 	switch (cs_ctl->type) {
377 	case WMFW_CTL_TYPE_ACKED:
378 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
379 		uinfo->value.integer.min = CS_DSP_ACKED_CTL_MIN_VALUE;
380 		uinfo->value.integer.max = CS_DSP_ACKED_CTL_MAX_VALUE;
381 		uinfo->value.integer.step = 1;
382 		uinfo->count = 1;
383 		break;
384 	default:
385 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
386 		uinfo->count = cs_ctl->len;
387 		break;
388 	}
389 
390 	return 0;
391 }
392 
393 static int wm_coeff_put(struct snd_kcontrol *kctl,
394 			struct snd_ctl_elem_value *ucontrol)
395 {
396 	struct soc_bytes_ext *bytes_ext =
397 		(struct soc_bytes_ext *)kctl->private_value;
398 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
399 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
400 	char *p = ucontrol->value.bytes.data;
401 	int ret = 0;
402 
403 	mutex_lock(&cs_ctl->dsp->pwr_lock);
404 	ret = cs_dsp_coeff_write_ctrl(cs_ctl, p, cs_ctl->len);
405 	mutex_unlock(&cs_ctl->dsp->pwr_lock);
406 
407 	return ret;
408 }
409 
410 static int wm_coeff_tlv_put(struct snd_kcontrol *kctl,
411 			    const unsigned int __user *bytes, unsigned int size)
412 {
413 	struct soc_bytes_ext *bytes_ext =
414 		(struct soc_bytes_ext *)kctl->private_value;
415 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
416 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
417 	int ret = 0;
418 
419 	mutex_lock(&cs_ctl->dsp->pwr_lock);
420 
421 	if (copy_from_user(cs_ctl->cache, bytes, size))
422 		ret = -EFAULT;
423 	else
424 		ret = cs_dsp_coeff_write_ctrl(cs_ctl, cs_ctl->cache, size);
425 
426 	mutex_unlock(&cs_ctl->dsp->pwr_lock);
427 
428 	return ret;
429 }
430 
431 static int wm_coeff_put_acked(struct snd_kcontrol *kctl,
432 			      struct snd_ctl_elem_value *ucontrol)
433 {
434 	struct soc_bytes_ext *bytes_ext =
435 		(struct soc_bytes_ext *)kctl->private_value;
436 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
437 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
438 	unsigned int val = ucontrol->value.integer.value[0];
439 	int ret;
440 
441 	if (val == 0)
442 		return 0;	/* 0 means no event */
443 
444 	mutex_lock(&cs_ctl->dsp->pwr_lock);
445 
446 	if (cs_ctl->enabled)
447 		ret = cs_dsp_coeff_write_acked_control(cs_ctl, val);
448 	else
449 		ret = -EPERM;
450 
451 	mutex_unlock(&cs_ctl->dsp->pwr_lock);
452 
453 	return ret;
454 }
455 
456 static int wm_coeff_get(struct snd_kcontrol *kctl,
457 			struct snd_ctl_elem_value *ucontrol)
458 {
459 	struct soc_bytes_ext *bytes_ext =
460 		(struct soc_bytes_ext *)kctl->private_value;
461 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
462 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
463 	char *p = ucontrol->value.bytes.data;
464 	int ret;
465 
466 	mutex_lock(&cs_ctl->dsp->pwr_lock);
467 	ret = cs_dsp_coeff_read_ctrl(cs_ctl, p, cs_ctl->len);
468 	mutex_unlock(&cs_ctl->dsp->pwr_lock);
469 
470 	return ret;
471 }
472 
473 static int wm_coeff_tlv_get(struct snd_kcontrol *kctl,
474 			    unsigned int __user *bytes, unsigned int size)
475 {
476 	struct soc_bytes_ext *bytes_ext =
477 		(struct soc_bytes_ext *)kctl->private_value;
478 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
479 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
480 	int ret = 0;
481 
482 	mutex_lock(&cs_ctl->dsp->pwr_lock);
483 
484 	ret = cs_dsp_coeff_read_ctrl(cs_ctl, cs_ctl->cache, size);
485 
486 	if (!ret && copy_to_user(bytes, cs_ctl->cache, size))
487 		ret = -EFAULT;
488 
489 	mutex_unlock(&cs_ctl->dsp->pwr_lock);
490 
491 	return ret;
492 }
493 
494 static int wm_coeff_get_acked(struct snd_kcontrol *kcontrol,
495 			      struct snd_ctl_elem_value *ucontrol)
496 {
497 	/*
498 	 * Although it's not useful to read an acked control, we must satisfy
499 	 * user-side assumptions that all controls are readable and that a
500 	 * write of the same value should be filtered out (it's valid to send
501 	 * the same event number again to the firmware). We therefore return 0,
502 	 * meaning "no event" so valid event numbers will always be a change
503 	 */
504 	ucontrol->value.integer.value[0] = 0;
505 
506 	return 0;
507 }
508 
509 static unsigned int wmfw_convert_flags(unsigned int in, unsigned int len)
510 {
511 	unsigned int out, rd, wr, vol;
512 
513 	if (len > ADSP_MAX_STD_CTRL_SIZE) {
514 		rd = SNDRV_CTL_ELEM_ACCESS_TLV_READ;
515 		wr = SNDRV_CTL_ELEM_ACCESS_TLV_WRITE;
516 		vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
517 
518 		out = SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
519 	} else {
520 		rd = SNDRV_CTL_ELEM_ACCESS_READ;
521 		wr = SNDRV_CTL_ELEM_ACCESS_WRITE;
522 		vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
523 
524 		out = 0;
525 	}
526 
527 	if (in) {
528 		out |= rd;
529 		if (in & WMFW_CTL_FLAG_WRITEABLE)
530 			out |= wr;
531 		if (in & WMFW_CTL_FLAG_VOLATILE)
532 			out |= vol;
533 	} else {
534 		out |= rd | wr | vol;
535 	}
536 
537 	return out;
538 }
539 
540 static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl)
541 {
542 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
543 	struct snd_kcontrol_new *kcontrol;
544 	int ret;
545 
546 	kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL);
547 	if (!kcontrol)
548 		return -ENOMEM;
549 
550 	kcontrol->name = ctl->name;
551 	kcontrol->info = wm_coeff_info;
552 	kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
553 	kcontrol->tlv.c = snd_soc_bytes_tlv_callback;
554 	kcontrol->private_value = (unsigned long)&ctl->bytes_ext;
555 	kcontrol->access = wmfw_convert_flags(cs_ctl->flags, cs_ctl->len);
556 
557 	switch (cs_ctl->type) {
558 	case WMFW_CTL_TYPE_ACKED:
559 		kcontrol->get = wm_coeff_get_acked;
560 		kcontrol->put = wm_coeff_put_acked;
561 		break;
562 	default:
563 		if (kcontrol->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
564 			ctl->bytes_ext.max = cs_ctl->len;
565 			ctl->bytes_ext.get = wm_coeff_tlv_get;
566 			ctl->bytes_ext.put = wm_coeff_tlv_put;
567 		} else {
568 			kcontrol->get = wm_coeff_get;
569 			kcontrol->put = wm_coeff_put;
570 		}
571 		break;
572 	}
573 
574 	ret = snd_soc_add_component_controls(dsp->component, kcontrol, 1);
575 	if (ret < 0)
576 		goto err_kcontrol;
577 
578 	kfree(kcontrol);
579 
580 	return 0;
581 
582 err_kcontrol:
583 	kfree(kcontrol);
584 	return ret;
585 }
586 
587 static void wm_adsp_ctl_work(struct work_struct *work)
588 {
589 	struct wm_coeff_ctl *ctl = container_of(work,
590 						struct wm_coeff_ctl,
591 						work);
592 	struct wm_adsp *dsp = container_of(ctl->cs_ctl->dsp,
593 					   struct wm_adsp,
594 					   cs_dsp);
595 
596 	wmfw_add_ctl(dsp, ctl);
597 }
598 
599 static int wm_adsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl)
600 {
601 	struct wm_adsp *dsp = container_of(cs_ctl->dsp, struct wm_adsp, cs_dsp);
602 	struct cs_dsp *cs_dsp = &dsp->cs_dsp;
603 	struct wm_coeff_ctl *ctl;
604 	char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
605 	const char *region_name;
606 	int ret;
607 
608 	if (cs_ctl->flags & WMFW_CTL_FLAG_SYS)
609 		return 0;
610 
611 	region_name = cs_dsp_mem_region_name(cs_ctl->alg_region.type);
612 	if (!region_name) {
613 		adsp_err(dsp, "Unknown region type: %d\n", cs_ctl->alg_region.type);
614 		return -EINVAL;
615 	}
616 
617 	switch (cs_dsp->fw_ver) {
618 	case 0:
619 	case 1:
620 		ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
621 				"%s %s %x", cs_dsp->name, region_name,
622 				cs_ctl->alg_region.alg);
623 		break;
624 	case 2:
625 		ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
626 				"%s%c %.12s %x", cs_dsp->name, *region_name,
627 				wm_adsp_fw_text[dsp->fw], cs_ctl->alg_region.alg);
628 		break;
629 	default:
630 		ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
631 				"%s %.12s %x", cs_dsp->name,
632 				wm_adsp_fw_text[dsp->fw], cs_ctl->alg_region.alg);
633 		break;
634 	}
635 
636 	if (cs_ctl->subname) {
637 		int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
638 		int skip = 0;
639 
640 		if (dsp->component->name_prefix)
641 			avail -= strlen(dsp->component->name_prefix) + 1;
642 
643 		/* Truncate the subname from the start if it is too long */
644 		if (cs_ctl->subname_len > avail)
645 			skip = cs_ctl->subname_len - avail;
646 
647 		snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret,
648 			 " %.*s", cs_ctl->subname_len - skip, cs_ctl->subname + skip);
649 	}
650 
651 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
652 	if (!ctl)
653 		return -ENOMEM;
654 	ctl->cs_ctl = cs_ctl;
655 
656 	ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
657 	if (!ctl->name) {
658 		ret = -ENOMEM;
659 		goto err_ctl;
660 	}
661 
662 	cs_ctl->priv = ctl;
663 
664 	INIT_WORK(&ctl->work, wm_adsp_ctl_work);
665 	schedule_work(&ctl->work);
666 
667 	return 0;
668 
669 err_ctl:
670 	kfree(ctl);
671 
672 	return ret;
673 }
674 
675 static void wm_adsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl)
676 {
677 	struct wm_coeff_ctl *ctl = cs_ctl->priv;
678 
679 	cancel_work_sync(&ctl->work);
680 
681 	kfree(ctl->name);
682 	kfree(ctl);
683 }
684 
685 int wm_adsp_write_ctl(struct wm_adsp *dsp, const char *name, int type,
686 		      unsigned int alg, void *buf, size_t len)
687 {
688 	struct cs_dsp_coeff_ctl *cs_ctl;
689 	struct wm_coeff_ctl *ctl;
690 	struct snd_kcontrol *kcontrol;
691 	char ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
692 	int ret;
693 
694 	cs_ctl = cs_dsp_get_ctl(&dsp->cs_dsp, name, type, alg);
695 	if (!cs_ctl)
696 		return -EINVAL;
697 
698 	ctl = cs_ctl->priv;
699 
700 	if (len > cs_ctl->len)
701 		return -EINVAL;
702 
703 	ret = cs_dsp_coeff_write_ctrl(cs_ctl, buf, len);
704 	if (ret)
705 		return ret;
706 
707 	if (cs_ctl->flags & WMFW_CTL_FLAG_SYS)
708 		return 0;
709 
710 	if (dsp->component->name_prefix)
711 		snprintf(ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s",
712 			 dsp->component->name_prefix, ctl->name);
713 	else
714 		snprintf(ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s",
715 			 ctl->name);
716 
717 	kcontrol = snd_soc_card_get_kcontrol(dsp->component->card, ctl_name);
718 	if (!kcontrol) {
719 		adsp_err(dsp, "Can't find kcontrol %s\n", ctl_name);
720 		return -EINVAL;
721 	}
722 
723 	snd_ctl_notify(dsp->component->card->snd_card,
724 		       SNDRV_CTL_EVENT_MASK_VALUE, &kcontrol->id);
725 
726 	return 0;
727 }
728 EXPORT_SYMBOL_GPL(wm_adsp_write_ctl);
729 
730 int wm_adsp_read_ctl(struct wm_adsp *dsp, const char *name, int type,
731 		     unsigned int alg, void *buf, size_t len)
732 {
733 	struct cs_dsp_coeff_ctl *cs_ctl;
734 
735 	cs_ctl = cs_dsp_get_ctl(&dsp->cs_dsp, name, type, alg);
736 	if (!cs_ctl)
737 		return -EINVAL;
738 
739 	if (len > cs_ctl->len)
740 		return -EINVAL;
741 
742 	return cs_dsp_coeff_read_ctrl(cs_ctl, buf, len);
743 }
744 EXPORT_SYMBOL_GPL(wm_adsp_read_ctl);
745 
746 static void wm_adsp_release_firmware_files(struct wm_adsp *dsp,
747 					   const struct firmware *wmfw_firmware,
748 					   char *wmfw_filename,
749 					   const struct firmware *coeff_firmware,
750 					   char *coeff_filename)
751 {
752 	if (wmfw_firmware)
753 		release_firmware(wmfw_firmware);
754 	kfree(wmfw_filename);
755 
756 	if (coeff_firmware)
757 		release_firmware(coeff_firmware);
758 	kfree(coeff_filename);
759 }
760 
761 static int wm_adsp_request_firmware_file(struct wm_adsp *dsp,
762 					 const struct firmware **firmware,
763 					 char **filename,
764 					 char *suffix)
765 {
766 	struct cs_dsp *cs_dsp = &dsp->cs_dsp;
767 	int ret = 0;
768 
769 	*filename = kasprintf(GFP_KERNEL, "%s-%s-%s.%s", dsp->part, dsp->fwf_name,
770 			      wm_adsp_fw[dsp->fw].file, suffix);
771 	if (*filename == NULL)
772 		return -ENOMEM;
773 
774 	ret = request_firmware(firmware, *filename, cs_dsp->dev);
775 	if (ret != 0) {
776 		adsp_err(dsp, "Failed to request '%s'\n", *filename);
777 		kfree(*filename);
778 		*filename = NULL;
779 	}
780 
781 	return ret;
782 }
783 
784 static int wm_adsp_request_firmware_files(struct wm_adsp *dsp,
785 					  const struct firmware **wmfw_firmware,
786 					  char **wmfw_filename,
787 					  const struct firmware **coeff_firmware,
788 					  char **coeff_filename)
789 {
790 	int ret = 0;
791 
792 	ret = wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, "wmfw");
793 	if (ret != 0)
794 		return ret;
795 
796 	wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, "bin");
797 
798 	return 0;
799 }
800 
801 static int wm_adsp_common_init(struct wm_adsp *dsp)
802 {
803 	char *p;
804 
805 	INIT_LIST_HEAD(&dsp->compr_list);
806 	INIT_LIST_HEAD(&dsp->buffer_list);
807 
808 	if (!dsp->fwf_name) {
809 		p = devm_kstrdup(dsp->cs_dsp.dev, dsp->cs_dsp.name, GFP_KERNEL);
810 		if (!p)
811 			return -ENOMEM;
812 
813 		dsp->fwf_name = p;
814 		for (; *p != 0; ++p)
815 			*p = tolower(*p);
816 	}
817 
818 	return 0;
819 }
820 
821 int wm_adsp1_init(struct wm_adsp *dsp)
822 {
823 	int ret;
824 
825 	dsp->cs_dsp.client_ops = &wm_adsp1_client_ops;
826 
827 	ret = cs_dsp_adsp1_init(&dsp->cs_dsp);
828 	if (ret)
829 		return ret;
830 
831 	return wm_adsp_common_init(dsp);
832 }
833 EXPORT_SYMBOL_GPL(wm_adsp1_init);
834 
835 int wm_adsp1_event(struct snd_soc_dapm_widget *w,
836 		   struct snd_kcontrol *kcontrol,
837 		   int event)
838 {
839 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
840 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
841 	struct wm_adsp *dsp = &dsps[w->shift];
842 	int ret = 0;
843 	char *wmfw_filename = NULL;
844 	const struct firmware *wmfw_firmware = NULL;
845 	char *coeff_filename = NULL;
846 	const struct firmware *coeff_firmware = NULL;
847 
848 	dsp->component = component;
849 
850 	switch (event) {
851 	case SND_SOC_DAPM_POST_PMU:
852 		ret = wm_adsp_request_firmware_files(dsp,
853 						     &wmfw_firmware, &wmfw_filename,
854 						     &coeff_firmware, &coeff_filename);
855 		if (ret)
856 			break;
857 
858 		ret = cs_dsp_adsp1_power_up(&dsp->cs_dsp,
859 					    wmfw_firmware, wmfw_filename,
860 					    coeff_firmware, coeff_filename,
861 					    wm_adsp_fw_text[dsp->fw]);
862 
863 		wm_adsp_release_firmware_files(dsp,
864 					       wmfw_firmware, wmfw_filename,
865 					       coeff_firmware, coeff_filename);
866 		break;
867 	case SND_SOC_DAPM_PRE_PMD:
868 		cs_dsp_adsp1_power_down(&dsp->cs_dsp);
869 		break;
870 	default:
871 		break;
872 	}
873 
874 	return ret;
875 }
876 EXPORT_SYMBOL_GPL(wm_adsp1_event);
877 
878 int wm_adsp2_set_dspclk(struct snd_soc_dapm_widget *w, unsigned int freq)
879 {
880 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
881 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
882 	struct wm_adsp *dsp = &dsps[w->shift];
883 
884 	return cs_dsp_set_dspclk(&dsp->cs_dsp, freq);
885 }
886 EXPORT_SYMBOL_GPL(wm_adsp2_set_dspclk);
887 
888 int wm_adsp2_preloader_get(struct snd_kcontrol *kcontrol,
889 			   struct snd_ctl_elem_value *ucontrol)
890 {
891 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
892 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
893 	struct soc_mixer_control *mc =
894 		(struct soc_mixer_control *)kcontrol->private_value;
895 	struct wm_adsp *dsp = &dsps[mc->shift - 1];
896 
897 	ucontrol->value.integer.value[0] = dsp->preloaded;
898 
899 	return 0;
900 }
901 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_get);
902 
903 int wm_adsp2_preloader_put(struct snd_kcontrol *kcontrol,
904 			   struct snd_ctl_elem_value *ucontrol)
905 {
906 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
907 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
908 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
909 	struct soc_mixer_control *mc =
910 		(struct soc_mixer_control *)kcontrol->private_value;
911 	struct wm_adsp *dsp = &dsps[mc->shift - 1];
912 	char preload[32];
913 
914 	snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->cs_dsp.name);
915 
916 	dsp->preloaded = ucontrol->value.integer.value[0];
917 
918 	if (ucontrol->value.integer.value[0])
919 		snd_soc_component_force_enable_pin(component, preload);
920 	else
921 		snd_soc_component_disable_pin(component, preload);
922 
923 	snd_soc_dapm_sync(dapm);
924 
925 	flush_work(&dsp->boot_work);
926 
927 	return 0;
928 }
929 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_put);
930 
931 static void wm_adsp_boot_work(struct work_struct *work)
932 {
933 	struct wm_adsp *dsp = container_of(work,
934 					   struct wm_adsp,
935 					   boot_work);
936 	int ret = 0;
937 	char *wmfw_filename = NULL;
938 	const struct firmware *wmfw_firmware = NULL;
939 	char *coeff_filename = NULL;
940 	const struct firmware *coeff_firmware = NULL;
941 
942 	ret = wm_adsp_request_firmware_files(dsp,
943 					     &wmfw_firmware, &wmfw_filename,
944 					     &coeff_firmware, &coeff_filename);
945 	if (ret)
946 		return;
947 
948 	cs_dsp_power_up(&dsp->cs_dsp,
949 			wmfw_firmware, wmfw_filename,
950 			coeff_firmware, coeff_filename,
951 			wm_adsp_fw_text[dsp->fw]);
952 
953 	wm_adsp_release_firmware_files(dsp,
954 				       wmfw_firmware, wmfw_filename,
955 				       coeff_firmware, coeff_filename);
956 }
957 
958 int wm_adsp_early_event(struct snd_soc_dapm_widget *w,
959 			struct snd_kcontrol *kcontrol, int event)
960 {
961 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
962 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
963 	struct wm_adsp *dsp = &dsps[w->shift];
964 
965 	switch (event) {
966 	case SND_SOC_DAPM_PRE_PMU:
967 		queue_work(system_unbound_wq, &dsp->boot_work);
968 		break;
969 	case SND_SOC_DAPM_PRE_PMD:
970 		cs_dsp_power_down(&dsp->cs_dsp);
971 		break;
972 	default:
973 		break;
974 	}
975 
976 	return 0;
977 }
978 EXPORT_SYMBOL_GPL(wm_adsp_early_event);
979 
980 static int wm_adsp_event_post_run(struct cs_dsp *cs_dsp)
981 {
982 	struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp);
983 
984 	if (wm_adsp_fw[dsp->fw].num_caps != 0)
985 		return wm_adsp_buffer_init(dsp);
986 
987 	return 0;
988 }
989 
990 static void wm_adsp_event_post_stop(struct cs_dsp *cs_dsp)
991 {
992 	struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp);
993 
994 	if (wm_adsp_fw[dsp->fw].num_caps != 0)
995 		wm_adsp_buffer_free(dsp);
996 
997 	dsp->fatal_error = false;
998 }
999 
1000 int wm_adsp_event(struct snd_soc_dapm_widget *w,
1001 		  struct snd_kcontrol *kcontrol, int event)
1002 {
1003 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1004 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
1005 	struct wm_adsp *dsp = &dsps[w->shift];
1006 	int ret = 0;
1007 
1008 	switch (event) {
1009 	case SND_SOC_DAPM_POST_PMU:
1010 		flush_work(&dsp->boot_work);
1011 		ret = cs_dsp_run(&dsp->cs_dsp);
1012 		break;
1013 	case SND_SOC_DAPM_PRE_PMD:
1014 		cs_dsp_stop(&dsp->cs_dsp);
1015 		break;
1016 	default:
1017 		break;
1018 	}
1019 
1020 	return ret;
1021 }
1022 EXPORT_SYMBOL_GPL(wm_adsp_event);
1023 
1024 int wm_adsp2_component_probe(struct wm_adsp *dsp, struct snd_soc_component *component)
1025 {
1026 	char preload[32];
1027 
1028 	snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->cs_dsp.name);
1029 	snd_soc_component_disable_pin(component, preload);
1030 
1031 	cs_dsp_init_debugfs(&dsp->cs_dsp, component->debugfs_root);
1032 
1033 	dsp->component = component;
1034 
1035 	return 0;
1036 }
1037 EXPORT_SYMBOL_GPL(wm_adsp2_component_probe);
1038 
1039 int wm_adsp2_component_remove(struct wm_adsp *dsp, struct snd_soc_component *component)
1040 {
1041 	cs_dsp_cleanup_debugfs(&dsp->cs_dsp);
1042 
1043 	return 0;
1044 }
1045 EXPORT_SYMBOL_GPL(wm_adsp2_component_remove);
1046 
1047 int wm_adsp2_init(struct wm_adsp *dsp)
1048 {
1049 	int ret;
1050 
1051 	INIT_WORK(&dsp->boot_work, wm_adsp_boot_work);
1052 
1053 	dsp->sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr);
1054 	dsp->cs_dsp.client_ops = &wm_adsp2_client_ops;
1055 
1056 	ret = cs_dsp_adsp2_init(&dsp->cs_dsp);
1057 	if (ret)
1058 		return ret;
1059 
1060 	return wm_adsp_common_init(dsp);
1061 }
1062 EXPORT_SYMBOL_GPL(wm_adsp2_init);
1063 
1064 int wm_halo_init(struct wm_adsp *dsp)
1065 {
1066 	int ret;
1067 
1068 	INIT_WORK(&dsp->boot_work, wm_adsp_boot_work);
1069 
1070 	dsp->sys_config_size = sizeof(struct wm_halo_system_config_xm_hdr);
1071 	dsp->cs_dsp.client_ops = &wm_adsp2_client_ops;
1072 
1073 	ret = cs_dsp_halo_init(&dsp->cs_dsp);
1074 	if (ret)
1075 		return ret;
1076 
1077 	return wm_adsp_common_init(dsp);
1078 }
1079 EXPORT_SYMBOL_GPL(wm_halo_init);
1080 
1081 void wm_adsp2_remove(struct wm_adsp *dsp)
1082 {
1083 	cs_dsp_remove(&dsp->cs_dsp);
1084 }
1085 EXPORT_SYMBOL_GPL(wm_adsp2_remove);
1086 
1087 static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr)
1088 {
1089 	return compr->buf != NULL;
1090 }
1091 
1092 static int wm_adsp_compr_attach(struct wm_adsp_compr *compr)
1093 {
1094 	struct wm_adsp_compr_buf *buf = NULL, *tmp;
1095 
1096 	if (compr->dsp->fatal_error)
1097 		return -EINVAL;
1098 
1099 	list_for_each_entry(tmp, &compr->dsp->buffer_list, list) {
1100 		if (!tmp->name || !strcmp(compr->name, tmp->name)) {
1101 			buf = tmp;
1102 			break;
1103 		}
1104 	}
1105 
1106 	if (!buf)
1107 		return -EINVAL;
1108 
1109 	compr->buf = buf;
1110 	buf->compr = compr;
1111 
1112 	return 0;
1113 }
1114 
1115 static void wm_adsp_compr_detach(struct wm_adsp_compr *compr)
1116 {
1117 	if (!compr)
1118 		return;
1119 
1120 	/* Wake the poll so it can see buffer is no longer attached */
1121 	if (compr->stream)
1122 		snd_compr_fragment_elapsed(compr->stream);
1123 
1124 	if (wm_adsp_compr_attached(compr)) {
1125 		compr->buf->compr = NULL;
1126 		compr->buf = NULL;
1127 	}
1128 }
1129 
1130 int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream)
1131 {
1132 	struct wm_adsp_compr *compr, *tmp;
1133 	struct snd_soc_pcm_runtime *rtd = stream->private_data;
1134 	int ret = 0;
1135 
1136 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1137 
1138 	if (wm_adsp_fw[dsp->fw].num_caps == 0) {
1139 		adsp_err(dsp, "%s: Firmware does not support compressed API\n",
1140 			 asoc_rtd_to_codec(rtd, 0)->name);
1141 		ret = -ENXIO;
1142 		goto out;
1143 	}
1144 
1145 	if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) {
1146 		adsp_err(dsp, "%s: Firmware does not support stream direction\n",
1147 			 asoc_rtd_to_codec(rtd, 0)->name);
1148 		ret = -EINVAL;
1149 		goto out;
1150 	}
1151 
1152 	list_for_each_entry(tmp, &dsp->compr_list, list) {
1153 		if (!strcmp(tmp->name, asoc_rtd_to_codec(rtd, 0)->name)) {
1154 			adsp_err(dsp, "%s: Only a single stream supported per dai\n",
1155 				 asoc_rtd_to_codec(rtd, 0)->name);
1156 			ret = -EBUSY;
1157 			goto out;
1158 		}
1159 	}
1160 
1161 	compr = kzalloc(sizeof(*compr), GFP_KERNEL);
1162 	if (!compr) {
1163 		ret = -ENOMEM;
1164 		goto out;
1165 	}
1166 
1167 	compr->dsp = dsp;
1168 	compr->stream = stream;
1169 	compr->name = asoc_rtd_to_codec(rtd, 0)->name;
1170 
1171 	list_add_tail(&compr->list, &dsp->compr_list);
1172 
1173 	stream->runtime->private_data = compr;
1174 
1175 out:
1176 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1177 
1178 	return ret;
1179 }
1180 EXPORT_SYMBOL_GPL(wm_adsp_compr_open);
1181 
1182 int wm_adsp_compr_free(struct snd_soc_component *component,
1183 		       struct snd_compr_stream *stream)
1184 {
1185 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1186 	struct wm_adsp *dsp = compr->dsp;
1187 
1188 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1189 
1190 	wm_adsp_compr_detach(compr);
1191 	list_del(&compr->list);
1192 
1193 	kfree(compr->raw_buf);
1194 	kfree(compr);
1195 
1196 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1197 
1198 	return 0;
1199 }
1200 EXPORT_SYMBOL_GPL(wm_adsp_compr_free);
1201 
1202 static int wm_adsp_compr_check_params(struct snd_compr_stream *stream,
1203 				      struct snd_compr_params *params)
1204 {
1205 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1206 	struct wm_adsp *dsp = compr->dsp;
1207 	const struct wm_adsp_fw_caps *caps;
1208 	const struct snd_codec_desc *desc;
1209 	int i, j;
1210 
1211 	if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE ||
1212 	    params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE ||
1213 	    params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS ||
1214 	    params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS ||
1215 	    params->buffer.fragment_size % CS_DSP_DATA_WORD_SIZE) {
1216 		compr_err(compr, "Invalid buffer fragsize=%d fragments=%d\n",
1217 			  params->buffer.fragment_size,
1218 			  params->buffer.fragments);
1219 
1220 		return -EINVAL;
1221 	}
1222 
1223 	for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) {
1224 		caps = &wm_adsp_fw[dsp->fw].caps[i];
1225 		desc = &caps->desc;
1226 
1227 		if (caps->id != params->codec.id)
1228 			continue;
1229 
1230 		if (stream->direction == SND_COMPRESS_PLAYBACK) {
1231 			if (desc->max_ch < params->codec.ch_out)
1232 				continue;
1233 		} else {
1234 			if (desc->max_ch < params->codec.ch_in)
1235 				continue;
1236 		}
1237 
1238 		if (!(desc->formats & (1 << params->codec.format)))
1239 			continue;
1240 
1241 		for (j = 0; j < desc->num_sample_rates; ++j)
1242 			if (desc->sample_rates[j] == params->codec.sample_rate)
1243 				return 0;
1244 	}
1245 
1246 	compr_err(compr, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n",
1247 		  params->codec.id, params->codec.ch_in, params->codec.ch_out,
1248 		  params->codec.sample_rate, params->codec.format);
1249 	return -EINVAL;
1250 }
1251 
1252 static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr)
1253 {
1254 	return compr->size.fragment_size / CS_DSP_DATA_WORD_SIZE;
1255 }
1256 
1257 int wm_adsp_compr_set_params(struct snd_soc_component *component,
1258 			     struct snd_compr_stream *stream,
1259 			     struct snd_compr_params *params)
1260 {
1261 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1262 	unsigned int size;
1263 	int ret;
1264 
1265 	ret = wm_adsp_compr_check_params(stream, params);
1266 	if (ret)
1267 		return ret;
1268 
1269 	compr->size = params->buffer;
1270 
1271 	compr_dbg(compr, "fragment_size=%d fragments=%d\n",
1272 		  compr->size.fragment_size, compr->size.fragments);
1273 
1274 	size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf);
1275 	compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL);
1276 	if (!compr->raw_buf)
1277 		return -ENOMEM;
1278 
1279 	compr->sample_rate = params->codec.sample_rate;
1280 
1281 	return 0;
1282 }
1283 EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params);
1284 
1285 int wm_adsp_compr_get_caps(struct snd_soc_component *component,
1286 			   struct snd_compr_stream *stream,
1287 			   struct snd_compr_caps *caps)
1288 {
1289 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1290 	int fw = compr->dsp->fw;
1291 	int i;
1292 
1293 	if (wm_adsp_fw[fw].caps) {
1294 		for (i = 0; i < wm_adsp_fw[fw].num_caps; i++)
1295 			caps->codecs[i] = wm_adsp_fw[fw].caps[i].id;
1296 
1297 		caps->num_codecs = i;
1298 		caps->direction = wm_adsp_fw[fw].compr_direction;
1299 
1300 		caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE;
1301 		caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE;
1302 		caps->min_fragments = WM_ADSP_MIN_FRAGMENTS;
1303 		caps->max_fragments = WM_ADSP_MAX_FRAGMENTS;
1304 	}
1305 
1306 	return 0;
1307 }
1308 EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps);
1309 
1310 static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf,
1311 				      unsigned int field_offset, u32 *data)
1312 {
1313 	return cs_dsp_read_data_word(&buf->dsp->cs_dsp, buf->host_buf_mem_type,
1314 				     buf->host_buf_ptr + field_offset, data);
1315 }
1316 
1317 static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf,
1318 				       unsigned int field_offset, u32 data)
1319 {
1320 	return cs_dsp_write_data_word(&buf->dsp->cs_dsp, buf->host_buf_mem_type,
1321 				      buf->host_buf_ptr + field_offset,
1322 				      data);
1323 }
1324 
1325 static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf)
1326 {
1327 	const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps;
1328 	struct wm_adsp_buffer_region *region;
1329 	u32 offset = 0;
1330 	int i, ret;
1331 
1332 	buf->regions = kcalloc(caps->num_regions, sizeof(*buf->regions),
1333 			       GFP_KERNEL);
1334 	if (!buf->regions)
1335 		return -ENOMEM;
1336 
1337 	for (i = 0; i < caps->num_regions; ++i) {
1338 		region = &buf->regions[i];
1339 
1340 		region->offset = offset;
1341 		region->mem_type = caps->region_defs[i].mem_type;
1342 
1343 		ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset,
1344 					  &region->base_addr);
1345 		if (ret < 0)
1346 			return ret;
1347 
1348 		ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset,
1349 					  &offset);
1350 		if (ret < 0)
1351 			return ret;
1352 
1353 		region->cumulative_size = offset;
1354 
1355 		compr_dbg(buf,
1356 			  "region=%d type=%d base=%08x off=%08x size=%08x\n",
1357 			  i, region->mem_type, region->base_addr,
1358 			  region->offset, region->cumulative_size);
1359 	}
1360 
1361 	return 0;
1362 }
1363 
1364 static void wm_adsp_buffer_clear(struct wm_adsp_compr_buf *buf)
1365 {
1366 	buf->irq_count = 0xFFFFFFFF;
1367 	buf->read_index = -1;
1368 	buf->avail = 0;
1369 }
1370 
1371 static struct wm_adsp_compr_buf *wm_adsp_buffer_alloc(struct wm_adsp *dsp)
1372 {
1373 	struct wm_adsp_compr_buf *buf;
1374 
1375 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1376 	if (!buf)
1377 		return NULL;
1378 
1379 	buf->dsp = dsp;
1380 
1381 	wm_adsp_buffer_clear(buf);
1382 
1383 	list_add_tail(&buf->list, &dsp->buffer_list);
1384 
1385 	return buf;
1386 }
1387 
1388 static int wm_adsp_buffer_parse_legacy(struct wm_adsp *dsp)
1389 {
1390 	struct cs_dsp_alg_region *alg_region;
1391 	struct wm_adsp_compr_buf *buf;
1392 	u32 xmalg, addr, magic;
1393 	int i, ret;
1394 
1395 	alg_region = cs_dsp_find_alg_region(&dsp->cs_dsp, WMFW_ADSP2_XM, dsp->cs_dsp.fw_id);
1396 	if (!alg_region) {
1397 		adsp_err(dsp, "No algorithm region found\n");
1398 		return -EINVAL;
1399 	}
1400 
1401 	buf = wm_adsp_buffer_alloc(dsp);
1402 	if (!buf)
1403 		return -ENOMEM;
1404 
1405 	xmalg = dsp->sys_config_size / sizeof(__be32);
1406 
1407 	addr = alg_region->base + xmalg + ALG_XM_FIELD(magic);
1408 	ret = cs_dsp_read_data_word(&dsp->cs_dsp, WMFW_ADSP2_XM, addr, &magic);
1409 	if (ret < 0)
1410 		return ret;
1411 
1412 	if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC)
1413 		return -ENODEV;
1414 
1415 	addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr);
1416 	for (i = 0; i < 5; ++i) {
1417 		ret = cs_dsp_read_data_word(&dsp->cs_dsp, WMFW_ADSP2_XM, addr,
1418 					    &buf->host_buf_ptr);
1419 		if (ret < 0)
1420 			return ret;
1421 
1422 		if (buf->host_buf_ptr)
1423 			break;
1424 
1425 		usleep_range(1000, 2000);
1426 	}
1427 
1428 	if (!buf->host_buf_ptr)
1429 		return -EIO;
1430 
1431 	buf->host_buf_mem_type = WMFW_ADSP2_XM;
1432 
1433 	ret = wm_adsp_buffer_populate(buf);
1434 	if (ret < 0)
1435 		return ret;
1436 
1437 	compr_dbg(buf, "legacy host_buf_ptr=%x\n", buf->host_buf_ptr);
1438 
1439 	return 0;
1440 }
1441 
1442 static int wm_adsp_buffer_parse_coeff(struct cs_dsp_coeff_ctl *cs_ctl)
1443 {
1444 	struct wm_adsp_host_buf_coeff_v1 coeff_v1;
1445 	struct wm_adsp_compr_buf *buf;
1446 	struct wm_adsp *dsp = container_of(cs_ctl->dsp, struct wm_adsp, cs_dsp);
1447 	unsigned int version;
1448 	int ret, i;
1449 
1450 	for (i = 0; i < 5; ++i) {
1451 		ret = cs_dsp_coeff_read_ctrl(cs_ctl, &coeff_v1, sizeof(coeff_v1));
1452 		if (ret < 0)
1453 			return ret;
1454 
1455 		if (coeff_v1.host_buf_ptr)
1456 			break;
1457 
1458 		usleep_range(1000, 2000);
1459 	}
1460 
1461 	if (!coeff_v1.host_buf_ptr) {
1462 		adsp_err(dsp, "Failed to acquire host buffer\n");
1463 		return -EIO;
1464 	}
1465 
1466 	buf = wm_adsp_buffer_alloc(dsp);
1467 	if (!buf)
1468 		return -ENOMEM;
1469 
1470 	buf->host_buf_mem_type = cs_ctl->alg_region.type;
1471 	buf->host_buf_ptr = be32_to_cpu(coeff_v1.host_buf_ptr);
1472 
1473 	ret = wm_adsp_buffer_populate(buf);
1474 	if (ret < 0)
1475 		return ret;
1476 
1477 	/*
1478 	 * v0 host_buffer coefficients didn't have versioning, so if the
1479 	 * control is one word, assume version 0.
1480 	 */
1481 	if (cs_ctl->len == 4) {
1482 		compr_dbg(buf, "host_buf_ptr=%x\n", buf->host_buf_ptr);
1483 		return 0;
1484 	}
1485 
1486 	version = be32_to_cpu(coeff_v1.versions) & HOST_BUF_COEFF_COMPAT_VER_MASK;
1487 	version >>= HOST_BUF_COEFF_COMPAT_VER_SHIFT;
1488 
1489 	if (version > HOST_BUF_COEFF_SUPPORTED_COMPAT_VER) {
1490 		adsp_err(dsp,
1491 			 "Host buffer coeff ver %u > supported version %u\n",
1492 			 version, HOST_BUF_COEFF_SUPPORTED_COMPAT_VER);
1493 		return -EINVAL;
1494 	}
1495 
1496 	cs_dsp_remove_padding((u32 *)&coeff_v1.name, ARRAY_SIZE(coeff_v1.name));
1497 
1498 	buf->name = kasprintf(GFP_KERNEL, "%s-dsp-%s", dsp->part,
1499 			      (char *)&coeff_v1.name);
1500 
1501 	compr_dbg(buf, "host_buf_ptr=%x coeff version %u\n",
1502 		  buf->host_buf_ptr, version);
1503 
1504 	return version;
1505 }
1506 
1507 static int wm_adsp_buffer_init(struct wm_adsp *dsp)
1508 {
1509 	struct cs_dsp_coeff_ctl *cs_ctl;
1510 	int ret;
1511 
1512 	list_for_each_entry(cs_ctl, &dsp->cs_dsp.ctl_list, list) {
1513 		if (cs_ctl->type != WMFW_CTL_TYPE_HOST_BUFFER)
1514 			continue;
1515 
1516 		if (!cs_ctl->enabled)
1517 			continue;
1518 
1519 		ret = wm_adsp_buffer_parse_coeff(cs_ctl);
1520 		if (ret < 0) {
1521 			adsp_err(dsp, "Failed to parse coeff: %d\n", ret);
1522 			goto error;
1523 		} else if (ret == 0) {
1524 			/* Only one buffer supported for version 0 */
1525 			return 0;
1526 		}
1527 	}
1528 
1529 	if (list_empty(&dsp->buffer_list)) {
1530 		/* Fall back to legacy support */
1531 		ret = wm_adsp_buffer_parse_legacy(dsp);
1532 		if (ret) {
1533 			adsp_err(dsp, "Failed to parse legacy: %d\n", ret);
1534 			goto error;
1535 		}
1536 	}
1537 
1538 	return 0;
1539 
1540 error:
1541 	wm_adsp_buffer_free(dsp);
1542 	return ret;
1543 }
1544 
1545 static int wm_adsp_buffer_free(struct wm_adsp *dsp)
1546 {
1547 	struct wm_adsp_compr_buf *buf, *tmp;
1548 
1549 	list_for_each_entry_safe(buf, tmp, &dsp->buffer_list, list) {
1550 		wm_adsp_compr_detach(buf->compr);
1551 
1552 		kfree(buf->name);
1553 		kfree(buf->regions);
1554 		list_del(&buf->list);
1555 		kfree(buf);
1556 	}
1557 
1558 	return 0;
1559 }
1560 
1561 static int wm_adsp_buffer_get_error(struct wm_adsp_compr_buf *buf)
1562 {
1563 	int ret;
1564 
1565 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error);
1566 	if (ret < 0) {
1567 		compr_err(buf, "Failed to check buffer error: %d\n", ret);
1568 		return ret;
1569 	}
1570 	if (buf->error != 0) {
1571 		compr_err(buf, "Buffer error occurred: %d\n", buf->error);
1572 		return -EIO;
1573 	}
1574 
1575 	return 0;
1576 }
1577 
1578 int wm_adsp_compr_trigger(struct snd_soc_component *component,
1579 			  struct snd_compr_stream *stream, int cmd)
1580 {
1581 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1582 	struct wm_adsp *dsp = compr->dsp;
1583 	int ret = 0;
1584 
1585 	compr_dbg(compr, "Trigger: %d\n", cmd);
1586 
1587 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1588 
1589 	switch (cmd) {
1590 	case SNDRV_PCM_TRIGGER_START:
1591 		if (!wm_adsp_compr_attached(compr)) {
1592 			ret = wm_adsp_compr_attach(compr);
1593 			if (ret < 0) {
1594 				compr_err(compr, "Failed to link buffer and stream: %d\n",
1595 					  ret);
1596 				break;
1597 			}
1598 		}
1599 
1600 		ret = wm_adsp_buffer_get_error(compr->buf);
1601 		if (ret < 0)
1602 			break;
1603 
1604 		/* Trigger the IRQ at one fragment of data */
1605 		ret = wm_adsp_buffer_write(compr->buf,
1606 					   HOST_BUFFER_FIELD(high_water_mark),
1607 					   wm_adsp_compr_frag_words(compr));
1608 		if (ret < 0) {
1609 			compr_err(compr, "Failed to set high water mark: %d\n",
1610 				  ret);
1611 			break;
1612 		}
1613 		break;
1614 	case SNDRV_PCM_TRIGGER_STOP:
1615 		if (wm_adsp_compr_attached(compr))
1616 			wm_adsp_buffer_clear(compr->buf);
1617 		break;
1618 	default:
1619 		ret = -EINVAL;
1620 		break;
1621 	}
1622 
1623 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1624 
1625 	return ret;
1626 }
1627 EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger);
1628 
1629 static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf)
1630 {
1631 	int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1;
1632 
1633 	return buf->regions[last_region].cumulative_size;
1634 }
1635 
1636 static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf)
1637 {
1638 	u32 next_read_index, next_write_index;
1639 	int write_index, read_index, avail;
1640 	int ret;
1641 
1642 	/* Only sync read index if we haven't already read a valid index */
1643 	if (buf->read_index < 0) {
1644 		ret = wm_adsp_buffer_read(buf,
1645 				HOST_BUFFER_FIELD(next_read_index),
1646 				&next_read_index);
1647 		if (ret < 0)
1648 			return ret;
1649 
1650 		read_index = sign_extend32(next_read_index, 23);
1651 
1652 		if (read_index < 0) {
1653 			compr_dbg(buf, "Avail check on unstarted stream\n");
1654 			return 0;
1655 		}
1656 
1657 		buf->read_index = read_index;
1658 	}
1659 
1660 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index),
1661 			&next_write_index);
1662 	if (ret < 0)
1663 		return ret;
1664 
1665 	write_index = sign_extend32(next_write_index, 23);
1666 
1667 	avail = write_index - buf->read_index;
1668 	if (avail < 0)
1669 		avail += wm_adsp_buffer_size(buf);
1670 
1671 	compr_dbg(buf, "readindex=0x%x, writeindex=0x%x, avail=%d\n",
1672 		  buf->read_index, write_index, avail * CS_DSP_DATA_WORD_SIZE);
1673 
1674 	buf->avail = avail;
1675 
1676 	return 0;
1677 }
1678 
1679 int wm_adsp_compr_handle_irq(struct wm_adsp *dsp)
1680 {
1681 	struct wm_adsp_compr_buf *buf;
1682 	struct wm_adsp_compr *compr;
1683 	int ret = 0;
1684 
1685 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1686 
1687 	if (list_empty(&dsp->buffer_list)) {
1688 		ret = -ENODEV;
1689 		goto out;
1690 	}
1691 
1692 	adsp_dbg(dsp, "Handling buffer IRQ\n");
1693 
1694 	list_for_each_entry(buf, &dsp->buffer_list, list) {
1695 		compr = buf->compr;
1696 
1697 		ret = wm_adsp_buffer_get_error(buf);
1698 		if (ret < 0)
1699 			goto out_notify; /* Wake poll to report error */
1700 
1701 		ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count),
1702 					  &buf->irq_count);
1703 		if (ret < 0) {
1704 			compr_err(buf, "Failed to get irq_count: %d\n", ret);
1705 			goto out;
1706 		}
1707 
1708 		ret = wm_adsp_buffer_update_avail(buf);
1709 		if (ret < 0) {
1710 			compr_err(buf, "Error reading avail: %d\n", ret);
1711 			goto out;
1712 		}
1713 
1714 		if (wm_adsp_fw[dsp->fw].voice_trigger && buf->irq_count == 2)
1715 			ret = WM_ADSP_COMPR_VOICE_TRIGGER;
1716 
1717 out_notify:
1718 		if (compr && compr->stream)
1719 			snd_compr_fragment_elapsed(compr->stream);
1720 	}
1721 
1722 out:
1723 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1724 
1725 	return ret;
1726 }
1727 EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq);
1728 
1729 static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf)
1730 {
1731 	if (buf->irq_count & 0x01)
1732 		return 0;
1733 
1734 	compr_dbg(buf, "Enable IRQ(0x%x) for next fragment\n", buf->irq_count);
1735 
1736 	buf->irq_count |= 0x01;
1737 
1738 	return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack),
1739 				    buf->irq_count);
1740 }
1741 
1742 int wm_adsp_compr_pointer(struct snd_soc_component *component,
1743 			  struct snd_compr_stream *stream,
1744 			  struct snd_compr_tstamp *tstamp)
1745 {
1746 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1747 	struct wm_adsp *dsp = compr->dsp;
1748 	struct wm_adsp_compr_buf *buf;
1749 	int ret = 0;
1750 
1751 	compr_dbg(compr, "Pointer request\n");
1752 
1753 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1754 
1755 	buf = compr->buf;
1756 
1757 	if (dsp->fatal_error || !buf || buf->error) {
1758 		snd_compr_stop_error(stream, SNDRV_PCM_STATE_XRUN);
1759 		ret = -EIO;
1760 		goto out;
1761 	}
1762 
1763 	if (buf->avail < wm_adsp_compr_frag_words(compr)) {
1764 		ret = wm_adsp_buffer_update_avail(buf);
1765 		if (ret < 0) {
1766 			compr_err(compr, "Error reading avail: %d\n", ret);
1767 			goto out;
1768 		}
1769 
1770 		/*
1771 		 * If we really have less than 1 fragment available tell the
1772 		 * DSP to inform us once a whole fragment is available.
1773 		 */
1774 		if (buf->avail < wm_adsp_compr_frag_words(compr)) {
1775 			ret = wm_adsp_buffer_get_error(buf);
1776 			if (ret < 0) {
1777 				if (buf->error)
1778 					snd_compr_stop_error(stream,
1779 							SNDRV_PCM_STATE_XRUN);
1780 				goto out;
1781 			}
1782 
1783 			ret = wm_adsp_buffer_reenable_irq(buf);
1784 			if (ret < 0) {
1785 				compr_err(compr, "Failed to re-enable buffer IRQ: %d\n",
1786 					  ret);
1787 				goto out;
1788 			}
1789 		}
1790 	}
1791 
1792 	tstamp->copied_total = compr->copied_total;
1793 	tstamp->copied_total += buf->avail * CS_DSP_DATA_WORD_SIZE;
1794 	tstamp->sampling_rate = compr->sample_rate;
1795 
1796 out:
1797 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1798 
1799 	return ret;
1800 }
1801 EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer);
1802 
1803 static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target)
1804 {
1805 	struct wm_adsp_compr_buf *buf = compr->buf;
1806 	unsigned int adsp_addr;
1807 	int mem_type, nwords, max_read;
1808 	int i, ret;
1809 
1810 	/* Calculate read parameters */
1811 	for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i)
1812 		if (buf->read_index < buf->regions[i].cumulative_size)
1813 			break;
1814 
1815 	if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions)
1816 		return -EINVAL;
1817 
1818 	mem_type = buf->regions[i].mem_type;
1819 	adsp_addr = buf->regions[i].base_addr +
1820 		    (buf->read_index - buf->regions[i].offset);
1821 
1822 	max_read = wm_adsp_compr_frag_words(compr);
1823 	nwords = buf->regions[i].cumulative_size - buf->read_index;
1824 
1825 	if (nwords > target)
1826 		nwords = target;
1827 	if (nwords > buf->avail)
1828 		nwords = buf->avail;
1829 	if (nwords > max_read)
1830 		nwords = max_read;
1831 	if (!nwords)
1832 		return 0;
1833 
1834 	/* Read data from DSP */
1835 	ret = cs_dsp_read_raw_data_block(&buf->dsp->cs_dsp, mem_type, adsp_addr,
1836 					 nwords, (__be32 *)compr->raw_buf);
1837 	if (ret < 0)
1838 		return ret;
1839 
1840 	cs_dsp_remove_padding(compr->raw_buf, nwords);
1841 
1842 	/* update read index to account for words read */
1843 	buf->read_index += nwords;
1844 	if (buf->read_index == wm_adsp_buffer_size(buf))
1845 		buf->read_index = 0;
1846 
1847 	ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index),
1848 				   buf->read_index);
1849 	if (ret < 0)
1850 		return ret;
1851 
1852 	/* update avail to account for words read */
1853 	buf->avail -= nwords;
1854 
1855 	return nwords;
1856 }
1857 
1858 static int wm_adsp_compr_read(struct wm_adsp_compr *compr,
1859 			      char __user *buf, size_t count)
1860 {
1861 	struct wm_adsp *dsp = compr->dsp;
1862 	int ntotal = 0;
1863 	int nwords, nbytes;
1864 
1865 	compr_dbg(compr, "Requested read of %zu bytes\n", count);
1866 
1867 	if (dsp->fatal_error || !compr->buf || compr->buf->error) {
1868 		snd_compr_stop_error(compr->stream, SNDRV_PCM_STATE_XRUN);
1869 		return -EIO;
1870 	}
1871 
1872 	count /= CS_DSP_DATA_WORD_SIZE;
1873 
1874 	do {
1875 		nwords = wm_adsp_buffer_capture_block(compr, count);
1876 		if (nwords < 0) {
1877 			compr_err(compr, "Failed to capture block: %d\n",
1878 				  nwords);
1879 			return nwords;
1880 		}
1881 
1882 		nbytes = nwords * CS_DSP_DATA_WORD_SIZE;
1883 
1884 		compr_dbg(compr, "Read %d bytes\n", nbytes);
1885 
1886 		if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) {
1887 			compr_err(compr, "Failed to copy data to user: %d, %d\n",
1888 				  ntotal, nbytes);
1889 			return -EFAULT;
1890 		}
1891 
1892 		count -= nwords;
1893 		ntotal += nbytes;
1894 	} while (nwords > 0 && count > 0);
1895 
1896 	compr->copied_total += ntotal;
1897 
1898 	return ntotal;
1899 }
1900 
1901 int wm_adsp_compr_copy(struct snd_soc_component *component,
1902 		       struct snd_compr_stream *stream, char __user *buf,
1903 		       size_t count)
1904 {
1905 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1906 	struct wm_adsp *dsp = compr->dsp;
1907 	int ret;
1908 
1909 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1910 
1911 	if (stream->direction == SND_COMPRESS_CAPTURE)
1912 		ret = wm_adsp_compr_read(compr, buf, count);
1913 	else
1914 		ret = -ENOTSUPP;
1915 
1916 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1917 
1918 	return ret;
1919 }
1920 EXPORT_SYMBOL_GPL(wm_adsp_compr_copy);
1921 
1922 static void wm_adsp_fatal_error(struct cs_dsp *cs_dsp)
1923 {
1924 	struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp);
1925 	struct wm_adsp_compr *compr;
1926 
1927 	dsp->fatal_error = true;
1928 
1929 	list_for_each_entry(compr, &dsp->compr_list, list) {
1930 		if (compr->stream)
1931 			snd_compr_fragment_elapsed(compr->stream);
1932 	}
1933 }
1934 
1935 irqreturn_t wm_adsp2_bus_error(int irq, void *data)
1936 {
1937 	struct wm_adsp *dsp = (struct wm_adsp *)data;
1938 
1939 	cs_dsp_adsp2_bus_error(&dsp->cs_dsp);
1940 
1941 	return IRQ_HANDLED;
1942 }
1943 EXPORT_SYMBOL_GPL(wm_adsp2_bus_error);
1944 
1945 irqreturn_t wm_halo_bus_error(int irq, void *data)
1946 {
1947 	struct wm_adsp *dsp = (struct wm_adsp *)data;
1948 
1949 	cs_dsp_halo_bus_error(&dsp->cs_dsp);
1950 
1951 	return IRQ_HANDLED;
1952 }
1953 EXPORT_SYMBOL_GPL(wm_halo_bus_error);
1954 
1955 irqreturn_t wm_halo_wdt_expire(int irq, void *data)
1956 {
1957 	struct wm_adsp *dsp = data;
1958 
1959 	cs_dsp_halo_wdt_expire(&dsp->cs_dsp);
1960 
1961 	return IRQ_HANDLED;
1962 }
1963 EXPORT_SYMBOL_GPL(wm_halo_wdt_expire);
1964 
1965 static const struct cs_dsp_client_ops wm_adsp1_client_ops = {
1966 	.control_add = wm_adsp_control_add,
1967 	.control_remove = wm_adsp_control_remove,
1968 };
1969 
1970 static const struct cs_dsp_client_ops wm_adsp2_client_ops = {
1971 	.control_add = wm_adsp_control_add,
1972 	.control_remove = wm_adsp_control_remove,
1973 	.post_run = wm_adsp_event_post_run,
1974 	.post_stop = wm_adsp_event_post_stop,
1975 	.watchdog_expired = wm_adsp_fatal_error,
1976 };
1977 
1978 MODULE_LICENSE("GPL v2");
1979