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