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