xref: /openbmc/linux/sound/soc/codecs/wm_adsp.c (revision 63705da3)
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 		snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s %x",
621 			 cs_dsp->name, region_name, cs_ctl->alg_region.alg);
622 		break;
623 	case 2:
624 		ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
625 				"%s%c %.12s %x", cs_dsp->name, *region_name,
626 				wm_adsp_fw_text[dsp->fw], cs_ctl->alg_region.alg);
627 		break;
628 	default:
629 		ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
630 				"%s %.12s %x", cs_dsp->name,
631 				wm_adsp_fw_text[dsp->fw], cs_ctl->alg_region.alg);
632 		break;
633 	}
634 
635 	if (cs_ctl->subname) {
636 		int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
637 		int skip = 0;
638 
639 		if (dsp->component->name_prefix)
640 			avail -= strlen(dsp->component->name_prefix) + 1;
641 
642 		/* Truncate the subname from the start if it is too long */
643 		if (cs_ctl->subname_len > avail)
644 			skip = cs_ctl->subname_len - avail;
645 
646 		snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret,
647 			 " %.*s", cs_ctl->subname_len - skip, cs_ctl->subname + skip);
648 	}
649 
650 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
651 	if (!ctl)
652 		return -ENOMEM;
653 	ctl->cs_ctl = cs_ctl;
654 
655 	ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
656 	if (!ctl->name) {
657 		ret = -ENOMEM;
658 		goto err_ctl;
659 	}
660 
661 	cs_ctl->priv = ctl;
662 
663 	INIT_WORK(&ctl->work, wm_adsp_ctl_work);
664 	schedule_work(&ctl->work);
665 
666 	return 0;
667 
668 err_ctl:
669 	kfree(ctl);
670 
671 	return ret;
672 }
673 
674 static void wm_adsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl)
675 {
676 	struct wm_coeff_ctl *ctl = cs_ctl->priv;
677 
678 	cancel_work_sync(&ctl->work);
679 
680 	kfree(ctl->name);
681 	kfree(ctl);
682 }
683 
684 int wm_adsp_write_ctl(struct wm_adsp *dsp, const char *name, int type,
685 		      unsigned int alg, void *buf, size_t len)
686 {
687 	struct cs_dsp_coeff_ctl *cs_ctl;
688 	struct wm_coeff_ctl *ctl;
689 	struct snd_kcontrol *kcontrol;
690 	char ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
691 	int ret;
692 
693 	cs_ctl = cs_dsp_get_ctl(&dsp->cs_dsp, name, type, alg);
694 	if (!cs_ctl)
695 		return -EINVAL;
696 
697 	ctl = cs_ctl->priv;
698 
699 	if (len > cs_ctl->len)
700 		return -EINVAL;
701 
702 	ret = cs_dsp_coeff_write_ctrl(cs_ctl, buf, len);
703 	if (ret)
704 		return ret;
705 
706 	if (cs_ctl->flags & WMFW_CTL_FLAG_SYS)
707 		return 0;
708 
709 	if (dsp->component->name_prefix)
710 		snprintf(ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s",
711 			 dsp->component->name_prefix, ctl->name);
712 	else
713 		snprintf(ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s",
714 			 ctl->name);
715 
716 	kcontrol = snd_soc_card_get_kcontrol(dsp->component->card, ctl_name);
717 	if (!kcontrol) {
718 		adsp_err(dsp, "Can't find kcontrol %s\n", ctl_name);
719 		return -EINVAL;
720 	}
721 
722 	snd_ctl_notify(dsp->component->card->snd_card,
723 		       SNDRV_CTL_EVENT_MASK_VALUE, &kcontrol->id);
724 
725 	return 0;
726 }
727 EXPORT_SYMBOL_GPL(wm_adsp_write_ctl);
728 
729 int wm_adsp_read_ctl(struct wm_adsp *dsp, const char *name, int type,
730 		     unsigned int alg, void *buf, size_t len)
731 {
732 	struct cs_dsp_coeff_ctl *cs_ctl;
733 
734 	cs_ctl = cs_dsp_get_ctl(&dsp->cs_dsp, name, type, alg);
735 	if (!cs_ctl)
736 		return -EINVAL;
737 
738 	if (len > cs_ctl->len)
739 		return -EINVAL;
740 
741 	return cs_dsp_coeff_read_ctrl(cs_ctl, buf, len);
742 }
743 EXPORT_SYMBOL_GPL(wm_adsp_read_ctl);
744 
745 static void wm_adsp_release_firmware_files(struct wm_adsp *dsp,
746 					   const struct firmware *wmfw_firmware,
747 					   char *wmfw_filename,
748 					   const struct firmware *coeff_firmware,
749 					   char *coeff_filename)
750 {
751 	if (wmfw_firmware)
752 		release_firmware(wmfw_firmware);
753 	kfree(wmfw_filename);
754 
755 	if (coeff_firmware)
756 		release_firmware(coeff_firmware);
757 	kfree(coeff_filename);
758 }
759 
760 static int wm_adsp_request_firmware_file(struct wm_adsp *dsp,
761 					 const struct firmware **firmware,
762 					 char **filename,
763 					 char *suffix)
764 {
765 	struct cs_dsp *cs_dsp = &dsp->cs_dsp;
766 	int ret = 0;
767 
768 	*filename = kasprintf(GFP_KERNEL, "%s-%s-%s.%s", dsp->part, dsp->fwf_name,
769 			      wm_adsp_fw[dsp->fw].file, suffix);
770 	if (*filename == NULL)
771 		return -ENOMEM;
772 
773 	ret = request_firmware(firmware, *filename, cs_dsp->dev);
774 	if (ret != 0) {
775 		adsp_err(dsp, "Failed to request '%s'\n", *filename);
776 		kfree(*filename);
777 		*filename = NULL;
778 	}
779 
780 	return ret;
781 }
782 
783 static int wm_adsp_request_firmware_files(struct wm_adsp *dsp,
784 					  const struct firmware **wmfw_firmware,
785 					  char **wmfw_filename,
786 					  const struct firmware **coeff_firmware,
787 					  char **coeff_filename)
788 {
789 	int ret = 0;
790 
791 	ret = wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, "wmfw");
792 	if (ret != 0)
793 		return ret;
794 
795 	wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, "bin");
796 
797 	return 0;
798 }
799 
800 static int wm_adsp_common_init(struct wm_adsp *dsp)
801 {
802 	char *p;
803 
804 	INIT_LIST_HEAD(&dsp->compr_list);
805 	INIT_LIST_HEAD(&dsp->buffer_list);
806 
807 	if (!dsp->fwf_name) {
808 		p = devm_kstrdup(dsp->cs_dsp.dev, dsp->cs_dsp.name, GFP_KERNEL);
809 		if (!p)
810 			return -ENOMEM;
811 
812 		dsp->fwf_name = p;
813 		for (; *p != 0; ++p)
814 			*p = tolower(*p);
815 	}
816 
817 	return 0;
818 }
819 
820 int wm_adsp1_init(struct wm_adsp *dsp)
821 {
822 	int ret;
823 
824 	dsp->cs_dsp.client_ops = &wm_adsp1_client_ops;
825 
826 	ret = cs_dsp_adsp1_init(&dsp->cs_dsp);
827 	if (ret)
828 		return ret;
829 
830 	return wm_adsp_common_init(dsp);
831 }
832 EXPORT_SYMBOL_GPL(wm_adsp1_init);
833 
834 int wm_adsp1_event(struct snd_soc_dapm_widget *w,
835 		   struct snd_kcontrol *kcontrol,
836 		   int event)
837 {
838 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
839 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
840 	struct wm_adsp *dsp = &dsps[w->shift];
841 	int ret = 0;
842 	char *wmfw_filename = NULL;
843 	const struct firmware *wmfw_firmware = NULL;
844 	char *coeff_filename = NULL;
845 	const struct firmware *coeff_firmware = NULL;
846 
847 	dsp->component = component;
848 
849 	switch (event) {
850 	case SND_SOC_DAPM_POST_PMU:
851 		ret = wm_adsp_request_firmware_files(dsp,
852 						     &wmfw_firmware, &wmfw_filename,
853 						     &coeff_firmware, &coeff_filename);
854 		if (ret)
855 			break;
856 
857 		ret = cs_dsp_adsp1_power_up(&dsp->cs_dsp,
858 					    wmfw_firmware, wmfw_filename,
859 					    coeff_firmware, coeff_filename,
860 					    wm_adsp_fw_text[dsp->fw]);
861 
862 		wm_adsp_release_firmware_files(dsp,
863 					       wmfw_firmware, wmfw_filename,
864 					       coeff_firmware, coeff_filename);
865 		break;
866 	case SND_SOC_DAPM_PRE_PMD:
867 		cs_dsp_adsp1_power_down(&dsp->cs_dsp);
868 		break;
869 	default:
870 		break;
871 	}
872 
873 	return ret;
874 }
875 EXPORT_SYMBOL_GPL(wm_adsp1_event);
876 
877 int wm_adsp2_set_dspclk(struct snd_soc_dapm_widget *w, unsigned int freq)
878 {
879 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
880 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
881 	struct wm_adsp *dsp = &dsps[w->shift];
882 
883 	return cs_dsp_set_dspclk(&dsp->cs_dsp, freq);
884 }
885 EXPORT_SYMBOL_GPL(wm_adsp2_set_dspclk);
886 
887 int wm_adsp2_preloader_get(struct snd_kcontrol *kcontrol,
888 			   struct snd_ctl_elem_value *ucontrol)
889 {
890 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
891 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
892 	struct soc_mixer_control *mc =
893 		(struct soc_mixer_control *)kcontrol->private_value;
894 	struct wm_adsp *dsp = &dsps[mc->shift - 1];
895 
896 	ucontrol->value.integer.value[0] = dsp->preloaded;
897 
898 	return 0;
899 }
900 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_get);
901 
902 int wm_adsp2_preloader_put(struct snd_kcontrol *kcontrol,
903 			   struct snd_ctl_elem_value *ucontrol)
904 {
905 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
906 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
907 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
908 	struct soc_mixer_control *mc =
909 		(struct soc_mixer_control *)kcontrol->private_value;
910 	struct wm_adsp *dsp = &dsps[mc->shift - 1];
911 	char preload[32];
912 
913 	snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->cs_dsp.name);
914 
915 	dsp->preloaded = ucontrol->value.integer.value[0];
916 
917 	if (ucontrol->value.integer.value[0])
918 		snd_soc_component_force_enable_pin(component, preload);
919 	else
920 		snd_soc_component_disable_pin(component, preload);
921 
922 	snd_soc_dapm_sync(dapm);
923 
924 	flush_work(&dsp->boot_work);
925 
926 	return 0;
927 }
928 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_put);
929 
930 static void wm_adsp_boot_work(struct work_struct *work)
931 {
932 	struct wm_adsp *dsp = container_of(work,
933 					   struct wm_adsp,
934 					   boot_work);
935 	int ret = 0;
936 	char *wmfw_filename = NULL;
937 	const struct firmware *wmfw_firmware = NULL;
938 	char *coeff_filename = NULL;
939 	const struct firmware *coeff_firmware = NULL;
940 
941 	ret = wm_adsp_request_firmware_files(dsp,
942 					     &wmfw_firmware, &wmfw_filename,
943 					     &coeff_firmware, &coeff_filename);
944 	if (ret)
945 		return;
946 
947 	cs_dsp_power_up(&dsp->cs_dsp,
948 			wmfw_firmware, wmfw_filename,
949 			coeff_firmware, coeff_filename,
950 			wm_adsp_fw_text[dsp->fw]);
951 
952 	wm_adsp_release_firmware_files(dsp,
953 				       wmfw_firmware, wmfw_filename,
954 				       coeff_firmware, coeff_filename);
955 }
956 
957 int wm_adsp_early_event(struct snd_soc_dapm_widget *w,
958 			struct snd_kcontrol *kcontrol, int event)
959 {
960 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
961 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
962 	struct wm_adsp *dsp = &dsps[w->shift];
963 
964 	switch (event) {
965 	case SND_SOC_DAPM_PRE_PMU:
966 		queue_work(system_unbound_wq, &dsp->boot_work);
967 		break;
968 	case SND_SOC_DAPM_PRE_PMD:
969 		cs_dsp_power_down(&dsp->cs_dsp);
970 		break;
971 	default:
972 		break;
973 	}
974 
975 	return 0;
976 }
977 EXPORT_SYMBOL_GPL(wm_adsp_early_event);
978 
979 static int wm_adsp_event_post_run(struct cs_dsp *cs_dsp)
980 {
981 	struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp);
982 
983 	if (wm_adsp_fw[dsp->fw].num_caps != 0)
984 		return wm_adsp_buffer_init(dsp);
985 
986 	return 0;
987 }
988 
989 static void wm_adsp_event_post_stop(struct cs_dsp *cs_dsp)
990 {
991 	struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp);
992 
993 	if (wm_adsp_fw[dsp->fw].num_caps != 0)
994 		wm_adsp_buffer_free(dsp);
995 
996 	dsp->fatal_error = false;
997 }
998 
999 int wm_adsp_event(struct snd_soc_dapm_widget *w,
1000 		  struct snd_kcontrol *kcontrol, int event)
1001 {
1002 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1003 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
1004 	struct wm_adsp *dsp = &dsps[w->shift];
1005 	int ret = 0;
1006 
1007 	switch (event) {
1008 	case SND_SOC_DAPM_POST_PMU:
1009 		flush_work(&dsp->boot_work);
1010 		ret = cs_dsp_run(&dsp->cs_dsp);
1011 		break;
1012 	case SND_SOC_DAPM_PRE_PMD:
1013 		cs_dsp_stop(&dsp->cs_dsp);
1014 		break;
1015 	default:
1016 		break;
1017 	}
1018 
1019 	return ret;
1020 }
1021 EXPORT_SYMBOL_GPL(wm_adsp_event);
1022 
1023 int wm_adsp2_component_probe(struct wm_adsp *dsp, struct snd_soc_component *component)
1024 {
1025 	char preload[32];
1026 
1027 	snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->cs_dsp.name);
1028 	snd_soc_component_disable_pin(component, preload);
1029 
1030 	cs_dsp_init_debugfs(&dsp->cs_dsp, component->debugfs_root);
1031 
1032 	dsp->component = component;
1033 
1034 	return 0;
1035 }
1036 EXPORT_SYMBOL_GPL(wm_adsp2_component_probe);
1037 
1038 int wm_adsp2_component_remove(struct wm_adsp *dsp, struct snd_soc_component *component)
1039 {
1040 	cs_dsp_cleanup_debugfs(&dsp->cs_dsp);
1041 
1042 	return 0;
1043 }
1044 EXPORT_SYMBOL_GPL(wm_adsp2_component_remove);
1045 
1046 int wm_adsp2_init(struct wm_adsp *dsp)
1047 {
1048 	int ret;
1049 
1050 	INIT_WORK(&dsp->boot_work, wm_adsp_boot_work);
1051 
1052 	dsp->sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr);
1053 	dsp->cs_dsp.client_ops = &wm_adsp2_client_ops;
1054 
1055 	ret = cs_dsp_adsp2_init(&dsp->cs_dsp);
1056 	if (ret)
1057 		return ret;
1058 
1059 	return wm_adsp_common_init(dsp);
1060 }
1061 EXPORT_SYMBOL_GPL(wm_adsp2_init);
1062 
1063 int wm_halo_init(struct wm_adsp *dsp)
1064 {
1065 	int ret;
1066 
1067 	INIT_WORK(&dsp->boot_work, wm_adsp_boot_work);
1068 
1069 	dsp->sys_config_size = sizeof(struct wm_halo_system_config_xm_hdr);
1070 	dsp->cs_dsp.client_ops = &wm_adsp2_client_ops;
1071 
1072 	ret = cs_dsp_halo_init(&dsp->cs_dsp);
1073 	if (ret)
1074 		return ret;
1075 
1076 	return wm_adsp_common_init(dsp);
1077 }
1078 EXPORT_SYMBOL_GPL(wm_halo_init);
1079 
1080 void wm_adsp2_remove(struct wm_adsp *dsp)
1081 {
1082 	cs_dsp_remove(&dsp->cs_dsp);
1083 }
1084 EXPORT_SYMBOL_GPL(wm_adsp2_remove);
1085 
1086 static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr)
1087 {
1088 	return compr->buf != NULL;
1089 }
1090 
1091 static int wm_adsp_compr_attach(struct wm_adsp_compr *compr)
1092 {
1093 	struct wm_adsp_compr_buf *buf = NULL, *tmp;
1094 
1095 	if (compr->dsp->fatal_error)
1096 		return -EINVAL;
1097 
1098 	list_for_each_entry(tmp, &compr->dsp->buffer_list, list) {
1099 		if (!tmp->name || !strcmp(compr->name, tmp->name)) {
1100 			buf = tmp;
1101 			break;
1102 		}
1103 	}
1104 
1105 	if (!buf)
1106 		return -EINVAL;
1107 
1108 	compr->buf = buf;
1109 	buf->compr = compr;
1110 
1111 	return 0;
1112 }
1113 
1114 static void wm_adsp_compr_detach(struct wm_adsp_compr *compr)
1115 {
1116 	if (!compr)
1117 		return;
1118 
1119 	/* Wake the poll so it can see buffer is no longer attached */
1120 	if (compr->stream)
1121 		snd_compr_fragment_elapsed(compr->stream);
1122 
1123 	if (wm_adsp_compr_attached(compr)) {
1124 		compr->buf->compr = NULL;
1125 		compr->buf = NULL;
1126 	}
1127 }
1128 
1129 int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream)
1130 {
1131 	struct wm_adsp_compr *compr, *tmp;
1132 	struct snd_soc_pcm_runtime *rtd = stream->private_data;
1133 	int ret = 0;
1134 
1135 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1136 
1137 	if (wm_adsp_fw[dsp->fw].num_caps == 0) {
1138 		adsp_err(dsp, "%s: Firmware does not support compressed API\n",
1139 			 asoc_rtd_to_codec(rtd, 0)->name);
1140 		ret = -ENXIO;
1141 		goto out;
1142 	}
1143 
1144 	if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) {
1145 		adsp_err(dsp, "%s: Firmware does not support stream direction\n",
1146 			 asoc_rtd_to_codec(rtd, 0)->name);
1147 		ret = -EINVAL;
1148 		goto out;
1149 	}
1150 
1151 	list_for_each_entry(tmp, &dsp->compr_list, list) {
1152 		if (!strcmp(tmp->name, asoc_rtd_to_codec(rtd, 0)->name)) {
1153 			adsp_err(dsp, "%s: Only a single stream supported per dai\n",
1154 				 asoc_rtd_to_codec(rtd, 0)->name);
1155 			ret = -EBUSY;
1156 			goto out;
1157 		}
1158 	}
1159 
1160 	compr = kzalloc(sizeof(*compr), GFP_KERNEL);
1161 	if (!compr) {
1162 		ret = -ENOMEM;
1163 		goto out;
1164 	}
1165 
1166 	compr->dsp = dsp;
1167 	compr->stream = stream;
1168 	compr->name = asoc_rtd_to_codec(rtd, 0)->name;
1169 
1170 	list_add_tail(&compr->list, &dsp->compr_list);
1171 
1172 	stream->runtime->private_data = compr;
1173 
1174 out:
1175 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1176 
1177 	return ret;
1178 }
1179 EXPORT_SYMBOL_GPL(wm_adsp_compr_open);
1180 
1181 int wm_adsp_compr_free(struct snd_soc_component *component,
1182 		       struct snd_compr_stream *stream)
1183 {
1184 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1185 	struct wm_adsp *dsp = compr->dsp;
1186 
1187 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1188 
1189 	wm_adsp_compr_detach(compr);
1190 	list_del(&compr->list);
1191 
1192 	kfree(compr->raw_buf);
1193 	kfree(compr);
1194 
1195 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1196 
1197 	return 0;
1198 }
1199 EXPORT_SYMBOL_GPL(wm_adsp_compr_free);
1200 
1201 static int wm_adsp_compr_check_params(struct snd_compr_stream *stream,
1202 				      struct snd_compr_params *params)
1203 {
1204 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1205 	struct wm_adsp *dsp = compr->dsp;
1206 	const struct wm_adsp_fw_caps *caps;
1207 	const struct snd_codec_desc *desc;
1208 	int i, j;
1209 
1210 	if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE ||
1211 	    params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE ||
1212 	    params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS ||
1213 	    params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS ||
1214 	    params->buffer.fragment_size % CS_DSP_DATA_WORD_SIZE) {
1215 		compr_err(compr, "Invalid buffer fragsize=%d fragments=%d\n",
1216 			  params->buffer.fragment_size,
1217 			  params->buffer.fragments);
1218 
1219 		return -EINVAL;
1220 	}
1221 
1222 	for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) {
1223 		caps = &wm_adsp_fw[dsp->fw].caps[i];
1224 		desc = &caps->desc;
1225 
1226 		if (caps->id != params->codec.id)
1227 			continue;
1228 
1229 		if (stream->direction == SND_COMPRESS_PLAYBACK) {
1230 			if (desc->max_ch < params->codec.ch_out)
1231 				continue;
1232 		} else {
1233 			if (desc->max_ch < params->codec.ch_in)
1234 				continue;
1235 		}
1236 
1237 		if (!(desc->formats & (1 << params->codec.format)))
1238 			continue;
1239 
1240 		for (j = 0; j < desc->num_sample_rates; ++j)
1241 			if (desc->sample_rates[j] == params->codec.sample_rate)
1242 				return 0;
1243 	}
1244 
1245 	compr_err(compr, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n",
1246 		  params->codec.id, params->codec.ch_in, params->codec.ch_out,
1247 		  params->codec.sample_rate, params->codec.format);
1248 	return -EINVAL;
1249 }
1250 
1251 static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr)
1252 {
1253 	return compr->size.fragment_size / CS_DSP_DATA_WORD_SIZE;
1254 }
1255 
1256 int wm_adsp_compr_set_params(struct snd_soc_component *component,
1257 			     struct snd_compr_stream *stream,
1258 			     struct snd_compr_params *params)
1259 {
1260 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1261 	unsigned int size;
1262 	int ret;
1263 
1264 	ret = wm_adsp_compr_check_params(stream, params);
1265 	if (ret)
1266 		return ret;
1267 
1268 	compr->size = params->buffer;
1269 
1270 	compr_dbg(compr, "fragment_size=%d fragments=%d\n",
1271 		  compr->size.fragment_size, compr->size.fragments);
1272 
1273 	size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf);
1274 	compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL);
1275 	if (!compr->raw_buf)
1276 		return -ENOMEM;
1277 
1278 	compr->sample_rate = params->codec.sample_rate;
1279 
1280 	return 0;
1281 }
1282 EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params);
1283 
1284 int wm_adsp_compr_get_caps(struct snd_soc_component *component,
1285 			   struct snd_compr_stream *stream,
1286 			   struct snd_compr_caps *caps)
1287 {
1288 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1289 	int fw = compr->dsp->fw;
1290 	int i;
1291 
1292 	if (wm_adsp_fw[fw].caps) {
1293 		for (i = 0; i < wm_adsp_fw[fw].num_caps; i++)
1294 			caps->codecs[i] = wm_adsp_fw[fw].caps[i].id;
1295 
1296 		caps->num_codecs = i;
1297 		caps->direction = wm_adsp_fw[fw].compr_direction;
1298 
1299 		caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE;
1300 		caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE;
1301 		caps->min_fragments = WM_ADSP_MIN_FRAGMENTS;
1302 		caps->max_fragments = WM_ADSP_MAX_FRAGMENTS;
1303 	}
1304 
1305 	return 0;
1306 }
1307 EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps);
1308 
1309 static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf,
1310 				      unsigned int field_offset, u32 *data)
1311 {
1312 	return cs_dsp_read_data_word(&buf->dsp->cs_dsp, buf->host_buf_mem_type,
1313 				     buf->host_buf_ptr + field_offset, data);
1314 }
1315 
1316 static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf,
1317 				       unsigned int field_offset, u32 data)
1318 {
1319 	return cs_dsp_write_data_word(&buf->dsp->cs_dsp, buf->host_buf_mem_type,
1320 				      buf->host_buf_ptr + field_offset,
1321 				      data);
1322 }
1323 
1324 static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf)
1325 {
1326 	const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps;
1327 	struct wm_adsp_buffer_region *region;
1328 	u32 offset = 0;
1329 	int i, ret;
1330 
1331 	buf->regions = kcalloc(caps->num_regions, sizeof(*buf->regions),
1332 			       GFP_KERNEL);
1333 	if (!buf->regions)
1334 		return -ENOMEM;
1335 
1336 	for (i = 0; i < caps->num_regions; ++i) {
1337 		region = &buf->regions[i];
1338 
1339 		region->offset = offset;
1340 		region->mem_type = caps->region_defs[i].mem_type;
1341 
1342 		ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset,
1343 					  &region->base_addr);
1344 		if (ret < 0)
1345 			return ret;
1346 
1347 		ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset,
1348 					  &offset);
1349 		if (ret < 0)
1350 			return ret;
1351 
1352 		region->cumulative_size = offset;
1353 
1354 		compr_dbg(buf,
1355 			  "region=%d type=%d base=%08x off=%08x size=%08x\n",
1356 			  i, region->mem_type, region->base_addr,
1357 			  region->offset, region->cumulative_size);
1358 	}
1359 
1360 	return 0;
1361 }
1362 
1363 static void wm_adsp_buffer_clear(struct wm_adsp_compr_buf *buf)
1364 {
1365 	buf->irq_count = 0xFFFFFFFF;
1366 	buf->read_index = -1;
1367 	buf->avail = 0;
1368 }
1369 
1370 static struct wm_adsp_compr_buf *wm_adsp_buffer_alloc(struct wm_adsp *dsp)
1371 {
1372 	struct wm_adsp_compr_buf *buf;
1373 
1374 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1375 	if (!buf)
1376 		return NULL;
1377 
1378 	buf->dsp = dsp;
1379 
1380 	wm_adsp_buffer_clear(buf);
1381 
1382 	list_add_tail(&buf->list, &dsp->buffer_list);
1383 
1384 	return buf;
1385 }
1386 
1387 static int wm_adsp_buffer_parse_legacy(struct wm_adsp *dsp)
1388 {
1389 	struct cs_dsp_alg_region *alg_region;
1390 	struct wm_adsp_compr_buf *buf;
1391 	u32 xmalg, addr, magic;
1392 	int i, ret;
1393 
1394 	alg_region = cs_dsp_find_alg_region(&dsp->cs_dsp, WMFW_ADSP2_XM, dsp->cs_dsp.fw_id);
1395 	if (!alg_region) {
1396 		adsp_err(dsp, "No algorithm region found\n");
1397 		return -EINVAL;
1398 	}
1399 
1400 	buf = wm_adsp_buffer_alloc(dsp);
1401 	if (!buf)
1402 		return -ENOMEM;
1403 
1404 	xmalg = dsp->sys_config_size / sizeof(__be32);
1405 
1406 	addr = alg_region->base + xmalg + ALG_XM_FIELD(magic);
1407 	ret = cs_dsp_read_data_word(&dsp->cs_dsp, WMFW_ADSP2_XM, addr, &magic);
1408 	if (ret < 0)
1409 		return ret;
1410 
1411 	if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC)
1412 		return -ENODEV;
1413 
1414 	addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr);
1415 	for (i = 0; i < 5; ++i) {
1416 		ret = cs_dsp_read_data_word(&dsp->cs_dsp, WMFW_ADSP2_XM, addr,
1417 					    &buf->host_buf_ptr);
1418 		if (ret < 0)
1419 			return ret;
1420 
1421 		if (buf->host_buf_ptr)
1422 			break;
1423 
1424 		usleep_range(1000, 2000);
1425 	}
1426 
1427 	if (!buf->host_buf_ptr)
1428 		return -EIO;
1429 
1430 	buf->host_buf_mem_type = WMFW_ADSP2_XM;
1431 
1432 	ret = wm_adsp_buffer_populate(buf);
1433 	if (ret < 0)
1434 		return ret;
1435 
1436 	compr_dbg(buf, "legacy host_buf_ptr=%x\n", buf->host_buf_ptr);
1437 
1438 	return 0;
1439 }
1440 
1441 static int wm_adsp_buffer_parse_coeff(struct cs_dsp_coeff_ctl *cs_ctl)
1442 {
1443 	struct wm_adsp_host_buf_coeff_v1 coeff_v1;
1444 	struct wm_adsp_compr_buf *buf;
1445 	struct wm_adsp *dsp = container_of(cs_ctl->dsp, struct wm_adsp, cs_dsp);
1446 	unsigned int version;
1447 	int ret, i;
1448 
1449 	for (i = 0; i < 5; ++i) {
1450 		ret = cs_dsp_coeff_read_ctrl(cs_ctl, &coeff_v1, sizeof(coeff_v1));
1451 		if (ret < 0)
1452 			return ret;
1453 
1454 		if (coeff_v1.host_buf_ptr)
1455 			break;
1456 
1457 		usleep_range(1000, 2000);
1458 	}
1459 
1460 	if (!coeff_v1.host_buf_ptr) {
1461 		adsp_err(dsp, "Failed to acquire host buffer\n");
1462 		return -EIO;
1463 	}
1464 
1465 	buf = wm_adsp_buffer_alloc(dsp);
1466 	if (!buf)
1467 		return -ENOMEM;
1468 
1469 	buf->host_buf_mem_type = cs_ctl->alg_region.type;
1470 	buf->host_buf_ptr = be32_to_cpu(coeff_v1.host_buf_ptr);
1471 
1472 	ret = wm_adsp_buffer_populate(buf);
1473 	if (ret < 0)
1474 		return ret;
1475 
1476 	/*
1477 	 * v0 host_buffer coefficients didn't have versioning, so if the
1478 	 * control is one word, assume version 0.
1479 	 */
1480 	if (cs_ctl->len == 4) {
1481 		compr_dbg(buf, "host_buf_ptr=%x\n", buf->host_buf_ptr);
1482 		return 0;
1483 	}
1484 
1485 	version = be32_to_cpu(coeff_v1.versions) & HOST_BUF_COEFF_COMPAT_VER_MASK;
1486 	version >>= HOST_BUF_COEFF_COMPAT_VER_SHIFT;
1487 
1488 	if (version > HOST_BUF_COEFF_SUPPORTED_COMPAT_VER) {
1489 		adsp_err(dsp,
1490 			 "Host buffer coeff ver %u > supported version %u\n",
1491 			 version, HOST_BUF_COEFF_SUPPORTED_COMPAT_VER);
1492 		return -EINVAL;
1493 	}
1494 
1495 	cs_dsp_remove_padding((u32 *)&coeff_v1.name, ARRAY_SIZE(coeff_v1.name));
1496 
1497 	buf->name = kasprintf(GFP_KERNEL, "%s-dsp-%s", dsp->part,
1498 			      (char *)&coeff_v1.name);
1499 
1500 	compr_dbg(buf, "host_buf_ptr=%x coeff version %u\n",
1501 		  buf->host_buf_ptr, version);
1502 
1503 	return version;
1504 }
1505 
1506 static int wm_adsp_buffer_init(struct wm_adsp *dsp)
1507 {
1508 	struct cs_dsp_coeff_ctl *cs_ctl;
1509 	int ret;
1510 
1511 	list_for_each_entry(cs_ctl, &dsp->cs_dsp.ctl_list, list) {
1512 		if (cs_ctl->type != WMFW_CTL_TYPE_HOST_BUFFER)
1513 			continue;
1514 
1515 		if (!cs_ctl->enabled)
1516 			continue;
1517 
1518 		ret = wm_adsp_buffer_parse_coeff(cs_ctl);
1519 		if (ret < 0) {
1520 			adsp_err(dsp, "Failed to parse coeff: %d\n", ret);
1521 			goto error;
1522 		} else if (ret == 0) {
1523 			/* Only one buffer supported for version 0 */
1524 			return 0;
1525 		}
1526 	}
1527 
1528 	if (list_empty(&dsp->buffer_list)) {
1529 		/* Fall back to legacy support */
1530 		ret = wm_adsp_buffer_parse_legacy(dsp);
1531 		if (ret) {
1532 			adsp_err(dsp, "Failed to parse legacy: %d\n", ret);
1533 			goto error;
1534 		}
1535 	}
1536 
1537 	return 0;
1538 
1539 error:
1540 	wm_adsp_buffer_free(dsp);
1541 	return ret;
1542 }
1543 
1544 static int wm_adsp_buffer_free(struct wm_adsp *dsp)
1545 {
1546 	struct wm_adsp_compr_buf *buf, *tmp;
1547 
1548 	list_for_each_entry_safe(buf, tmp, &dsp->buffer_list, list) {
1549 		wm_adsp_compr_detach(buf->compr);
1550 
1551 		kfree(buf->name);
1552 		kfree(buf->regions);
1553 		list_del(&buf->list);
1554 		kfree(buf);
1555 	}
1556 
1557 	return 0;
1558 }
1559 
1560 static int wm_adsp_buffer_get_error(struct wm_adsp_compr_buf *buf)
1561 {
1562 	int ret;
1563 
1564 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error);
1565 	if (ret < 0) {
1566 		compr_err(buf, "Failed to check buffer error: %d\n", ret);
1567 		return ret;
1568 	}
1569 	if (buf->error != 0) {
1570 		compr_err(buf, "Buffer error occurred: %d\n", buf->error);
1571 		return -EIO;
1572 	}
1573 
1574 	return 0;
1575 }
1576 
1577 int wm_adsp_compr_trigger(struct snd_soc_component *component,
1578 			  struct snd_compr_stream *stream, int cmd)
1579 {
1580 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1581 	struct wm_adsp *dsp = compr->dsp;
1582 	int ret = 0;
1583 
1584 	compr_dbg(compr, "Trigger: %d\n", cmd);
1585 
1586 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1587 
1588 	switch (cmd) {
1589 	case SNDRV_PCM_TRIGGER_START:
1590 		if (!wm_adsp_compr_attached(compr)) {
1591 			ret = wm_adsp_compr_attach(compr);
1592 			if (ret < 0) {
1593 				compr_err(compr, "Failed to link buffer and stream: %d\n",
1594 					  ret);
1595 				break;
1596 			}
1597 		}
1598 
1599 		ret = wm_adsp_buffer_get_error(compr->buf);
1600 		if (ret < 0)
1601 			break;
1602 
1603 		/* Trigger the IRQ at one fragment of data */
1604 		ret = wm_adsp_buffer_write(compr->buf,
1605 					   HOST_BUFFER_FIELD(high_water_mark),
1606 					   wm_adsp_compr_frag_words(compr));
1607 		if (ret < 0) {
1608 			compr_err(compr, "Failed to set high water mark: %d\n",
1609 				  ret);
1610 			break;
1611 		}
1612 		break;
1613 	case SNDRV_PCM_TRIGGER_STOP:
1614 		if (wm_adsp_compr_attached(compr))
1615 			wm_adsp_buffer_clear(compr->buf);
1616 		break;
1617 	default:
1618 		ret = -EINVAL;
1619 		break;
1620 	}
1621 
1622 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1623 
1624 	return ret;
1625 }
1626 EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger);
1627 
1628 static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf)
1629 {
1630 	int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1;
1631 
1632 	return buf->regions[last_region].cumulative_size;
1633 }
1634 
1635 static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf)
1636 {
1637 	u32 next_read_index, next_write_index;
1638 	int write_index, read_index, avail;
1639 	int ret;
1640 
1641 	/* Only sync read index if we haven't already read a valid index */
1642 	if (buf->read_index < 0) {
1643 		ret = wm_adsp_buffer_read(buf,
1644 				HOST_BUFFER_FIELD(next_read_index),
1645 				&next_read_index);
1646 		if (ret < 0)
1647 			return ret;
1648 
1649 		read_index = sign_extend32(next_read_index, 23);
1650 
1651 		if (read_index < 0) {
1652 			compr_dbg(buf, "Avail check on unstarted stream\n");
1653 			return 0;
1654 		}
1655 
1656 		buf->read_index = read_index;
1657 	}
1658 
1659 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index),
1660 			&next_write_index);
1661 	if (ret < 0)
1662 		return ret;
1663 
1664 	write_index = sign_extend32(next_write_index, 23);
1665 
1666 	avail = write_index - buf->read_index;
1667 	if (avail < 0)
1668 		avail += wm_adsp_buffer_size(buf);
1669 
1670 	compr_dbg(buf, "readindex=0x%x, writeindex=0x%x, avail=%d\n",
1671 		  buf->read_index, write_index, avail * CS_DSP_DATA_WORD_SIZE);
1672 
1673 	buf->avail = avail;
1674 
1675 	return 0;
1676 }
1677 
1678 int wm_adsp_compr_handle_irq(struct wm_adsp *dsp)
1679 {
1680 	struct wm_adsp_compr_buf *buf;
1681 	struct wm_adsp_compr *compr;
1682 	int ret = 0;
1683 
1684 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1685 
1686 	if (list_empty(&dsp->buffer_list)) {
1687 		ret = -ENODEV;
1688 		goto out;
1689 	}
1690 
1691 	adsp_dbg(dsp, "Handling buffer IRQ\n");
1692 
1693 	list_for_each_entry(buf, &dsp->buffer_list, list) {
1694 		compr = buf->compr;
1695 
1696 		ret = wm_adsp_buffer_get_error(buf);
1697 		if (ret < 0)
1698 			goto out_notify; /* Wake poll to report error */
1699 
1700 		ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count),
1701 					  &buf->irq_count);
1702 		if (ret < 0) {
1703 			compr_err(buf, "Failed to get irq_count: %d\n", ret);
1704 			goto out;
1705 		}
1706 
1707 		ret = wm_adsp_buffer_update_avail(buf);
1708 		if (ret < 0) {
1709 			compr_err(buf, "Error reading avail: %d\n", ret);
1710 			goto out;
1711 		}
1712 
1713 		if (wm_adsp_fw[dsp->fw].voice_trigger && buf->irq_count == 2)
1714 			ret = WM_ADSP_COMPR_VOICE_TRIGGER;
1715 
1716 out_notify:
1717 		if (compr && compr->stream)
1718 			snd_compr_fragment_elapsed(compr->stream);
1719 	}
1720 
1721 out:
1722 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1723 
1724 	return ret;
1725 }
1726 EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq);
1727 
1728 static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf)
1729 {
1730 	if (buf->irq_count & 0x01)
1731 		return 0;
1732 
1733 	compr_dbg(buf, "Enable IRQ(0x%x) for next fragment\n", buf->irq_count);
1734 
1735 	buf->irq_count |= 0x01;
1736 
1737 	return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack),
1738 				    buf->irq_count);
1739 }
1740 
1741 int wm_adsp_compr_pointer(struct snd_soc_component *component,
1742 			  struct snd_compr_stream *stream,
1743 			  struct snd_compr_tstamp *tstamp)
1744 {
1745 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1746 	struct wm_adsp *dsp = compr->dsp;
1747 	struct wm_adsp_compr_buf *buf;
1748 	int ret = 0;
1749 
1750 	compr_dbg(compr, "Pointer request\n");
1751 
1752 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1753 
1754 	buf = compr->buf;
1755 
1756 	if (dsp->fatal_error || !buf || buf->error) {
1757 		snd_compr_stop_error(stream, SNDRV_PCM_STATE_XRUN);
1758 		ret = -EIO;
1759 		goto out;
1760 	}
1761 
1762 	if (buf->avail < wm_adsp_compr_frag_words(compr)) {
1763 		ret = wm_adsp_buffer_update_avail(buf);
1764 		if (ret < 0) {
1765 			compr_err(compr, "Error reading avail: %d\n", ret);
1766 			goto out;
1767 		}
1768 
1769 		/*
1770 		 * If we really have less than 1 fragment available tell the
1771 		 * DSP to inform us once a whole fragment is available.
1772 		 */
1773 		if (buf->avail < wm_adsp_compr_frag_words(compr)) {
1774 			ret = wm_adsp_buffer_get_error(buf);
1775 			if (ret < 0) {
1776 				if (buf->error)
1777 					snd_compr_stop_error(stream,
1778 							SNDRV_PCM_STATE_XRUN);
1779 				goto out;
1780 			}
1781 
1782 			ret = wm_adsp_buffer_reenable_irq(buf);
1783 			if (ret < 0) {
1784 				compr_err(compr, "Failed to re-enable buffer IRQ: %d\n",
1785 					  ret);
1786 				goto out;
1787 			}
1788 		}
1789 	}
1790 
1791 	tstamp->copied_total = compr->copied_total;
1792 	tstamp->copied_total += buf->avail * CS_DSP_DATA_WORD_SIZE;
1793 	tstamp->sampling_rate = compr->sample_rate;
1794 
1795 out:
1796 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1797 
1798 	return ret;
1799 }
1800 EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer);
1801 
1802 static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target)
1803 {
1804 	struct wm_adsp_compr_buf *buf = compr->buf;
1805 	unsigned int adsp_addr;
1806 	int mem_type, nwords, max_read;
1807 	int i, ret;
1808 
1809 	/* Calculate read parameters */
1810 	for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i)
1811 		if (buf->read_index < buf->regions[i].cumulative_size)
1812 			break;
1813 
1814 	if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions)
1815 		return -EINVAL;
1816 
1817 	mem_type = buf->regions[i].mem_type;
1818 	adsp_addr = buf->regions[i].base_addr +
1819 		    (buf->read_index - buf->regions[i].offset);
1820 
1821 	max_read = wm_adsp_compr_frag_words(compr);
1822 	nwords = buf->regions[i].cumulative_size - buf->read_index;
1823 
1824 	if (nwords > target)
1825 		nwords = target;
1826 	if (nwords > buf->avail)
1827 		nwords = buf->avail;
1828 	if (nwords > max_read)
1829 		nwords = max_read;
1830 	if (!nwords)
1831 		return 0;
1832 
1833 	/* Read data from DSP */
1834 	ret = cs_dsp_read_raw_data_block(&buf->dsp->cs_dsp, mem_type, adsp_addr,
1835 					 nwords, (__be32 *)compr->raw_buf);
1836 	if (ret < 0)
1837 		return ret;
1838 
1839 	cs_dsp_remove_padding(compr->raw_buf, nwords);
1840 
1841 	/* update read index to account for words read */
1842 	buf->read_index += nwords;
1843 	if (buf->read_index == wm_adsp_buffer_size(buf))
1844 		buf->read_index = 0;
1845 
1846 	ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index),
1847 				   buf->read_index);
1848 	if (ret < 0)
1849 		return ret;
1850 
1851 	/* update avail to account for words read */
1852 	buf->avail -= nwords;
1853 
1854 	return nwords;
1855 }
1856 
1857 static int wm_adsp_compr_read(struct wm_adsp_compr *compr,
1858 			      char __user *buf, size_t count)
1859 {
1860 	struct wm_adsp *dsp = compr->dsp;
1861 	int ntotal = 0;
1862 	int nwords, nbytes;
1863 
1864 	compr_dbg(compr, "Requested read of %zu bytes\n", count);
1865 
1866 	if (dsp->fatal_error || !compr->buf || compr->buf->error) {
1867 		snd_compr_stop_error(compr->stream, SNDRV_PCM_STATE_XRUN);
1868 		return -EIO;
1869 	}
1870 
1871 	count /= CS_DSP_DATA_WORD_SIZE;
1872 
1873 	do {
1874 		nwords = wm_adsp_buffer_capture_block(compr, count);
1875 		if (nwords < 0) {
1876 			compr_err(compr, "Failed to capture block: %d\n",
1877 				  nwords);
1878 			return nwords;
1879 		}
1880 
1881 		nbytes = nwords * CS_DSP_DATA_WORD_SIZE;
1882 
1883 		compr_dbg(compr, "Read %d bytes\n", nbytes);
1884 
1885 		if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) {
1886 			compr_err(compr, "Failed to copy data to user: %d, %d\n",
1887 				  ntotal, nbytes);
1888 			return -EFAULT;
1889 		}
1890 
1891 		count -= nwords;
1892 		ntotal += nbytes;
1893 	} while (nwords > 0 && count > 0);
1894 
1895 	compr->copied_total += ntotal;
1896 
1897 	return ntotal;
1898 }
1899 
1900 int wm_adsp_compr_copy(struct snd_soc_component *component,
1901 		       struct snd_compr_stream *stream, char __user *buf,
1902 		       size_t count)
1903 {
1904 	struct wm_adsp_compr *compr = stream->runtime->private_data;
1905 	struct wm_adsp *dsp = compr->dsp;
1906 	int ret;
1907 
1908 	mutex_lock(&dsp->cs_dsp.pwr_lock);
1909 
1910 	if (stream->direction == SND_COMPRESS_CAPTURE)
1911 		ret = wm_adsp_compr_read(compr, buf, count);
1912 	else
1913 		ret = -ENOTSUPP;
1914 
1915 	mutex_unlock(&dsp->cs_dsp.pwr_lock);
1916 
1917 	return ret;
1918 }
1919 EXPORT_SYMBOL_GPL(wm_adsp_compr_copy);
1920 
1921 static void wm_adsp_fatal_error(struct cs_dsp *cs_dsp)
1922 {
1923 	struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp);
1924 	struct wm_adsp_compr *compr;
1925 
1926 	dsp->fatal_error = true;
1927 
1928 	list_for_each_entry(compr, &dsp->compr_list, list) {
1929 		if (compr->stream)
1930 			snd_compr_fragment_elapsed(compr->stream);
1931 	}
1932 }
1933 
1934 irqreturn_t wm_adsp2_bus_error(int irq, void *data)
1935 {
1936 	struct wm_adsp *dsp = (struct wm_adsp *)data;
1937 
1938 	cs_dsp_adsp2_bus_error(&dsp->cs_dsp);
1939 
1940 	return IRQ_HANDLED;
1941 }
1942 EXPORT_SYMBOL_GPL(wm_adsp2_bus_error);
1943 
1944 irqreturn_t wm_halo_bus_error(int irq, void *data)
1945 {
1946 	struct wm_adsp *dsp = (struct wm_adsp *)data;
1947 
1948 	cs_dsp_halo_bus_error(&dsp->cs_dsp);
1949 
1950 	return IRQ_HANDLED;
1951 }
1952 EXPORT_SYMBOL_GPL(wm_halo_bus_error);
1953 
1954 irqreturn_t wm_halo_wdt_expire(int irq, void *data)
1955 {
1956 	struct wm_adsp *dsp = data;
1957 
1958 	cs_dsp_halo_wdt_expire(&dsp->cs_dsp);
1959 
1960 	return IRQ_HANDLED;
1961 }
1962 EXPORT_SYMBOL_GPL(wm_halo_wdt_expire);
1963 
1964 static const struct cs_dsp_client_ops wm_adsp1_client_ops = {
1965 	.control_add = wm_adsp_control_add,
1966 	.control_remove = wm_adsp_control_remove,
1967 };
1968 
1969 static const struct cs_dsp_client_ops wm_adsp2_client_ops = {
1970 	.control_add = wm_adsp_control_add,
1971 	.control_remove = wm_adsp_control_remove,
1972 	.post_run = wm_adsp_event_post_run,
1973 	.post_stop = wm_adsp_event_post_stop,
1974 	.watchdog_expired = wm_adsp_fatal_error,
1975 };
1976 
1977 MODULE_LICENSE("GPL v2");
1978