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