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