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