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