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