1 // SPDX-License-Identifier: GPL-2.0-only
3 * wm_adsp.c -- Wolfson ADSP support
5 * Copyright 2012 Wolfson Microelectronics plc
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
23 #include <linux/workqueue.h>
24 #include <linux/debugfs.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/jack.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
35 #define adsp_crit(_dsp, fmt, ...) \
36 dev_crit(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
37 #define adsp_err(_dsp, fmt, ...) \
38 dev_err(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
39 #define adsp_warn(_dsp, fmt, ...) \
40 dev_warn(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
41 #define adsp_info(_dsp, fmt, ...) \
42 dev_info(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
43 #define adsp_dbg(_dsp, fmt, ...) \
44 dev_dbg(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
46 #define compr_err(_obj, fmt, ...) \
47 adsp_err(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \
49 #define compr_dbg(_obj, fmt, ...) \
50 adsp_dbg(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \
53 #define ADSP1_CONTROL_1 0x00
54 #define ADSP1_CONTROL_2 0x02
55 #define ADSP1_CONTROL_3 0x03
56 #define ADSP1_CONTROL_4 0x04
57 #define ADSP1_CONTROL_5 0x06
58 #define ADSP1_CONTROL_6 0x07
59 #define ADSP1_CONTROL_7 0x08
60 #define ADSP1_CONTROL_8 0x09
61 #define ADSP1_CONTROL_9 0x0A
62 #define ADSP1_CONTROL_10 0x0B
63 #define ADSP1_CONTROL_11 0x0C
64 #define ADSP1_CONTROL_12 0x0D
65 #define ADSP1_CONTROL_13 0x0F
66 #define ADSP1_CONTROL_14 0x10
67 #define ADSP1_CONTROL_15 0x11
68 #define ADSP1_CONTROL_16 0x12
69 #define ADSP1_CONTROL_17 0x13
70 #define ADSP1_CONTROL_18 0x14
71 #define ADSP1_CONTROL_19 0x16
72 #define ADSP1_CONTROL_20 0x17
73 #define ADSP1_CONTROL_21 0x18
74 #define ADSP1_CONTROL_22 0x1A
75 #define ADSP1_CONTROL_23 0x1B
76 #define ADSP1_CONTROL_24 0x1C
77 #define ADSP1_CONTROL_25 0x1E
78 #define ADSP1_CONTROL_26 0x20
79 #define ADSP1_CONTROL_27 0x21
80 #define ADSP1_CONTROL_28 0x22
81 #define ADSP1_CONTROL_29 0x23
82 #define ADSP1_CONTROL_30 0x24
83 #define ADSP1_CONTROL_31 0x26
88 #define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
89 #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
90 #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
96 #define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */
97 #define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */
98 #define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */
99 #define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */
100 #define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
101 #define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
102 #define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
103 #define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
104 #define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
105 #define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
106 #define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
107 #define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
108 #define ADSP1_START 0x0001 /* DSP1_START */
109 #define ADSP1_START_MASK 0x0001 /* DSP1_START */
110 #define ADSP1_START_SHIFT 0 /* DSP1_START */
111 #define ADSP1_START_WIDTH 1 /* DSP1_START */
116 #define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
117 #define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
118 #define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
120 #define ADSP2_CONTROL 0x0
121 #define ADSP2_CLOCKING 0x1
122 #define ADSP2V2_CLOCKING 0x2
123 #define ADSP2_STATUS1 0x4
124 #define ADSP2_WDMA_CONFIG_1 0x30
125 #define ADSP2_WDMA_CONFIG_2 0x31
126 #define ADSP2V2_WDMA_CONFIG_2 0x32
127 #define ADSP2_RDMA_CONFIG_1 0x34
129 #define ADSP2_SCRATCH0 0x40
130 #define ADSP2_SCRATCH1 0x41
131 #define ADSP2_SCRATCH2 0x42
132 #define ADSP2_SCRATCH3 0x43
134 #define ADSP2V2_SCRATCH0_1 0x40
135 #define ADSP2V2_SCRATCH2_3 0x42
141 #define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */
142 #define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */
143 #define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */
144 #define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */
145 #define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
146 #define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
147 #define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
148 #define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
149 #define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
150 #define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
151 #define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
152 #define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
153 #define ADSP2_START 0x0001 /* DSP1_START */
154 #define ADSP2_START_MASK 0x0001 /* DSP1_START */
155 #define ADSP2_START_SHIFT 0 /* DSP1_START */
156 #define ADSP2_START_WIDTH 1 /* DSP1_START */
161 #define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
162 #define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
163 #define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
168 #define ADSP2V2_CLK_SEL_MASK 0x70000 /* CLK_SEL_ENA */
169 #define ADSP2V2_CLK_SEL_SHIFT 16 /* CLK_SEL_ENA */
170 #define ADSP2V2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
172 #define ADSP2V2_RATE_MASK 0x7800 /* DSP_RATE */
173 #define ADSP2V2_RATE_SHIFT 11 /* DSP_RATE */
174 #define ADSP2V2_RATE_WIDTH 4 /* DSP_RATE */
179 #define ADSP2_RAM_RDY 0x0001
180 #define ADSP2_RAM_RDY_MASK 0x0001
181 #define ADSP2_RAM_RDY_SHIFT 0
182 #define ADSP2_RAM_RDY_WIDTH 1
187 #define ADSP2_LOCK_CODE_0 0x5555
188 #define ADSP2_LOCK_CODE_1 0xAAAA
190 #define ADSP2_WATCHDOG 0x0A
191 #define ADSP2_BUS_ERR_ADDR 0x52
192 #define ADSP2_REGION_LOCK_STATUS 0x64
193 #define ADSP2_LOCK_REGION_1_LOCK_REGION_0 0x66
194 #define ADSP2_LOCK_REGION_3_LOCK_REGION_2 0x68
195 #define ADSP2_LOCK_REGION_5_LOCK_REGION_4 0x6A
196 #define ADSP2_LOCK_REGION_7_LOCK_REGION_6 0x6C
197 #define ADSP2_LOCK_REGION_9_LOCK_REGION_8 0x6E
198 #define ADSP2_LOCK_REGION_CTRL 0x7A
199 #define ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR 0x7C
201 #define ADSP2_REGION_LOCK_ERR_MASK 0x8000
202 #define ADSP2_ADDR_ERR_MASK 0x4000
203 #define ADSP2_WDT_TIMEOUT_STS_MASK 0x2000
204 #define ADSP2_CTRL_ERR_PAUSE_ENA 0x0002
205 #define ADSP2_CTRL_ERR_EINT 0x0001
207 #define ADSP2_BUS_ERR_ADDR_MASK 0x00FFFFFF
208 #define ADSP2_XMEM_ERR_ADDR_MASK 0x0000FFFF
209 #define ADSP2_PMEM_ERR_ADDR_MASK 0x7FFF0000
210 #define ADSP2_PMEM_ERR_ADDR_SHIFT 16
211 #define ADSP2_WDT_ENA_MASK 0xFFFFFFFD
213 #define ADSP2_LOCK_REGION_SHIFT 16
215 #define ADSP_MAX_STD_CTRL_SIZE 512
217 #define WM_ADSP_ACKED_CTL_TIMEOUT_MS 100
218 #define WM_ADSP_ACKED_CTL_N_QUICKPOLLS 10
219 #define WM_ADSP_ACKED_CTL_MIN_VALUE 0
220 #define WM_ADSP_ACKED_CTL_MAX_VALUE 0xFFFFFF
223 * Event control messages
225 #define WM_ADSP_FW_EVENT_SHUTDOWN 0x000001
230 #define HALO_AHBM_WINDOW_DEBUG_0 0x02040
231 #define HALO_AHBM_WINDOW_DEBUG_1 0x02044
236 #define HALO_SCRATCH1 0x005c0
237 #define HALO_SCRATCH2 0x005c8
238 #define HALO_SCRATCH3 0x005d0
239 #define HALO_SCRATCH4 0x005d8
240 #define HALO_CCM_CORE_CONTROL 0x41000
241 #define HALO_CORE_SOFT_RESET 0x00010
242 #define HALO_WDT_CONTROL 0x47000
247 #define HALO_MPU_XMEM_ACCESS_0 0x43000
248 #define HALO_MPU_YMEM_ACCESS_0 0x43004
249 #define HALO_MPU_WINDOW_ACCESS_0 0x43008
250 #define HALO_MPU_XREG_ACCESS_0 0x4300C
251 #define HALO_MPU_YREG_ACCESS_0 0x43014
252 #define HALO_MPU_XMEM_ACCESS_1 0x43018
253 #define HALO_MPU_YMEM_ACCESS_1 0x4301C
254 #define HALO_MPU_WINDOW_ACCESS_1 0x43020
255 #define HALO_MPU_XREG_ACCESS_1 0x43024
256 #define HALO_MPU_YREG_ACCESS_1 0x4302C
257 #define HALO_MPU_XMEM_ACCESS_2 0x43030
258 #define HALO_MPU_YMEM_ACCESS_2 0x43034
259 #define HALO_MPU_WINDOW_ACCESS_2 0x43038
260 #define HALO_MPU_XREG_ACCESS_2 0x4303C
261 #define HALO_MPU_YREG_ACCESS_2 0x43044
262 #define HALO_MPU_XMEM_ACCESS_3 0x43048
263 #define HALO_MPU_YMEM_ACCESS_3 0x4304C
264 #define HALO_MPU_WINDOW_ACCESS_3 0x43050
265 #define HALO_MPU_XREG_ACCESS_3 0x43054
266 #define HALO_MPU_YREG_ACCESS_3 0x4305C
267 #define HALO_MPU_XM_VIO_ADDR 0x43100
268 #define HALO_MPU_XM_VIO_STATUS 0x43104
269 #define HALO_MPU_YM_VIO_ADDR 0x43108
270 #define HALO_MPU_YM_VIO_STATUS 0x4310C
271 #define HALO_MPU_PM_VIO_ADDR 0x43110
272 #define HALO_MPU_PM_VIO_STATUS 0x43114
273 #define HALO_MPU_LOCK_CONFIG 0x43140
276 * HALO_AHBM_WINDOW_DEBUG_1
278 #define HALO_AHBM_CORE_ERR_ADDR_MASK 0x0fffff00
279 #define HALO_AHBM_CORE_ERR_ADDR_SHIFT 8
280 #define HALO_AHBM_FLAGS_ERR_MASK 0x000000ff
283 * HALO_CCM_CORE_CONTROL
285 #define HALO_CORE_EN 0x00000001
288 * HALO_CORE_SOFT_RESET
290 #define HALO_CORE_SOFT_RESET_MASK 0x00000001
295 #define HALO_WDT_EN_MASK 0x00000001
298 * HALO_MPU_?M_VIO_STATUS
300 #define HALO_MPU_VIO_STS_MASK 0x007e0000
301 #define HALO_MPU_VIO_STS_SHIFT 17
302 #define HALO_MPU_VIO_ERR_WR_MASK 0x00008000
303 #define HALO_MPU_VIO_ERR_SRC_MASK 0x00007fff
304 #define HALO_MPU_VIO_ERR_SRC_SHIFT 0
306 static struct wm_adsp_ops wm_adsp1_ops;
307 static struct wm_adsp_ops wm_adsp2_ops[];
308 static struct wm_adsp_ops wm_halo_ops;
311 struct list_head list;
315 static struct wm_adsp_buf *wm_adsp_buf_alloc(const void *src, size_t len,
316 struct list_head *list)
318 struct wm_adsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL);
323 buf->buf = vmalloc(len);
328 memcpy(buf->buf, src, len);
331 list_add_tail(&buf->list, list);
336 static void wm_adsp_buf_free(struct list_head *list)
338 while (!list_empty(list)) {
339 struct wm_adsp_buf *buf = list_first_entry(list,
342 list_del(&buf->list);
348 #define WM_ADSP_FW_MBC_VSS 0
349 #define WM_ADSP_FW_HIFI 1
350 #define WM_ADSP_FW_TX 2
351 #define WM_ADSP_FW_TX_SPK 3
352 #define WM_ADSP_FW_RX 4
353 #define WM_ADSP_FW_RX_ANC 5
354 #define WM_ADSP_FW_CTRL 6
355 #define WM_ADSP_FW_ASR 7
356 #define WM_ADSP_FW_TRACE 8
357 #define WM_ADSP_FW_SPK_PROT 9
358 #define WM_ADSP_FW_SPK_CALI 10
359 #define WM_ADSP_FW_SPK_DIAG 11
360 #define WM_ADSP_FW_MISC 12
362 #define WM_ADSP_NUM_FW 13
364 static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = {
365 [WM_ADSP_FW_MBC_VSS] = "MBC/VSS",
366 [WM_ADSP_FW_HIFI] = "MasterHiFi",
367 [WM_ADSP_FW_TX] = "Tx",
368 [WM_ADSP_FW_TX_SPK] = "Tx Speaker",
369 [WM_ADSP_FW_RX] = "Rx",
370 [WM_ADSP_FW_RX_ANC] = "Rx ANC",
371 [WM_ADSP_FW_CTRL] = "Voice Ctrl",
372 [WM_ADSP_FW_ASR] = "ASR Assist",
373 [WM_ADSP_FW_TRACE] = "Dbg Trace",
374 [WM_ADSP_FW_SPK_PROT] = "Protection",
375 [WM_ADSP_FW_SPK_CALI] = "Calibration",
376 [WM_ADSP_FW_SPK_DIAG] = "Diagnostic",
377 [WM_ADSP_FW_MISC] = "Misc",
380 struct wm_adsp_system_config_xm_hdr {
386 __be32 dma_buffer_size;
389 __be32 build_job_name[3];
390 __be32 build_job_number;
393 struct wm_halo_system_config_xm_hdr {
394 __be32 halo_heartbeat;
395 __be32 build_job_name[3];
396 __be32 build_job_number;
399 struct wm_adsp_alg_xm_struct {
405 __be32 high_water_mark;
406 __be32 low_water_mark;
407 __be64 smoothed_power;
410 struct wm_adsp_host_buf_coeff_v1 {
411 __be32 host_buf_ptr; /* Host buffer pointer */
412 __be32 versions; /* Version numbers */
413 __be32 name[4]; /* The buffer name */
416 struct wm_adsp_buffer {
417 __be32 buf1_base; /* Base addr of first buffer area */
418 __be32 buf1_size; /* Size of buf1 area in DSP words */
419 __be32 buf2_base; /* Base addr of 2nd buffer area */
420 __be32 buf1_buf2_size; /* Size of buf1+buf2 in DSP words */
421 __be32 buf3_base; /* Base addr of buf3 area */
422 __be32 buf_total_size; /* Size of buf1+buf2+buf3 in DSP words */
423 __be32 high_water_mark; /* Point at which IRQ is asserted */
424 __be32 irq_count; /* bits 1-31 count IRQ assertions */
425 __be32 irq_ack; /* acked IRQ count, bit 0 enables IRQ */
426 __be32 next_write_index; /* word index of next write */
427 __be32 next_read_index; /* word index of next read */
428 __be32 error; /* error if any */
429 __be32 oldest_block_index; /* word index of oldest surviving */
430 __be32 requested_rewind; /* how many blocks rewind was done */
431 __be32 reserved_space; /* internal */
432 __be32 min_free; /* min free space since stream start */
433 __be32 blocks_written[2]; /* total blocks written (64 bit) */
434 __be32 words_written[2]; /* total words written (64 bit) */
437 struct wm_adsp_compr;
439 struct wm_adsp_compr_buf {
440 struct list_head list;
442 struct wm_adsp_compr *compr;
444 struct wm_adsp_buffer_region *regions;
451 int host_buf_mem_type;
456 struct wm_adsp_compr {
457 struct list_head list;
459 struct wm_adsp_compr_buf *buf;
461 struct snd_compr_stream *stream;
462 struct snd_compressed_buffer size;
465 unsigned int copied_total;
467 unsigned int sample_rate;
472 #define WM_ADSP_DATA_WORD_SIZE 3
474 #define WM_ADSP_MIN_FRAGMENTS 1
475 #define WM_ADSP_MAX_FRAGMENTS 256
476 #define WM_ADSP_MIN_FRAGMENT_SIZE (64 * WM_ADSP_DATA_WORD_SIZE)
477 #define WM_ADSP_MAX_FRAGMENT_SIZE (4096 * WM_ADSP_DATA_WORD_SIZE)
479 #define WM_ADSP_ALG_XM_STRUCT_MAGIC 0x49aec7
481 #define HOST_BUFFER_FIELD(field) \
482 (offsetof(struct wm_adsp_buffer, field) / sizeof(__be32))
484 #define ALG_XM_FIELD(field) \
485 (offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32))
487 #define HOST_BUF_COEFF_SUPPORTED_COMPAT_VER 1
489 #define HOST_BUF_COEFF_COMPAT_VER_MASK 0xFF00
490 #define HOST_BUF_COEFF_COMPAT_VER_SHIFT 8
492 static int wm_adsp_buffer_init(struct wm_adsp *dsp);
493 static int wm_adsp_buffer_free(struct wm_adsp *dsp);
495 struct wm_adsp_buffer_region {
497 unsigned int cumulative_size;
498 unsigned int mem_type;
499 unsigned int base_addr;
502 struct wm_adsp_buffer_region_def {
503 unsigned int mem_type;
504 unsigned int base_offset;
505 unsigned int size_offset;
508 static const struct wm_adsp_buffer_region_def default_regions[] = {
510 .mem_type = WMFW_ADSP2_XM,
511 .base_offset = HOST_BUFFER_FIELD(buf1_base),
512 .size_offset = HOST_BUFFER_FIELD(buf1_size),
515 .mem_type = WMFW_ADSP2_XM,
516 .base_offset = HOST_BUFFER_FIELD(buf2_base),
517 .size_offset = HOST_BUFFER_FIELD(buf1_buf2_size),
520 .mem_type = WMFW_ADSP2_YM,
521 .base_offset = HOST_BUFFER_FIELD(buf3_base),
522 .size_offset = HOST_BUFFER_FIELD(buf_total_size),
526 struct wm_adsp_fw_caps {
528 struct snd_codec_desc desc;
530 const struct wm_adsp_buffer_region_def *region_defs;
533 static const struct wm_adsp_fw_caps ctrl_caps[] = {
535 .id = SND_AUDIOCODEC_BESPOKE,
538 .sample_rates = { 16000 },
539 .num_sample_rates = 1,
540 .formats = SNDRV_PCM_FMTBIT_S16_LE,
542 .num_regions = ARRAY_SIZE(default_regions),
543 .region_defs = default_regions,
547 static const struct wm_adsp_fw_caps trace_caps[] = {
549 .id = SND_AUDIOCODEC_BESPOKE,
553 4000, 8000, 11025, 12000, 16000, 22050,
554 24000, 32000, 44100, 48000, 64000, 88200,
555 96000, 176400, 192000
557 .num_sample_rates = 15,
558 .formats = SNDRV_PCM_FMTBIT_S16_LE,
560 .num_regions = ARRAY_SIZE(default_regions),
561 .region_defs = default_regions,
565 static const struct {
569 const struct wm_adsp_fw_caps *caps;
571 } wm_adsp_fw[WM_ADSP_NUM_FW] = {
572 [WM_ADSP_FW_MBC_VSS] = { .file = "mbc-vss" },
573 [WM_ADSP_FW_HIFI] = { .file = "hifi" },
574 [WM_ADSP_FW_TX] = { .file = "tx" },
575 [WM_ADSP_FW_TX_SPK] = { .file = "tx-spk" },
576 [WM_ADSP_FW_RX] = { .file = "rx" },
577 [WM_ADSP_FW_RX_ANC] = { .file = "rx-anc" },
578 [WM_ADSP_FW_CTRL] = {
580 .compr_direction = SND_COMPRESS_CAPTURE,
581 .num_caps = ARRAY_SIZE(ctrl_caps),
583 .voice_trigger = true,
585 [WM_ADSP_FW_ASR] = { .file = "asr" },
586 [WM_ADSP_FW_TRACE] = {
588 .compr_direction = SND_COMPRESS_CAPTURE,
589 .num_caps = ARRAY_SIZE(trace_caps),
592 [WM_ADSP_FW_SPK_PROT] = { .file = "spk-prot" },
593 [WM_ADSP_FW_SPK_CALI] = { .file = "spk-cali" },
594 [WM_ADSP_FW_SPK_DIAG] = { .file = "spk-diag" },
595 [WM_ADSP_FW_MISC] = { .file = "misc" },
598 struct wm_coeff_ctl_ops {
599 int (*xget)(struct snd_kcontrol *kcontrol,
600 struct snd_ctl_elem_value *ucontrol);
601 int (*xput)(struct snd_kcontrol *kcontrol,
602 struct snd_ctl_elem_value *ucontrol);
605 struct wm_coeff_ctl {
608 /* Subname is needed to match with firmware */
610 unsigned int subname_len;
611 struct wm_adsp_alg_region alg_region;
612 struct wm_coeff_ctl_ops ops;
614 unsigned int enabled:1;
615 struct list_head list;
620 struct soc_bytes_ext bytes_ext;
625 static const char *wm_adsp_mem_region_name(unsigned int type)
630 case WMFW_HALO_PM_PACKED:
636 case WMFW_HALO_XM_PACKED:
640 case WMFW_HALO_YM_PACKED:
649 #ifdef CONFIG_DEBUG_FS
650 static void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, const char *s)
652 char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
654 kfree(dsp->wmfw_file_name);
655 dsp->wmfw_file_name = tmp;
658 static void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, const char *s)
660 char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
662 kfree(dsp->bin_file_name);
663 dsp->bin_file_name = tmp;
666 static void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
668 kfree(dsp->wmfw_file_name);
669 kfree(dsp->bin_file_name);
670 dsp->wmfw_file_name = NULL;
671 dsp->bin_file_name = NULL;
674 static ssize_t wm_adsp_debugfs_wmfw_read(struct file *file,
675 char __user *user_buf,
676 size_t count, loff_t *ppos)
678 struct wm_adsp *dsp = file->private_data;
681 mutex_lock(&dsp->pwr_lock);
683 if (!dsp->wmfw_file_name || !dsp->booted)
686 ret = simple_read_from_buffer(user_buf, count, ppos,
688 strlen(dsp->wmfw_file_name));
690 mutex_unlock(&dsp->pwr_lock);
694 static ssize_t wm_adsp_debugfs_bin_read(struct file *file,
695 char __user *user_buf,
696 size_t count, loff_t *ppos)
698 struct wm_adsp *dsp = file->private_data;
701 mutex_lock(&dsp->pwr_lock);
703 if (!dsp->bin_file_name || !dsp->booted)
706 ret = simple_read_from_buffer(user_buf, count, ppos,
708 strlen(dsp->bin_file_name));
710 mutex_unlock(&dsp->pwr_lock);
714 static const struct {
716 const struct file_operations fops;
717 } wm_adsp_debugfs_fops[] = {
719 .name = "wmfw_file_name",
722 .read = wm_adsp_debugfs_wmfw_read,
726 .name = "bin_file_name",
729 .read = wm_adsp_debugfs_bin_read,
734 static void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
735 struct snd_soc_component *component)
737 struct dentry *root = NULL;
740 root = debugfs_create_dir(dsp->name, component->debugfs_root);
742 debugfs_create_bool("booted", 0444, root, &dsp->booted);
743 debugfs_create_bool("running", 0444, root, &dsp->running);
744 debugfs_create_x32("fw_id", 0444, root, &dsp->fw_id);
745 debugfs_create_x32("fw_version", 0444, root, &dsp->fw_id_version);
747 for (i = 0; i < ARRAY_SIZE(wm_adsp_debugfs_fops); ++i)
748 debugfs_create_file(wm_adsp_debugfs_fops[i].name, 0444, root,
749 dsp, &wm_adsp_debugfs_fops[i].fops);
751 dsp->debugfs_root = root;
754 static void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
756 wm_adsp_debugfs_clear(dsp);
757 debugfs_remove_recursive(dsp->debugfs_root);
760 static inline void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
761 struct snd_soc_component *component)
765 static inline void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
769 static inline void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp,
774 static inline void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp,
779 static inline void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
784 int wm_adsp_fw_get(struct snd_kcontrol *kcontrol,
785 struct snd_ctl_elem_value *ucontrol)
787 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
788 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
789 struct wm_adsp *dsp = snd_soc_component_get_drvdata(component);
791 ucontrol->value.enumerated.item[0] = dsp[e->shift_l].fw;
795 EXPORT_SYMBOL_GPL(wm_adsp_fw_get);
797 int wm_adsp_fw_put(struct snd_kcontrol *kcontrol,
798 struct snd_ctl_elem_value *ucontrol)
800 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
801 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
802 struct wm_adsp *dsp = snd_soc_component_get_drvdata(component);
805 if (ucontrol->value.enumerated.item[0] == dsp[e->shift_l].fw)
808 if (ucontrol->value.enumerated.item[0] >= WM_ADSP_NUM_FW)
811 mutex_lock(&dsp[e->shift_l].pwr_lock);
813 if (dsp[e->shift_l].booted || !list_empty(&dsp[e->shift_l].compr_list))
816 dsp[e->shift_l].fw = ucontrol->value.enumerated.item[0];
818 mutex_unlock(&dsp[e->shift_l].pwr_lock);
822 EXPORT_SYMBOL_GPL(wm_adsp_fw_put);
824 const struct soc_enum wm_adsp_fw_enum[] = {
825 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
826 SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
827 SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
828 SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
829 SOC_ENUM_SINGLE(0, 4, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
830 SOC_ENUM_SINGLE(0, 5, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
831 SOC_ENUM_SINGLE(0, 6, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
833 EXPORT_SYMBOL_GPL(wm_adsp_fw_enum);
835 static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp,
840 for (i = 0; i < dsp->num_mems; i++)
841 if (dsp->mem[i].type == type)
847 static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *mem,
852 return mem->base + (offset * 3);
857 return mem->base + (offset * 2);
859 WARN(1, "Unknown memory region type");
864 static unsigned int wm_halo_region_to_reg(struct wm_adsp_region const *mem,
870 return mem->base + (offset * 4);
871 case WMFW_HALO_XM_PACKED:
872 case WMFW_HALO_YM_PACKED:
873 return (mem->base + (offset * 3)) & ~0x3;
874 case WMFW_HALO_PM_PACKED:
875 return mem->base + (offset * 5);
877 WARN(1, "Unknown memory region type");
882 static void wm_adsp_read_fw_status(struct wm_adsp *dsp,
883 int noffs, unsigned int *offs)
888 for (i = 0; i < noffs; ++i) {
889 ret = regmap_read(dsp->regmap, dsp->base + offs[i], &offs[i]);
891 adsp_err(dsp, "Failed to read SCRATCH%u: %d\n", i, ret);
897 static void wm_adsp2_show_fw_status(struct wm_adsp *dsp)
899 unsigned int offs[] = {
900 ADSP2_SCRATCH0, ADSP2_SCRATCH1, ADSP2_SCRATCH2, ADSP2_SCRATCH3,
903 wm_adsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs);
905 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
906 offs[0], offs[1], offs[2], offs[3]);
909 static void wm_adsp2v2_show_fw_status(struct wm_adsp *dsp)
911 unsigned int offs[] = { ADSP2V2_SCRATCH0_1, ADSP2V2_SCRATCH2_3 };
913 wm_adsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs);
915 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
916 offs[0] & 0xFFFF, offs[0] >> 16,
917 offs[1] & 0xFFFF, offs[1] >> 16);
920 static void wm_halo_show_fw_status(struct wm_adsp *dsp)
922 unsigned int offs[] = {
923 HALO_SCRATCH1, HALO_SCRATCH2, HALO_SCRATCH3, HALO_SCRATCH4,
926 wm_adsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs);
928 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
929 offs[0], offs[1], offs[2], offs[3]);
932 static inline struct wm_coeff_ctl *bytes_ext_to_ctl(struct soc_bytes_ext *ext)
934 return container_of(ext, struct wm_coeff_ctl, bytes_ext);
937 static int wm_coeff_base_reg(struct wm_coeff_ctl *ctl, unsigned int *reg)
939 const struct wm_adsp_alg_region *alg_region = &ctl->alg_region;
940 struct wm_adsp *dsp = ctl->dsp;
941 const struct wm_adsp_region *mem;
943 mem = wm_adsp_find_region(dsp, alg_region->type);
945 adsp_err(dsp, "No base for region %x\n",
950 *reg = dsp->ops->region_to_reg(mem, ctl->alg_region.base + ctl->offset);
955 static int wm_coeff_info(struct snd_kcontrol *kctl,
956 struct snd_ctl_elem_info *uinfo)
958 struct soc_bytes_ext *bytes_ext =
959 (struct soc_bytes_ext *)kctl->private_value;
960 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
963 case WMFW_CTL_TYPE_ACKED:
964 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
965 uinfo->value.integer.min = WM_ADSP_ACKED_CTL_MIN_VALUE;
966 uinfo->value.integer.max = WM_ADSP_ACKED_CTL_MAX_VALUE;
967 uinfo->value.integer.step = 1;
971 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
972 uinfo->count = ctl->len;
979 static int wm_coeff_write_acked_control(struct wm_coeff_ctl *ctl,
980 unsigned int event_id)
982 struct wm_adsp *dsp = ctl->dsp;
983 u32 val = cpu_to_be32(event_id);
987 ret = wm_coeff_base_reg(ctl, ®);
991 adsp_dbg(dsp, "Sending 0x%x to acked control alg 0x%x %s:0x%x\n",
992 event_id, ctl->alg_region.alg,
993 wm_adsp_mem_region_name(ctl->alg_region.type), ctl->offset);
995 ret = regmap_raw_write(dsp->regmap, reg, &val, sizeof(val));
997 adsp_err(dsp, "Failed to write %x: %d\n", reg, ret);
1002 * Poll for ack, we initially poll at ~1ms intervals for firmwares
1003 * that respond quickly, then go to ~10ms polls. A firmware is unlikely
1004 * to ack instantly so we do the first 1ms delay before reading the
1005 * control to avoid a pointless bus transaction
1007 for (i = 0; i < WM_ADSP_ACKED_CTL_TIMEOUT_MS;) {
1009 case 0 ... WM_ADSP_ACKED_CTL_N_QUICKPOLLS - 1:
1010 usleep_range(1000, 2000);
1014 usleep_range(10000, 20000);
1019 ret = regmap_raw_read(dsp->regmap, reg, &val, sizeof(val));
1021 adsp_err(dsp, "Failed to read %x: %d\n", reg, ret);
1026 adsp_dbg(dsp, "Acked control ACKED at poll %u\n", i);
1031 adsp_warn(dsp, "Acked control @0x%x alg:0x%x %s:0x%x timed out\n",
1032 reg, ctl->alg_region.alg,
1033 wm_adsp_mem_region_name(ctl->alg_region.type),
1039 static int wm_coeff_write_ctrl_raw(struct wm_coeff_ctl *ctl,
1040 const void *buf, size_t len)
1042 struct wm_adsp *dsp = ctl->dsp;
1047 ret = wm_coeff_base_reg(ctl, ®);
1051 scratch = kmemdup(buf, len, GFP_KERNEL | GFP_DMA);
1055 ret = regmap_raw_write(dsp->regmap, reg, scratch,
1058 adsp_err(dsp, "Failed to write %zu bytes to %x: %d\n",
1063 adsp_dbg(dsp, "Wrote %zu bytes to %x\n", len, reg);
1070 static int wm_coeff_write_ctrl(struct wm_coeff_ctl *ctl,
1071 const void *buf, size_t len)
1075 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
1077 else if (buf != ctl->cache)
1078 memcpy(ctl->cache, buf, len);
1081 if (ctl->enabled && ctl->dsp->running)
1082 ret = wm_coeff_write_ctrl_raw(ctl, buf, len);
1087 static int wm_coeff_put(struct snd_kcontrol *kctl,
1088 struct snd_ctl_elem_value *ucontrol)
1090 struct soc_bytes_ext *bytes_ext =
1091 (struct soc_bytes_ext *)kctl->private_value;
1092 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1093 char *p = ucontrol->value.bytes.data;
1096 mutex_lock(&ctl->dsp->pwr_lock);
1097 ret = wm_coeff_write_ctrl(ctl, p, ctl->len);
1098 mutex_unlock(&ctl->dsp->pwr_lock);
1103 static int wm_coeff_tlv_put(struct snd_kcontrol *kctl,
1104 const unsigned int __user *bytes, unsigned int size)
1106 struct soc_bytes_ext *bytes_ext =
1107 (struct soc_bytes_ext *)kctl->private_value;
1108 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1111 mutex_lock(&ctl->dsp->pwr_lock);
1113 if (copy_from_user(ctl->cache, bytes, size))
1116 ret = wm_coeff_write_ctrl(ctl, ctl->cache, size);
1118 mutex_unlock(&ctl->dsp->pwr_lock);
1123 static int wm_coeff_put_acked(struct snd_kcontrol *kctl,
1124 struct snd_ctl_elem_value *ucontrol)
1126 struct soc_bytes_ext *bytes_ext =
1127 (struct soc_bytes_ext *)kctl->private_value;
1128 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1129 unsigned int val = ucontrol->value.integer.value[0];
1133 return 0; /* 0 means no event */
1135 mutex_lock(&ctl->dsp->pwr_lock);
1137 if (ctl->enabled && ctl->dsp->running)
1138 ret = wm_coeff_write_acked_control(ctl, val);
1142 mutex_unlock(&ctl->dsp->pwr_lock);
1147 static int wm_coeff_read_ctrl_raw(struct wm_coeff_ctl *ctl,
1148 void *buf, size_t len)
1150 struct wm_adsp *dsp = ctl->dsp;
1155 ret = wm_coeff_base_reg(ctl, ®);
1159 scratch = kmalloc(len, GFP_KERNEL | GFP_DMA);
1163 ret = regmap_raw_read(dsp->regmap, reg, scratch, len);
1165 adsp_err(dsp, "Failed to read %zu bytes from %x: %d\n",
1170 adsp_dbg(dsp, "Read %zu bytes from %x\n", len, reg);
1172 memcpy(buf, scratch, len);
1178 static int wm_coeff_read_ctrl(struct wm_coeff_ctl *ctl, void *buf, size_t len)
1182 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
1183 if (ctl->enabled && ctl->dsp->running)
1184 return wm_coeff_read_ctrl_raw(ctl, buf, len);
1188 if (!ctl->flags && ctl->enabled && ctl->dsp->running)
1189 ret = wm_coeff_read_ctrl_raw(ctl, ctl->cache, ctl->len);
1191 if (buf != ctl->cache)
1192 memcpy(buf, ctl->cache, len);
1198 static int wm_coeff_get(struct snd_kcontrol *kctl,
1199 struct snd_ctl_elem_value *ucontrol)
1201 struct soc_bytes_ext *bytes_ext =
1202 (struct soc_bytes_ext *)kctl->private_value;
1203 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1204 char *p = ucontrol->value.bytes.data;
1207 mutex_lock(&ctl->dsp->pwr_lock);
1208 ret = wm_coeff_read_ctrl(ctl, p, ctl->len);
1209 mutex_unlock(&ctl->dsp->pwr_lock);
1214 static int wm_coeff_tlv_get(struct snd_kcontrol *kctl,
1215 unsigned int __user *bytes, unsigned int size)
1217 struct soc_bytes_ext *bytes_ext =
1218 (struct soc_bytes_ext *)kctl->private_value;
1219 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1222 mutex_lock(&ctl->dsp->pwr_lock);
1224 ret = wm_coeff_read_ctrl_raw(ctl, ctl->cache, size);
1226 if (!ret && copy_to_user(bytes, ctl->cache, size))
1229 mutex_unlock(&ctl->dsp->pwr_lock);
1234 static int wm_coeff_get_acked(struct snd_kcontrol *kcontrol,
1235 struct snd_ctl_elem_value *ucontrol)
1238 * Although it's not useful to read an acked control, we must satisfy
1239 * user-side assumptions that all controls are readable and that a
1240 * write of the same value should be filtered out (it's valid to send
1241 * the same event number again to the firmware). We therefore return 0,
1242 * meaning "no event" so valid event numbers will always be a change
1244 ucontrol->value.integer.value[0] = 0;
1249 struct wmfw_ctl_work {
1250 struct wm_adsp *dsp;
1251 struct wm_coeff_ctl *ctl;
1252 struct work_struct work;
1255 static unsigned int wmfw_convert_flags(unsigned int in, unsigned int len)
1257 unsigned int out, rd, wr, vol;
1259 if (len > ADSP_MAX_STD_CTRL_SIZE) {
1260 rd = SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1261 wr = SNDRV_CTL_ELEM_ACCESS_TLV_WRITE;
1262 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
1264 out = SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1266 rd = SNDRV_CTL_ELEM_ACCESS_READ;
1267 wr = SNDRV_CTL_ELEM_ACCESS_WRITE;
1268 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
1275 if (in & WMFW_CTL_FLAG_WRITEABLE)
1277 if (in & WMFW_CTL_FLAG_VOLATILE)
1280 out |= rd | wr | vol;
1286 static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl)
1288 struct snd_kcontrol_new *kcontrol;
1291 if (!ctl || !ctl->name)
1294 kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL);
1298 kcontrol->name = ctl->name;
1299 kcontrol->info = wm_coeff_info;
1300 kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1301 kcontrol->tlv.c = snd_soc_bytes_tlv_callback;
1302 kcontrol->private_value = (unsigned long)&ctl->bytes_ext;
1303 kcontrol->access = wmfw_convert_flags(ctl->flags, ctl->len);
1305 switch (ctl->type) {
1306 case WMFW_CTL_TYPE_ACKED:
1307 kcontrol->get = wm_coeff_get_acked;
1308 kcontrol->put = wm_coeff_put_acked;
1311 if (kcontrol->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1312 ctl->bytes_ext.max = ctl->len;
1313 ctl->bytes_ext.get = wm_coeff_tlv_get;
1314 ctl->bytes_ext.put = wm_coeff_tlv_put;
1316 kcontrol->get = wm_coeff_get;
1317 kcontrol->put = wm_coeff_put;
1322 ret = snd_soc_add_component_controls(dsp->component, kcontrol, 1);
1335 static int wm_coeff_init_control_caches(struct wm_adsp *dsp)
1337 struct wm_coeff_ctl *ctl;
1340 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1341 if (!ctl->enabled || ctl->set)
1343 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
1347 * For readable controls populate the cache from the DSP memory.
1348 * For non-readable controls the cache was zero-filled when
1349 * created so we don't need to do anything.
1351 if (!ctl->flags || (ctl->flags & WMFW_CTL_FLAG_READABLE)) {
1352 ret = wm_coeff_read_ctrl_raw(ctl, ctl->cache, ctl->len);
1361 static int wm_coeff_sync_controls(struct wm_adsp *dsp)
1363 struct wm_coeff_ctl *ctl;
1366 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1369 if (ctl->set && !(ctl->flags & WMFW_CTL_FLAG_VOLATILE)) {
1370 ret = wm_coeff_write_ctrl_raw(ctl, ctl->cache,
1380 static void wm_adsp_signal_event_controls(struct wm_adsp *dsp,
1383 struct wm_coeff_ctl *ctl;
1386 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1387 if (ctl->type != WMFW_CTL_TYPE_HOSTEVENT)
1393 ret = wm_coeff_write_acked_control(ctl, event);
1396 "Failed to send 0x%x event to alg 0x%x (%d)\n",
1397 event, ctl->alg_region.alg, ret);
1401 static void wm_adsp_ctl_work(struct work_struct *work)
1403 struct wmfw_ctl_work *ctl_work = container_of(work,
1404 struct wmfw_ctl_work,
1407 wmfw_add_ctl(ctl_work->dsp, ctl_work->ctl);
1411 static void wm_adsp_free_ctl_blk(struct wm_coeff_ctl *ctl)
1415 kfree(ctl->subname);
1419 static int wm_adsp_create_control(struct wm_adsp *dsp,
1420 const struct wm_adsp_alg_region *alg_region,
1421 unsigned int offset, unsigned int len,
1422 const char *subname, unsigned int subname_len,
1423 unsigned int flags, unsigned int type)
1425 struct wm_coeff_ctl *ctl;
1426 struct wmfw_ctl_work *ctl_work;
1427 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1428 const char *region_name;
1431 region_name = wm_adsp_mem_region_name(alg_region->type);
1433 adsp_err(dsp, "Unknown region type: %d\n", alg_region->type);
1437 switch (dsp->fw_ver) {
1440 snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s %x",
1441 dsp->name, region_name, alg_region->alg);
1442 subname = NULL; /* don't append subname */
1445 ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
1446 "%s%c %.12s %x", dsp->name, *region_name,
1447 wm_adsp_fw_text[dsp->fw], alg_region->alg);
1450 ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
1451 "%s %.12s %x", dsp->name,
1452 wm_adsp_fw_text[dsp->fw], alg_region->alg);
1457 int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
1460 if (dsp->component->name_prefix)
1461 avail -= strlen(dsp->component->name_prefix) + 1;
1463 /* Truncate the subname from the start if it is too long */
1464 if (subname_len > avail)
1465 skip = subname_len - avail;
1467 snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret,
1468 " %.*s", subname_len - skip, subname + skip);
1471 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1472 if (!strcmp(ctl->name, name)) {
1479 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1482 ctl->fw_name = wm_adsp_fw_text[dsp->fw];
1483 ctl->alg_region = *alg_region;
1484 ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
1490 ctl->subname_len = subname_len;
1491 ctl->subname = kmemdup(subname,
1492 strlen(subname) + 1, GFP_KERNEL);
1493 if (!ctl->subname) {
1500 ctl->ops.xget = wm_coeff_get;
1501 ctl->ops.xput = wm_coeff_put;
1506 ctl->offset = offset;
1508 ctl->cache = kzalloc(ctl->len, GFP_KERNEL);
1511 goto err_ctl_subname;
1514 list_add(&ctl->list, &dsp->ctl_list);
1516 if (flags & WMFW_CTL_FLAG_SYS)
1519 ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL);
1525 ctl_work->dsp = dsp;
1526 ctl_work->ctl = ctl;
1527 INIT_WORK(&ctl_work->work, wm_adsp_ctl_work);
1528 schedule_work(&ctl_work->work);
1535 kfree(ctl->subname);
1544 struct wm_coeff_parsed_alg {
1551 struct wm_coeff_parsed_coeff {
1561 static int wm_coeff_parse_string(int bytes, const u8 **pos, const u8 **str)
1570 length = le16_to_cpu(*((__le16 *)*pos));
1577 *str = *pos + bytes;
1579 *pos += ((length + bytes) + 3) & ~0x03;
1584 static int wm_coeff_parse_int(int bytes, const u8 **pos)
1590 val = le16_to_cpu(*((__le16 *)*pos));
1593 val = le32_to_cpu(*((__le32 *)*pos));
1604 static inline void wm_coeff_parse_alg(struct wm_adsp *dsp, const u8 **data,
1605 struct wm_coeff_parsed_alg *blk)
1607 const struct wmfw_adsp_alg_data *raw;
1609 switch (dsp->fw_ver) {
1612 raw = (const struct wmfw_adsp_alg_data *)*data;
1615 blk->id = le32_to_cpu(raw->id);
1616 blk->name = raw->name;
1617 blk->name_len = strlen(raw->name);
1618 blk->ncoeff = le32_to_cpu(raw->ncoeff);
1621 blk->id = wm_coeff_parse_int(sizeof(raw->id), data);
1622 blk->name_len = wm_coeff_parse_string(sizeof(u8), data,
1624 wm_coeff_parse_string(sizeof(u16), data, NULL);
1625 blk->ncoeff = wm_coeff_parse_int(sizeof(raw->ncoeff), data);
1629 adsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id);
1630 adsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name);
1631 adsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff);
1634 static inline void wm_coeff_parse_coeff(struct wm_adsp *dsp, const u8 **data,
1635 struct wm_coeff_parsed_coeff *blk)
1637 const struct wmfw_adsp_coeff_data *raw;
1641 switch (dsp->fw_ver) {
1644 raw = (const struct wmfw_adsp_coeff_data *)*data;
1645 *data = *data + sizeof(raw->hdr) + le32_to_cpu(raw->hdr.size);
1647 blk->offset = le16_to_cpu(raw->hdr.offset);
1648 blk->mem_type = le16_to_cpu(raw->hdr.type);
1649 blk->name = raw->name;
1650 blk->name_len = strlen(raw->name);
1651 blk->ctl_type = le16_to_cpu(raw->ctl_type);
1652 blk->flags = le16_to_cpu(raw->flags);
1653 blk->len = le32_to_cpu(raw->len);
1657 blk->offset = wm_coeff_parse_int(sizeof(raw->hdr.offset), &tmp);
1658 blk->mem_type = wm_coeff_parse_int(sizeof(raw->hdr.type), &tmp);
1659 length = wm_coeff_parse_int(sizeof(raw->hdr.size), &tmp);
1660 blk->name_len = wm_coeff_parse_string(sizeof(u8), &tmp,
1662 wm_coeff_parse_string(sizeof(u8), &tmp, NULL);
1663 wm_coeff_parse_string(sizeof(u16), &tmp, NULL);
1664 blk->ctl_type = wm_coeff_parse_int(sizeof(raw->ctl_type), &tmp);
1665 blk->flags = wm_coeff_parse_int(sizeof(raw->flags), &tmp);
1666 blk->len = wm_coeff_parse_int(sizeof(raw->len), &tmp);
1668 *data = *data + sizeof(raw->hdr) + length;
1672 adsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type);
1673 adsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset);
1674 adsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name);
1675 adsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags);
1676 adsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type);
1677 adsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len);
1680 static int wm_adsp_check_coeff_flags(struct wm_adsp *dsp,
1681 const struct wm_coeff_parsed_coeff *coeff_blk,
1682 unsigned int f_required,
1683 unsigned int f_illegal)
1685 if ((coeff_blk->flags & f_illegal) ||
1686 ((coeff_blk->flags & f_required) != f_required)) {
1687 adsp_err(dsp, "Illegal flags 0x%x for control type 0x%x\n",
1688 coeff_blk->flags, coeff_blk->ctl_type);
1695 static int wm_adsp_parse_coeff(struct wm_adsp *dsp,
1696 const struct wmfw_region *region)
1698 struct wm_adsp_alg_region alg_region = {};
1699 struct wm_coeff_parsed_alg alg_blk;
1700 struct wm_coeff_parsed_coeff coeff_blk;
1701 const u8 *data = region->data;
1704 wm_coeff_parse_alg(dsp, &data, &alg_blk);
1705 for (i = 0; i < alg_blk.ncoeff; i++) {
1706 wm_coeff_parse_coeff(dsp, &data, &coeff_blk);
1708 switch (coeff_blk.ctl_type) {
1709 case SNDRV_CTL_ELEM_TYPE_BYTES:
1711 case WMFW_CTL_TYPE_ACKED:
1712 if (coeff_blk.flags & WMFW_CTL_FLAG_SYS)
1713 continue; /* ignore */
1715 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk,
1716 WMFW_CTL_FLAG_VOLATILE |
1717 WMFW_CTL_FLAG_WRITEABLE |
1718 WMFW_CTL_FLAG_READABLE,
1723 case WMFW_CTL_TYPE_HOSTEVENT:
1724 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk,
1726 WMFW_CTL_FLAG_VOLATILE |
1727 WMFW_CTL_FLAG_WRITEABLE |
1728 WMFW_CTL_FLAG_READABLE,
1733 case WMFW_CTL_TYPE_HOST_BUFFER:
1734 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk,
1736 WMFW_CTL_FLAG_VOLATILE |
1737 WMFW_CTL_FLAG_READABLE,
1743 adsp_err(dsp, "Unknown control type: %d\n",
1744 coeff_blk.ctl_type);
1748 alg_region.type = coeff_blk.mem_type;
1749 alg_region.alg = alg_blk.id;
1751 ret = wm_adsp_create_control(dsp, &alg_region,
1757 coeff_blk.ctl_type);
1759 adsp_err(dsp, "Failed to create control: %.*s, %d\n",
1760 coeff_blk.name_len, coeff_blk.name, ret);
1766 static unsigned int wm_adsp1_parse_sizes(struct wm_adsp *dsp,
1767 const char * const file,
1769 const struct firmware *firmware)
1771 const struct wmfw_adsp1_sizes *adsp1_sizes;
1773 adsp1_sizes = (void *)&firmware->data[pos];
1775 adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n", file,
1776 le32_to_cpu(adsp1_sizes->dm), le32_to_cpu(adsp1_sizes->pm),
1777 le32_to_cpu(adsp1_sizes->zm));
1779 return pos + sizeof(*adsp1_sizes);
1782 static unsigned int wm_adsp2_parse_sizes(struct wm_adsp *dsp,
1783 const char * const file,
1785 const struct firmware *firmware)
1787 const struct wmfw_adsp2_sizes *adsp2_sizes;
1789 adsp2_sizes = (void *)&firmware->data[pos];
1791 adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n", file,
1792 le32_to_cpu(adsp2_sizes->xm), le32_to_cpu(adsp2_sizes->ym),
1793 le32_to_cpu(adsp2_sizes->pm), le32_to_cpu(adsp2_sizes->zm));
1795 return pos + sizeof(*adsp2_sizes);
1798 static bool wm_adsp_validate_version(struct wm_adsp *dsp, unsigned int version)
1802 adsp_warn(dsp, "Deprecated file format %d\n", version);
1812 static bool wm_halo_validate_version(struct wm_adsp *dsp, unsigned int version)
1822 static int wm_adsp_load(struct wm_adsp *dsp)
1824 LIST_HEAD(buf_list);
1825 const struct firmware *firmware;
1826 struct regmap *regmap = dsp->regmap;
1827 unsigned int pos = 0;
1828 const struct wmfw_header *header;
1829 const struct wmfw_adsp1_sizes *adsp1_sizes;
1830 const struct wmfw_footer *footer;
1831 const struct wmfw_region *region;
1832 const struct wm_adsp_region *mem;
1833 const char *region_name;
1834 char *file, *text = NULL;
1835 struct wm_adsp_buf *buf;
1838 int ret, offset, type;
1840 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
1844 snprintf(file, PAGE_SIZE, "%s-%s-%s.wmfw", dsp->part, dsp->fwf_name,
1845 wm_adsp_fw[dsp->fw].file);
1846 file[PAGE_SIZE - 1] = '\0';
1848 ret = request_firmware(&firmware, file, dsp->dev);
1850 adsp_err(dsp, "Failed to request '%s'\n", file);
1855 pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
1856 if (pos >= firmware->size) {
1857 adsp_err(dsp, "%s: file too short, %zu bytes\n",
1858 file, firmware->size);
1862 header = (void *)&firmware->data[0];
1864 if (memcmp(&header->magic[0], "WMFW", 4) != 0) {
1865 adsp_err(dsp, "%s: invalid magic\n", file);
1869 if (!dsp->ops->validate_version(dsp, header->ver)) {
1870 adsp_err(dsp, "%s: unknown file format %d\n",
1875 adsp_info(dsp, "Firmware version: %d\n", header->ver);
1876 dsp->fw_ver = header->ver;
1878 if (header->core != dsp->type) {
1879 adsp_err(dsp, "%s: invalid core %d != %d\n",
1880 file, header->core, dsp->type);
1884 pos = sizeof(*header);
1885 pos = dsp->ops->parse_sizes(dsp, file, pos, firmware);
1887 footer = (void *)&firmware->data[pos];
1888 pos += sizeof(*footer);
1890 if (le32_to_cpu(header->len) != pos) {
1891 adsp_err(dsp, "%s: unexpected header length %d\n",
1892 file, le32_to_cpu(header->len));
1896 adsp_dbg(dsp, "%s: timestamp %llu\n", file,
1897 le64_to_cpu(footer->timestamp));
1899 while (pos < firmware->size &&
1900 sizeof(*region) < firmware->size - pos) {
1901 region = (void *)&(firmware->data[pos]);
1902 region_name = "Unknown";
1905 offset = le32_to_cpu(region->offset) & 0xffffff;
1906 type = be32_to_cpu(region->type) & 0xff;
1909 case WMFW_NAME_TEXT:
1910 region_name = "Firmware name";
1911 text = kzalloc(le32_to_cpu(region->len) + 1,
1914 case WMFW_ALGORITHM_DATA:
1915 region_name = "Algorithm";
1916 ret = wm_adsp_parse_coeff(dsp, region);
1920 case WMFW_INFO_TEXT:
1921 region_name = "Information";
1922 text = kzalloc(le32_to_cpu(region->len) + 1,
1926 region_name = "Absolute";
1934 case WMFW_HALO_PM_PACKED:
1935 case WMFW_HALO_XM_PACKED:
1936 case WMFW_HALO_YM_PACKED:
1937 mem = wm_adsp_find_region(dsp, type);
1939 adsp_err(dsp, "No region of type: %x\n", type);
1944 region_name = wm_adsp_mem_region_name(type);
1945 reg = dsp->ops->region_to_reg(mem, offset);
1949 "%s.%d: Unknown region type %x at %d(%x)\n",
1950 file, regions, type, pos, pos);
1954 adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file,
1955 regions, le32_to_cpu(region->len), offset,
1958 if (le32_to_cpu(region->len) >
1959 firmware->size - pos - sizeof(*region)) {
1961 "%s.%d: %s region len %d bytes exceeds file length %zu\n",
1962 file, regions, region_name,
1963 le32_to_cpu(region->len), firmware->size);
1969 memcpy(text, region->data, le32_to_cpu(region->len));
1970 adsp_info(dsp, "%s: %s\n", file, text);
1976 buf = wm_adsp_buf_alloc(region->data,
1977 le32_to_cpu(region->len),
1980 adsp_err(dsp, "Out of memory\n");
1985 ret = regmap_raw_write_async(regmap, reg, buf->buf,
1986 le32_to_cpu(region->len));
1989 "%s.%d: Failed to write %d bytes at %d in %s: %d\n",
1991 le32_to_cpu(region->len), offset,
1997 pos += le32_to_cpu(region->len) + sizeof(*region);
2001 ret = regmap_async_complete(regmap);
2003 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
2007 if (pos > firmware->size)
2008 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
2009 file, regions, pos - firmware->size);
2011 wm_adsp_debugfs_save_wmfwname(dsp, file);
2014 regmap_async_complete(regmap);
2015 wm_adsp_buf_free(&buf_list);
2016 release_firmware(firmware);
2025 * Find wm_coeff_ctl with input name as its subname
2026 * If not found, return NULL
2028 static struct wm_coeff_ctl *wm_adsp_get_ctl(struct wm_adsp *dsp,
2029 const char *name, int type,
2032 struct wm_coeff_ctl *pos, *rslt = NULL;
2034 list_for_each_entry(pos, &dsp->ctl_list, list) {
2037 if (strncmp(pos->subname, name, pos->subname_len) == 0 &&
2038 pos->alg_region.alg == alg &&
2039 pos->alg_region.type == type) {
2048 int wm_adsp_write_ctl(struct wm_adsp *dsp, const char *name, int type,
2049 unsigned int alg, void *buf, size_t len)
2051 struct wm_coeff_ctl *ctl;
2052 struct snd_kcontrol *kcontrol;
2053 char ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2056 ctl = wm_adsp_get_ctl(dsp, name, type, alg);
2063 ret = wm_coeff_write_ctrl(ctl, buf, len);
2067 if (ctl->flags & WMFW_CTL_FLAG_SYS)
2070 if (dsp->component->name_prefix)
2071 snprintf(ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s",
2072 dsp->component->name_prefix, ctl->name);
2074 snprintf(ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s",
2077 kcontrol = snd_soc_card_get_kcontrol(dsp->component->card, ctl_name);
2079 adsp_err(dsp, "Can't find kcontrol %s\n", ctl_name);
2083 snd_ctl_notify(dsp->component->card->snd_card,
2084 SNDRV_CTL_EVENT_MASK_VALUE, &kcontrol->id);
2088 EXPORT_SYMBOL_GPL(wm_adsp_write_ctl);
2090 int wm_adsp_read_ctl(struct wm_adsp *dsp, const char *name, int type,
2091 unsigned int alg, void *buf, size_t len)
2093 struct wm_coeff_ctl *ctl;
2095 ctl = wm_adsp_get_ctl(dsp, name, type, alg);
2102 return wm_coeff_read_ctrl(ctl, buf, len);
2104 EXPORT_SYMBOL_GPL(wm_adsp_read_ctl);
2106 static void wm_adsp_ctl_fixup_base(struct wm_adsp *dsp,
2107 const struct wm_adsp_alg_region *alg_region)
2109 struct wm_coeff_ctl *ctl;
2111 list_for_each_entry(ctl, &dsp->ctl_list, list) {
2112 if (ctl->fw_name == wm_adsp_fw_text[dsp->fw] &&
2113 alg_region->alg == ctl->alg_region.alg &&
2114 alg_region->type == ctl->alg_region.type) {
2115 ctl->alg_region.base = alg_region->base;
2120 static void *wm_adsp_read_algs(struct wm_adsp *dsp, size_t n_algs,
2121 const struct wm_adsp_region *mem,
2122 unsigned int pos, unsigned int len)
2130 adsp_err(dsp, "No algorithms\n");
2131 return ERR_PTR(-EINVAL);
2134 if (n_algs > 1024) {
2135 adsp_err(dsp, "Algorithm count %zx excessive\n", n_algs);
2136 return ERR_PTR(-EINVAL);
2139 /* Read the terminator first to validate the length */
2140 reg = dsp->ops->region_to_reg(mem, pos + len);
2142 ret = regmap_raw_read(dsp->regmap, reg, &val, sizeof(val));
2144 adsp_err(dsp, "Failed to read algorithm list end: %d\n",
2146 return ERR_PTR(ret);
2149 if (be32_to_cpu(val) != 0xbedead)
2150 adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbedead\n",
2151 reg, be32_to_cpu(val));
2153 /* Convert length from DSP words to bytes */
2156 alg = kzalloc(len, GFP_KERNEL | GFP_DMA);
2158 return ERR_PTR(-ENOMEM);
2160 reg = dsp->ops->region_to_reg(mem, pos);
2162 ret = regmap_raw_read(dsp->regmap, reg, alg, len);
2164 adsp_err(dsp, "Failed to read algorithm list: %d\n", ret);
2166 return ERR_PTR(ret);
2172 static struct wm_adsp_alg_region *
2173 wm_adsp_find_alg_region(struct wm_adsp *dsp, int type, unsigned int id)
2175 struct wm_adsp_alg_region *alg_region;
2177 list_for_each_entry(alg_region, &dsp->alg_regions, list) {
2178 if (id == alg_region->alg && type == alg_region->type)
2185 static struct wm_adsp_alg_region *wm_adsp_create_region(struct wm_adsp *dsp,
2186 int type, __be32 id,
2189 struct wm_adsp_alg_region *alg_region;
2191 alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL);
2193 return ERR_PTR(-ENOMEM);
2195 alg_region->type = type;
2196 alg_region->alg = be32_to_cpu(id);
2197 alg_region->base = be32_to_cpu(base);
2199 list_add_tail(&alg_region->list, &dsp->alg_regions);
2201 if (dsp->fw_ver > 0)
2202 wm_adsp_ctl_fixup_base(dsp, alg_region);
2207 static void wm_adsp_free_alg_regions(struct wm_adsp *dsp)
2209 struct wm_adsp_alg_region *alg_region;
2211 while (!list_empty(&dsp->alg_regions)) {
2212 alg_region = list_first_entry(&dsp->alg_regions,
2213 struct wm_adsp_alg_region,
2215 list_del(&alg_region->list);
2220 static void wmfw_parse_id_header(struct wm_adsp *dsp,
2221 struct wmfw_id_hdr *fw, int nalgs)
2223 dsp->fw_id = be32_to_cpu(fw->id);
2224 dsp->fw_id_version = be32_to_cpu(fw->ver);
2226 adsp_info(dsp, "Firmware: %x v%d.%d.%d, %d algorithms\n",
2227 dsp->fw_id, (dsp->fw_id_version & 0xff0000) >> 16,
2228 (dsp->fw_id_version & 0xff00) >> 8, dsp->fw_id_version & 0xff,
2232 static void wmfw_v3_parse_id_header(struct wm_adsp *dsp,
2233 struct wmfw_v3_id_hdr *fw, int nalgs)
2235 dsp->fw_id = be32_to_cpu(fw->id);
2236 dsp->fw_id_version = be32_to_cpu(fw->ver);
2237 dsp->fw_vendor_id = be32_to_cpu(fw->vendor_id);
2239 adsp_info(dsp, "Firmware: %x vendor: 0x%x v%d.%d.%d, %d algorithms\n",
2240 dsp->fw_id, dsp->fw_vendor_id,
2241 (dsp->fw_id_version & 0xff0000) >> 16,
2242 (dsp->fw_id_version & 0xff00) >> 8, dsp->fw_id_version & 0xff,
2246 static int wm_adsp_create_regions(struct wm_adsp *dsp, __be32 id, int nregions,
2247 int *type, __be32 *base)
2249 struct wm_adsp_alg_region *alg_region;
2252 for (i = 0; i < nregions; i++) {
2253 alg_region = wm_adsp_create_region(dsp, type[i], id, base[i]);
2254 if (IS_ERR(alg_region))
2255 return PTR_ERR(alg_region);
2261 static int wm_adsp1_setup_algs(struct wm_adsp *dsp)
2263 struct wmfw_adsp1_id_hdr adsp1_id;
2264 struct wmfw_adsp1_alg_hdr *adsp1_alg;
2265 struct wm_adsp_alg_region *alg_region;
2266 const struct wm_adsp_region *mem;
2267 unsigned int pos, len;
2271 mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM);
2275 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id,
2278 adsp_err(dsp, "Failed to read algorithm info: %d\n",
2283 n_algs = be32_to_cpu(adsp1_id.n_algs);
2285 wmfw_parse_id_header(dsp, &adsp1_id.fw, n_algs);
2287 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
2288 adsp1_id.fw.id, adsp1_id.zm);
2289 if (IS_ERR(alg_region))
2290 return PTR_ERR(alg_region);
2292 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
2293 adsp1_id.fw.id, adsp1_id.dm);
2294 if (IS_ERR(alg_region))
2295 return PTR_ERR(alg_region);
2297 /* Calculate offset and length in DSP words */
2298 pos = sizeof(adsp1_id) / sizeof(u32);
2299 len = (sizeof(*adsp1_alg) * n_algs) / sizeof(u32);
2301 adsp1_alg = wm_adsp_read_algs(dsp, n_algs, mem, pos, len);
2302 if (IS_ERR(adsp1_alg))
2303 return PTR_ERR(adsp1_alg);
2305 for (i = 0; i < n_algs; i++) {
2306 adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
2307 i, be32_to_cpu(adsp1_alg[i].alg.id),
2308 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16,
2309 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8,
2310 be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff,
2311 be32_to_cpu(adsp1_alg[i].dm),
2312 be32_to_cpu(adsp1_alg[i].zm));
2314 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
2315 adsp1_alg[i].alg.id,
2317 if (IS_ERR(alg_region)) {
2318 ret = PTR_ERR(alg_region);
2321 if (dsp->fw_ver == 0) {
2322 if (i + 1 < n_algs) {
2323 len = be32_to_cpu(adsp1_alg[i + 1].dm);
2324 len -= be32_to_cpu(adsp1_alg[i].dm);
2326 wm_adsp_create_control(dsp, alg_region, 0,
2328 SNDRV_CTL_ELEM_TYPE_BYTES);
2330 adsp_warn(dsp, "Missing length info for region DM with ID %x\n",
2331 be32_to_cpu(adsp1_alg[i].alg.id));
2335 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
2336 adsp1_alg[i].alg.id,
2338 if (IS_ERR(alg_region)) {
2339 ret = PTR_ERR(alg_region);
2342 if (dsp->fw_ver == 0) {
2343 if (i + 1 < n_algs) {
2344 len = be32_to_cpu(adsp1_alg[i + 1].zm);
2345 len -= be32_to_cpu(adsp1_alg[i].zm);
2347 wm_adsp_create_control(dsp, alg_region, 0,
2349 SNDRV_CTL_ELEM_TYPE_BYTES);
2351 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
2352 be32_to_cpu(adsp1_alg[i].alg.id));
2362 static int wm_adsp2_setup_algs(struct wm_adsp *dsp)
2364 struct wmfw_adsp2_id_hdr adsp2_id;
2365 struct wmfw_adsp2_alg_hdr *adsp2_alg;
2366 struct wm_adsp_alg_region *alg_region;
2367 const struct wm_adsp_region *mem;
2368 unsigned int pos, len;
2372 mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM);
2376 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id,
2379 adsp_err(dsp, "Failed to read algorithm info: %d\n",
2384 n_algs = be32_to_cpu(adsp2_id.n_algs);
2386 wmfw_parse_id_header(dsp, &adsp2_id.fw, n_algs);
2388 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
2389 adsp2_id.fw.id, adsp2_id.xm);
2390 if (IS_ERR(alg_region))
2391 return PTR_ERR(alg_region);
2393 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
2394 adsp2_id.fw.id, adsp2_id.ym);
2395 if (IS_ERR(alg_region))
2396 return PTR_ERR(alg_region);
2398 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
2399 adsp2_id.fw.id, adsp2_id.zm);
2400 if (IS_ERR(alg_region))
2401 return PTR_ERR(alg_region);
2403 /* Calculate offset and length in DSP words */
2404 pos = sizeof(adsp2_id) / sizeof(u32);
2405 len = (sizeof(*adsp2_alg) * n_algs) / sizeof(u32);
2407 adsp2_alg = wm_adsp_read_algs(dsp, n_algs, mem, pos, len);
2408 if (IS_ERR(adsp2_alg))
2409 return PTR_ERR(adsp2_alg);
2411 for (i = 0; i < n_algs; i++) {
2413 "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
2414 i, be32_to_cpu(adsp2_alg[i].alg.id),
2415 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16,
2416 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8,
2417 be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff,
2418 be32_to_cpu(adsp2_alg[i].xm),
2419 be32_to_cpu(adsp2_alg[i].ym),
2420 be32_to_cpu(adsp2_alg[i].zm));
2422 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
2423 adsp2_alg[i].alg.id,
2425 if (IS_ERR(alg_region)) {
2426 ret = PTR_ERR(alg_region);
2429 if (dsp->fw_ver == 0) {
2430 if (i + 1 < n_algs) {
2431 len = be32_to_cpu(adsp2_alg[i + 1].xm);
2432 len -= be32_to_cpu(adsp2_alg[i].xm);
2434 wm_adsp_create_control(dsp, alg_region, 0,
2436 SNDRV_CTL_ELEM_TYPE_BYTES);
2438 adsp_warn(dsp, "Missing length info for region XM with ID %x\n",
2439 be32_to_cpu(adsp2_alg[i].alg.id));
2443 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
2444 adsp2_alg[i].alg.id,
2446 if (IS_ERR(alg_region)) {
2447 ret = PTR_ERR(alg_region);
2450 if (dsp->fw_ver == 0) {
2451 if (i + 1 < n_algs) {
2452 len = be32_to_cpu(adsp2_alg[i + 1].ym);
2453 len -= be32_to_cpu(adsp2_alg[i].ym);
2455 wm_adsp_create_control(dsp, alg_region, 0,
2457 SNDRV_CTL_ELEM_TYPE_BYTES);
2459 adsp_warn(dsp, "Missing length info for region YM with ID %x\n",
2460 be32_to_cpu(adsp2_alg[i].alg.id));
2464 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
2465 adsp2_alg[i].alg.id,
2467 if (IS_ERR(alg_region)) {
2468 ret = PTR_ERR(alg_region);
2471 if (dsp->fw_ver == 0) {
2472 if (i + 1 < n_algs) {
2473 len = be32_to_cpu(adsp2_alg[i + 1].zm);
2474 len -= be32_to_cpu(adsp2_alg[i].zm);
2476 wm_adsp_create_control(dsp, alg_region, 0,
2478 SNDRV_CTL_ELEM_TYPE_BYTES);
2480 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
2481 be32_to_cpu(adsp2_alg[i].alg.id));
2491 static int wm_halo_create_regions(struct wm_adsp *dsp, __be32 id,
2492 __be32 xm_base, __be32 ym_base)
2495 WMFW_ADSP2_XM, WMFW_HALO_XM_PACKED,
2496 WMFW_ADSP2_YM, WMFW_HALO_YM_PACKED
2498 __be32 bases[] = { xm_base, xm_base, ym_base, ym_base };
2500 return wm_adsp_create_regions(dsp, id, ARRAY_SIZE(types), types, bases);
2503 static int wm_halo_setup_algs(struct wm_adsp *dsp)
2505 struct wmfw_halo_id_hdr halo_id;
2506 struct wmfw_halo_alg_hdr *halo_alg;
2507 const struct wm_adsp_region *mem;
2508 unsigned int pos, len;
2512 mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM);
2516 ret = regmap_raw_read(dsp->regmap, mem->base, &halo_id,
2519 adsp_err(dsp, "Failed to read algorithm info: %d\n",
2524 n_algs = be32_to_cpu(halo_id.n_algs);
2526 wmfw_v3_parse_id_header(dsp, &halo_id.fw, n_algs);
2528 ret = wm_halo_create_regions(dsp, halo_id.fw.id,
2529 halo_id.xm_base, halo_id.ym_base);
2533 /* Calculate offset and length in DSP words */
2534 pos = sizeof(halo_id) / sizeof(u32);
2535 len = (sizeof(*halo_alg) * n_algs) / sizeof(u32);
2537 halo_alg = wm_adsp_read_algs(dsp, n_algs, mem, pos, len);
2538 if (IS_ERR(halo_alg))
2539 return PTR_ERR(halo_alg);
2541 for (i = 0; i < n_algs; i++) {
2543 "%d: ID %x v%d.%d.%d XM@%x YM@%x\n",
2544 i, be32_to_cpu(halo_alg[i].alg.id),
2545 (be32_to_cpu(halo_alg[i].alg.ver) & 0xff0000) >> 16,
2546 (be32_to_cpu(halo_alg[i].alg.ver) & 0xff00) >> 8,
2547 be32_to_cpu(halo_alg[i].alg.ver) & 0xff,
2548 be32_to_cpu(halo_alg[i].xm_base),
2549 be32_to_cpu(halo_alg[i].ym_base));
2551 ret = wm_halo_create_regions(dsp, halo_alg[i].alg.id,
2552 halo_alg[i].xm_base,
2553 halo_alg[i].ym_base);
2563 static int wm_adsp_load_coeff(struct wm_adsp *dsp)
2565 LIST_HEAD(buf_list);
2566 struct regmap *regmap = dsp->regmap;
2567 struct wmfw_coeff_hdr *hdr;
2568 struct wmfw_coeff_item *blk;
2569 const struct firmware *firmware;
2570 const struct wm_adsp_region *mem;
2571 struct wm_adsp_alg_region *alg_region;
2572 const char *region_name;
2573 int ret, pos, blocks, type, offset, reg;
2575 struct wm_adsp_buf *buf;
2577 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
2581 snprintf(file, PAGE_SIZE, "%s-%s-%s.bin", dsp->part, dsp->fwf_name,
2582 wm_adsp_fw[dsp->fw].file);
2583 file[PAGE_SIZE - 1] = '\0';
2585 ret = request_firmware(&firmware, file, dsp->dev);
2587 adsp_warn(dsp, "Failed to request '%s'\n", file);
2593 if (sizeof(*hdr) >= firmware->size) {
2594 adsp_err(dsp, "%s: file too short, %zu bytes\n",
2595 file, firmware->size);
2599 hdr = (void *)&firmware->data[0];
2600 if (memcmp(hdr->magic, "WMDR", 4) != 0) {
2601 adsp_err(dsp, "%s: invalid magic\n", file);
2605 switch (be32_to_cpu(hdr->rev) & 0xff) {
2609 adsp_err(dsp, "%s: Unsupported coefficient file format %d\n",
2610 file, be32_to_cpu(hdr->rev) & 0xff);
2615 adsp_dbg(dsp, "%s: v%d.%d.%d\n", file,
2616 (le32_to_cpu(hdr->ver) >> 16) & 0xff,
2617 (le32_to_cpu(hdr->ver) >> 8) & 0xff,
2618 le32_to_cpu(hdr->ver) & 0xff);
2620 pos = le32_to_cpu(hdr->len);
2623 while (pos < firmware->size &&
2624 sizeof(*blk) < firmware->size - pos) {
2625 blk = (void *)(&firmware->data[pos]);
2627 type = le16_to_cpu(blk->type);
2628 offset = le16_to_cpu(blk->offset);
2630 adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n",
2631 file, blocks, le32_to_cpu(blk->id),
2632 (le32_to_cpu(blk->ver) >> 16) & 0xff,
2633 (le32_to_cpu(blk->ver) >> 8) & 0xff,
2634 le32_to_cpu(blk->ver) & 0xff);
2635 adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n",
2636 file, blocks, le32_to_cpu(blk->len), offset, type);
2639 region_name = "Unknown";
2641 case (WMFW_NAME_TEXT << 8):
2642 case (WMFW_INFO_TEXT << 8):
2643 case (WMFW_METADATA << 8):
2645 case (WMFW_ABSOLUTE << 8):
2647 * Old files may use this for global
2650 if (le32_to_cpu(blk->id) == dsp->fw_id &&
2652 region_name = "global coefficients";
2653 mem = wm_adsp_find_region(dsp, type);
2655 adsp_err(dsp, "No ZM\n");
2658 reg = dsp->ops->region_to_reg(mem, 0);
2661 region_name = "register";
2670 case WMFW_HALO_XM_PACKED:
2671 case WMFW_HALO_YM_PACKED:
2672 case WMFW_HALO_PM_PACKED:
2673 adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n",
2674 file, blocks, le32_to_cpu(blk->len),
2675 type, le32_to_cpu(blk->id));
2677 mem = wm_adsp_find_region(dsp, type);
2679 adsp_err(dsp, "No base for region %x\n", type);
2683 alg_region = wm_adsp_find_alg_region(dsp, type,
2684 le32_to_cpu(blk->id));
2686 reg = alg_region->base;
2687 reg = dsp->ops->region_to_reg(mem, reg);
2690 adsp_err(dsp, "No %x for algorithm %x\n",
2691 type, le32_to_cpu(blk->id));
2696 adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n",
2697 file, blocks, type, pos);
2702 if (le32_to_cpu(blk->len) >
2703 firmware->size - pos - sizeof(*blk)) {
2705 "%s.%d: %s region len %d bytes exceeds file length %zu\n",
2706 file, blocks, region_name,
2707 le32_to_cpu(blk->len),
2713 buf = wm_adsp_buf_alloc(blk->data,
2714 le32_to_cpu(blk->len),
2717 adsp_err(dsp, "Out of memory\n");
2722 adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n",
2723 file, blocks, le32_to_cpu(blk->len),
2725 ret = regmap_raw_write_async(regmap, reg, buf->buf,
2726 le32_to_cpu(blk->len));
2729 "%s.%d: Failed to write to %x in %s: %d\n",
2730 file, blocks, reg, region_name, ret);
2734 pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03;
2738 ret = regmap_async_complete(regmap);
2740 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
2742 if (pos > firmware->size)
2743 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
2744 file, blocks, pos - firmware->size);
2746 wm_adsp_debugfs_save_binname(dsp, file);
2749 regmap_async_complete(regmap);
2750 release_firmware(firmware);
2751 wm_adsp_buf_free(&buf_list);
2757 static int wm_adsp_create_name(struct wm_adsp *dsp)
2762 dsp->name = devm_kasprintf(dsp->dev, GFP_KERNEL, "DSP%d",
2768 if (!dsp->fwf_name) {
2769 p = devm_kstrdup(dsp->dev, dsp->name, GFP_KERNEL);
2774 for (; *p != 0; ++p)
2781 static int wm_adsp_common_init(struct wm_adsp *dsp)
2785 ret = wm_adsp_create_name(dsp);
2789 INIT_LIST_HEAD(&dsp->alg_regions);
2790 INIT_LIST_HEAD(&dsp->ctl_list);
2791 INIT_LIST_HEAD(&dsp->compr_list);
2792 INIT_LIST_HEAD(&dsp->buffer_list);
2794 mutex_init(&dsp->pwr_lock);
2799 int wm_adsp1_init(struct wm_adsp *dsp)
2801 dsp->ops = &wm_adsp1_ops;
2803 return wm_adsp_common_init(dsp);
2805 EXPORT_SYMBOL_GPL(wm_adsp1_init);
2807 int wm_adsp1_event(struct snd_soc_dapm_widget *w,
2808 struct snd_kcontrol *kcontrol,
2811 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
2812 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
2813 struct wm_adsp *dsp = &dsps[w->shift];
2814 struct wm_coeff_ctl *ctl;
2818 dsp->component = component;
2820 mutex_lock(&dsp->pwr_lock);
2823 case SND_SOC_DAPM_POST_PMU:
2824 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2825 ADSP1_SYS_ENA, ADSP1_SYS_ENA);
2828 * For simplicity set the DSP clock rate to be the
2829 * SYSCLK rate rather than making it configurable.
2831 if (dsp->sysclk_reg) {
2832 ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val);
2834 adsp_err(dsp, "Failed to read SYSCLK state: %d\n",
2839 val = (val & dsp->sysclk_mask) >> dsp->sysclk_shift;
2841 ret = regmap_update_bits(dsp->regmap,
2842 dsp->base + ADSP1_CONTROL_31,
2843 ADSP1_CLK_SEL_MASK, val);
2845 adsp_err(dsp, "Failed to set clock rate: %d\n",
2851 ret = wm_adsp_load(dsp);
2855 ret = wm_adsp1_setup_algs(dsp);
2859 ret = wm_adsp_load_coeff(dsp);
2863 /* Initialize caches for enabled and unset controls */
2864 ret = wm_coeff_init_control_caches(dsp);
2868 /* Sync set controls */
2869 ret = wm_coeff_sync_controls(dsp);
2875 /* Start the core running */
2876 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2877 ADSP1_CORE_ENA | ADSP1_START,
2878 ADSP1_CORE_ENA | ADSP1_START);
2880 dsp->running = true;
2883 case SND_SOC_DAPM_PRE_PMD:
2884 dsp->running = false;
2885 dsp->booted = false;
2888 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2889 ADSP1_CORE_ENA | ADSP1_START, 0);
2891 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19,
2892 ADSP1_WDMA_BUFFER_LENGTH_MASK, 0);
2894 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2897 list_for_each_entry(ctl, &dsp->ctl_list, list)
2901 wm_adsp_free_alg_regions(dsp);
2908 mutex_unlock(&dsp->pwr_lock);
2913 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2916 mutex_unlock(&dsp->pwr_lock);
2920 EXPORT_SYMBOL_GPL(wm_adsp1_event);
2922 static int wm_adsp2v2_enable_core(struct wm_adsp *dsp)
2927 /* Wait for the RAM to start, should be near instantaneous */
2928 for (count = 0; count < 10; ++count) {
2929 ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1, &val);
2933 if (val & ADSP2_RAM_RDY)
2936 usleep_range(250, 500);
2939 if (!(val & ADSP2_RAM_RDY)) {
2940 adsp_err(dsp, "Failed to start DSP RAM\n");
2944 adsp_dbg(dsp, "RAM ready after %d polls\n", count);
2949 static int wm_adsp2_enable_core(struct wm_adsp *dsp)
2953 ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL,
2954 ADSP2_SYS_ENA, ADSP2_SYS_ENA);
2958 return wm_adsp2v2_enable_core(dsp);
2961 static int wm_adsp2_lock(struct wm_adsp *dsp, unsigned int lock_regions)
2963 struct regmap *regmap = dsp->regmap;
2964 unsigned int code0, code1, lock_reg;
2966 if (!(lock_regions & WM_ADSP2_REGION_ALL))
2969 lock_regions &= WM_ADSP2_REGION_ALL;
2970 lock_reg = dsp->base + ADSP2_LOCK_REGION_1_LOCK_REGION_0;
2972 while (lock_regions) {
2974 if (lock_regions & BIT(0)) {
2975 code0 = ADSP2_LOCK_CODE_0;
2976 code1 = ADSP2_LOCK_CODE_1;
2978 if (lock_regions & BIT(1)) {
2979 code0 |= ADSP2_LOCK_CODE_0 << ADSP2_LOCK_REGION_SHIFT;
2980 code1 |= ADSP2_LOCK_CODE_1 << ADSP2_LOCK_REGION_SHIFT;
2982 regmap_write(regmap, lock_reg, code0);
2983 regmap_write(regmap, lock_reg, code1);
2991 static int wm_adsp2_enable_memory(struct wm_adsp *dsp)
2993 return regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2994 ADSP2_MEM_ENA, ADSP2_MEM_ENA);
2997 static void wm_adsp2_disable_memory(struct wm_adsp *dsp)
2999 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
3003 static void wm_adsp2_disable_core(struct wm_adsp *dsp)
3005 regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
3006 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
3007 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0);
3009 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
3013 static void wm_adsp2v2_disable_core(struct wm_adsp *dsp)
3015 regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
3016 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
3017 regmap_write(dsp->regmap, dsp->base + ADSP2V2_WDMA_CONFIG_2, 0);
3020 static void wm_adsp_boot_work(struct work_struct *work)
3022 struct wm_adsp *dsp = container_of(work,
3027 mutex_lock(&dsp->pwr_lock);
3029 if (dsp->ops->enable_memory) {
3030 ret = dsp->ops->enable_memory(dsp);
3035 if (dsp->ops->enable_core) {
3036 ret = dsp->ops->enable_core(dsp);
3041 ret = wm_adsp_load(dsp);
3045 ret = dsp->ops->setup_algs(dsp);
3049 ret = wm_adsp_load_coeff(dsp);
3053 /* Initialize caches for enabled and unset controls */
3054 ret = wm_coeff_init_control_caches(dsp);
3058 if (dsp->ops->disable_core)
3059 dsp->ops->disable_core(dsp);
3063 mutex_unlock(&dsp->pwr_lock);
3068 if (dsp->ops->disable_core)
3069 dsp->ops->disable_core(dsp);
3071 if (dsp->ops->disable_memory)
3072 dsp->ops->disable_memory(dsp);
3074 mutex_unlock(&dsp->pwr_lock);
3077 static int wm_halo_configure_mpu(struct wm_adsp *dsp, unsigned int lock_regions)
3079 struct reg_sequence config[] = {
3080 { dsp->base + HALO_MPU_LOCK_CONFIG, 0x5555 },
3081 { dsp->base + HALO_MPU_LOCK_CONFIG, 0xAAAA },
3082 { dsp->base + HALO_MPU_XMEM_ACCESS_0, 0xFFFFFFFF },
3083 { dsp->base + HALO_MPU_YMEM_ACCESS_0, 0xFFFFFFFF },
3084 { dsp->base + HALO_MPU_WINDOW_ACCESS_0, lock_regions },
3085 { dsp->base + HALO_MPU_XREG_ACCESS_0, lock_regions },
3086 { dsp->base + HALO_MPU_YREG_ACCESS_0, lock_regions },
3087 { dsp->base + HALO_MPU_XMEM_ACCESS_1, 0xFFFFFFFF },
3088 { dsp->base + HALO_MPU_YMEM_ACCESS_1, 0xFFFFFFFF },
3089 { dsp->base + HALO_MPU_WINDOW_ACCESS_1, lock_regions },
3090 { dsp->base + HALO_MPU_XREG_ACCESS_1, lock_regions },
3091 { dsp->base + HALO_MPU_YREG_ACCESS_1, lock_regions },
3092 { dsp->base + HALO_MPU_XMEM_ACCESS_2, 0xFFFFFFFF },
3093 { dsp->base + HALO_MPU_YMEM_ACCESS_2, 0xFFFFFFFF },
3094 { dsp->base + HALO_MPU_WINDOW_ACCESS_2, lock_regions },
3095 { dsp->base + HALO_MPU_XREG_ACCESS_2, lock_regions },
3096 { dsp->base + HALO_MPU_YREG_ACCESS_2, lock_regions },
3097 { dsp->base + HALO_MPU_XMEM_ACCESS_3, 0xFFFFFFFF },
3098 { dsp->base + HALO_MPU_YMEM_ACCESS_3, 0xFFFFFFFF },
3099 { dsp->base + HALO_MPU_WINDOW_ACCESS_3, lock_regions },
3100 { dsp->base + HALO_MPU_XREG_ACCESS_3, lock_regions },
3101 { dsp->base + HALO_MPU_YREG_ACCESS_3, lock_regions },
3102 { dsp->base + HALO_MPU_LOCK_CONFIG, 0 },
3105 return regmap_multi_reg_write(dsp->regmap, config, ARRAY_SIZE(config));
3108 int wm_adsp2_set_dspclk(struct snd_soc_dapm_widget *w, unsigned int freq)
3110 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
3111 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
3112 struct wm_adsp *dsp = &dsps[w->shift];
3115 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CLOCKING,
3117 freq << ADSP2_CLK_SEL_SHIFT);
3119 adsp_err(dsp, "Failed to set clock rate: %d\n", ret);
3123 EXPORT_SYMBOL_GPL(wm_adsp2_set_dspclk);
3125 int wm_adsp2_preloader_get(struct snd_kcontrol *kcontrol,
3126 struct snd_ctl_elem_value *ucontrol)
3128 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
3129 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
3130 struct soc_mixer_control *mc =
3131 (struct soc_mixer_control *)kcontrol->private_value;
3132 struct wm_adsp *dsp = &dsps[mc->shift - 1];
3134 ucontrol->value.integer.value[0] = dsp->preloaded;
3138 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_get);
3140 int wm_adsp2_preloader_put(struct snd_kcontrol *kcontrol,
3141 struct snd_ctl_elem_value *ucontrol)
3143 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
3144 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
3145 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
3146 struct soc_mixer_control *mc =
3147 (struct soc_mixer_control *)kcontrol->private_value;
3148 struct wm_adsp *dsp = &dsps[mc->shift - 1];
3151 snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->name);
3153 dsp->preloaded = ucontrol->value.integer.value[0];
3155 if (ucontrol->value.integer.value[0])
3156 snd_soc_component_force_enable_pin(component, preload);
3158 snd_soc_component_disable_pin(component, preload);
3160 snd_soc_dapm_sync(dapm);
3162 flush_work(&dsp->boot_work);
3166 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_put);
3168 static void wm_adsp_stop_watchdog(struct wm_adsp *dsp)
3170 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_WATCHDOG,
3171 ADSP2_WDT_ENA_MASK, 0);
3174 static void wm_halo_stop_watchdog(struct wm_adsp *dsp)
3176 regmap_update_bits(dsp->regmap, dsp->base + HALO_WDT_CONTROL,
3177 HALO_WDT_EN_MASK, 0);
3180 int wm_adsp_early_event(struct snd_soc_dapm_widget *w,
3181 struct snd_kcontrol *kcontrol, int event)
3183 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
3184 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
3185 struct wm_adsp *dsp = &dsps[w->shift];
3186 struct wm_coeff_ctl *ctl;
3189 case SND_SOC_DAPM_PRE_PMU:
3190 queue_work(system_unbound_wq, &dsp->boot_work);
3192 case SND_SOC_DAPM_PRE_PMD:
3193 mutex_lock(&dsp->pwr_lock);
3195 wm_adsp_debugfs_clear(dsp);
3198 dsp->fw_id_version = 0;
3200 dsp->booted = false;
3202 if (dsp->ops->disable_memory)
3203 dsp->ops->disable_memory(dsp);
3205 list_for_each_entry(ctl, &dsp->ctl_list, list)
3208 wm_adsp_free_alg_regions(dsp);
3210 mutex_unlock(&dsp->pwr_lock);
3212 adsp_dbg(dsp, "Shutdown complete\n");
3220 EXPORT_SYMBOL_GPL(wm_adsp_early_event);
3222 static int wm_adsp2_start_core(struct wm_adsp *dsp)
3224 return regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
3225 ADSP2_CORE_ENA | ADSP2_START,
3226 ADSP2_CORE_ENA | ADSP2_START);
3229 static void wm_adsp2_stop_core(struct wm_adsp *dsp)
3231 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
3232 ADSP2_CORE_ENA | ADSP2_START, 0);
3235 int wm_adsp_event(struct snd_soc_dapm_widget *w,
3236 struct snd_kcontrol *kcontrol, int event)
3238 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
3239 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
3240 struct wm_adsp *dsp = &dsps[w->shift];
3244 case SND_SOC_DAPM_POST_PMU:
3245 flush_work(&dsp->boot_work);
3247 mutex_lock(&dsp->pwr_lock);
3254 if (dsp->ops->enable_core) {
3255 ret = dsp->ops->enable_core(dsp);
3260 /* Sync set controls */
3261 ret = wm_coeff_sync_controls(dsp);
3265 if (dsp->ops->lock_memory) {
3266 ret = dsp->ops->lock_memory(dsp, dsp->lock_regions);
3268 adsp_err(dsp, "Error configuring MPU: %d\n",
3274 if (dsp->ops->start_core) {
3275 ret = dsp->ops->start_core(dsp);
3280 if (wm_adsp_fw[dsp->fw].num_caps != 0) {
3281 ret = wm_adsp_buffer_init(dsp);
3286 dsp->running = true;
3288 mutex_unlock(&dsp->pwr_lock);
3291 case SND_SOC_DAPM_PRE_PMD:
3292 /* Tell the firmware to cleanup */
3293 wm_adsp_signal_event_controls(dsp, WM_ADSP_FW_EVENT_SHUTDOWN);
3295 if (dsp->ops->stop_watchdog)
3296 dsp->ops->stop_watchdog(dsp);
3298 /* Log firmware state, it can be useful for analysis */
3299 if (dsp->ops->show_fw_status)
3300 dsp->ops->show_fw_status(dsp);
3302 mutex_lock(&dsp->pwr_lock);
3304 dsp->running = false;
3306 if (dsp->ops->stop_core)
3307 dsp->ops->stop_core(dsp);
3308 if (dsp->ops->disable_core)
3309 dsp->ops->disable_core(dsp);
3311 if (wm_adsp_fw[dsp->fw].num_caps != 0)
3312 wm_adsp_buffer_free(dsp);
3314 dsp->fatal_error = false;
3316 mutex_unlock(&dsp->pwr_lock);
3318 adsp_dbg(dsp, "Execution stopped\n");
3327 if (dsp->ops->stop_core)
3328 dsp->ops->stop_core(dsp);
3329 if (dsp->ops->disable_core)
3330 dsp->ops->disable_core(dsp);
3331 mutex_unlock(&dsp->pwr_lock);
3334 EXPORT_SYMBOL_GPL(wm_adsp_event);
3336 static int wm_halo_start_core(struct wm_adsp *dsp)
3338 return regmap_update_bits(dsp->regmap,
3339 dsp->base + HALO_CCM_CORE_CONTROL,
3340 HALO_CORE_EN, HALO_CORE_EN);
3343 static void wm_halo_stop_core(struct wm_adsp *dsp)
3345 regmap_update_bits(dsp->regmap, dsp->base + HALO_CCM_CORE_CONTROL,
3348 /* reset halo core with CORE_SOFT_RESET */
3349 regmap_update_bits(dsp->regmap, dsp->base + HALO_CORE_SOFT_RESET,
3350 HALO_CORE_SOFT_RESET_MASK, 1);
3353 int wm_adsp2_component_probe(struct wm_adsp *dsp, struct snd_soc_component *component)
3357 snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->name);
3358 snd_soc_component_disable_pin(component, preload);
3360 wm_adsp2_init_debugfs(dsp, component);
3362 dsp->component = component;
3366 EXPORT_SYMBOL_GPL(wm_adsp2_component_probe);
3368 int wm_adsp2_component_remove(struct wm_adsp *dsp, struct snd_soc_component *component)
3370 wm_adsp2_cleanup_debugfs(dsp);
3374 EXPORT_SYMBOL_GPL(wm_adsp2_component_remove);
3376 int wm_adsp2_init(struct wm_adsp *dsp)
3380 ret = wm_adsp_common_init(dsp);
3387 * Disable the DSP memory by default when in reset for a small
3390 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
3394 "Failed to clear memory retention: %d\n", ret);
3398 dsp->ops = &wm_adsp2_ops[0];
3401 dsp->ops = &wm_adsp2_ops[1];
3404 dsp->ops = &wm_adsp2_ops[2];
3408 INIT_WORK(&dsp->boot_work, wm_adsp_boot_work);
3412 EXPORT_SYMBOL_GPL(wm_adsp2_init);
3414 int wm_halo_init(struct wm_adsp *dsp)
3418 ret = wm_adsp_common_init(dsp);
3422 dsp->ops = &wm_halo_ops;
3424 INIT_WORK(&dsp->boot_work, wm_adsp_boot_work);
3428 EXPORT_SYMBOL_GPL(wm_halo_init);
3430 void wm_adsp2_remove(struct wm_adsp *dsp)
3432 struct wm_coeff_ctl *ctl;
3434 while (!list_empty(&dsp->ctl_list)) {
3435 ctl = list_first_entry(&dsp->ctl_list, struct wm_coeff_ctl,
3437 list_del(&ctl->list);
3438 wm_adsp_free_ctl_blk(ctl);
3441 EXPORT_SYMBOL_GPL(wm_adsp2_remove);
3443 static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr)
3445 return compr->buf != NULL;
3448 static int wm_adsp_compr_attach(struct wm_adsp_compr *compr)
3450 struct wm_adsp_compr_buf *buf = NULL, *tmp;
3452 if (compr->dsp->fatal_error)
3455 list_for_each_entry(tmp, &compr->dsp->buffer_list, list) {
3456 if (!tmp->name || !strcmp(compr->name, tmp->name)) {
3471 static void wm_adsp_compr_detach(struct wm_adsp_compr *compr)
3476 /* Wake the poll so it can see buffer is no longer attached */
3478 snd_compr_fragment_elapsed(compr->stream);
3480 if (wm_adsp_compr_attached(compr)) {
3481 compr->buf->compr = NULL;
3486 int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream)
3488 struct wm_adsp_compr *compr, *tmp;
3489 struct snd_soc_pcm_runtime *rtd = stream->private_data;
3492 mutex_lock(&dsp->pwr_lock);
3494 if (wm_adsp_fw[dsp->fw].num_caps == 0) {
3495 adsp_err(dsp, "%s: Firmware does not support compressed API\n",
3496 asoc_rtd_to_codec(rtd, 0)->name);
3501 if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) {
3502 adsp_err(dsp, "%s: Firmware does not support stream direction\n",
3503 asoc_rtd_to_codec(rtd, 0)->name);
3508 list_for_each_entry(tmp, &dsp->compr_list, list) {
3509 if (!strcmp(tmp->name, asoc_rtd_to_codec(rtd, 0)->name)) {
3510 adsp_err(dsp, "%s: Only a single stream supported per dai\n",
3511 asoc_rtd_to_codec(rtd, 0)->name);
3517 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
3524 compr->stream = stream;
3525 compr->name = asoc_rtd_to_codec(rtd, 0)->name;
3527 list_add_tail(&compr->list, &dsp->compr_list);
3529 stream->runtime->private_data = compr;
3532 mutex_unlock(&dsp->pwr_lock);
3536 EXPORT_SYMBOL_GPL(wm_adsp_compr_open);
3538 int wm_adsp_compr_free(struct snd_soc_component *component,
3539 struct snd_compr_stream *stream)
3541 struct wm_adsp_compr *compr = stream->runtime->private_data;
3542 struct wm_adsp *dsp = compr->dsp;
3544 mutex_lock(&dsp->pwr_lock);
3546 wm_adsp_compr_detach(compr);
3547 list_del(&compr->list);
3549 kfree(compr->raw_buf);
3552 mutex_unlock(&dsp->pwr_lock);
3556 EXPORT_SYMBOL_GPL(wm_adsp_compr_free);
3558 static int wm_adsp_compr_check_params(struct snd_compr_stream *stream,
3559 struct snd_compr_params *params)
3561 struct wm_adsp_compr *compr = stream->runtime->private_data;
3562 struct wm_adsp *dsp = compr->dsp;
3563 const struct wm_adsp_fw_caps *caps;
3564 const struct snd_codec_desc *desc;
3567 if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE ||
3568 params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE ||
3569 params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS ||
3570 params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS ||
3571 params->buffer.fragment_size % WM_ADSP_DATA_WORD_SIZE) {
3572 compr_err(compr, "Invalid buffer fragsize=%d fragments=%d\n",
3573 params->buffer.fragment_size,
3574 params->buffer.fragments);
3579 for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) {
3580 caps = &wm_adsp_fw[dsp->fw].caps[i];
3583 if (caps->id != params->codec.id)
3586 if (stream->direction == SND_COMPRESS_PLAYBACK) {
3587 if (desc->max_ch < params->codec.ch_out)
3590 if (desc->max_ch < params->codec.ch_in)
3594 if (!(desc->formats & (1 << params->codec.format)))
3597 for (j = 0; j < desc->num_sample_rates; ++j)
3598 if (desc->sample_rates[j] == params->codec.sample_rate)
3602 compr_err(compr, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n",
3603 params->codec.id, params->codec.ch_in, params->codec.ch_out,
3604 params->codec.sample_rate, params->codec.format);
3608 static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr)
3610 return compr->size.fragment_size / WM_ADSP_DATA_WORD_SIZE;
3613 int wm_adsp_compr_set_params(struct snd_soc_component *component,
3614 struct snd_compr_stream *stream,
3615 struct snd_compr_params *params)
3617 struct wm_adsp_compr *compr = stream->runtime->private_data;
3621 ret = wm_adsp_compr_check_params(stream, params);
3625 compr->size = params->buffer;
3627 compr_dbg(compr, "fragment_size=%d fragments=%d\n",
3628 compr->size.fragment_size, compr->size.fragments);
3630 size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf);
3631 compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL);
3632 if (!compr->raw_buf)
3635 compr->sample_rate = params->codec.sample_rate;
3639 EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params);
3641 int wm_adsp_compr_get_caps(struct snd_soc_component *component,
3642 struct snd_compr_stream *stream,
3643 struct snd_compr_caps *caps)
3645 struct wm_adsp_compr *compr = stream->runtime->private_data;
3646 int fw = compr->dsp->fw;
3649 if (wm_adsp_fw[fw].caps) {
3650 for (i = 0; i < wm_adsp_fw[fw].num_caps; i++)
3651 caps->codecs[i] = wm_adsp_fw[fw].caps[i].id;
3653 caps->num_codecs = i;
3654 caps->direction = wm_adsp_fw[fw].compr_direction;
3656 caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE;
3657 caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE;
3658 caps->min_fragments = WM_ADSP_MIN_FRAGMENTS;
3659 caps->max_fragments = WM_ADSP_MAX_FRAGMENTS;
3664 EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps);
3666 static int wm_adsp_read_data_block(struct wm_adsp *dsp, int mem_type,
3667 unsigned int mem_addr,
3668 unsigned int num_words, u32 *data)
3670 struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
3671 unsigned int i, reg;
3677 reg = dsp->ops->region_to_reg(mem, mem_addr);
3679 ret = regmap_raw_read(dsp->regmap, reg, data,
3680 sizeof(*data) * num_words);
3684 for (i = 0; i < num_words; ++i)
3685 data[i] = be32_to_cpu(data[i]) & 0x00ffffffu;
3690 static inline int wm_adsp_read_data_word(struct wm_adsp *dsp, int mem_type,
3691 unsigned int mem_addr, u32 *data)
3693 return wm_adsp_read_data_block(dsp, mem_type, mem_addr, 1, data);
3696 static int wm_adsp_write_data_word(struct wm_adsp *dsp, int mem_type,
3697 unsigned int mem_addr, u32 data)
3699 struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
3705 reg = dsp->ops->region_to_reg(mem, mem_addr);
3707 data = cpu_to_be32(data & 0x00ffffffu);
3709 return regmap_raw_write(dsp->regmap, reg, &data, sizeof(data));
3712 static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf,
3713 unsigned int field_offset, u32 *data)
3715 return wm_adsp_read_data_word(buf->dsp, buf->host_buf_mem_type,
3716 buf->host_buf_ptr + field_offset, data);
3719 static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf,
3720 unsigned int field_offset, u32 data)
3722 return wm_adsp_write_data_word(buf->dsp, buf->host_buf_mem_type,
3723 buf->host_buf_ptr + field_offset, data);
3726 static void wm_adsp_remove_padding(u32 *buf, int nwords, int data_word_size)
3728 u8 *pack_in = (u8 *)buf;
3729 u8 *pack_out = (u8 *)buf;
3732 /* Remove the padding bytes from the data read from the DSP */
3733 for (i = 0; i < nwords; i++) {
3734 for (j = 0; j < data_word_size; j++)
3735 *pack_out++ = *pack_in++;
3737 pack_in += sizeof(*buf) - data_word_size;
3741 static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf)
3743 const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps;
3744 struct wm_adsp_buffer_region *region;
3748 buf->regions = kcalloc(caps->num_regions, sizeof(*buf->regions),
3753 for (i = 0; i < caps->num_regions; ++i) {
3754 region = &buf->regions[i];
3756 region->offset = offset;
3757 region->mem_type = caps->region_defs[i].mem_type;
3759 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset,
3760 ®ion->base_addr);
3764 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset,
3769 region->cumulative_size = offset;
3772 "region=%d type=%d base=%08x off=%08x size=%08x\n",
3773 i, region->mem_type, region->base_addr,
3774 region->offset, region->cumulative_size);
3780 static void wm_adsp_buffer_clear(struct wm_adsp_compr_buf *buf)
3782 buf->irq_count = 0xFFFFFFFF;
3783 buf->read_index = -1;
3787 static struct wm_adsp_compr_buf *wm_adsp_buffer_alloc(struct wm_adsp *dsp)
3789 struct wm_adsp_compr_buf *buf;
3791 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
3797 wm_adsp_buffer_clear(buf);
3799 list_add_tail(&buf->list, &dsp->buffer_list);
3804 static int wm_adsp_buffer_parse_legacy(struct wm_adsp *dsp)
3806 struct wm_adsp_alg_region *alg_region;
3807 struct wm_adsp_compr_buf *buf;
3808 u32 xmalg, addr, magic;
3811 alg_region = wm_adsp_find_alg_region(dsp, WMFW_ADSP2_XM, dsp->fw_id);
3813 adsp_err(dsp, "No algorithm region found\n");
3817 buf = wm_adsp_buffer_alloc(dsp);
3821 xmalg = dsp->ops->sys_config_size / sizeof(__be32);
3823 addr = alg_region->base + xmalg + ALG_XM_FIELD(magic);
3824 ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr, &magic);
3828 if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC)
3831 addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr);
3832 for (i = 0; i < 5; ++i) {
3833 ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr,
3834 &buf->host_buf_ptr);
3838 if (buf->host_buf_ptr)
3841 usleep_range(1000, 2000);
3844 if (!buf->host_buf_ptr)
3847 buf->host_buf_mem_type = WMFW_ADSP2_XM;
3849 ret = wm_adsp_buffer_populate(buf);
3853 compr_dbg(buf, "legacy host_buf_ptr=%x\n", buf->host_buf_ptr);
3858 static int wm_adsp_buffer_parse_coeff(struct wm_coeff_ctl *ctl)
3860 struct wm_adsp_host_buf_coeff_v1 coeff_v1;
3861 struct wm_adsp_compr_buf *buf;
3862 unsigned int val, reg;
3865 ret = wm_coeff_base_reg(ctl, ®);
3869 for (i = 0; i < 5; ++i) {
3870 ret = regmap_raw_read(ctl->dsp->regmap, reg, &val, sizeof(val));
3877 usleep_range(1000, 2000);
3881 adsp_err(ctl->dsp, "Failed to acquire host buffer\n");
3885 buf = wm_adsp_buffer_alloc(ctl->dsp);
3889 buf->host_buf_mem_type = ctl->alg_region.type;
3890 buf->host_buf_ptr = be32_to_cpu(val);
3892 ret = wm_adsp_buffer_populate(buf);
3897 * v0 host_buffer coefficients didn't have versioning, so if the
3898 * control is one word, assume version 0.
3900 if (ctl->len == 4) {
3901 compr_dbg(buf, "host_buf_ptr=%x\n", buf->host_buf_ptr);
3905 ret = regmap_raw_read(ctl->dsp->regmap, reg, &coeff_v1,
3910 coeff_v1.versions = be32_to_cpu(coeff_v1.versions);
3911 val = coeff_v1.versions & HOST_BUF_COEFF_COMPAT_VER_MASK;
3912 val >>= HOST_BUF_COEFF_COMPAT_VER_SHIFT;
3914 if (val > HOST_BUF_COEFF_SUPPORTED_COMPAT_VER) {
3916 "Host buffer coeff ver %u > supported version %u\n",
3917 val, HOST_BUF_COEFF_SUPPORTED_COMPAT_VER);
3921 for (i = 0; i < ARRAY_SIZE(coeff_v1.name); i++)
3922 coeff_v1.name[i] = be32_to_cpu(coeff_v1.name[i]);
3924 wm_adsp_remove_padding((u32 *)&coeff_v1.name,
3925 ARRAY_SIZE(coeff_v1.name),
3926 WM_ADSP_DATA_WORD_SIZE);
3928 buf->name = kasprintf(GFP_KERNEL, "%s-dsp-%s", ctl->dsp->part,
3929 (char *)&coeff_v1.name);
3931 compr_dbg(buf, "host_buf_ptr=%x coeff version %u\n",
3932 buf->host_buf_ptr, val);
3937 static int wm_adsp_buffer_init(struct wm_adsp *dsp)
3939 struct wm_coeff_ctl *ctl;
3942 list_for_each_entry(ctl, &dsp->ctl_list, list) {
3943 if (ctl->type != WMFW_CTL_TYPE_HOST_BUFFER)
3949 ret = wm_adsp_buffer_parse_coeff(ctl);
3951 adsp_err(dsp, "Failed to parse coeff: %d\n", ret);
3953 } else if (ret == 0) {
3954 /* Only one buffer supported for version 0 */
3959 if (list_empty(&dsp->buffer_list)) {
3960 /* Fall back to legacy support */
3961 ret = wm_adsp_buffer_parse_legacy(dsp);
3963 adsp_err(dsp, "Failed to parse legacy: %d\n", ret);
3971 wm_adsp_buffer_free(dsp);
3975 static int wm_adsp_buffer_free(struct wm_adsp *dsp)
3977 struct wm_adsp_compr_buf *buf, *tmp;
3979 list_for_each_entry_safe(buf, tmp, &dsp->buffer_list, list) {
3980 wm_adsp_compr_detach(buf->compr);
3983 kfree(buf->regions);
3984 list_del(&buf->list);
3991 static int wm_adsp_buffer_get_error(struct wm_adsp_compr_buf *buf)
3995 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error);
3997 compr_err(buf, "Failed to check buffer error: %d\n", ret);
4000 if (buf->error != 0) {
4001 compr_err(buf, "Buffer error occurred: %d\n", buf->error);
4008 int wm_adsp_compr_trigger(struct snd_soc_component *component,
4009 struct snd_compr_stream *stream, int cmd)
4011 struct wm_adsp_compr *compr = stream->runtime->private_data;
4012 struct wm_adsp *dsp = compr->dsp;
4015 compr_dbg(compr, "Trigger: %d\n", cmd);
4017 mutex_lock(&dsp->pwr_lock);
4020 case SNDRV_PCM_TRIGGER_START:
4021 if (!wm_adsp_compr_attached(compr)) {
4022 ret = wm_adsp_compr_attach(compr);
4024 compr_err(compr, "Failed to link buffer and stream: %d\n",
4030 ret = wm_adsp_buffer_get_error(compr->buf);
4034 /* Trigger the IRQ at one fragment of data */
4035 ret = wm_adsp_buffer_write(compr->buf,
4036 HOST_BUFFER_FIELD(high_water_mark),
4037 wm_adsp_compr_frag_words(compr));
4039 compr_err(compr, "Failed to set high water mark: %d\n",
4044 case SNDRV_PCM_TRIGGER_STOP:
4045 if (wm_adsp_compr_attached(compr))
4046 wm_adsp_buffer_clear(compr->buf);
4053 mutex_unlock(&dsp->pwr_lock);
4057 EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger);
4059 static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf)
4061 int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1;
4063 return buf->regions[last_region].cumulative_size;
4066 static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf)
4068 u32 next_read_index, next_write_index;
4069 int write_index, read_index, avail;
4072 /* Only sync read index if we haven't already read a valid index */
4073 if (buf->read_index < 0) {
4074 ret = wm_adsp_buffer_read(buf,
4075 HOST_BUFFER_FIELD(next_read_index),
4080 read_index = sign_extend32(next_read_index, 23);
4082 if (read_index < 0) {
4083 compr_dbg(buf, "Avail check on unstarted stream\n");
4087 buf->read_index = read_index;
4090 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index),
4095 write_index = sign_extend32(next_write_index, 23);
4097 avail = write_index - buf->read_index;
4099 avail += wm_adsp_buffer_size(buf);
4101 compr_dbg(buf, "readindex=0x%x, writeindex=0x%x, avail=%d\n",
4102 buf->read_index, write_index, avail * WM_ADSP_DATA_WORD_SIZE);
4109 int wm_adsp_compr_handle_irq(struct wm_adsp *dsp)
4111 struct wm_adsp_compr_buf *buf;
4112 struct wm_adsp_compr *compr;
4115 mutex_lock(&dsp->pwr_lock);
4117 if (list_empty(&dsp->buffer_list)) {
4122 adsp_dbg(dsp, "Handling buffer IRQ\n");
4124 list_for_each_entry(buf, &dsp->buffer_list, list) {
4127 ret = wm_adsp_buffer_get_error(buf);
4129 goto out_notify; /* Wake poll to report error */
4131 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count),
4134 compr_err(buf, "Failed to get irq_count: %d\n", ret);
4138 ret = wm_adsp_buffer_update_avail(buf);
4140 compr_err(buf, "Error reading avail: %d\n", ret);
4144 if (wm_adsp_fw[dsp->fw].voice_trigger && buf->irq_count == 2)
4145 ret = WM_ADSP_COMPR_VOICE_TRIGGER;
4148 if (compr && compr->stream)
4149 snd_compr_fragment_elapsed(compr->stream);
4153 mutex_unlock(&dsp->pwr_lock);
4157 EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq);
4159 static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf)
4161 if (buf->irq_count & 0x01)
4164 compr_dbg(buf, "Enable IRQ(0x%x) for next fragment\n", buf->irq_count);
4166 buf->irq_count |= 0x01;
4168 return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack),
4172 int wm_adsp_compr_pointer(struct snd_soc_component *component,
4173 struct snd_compr_stream *stream,
4174 struct snd_compr_tstamp *tstamp)
4176 struct wm_adsp_compr *compr = stream->runtime->private_data;
4177 struct wm_adsp *dsp = compr->dsp;
4178 struct wm_adsp_compr_buf *buf;
4181 compr_dbg(compr, "Pointer request\n");
4183 mutex_lock(&dsp->pwr_lock);
4187 if (dsp->fatal_error || !buf || buf->error) {
4188 snd_compr_stop_error(stream, SNDRV_PCM_STATE_XRUN);
4193 if (buf->avail < wm_adsp_compr_frag_words(compr)) {
4194 ret = wm_adsp_buffer_update_avail(buf);
4196 compr_err(compr, "Error reading avail: %d\n", ret);
4201 * If we really have less than 1 fragment available tell the
4202 * DSP to inform us once a whole fragment is available.
4204 if (buf->avail < wm_adsp_compr_frag_words(compr)) {
4205 ret = wm_adsp_buffer_get_error(buf);
4208 snd_compr_stop_error(stream,
4209 SNDRV_PCM_STATE_XRUN);
4213 ret = wm_adsp_buffer_reenable_irq(buf);
4215 compr_err(compr, "Failed to re-enable buffer IRQ: %d\n",
4222 tstamp->copied_total = compr->copied_total;
4223 tstamp->copied_total += buf->avail * WM_ADSP_DATA_WORD_SIZE;
4224 tstamp->sampling_rate = compr->sample_rate;
4227 mutex_unlock(&dsp->pwr_lock);
4231 EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer);
4233 static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target)
4235 struct wm_adsp_compr_buf *buf = compr->buf;
4236 unsigned int adsp_addr;
4237 int mem_type, nwords, max_read;
4240 /* Calculate read parameters */
4241 for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i)
4242 if (buf->read_index < buf->regions[i].cumulative_size)
4245 if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions)
4248 mem_type = buf->regions[i].mem_type;
4249 adsp_addr = buf->regions[i].base_addr +
4250 (buf->read_index - buf->regions[i].offset);
4252 max_read = wm_adsp_compr_frag_words(compr);
4253 nwords = buf->regions[i].cumulative_size - buf->read_index;
4255 if (nwords > target)
4257 if (nwords > buf->avail)
4258 nwords = buf->avail;
4259 if (nwords > max_read)
4264 /* Read data from DSP */
4265 ret = wm_adsp_read_data_block(buf->dsp, mem_type, adsp_addr,
4266 nwords, compr->raw_buf);
4270 wm_adsp_remove_padding(compr->raw_buf, nwords, WM_ADSP_DATA_WORD_SIZE);
4272 /* update read index to account for words read */
4273 buf->read_index += nwords;
4274 if (buf->read_index == wm_adsp_buffer_size(buf))
4275 buf->read_index = 0;
4277 ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index),
4282 /* update avail to account for words read */
4283 buf->avail -= nwords;
4288 static int wm_adsp_compr_read(struct wm_adsp_compr *compr,
4289 char __user *buf, size_t count)
4291 struct wm_adsp *dsp = compr->dsp;
4295 compr_dbg(compr, "Requested read of %zu bytes\n", count);
4297 if (dsp->fatal_error || !compr->buf || compr->buf->error) {
4298 snd_compr_stop_error(compr->stream, SNDRV_PCM_STATE_XRUN);
4302 count /= WM_ADSP_DATA_WORD_SIZE;
4305 nwords = wm_adsp_buffer_capture_block(compr, count);
4307 compr_err(compr, "Failed to capture block: %d\n",
4312 nbytes = nwords * WM_ADSP_DATA_WORD_SIZE;
4314 compr_dbg(compr, "Read %d bytes\n", nbytes);
4316 if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) {
4317 compr_err(compr, "Failed to copy data to user: %d, %d\n",
4324 } while (nwords > 0 && count > 0);
4326 compr->copied_total += ntotal;
4331 int wm_adsp_compr_copy(struct snd_soc_component *component,
4332 struct snd_compr_stream *stream, char __user *buf,
4335 struct wm_adsp_compr *compr = stream->runtime->private_data;
4336 struct wm_adsp *dsp = compr->dsp;
4339 mutex_lock(&dsp->pwr_lock);
4341 if (stream->direction == SND_COMPRESS_CAPTURE)
4342 ret = wm_adsp_compr_read(compr, buf, count);
4346 mutex_unlock(&dsp->pwr_lock);
4350 EXPORT_SYMBOL_GPL(wm_adsp_compr_copy);
4352 static void wm_adsp_fatal_error(struct wm_adsp *dsp)
4354 struct wm_adsp_compr *compr;
4356 dsp->fatal_error = true;
4358 list_for_each_entry(compr, &dsp->compr_list, list) {
4360 snd_compr_fragment_elapsed(compr->stream);
4364 irqreturn_t wm_adsp2_bus_error(int irq, void *data)
4366 struct wm_adsp *dsp = (struct wm_adsp *)data;
4368 struct regmap *regmap = dsp->regmap;
4371 mutex_lock(&dsp->pwr_lock);
4373 ret = regmap_read(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL, &val);
4376 "Failed to read Region Lock Ctrl register: %d\n", ret);
4380 if (val & ADSP2_WDT_TIMEOUT_STS_MASK) {
4381 adsp_err(dsp, "watchdog timeout error\n");
4382 dsp->ops->stop_watchdog(dsp);
4383 wm_adsp_fatal_error(dsp);
4386 if (val & (ADSP2_ADDR_ERR_MASK | ADSP2_REGION_LOCK_ERR_MASK)) {
4387 if (val & ADSP2_ADDR_ERR_MASK)
4388 adsp_err(dsp, "bus error: address error\n");
4390 adsp_err(dsp, "bus error: region lock error\n");
4392 ret = regmap_read(regmap, dsp->base + ADSP2_BUS_ERR_ADDR, &val);
4395 "Failed to read Bus Err Addr register: %d\n",
4400 adsp_err(dsp, "bus error address = 0x%x\n",
4401 val & ADSP2_BUS_ERR_ADDR_MASK);
4403 ret = regmap_read(regmap,
4404 dsp->base + ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR,
4408 "Failed to read Pmem Xmem Err Addr register: %d\n",
4413 adsp_err(dsp, "xmem error address = 0x%x\n",
4414 val & ADSP2_XMEM_ERR_ADDR_MASK);
4415 adsp_err(dsp, "pmem error address = 0x%x\n",
4416 (val & ADSP2_PMEM_ERR_ADDR_MASK) >>
4417 ADSP2_PMEM_ERR_ADDR_SHIFT);
4420 regmap_update_bits(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL,
4421 ADSP2_CTRL_ERR_EINT, ADSP2_CTRL_ERR_EINT);
4424 mutex_unlock(&dsp->pwr_lock);
4428 EXPORT_SYMBOL_GPL(wm_adsp2_bus_error);
4430 irqreturn_t wm_halo_bus_error(int irq, void *data)
4432 struct wm_adsp *dsp = (struct wm_adsp *)data;
4433 struct regmap *regmap = dsp->regmap;
4434 unsigned int fault[6];
4435 struct reg_sequence clear[] = {
4436 { dsp->base + HALO_MPU_XM_VIO_STATUS, 0x0 },
4437 { dsp->base + HALO_MPU_YM_VIO_STATUS, 0x0 },
4438 { dsp->base + HALO_MPU_PM_VIO_STATUS, 0x0 },
4442 mutex_lock(&dsp->pwr_lock);
4444 ret = regmap_read(regmap, dsp->base_sysinfo + HALO_AHBM_WINDOW_DEBUG_1,
4447 adsp_warn(dsp, "Failed to read AHB DEBUG_1: %d\n", ret);
4451 adsp_warn(dsp, "AHB: STATUS: 0x%x ADDR: 0x%x\n",
4452 *fault & HALO_AHBM_FLAGS_ERR_MASK,
4453 (*fault & HALO_AHBM_CORE_ERR_ADDR_MASK) >>
4454 HALO_AHBM_CORE_ERR_ADDR_SHIFT);
4456 ret = regmap_read(regmap, dsp->base_sysinfo + HALO_AHBM_WINDOW_DEBUG_0,
4459 adsp_warn(dsp, "Failed to read AHB DEBUG_0: %d\n", ret);
4463 adsp_warn(dsp, "AHB: SYS_ADDR: 0x%x\n", *fault);
4465 ret = regmap_bulk_read(regmap, dsp->base + HALO_MPU_XM_VIO_ADDR,
4466 fault, ARRAY_SIZE(fault));
4468 adsp_warn(dsp, "Failed to read MPU fault info: %d\n", ret);
4472 adsp_warn(dsp, "XM: STATUS:0x%x ADDR:0x%x\n", fault[1], fault[0]);
4473 adsp_warn(dsp, "YM: STATUS:0x%x ADDR:0x%x\n", fault[3], fault[2]);
4474 adsp_warn(dsp, "PM: STATUS:0x%x ADDR:0x%x\n", fault[5], fault[4]);
4476 ret = regmap_multi_reg_write(dsp->regmap, clear, ARRAY_SIZE(clear));
4478 adsp_warn(dsp, "Failed to clear MPU status: %d\n", ret);
4481 mutex_unlock(&dsp->pwr_lock);
4485 EXPORT_SYMBOL_GPL(wm_halo_bus_error);
4487 irqreturn_t wm_halo_wdt_expire(int irq, void *data)
4489 struct wm_adsp *dsp = data;
4491 mutex_lock(&dsp->pwr_lock);
4493 adsp_warn(dsp, "WDT Expiry Fault\n");
4494 dsp->ops->stop_watchdog(dsp);
4495 wm_adsp_fatal_error(dsp);
4497 mutex_unlock(&dsp->pwr_lock);
4501 EXPORT_SYMBOL_GPL(wm_halo_wdt_expire);
4503 static struct wm_adsp_ops wm_adsp1_ops = {
4504 .validate_version = wm_adsp_validate_version,
4505 .parse_sizes = wm_adsp1_parse_sizes,
4506 .region_to_reg = wm_adsp_region_to_reg,
4509 static struct wm_adsp_ops wm_adsp2_ops[] = {
4511 .sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr),
4512 .parse_sizes = wm_adsp2_parse_sizes,
4513 .validate_version = wm_adsp_validate_version,
4514 .setup_algs = wm_adsp2_setup_algs,
4515 .region_to_reg = wm_adsp_region_to_reg,
4517 .show_fw_status = wm_adsp2_show_fw_status,
4519 .enable_memory = wm_adsp2_enable_memory,
4520 .disable_memory = wm_adsp2_disable_memory,
4522 .enable_core = wm_adsp2_enable_core,
4523 .disable_core = wm_adsp2_disable_core,
4525 .start_core = wm_adsp2_start_core,
4526 .stop_core = wm_adsp2_stop_core,
4530 .sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr),
4531 .parse_sizes = wm_adsp2_parse_sizes,
4532 .validate_version = wm_adsp_validate_version,
4533 .setup_algs = wm_adsp2_setup_algs,
4534 .region_to_reg = wm_adsp_region_to_reg,
4536 .show_fw_status = wm_adsp2v2_show_fw_status,
4538 .enable_memory = wm_adsp2_enable_memory,
4539 .disable_memory = wm_adsp2_disable_memory,
4540 .lock_memory = wm_adsp2_lock,
4542 .enable_core = wm_adsp2v2_enable_core,
4543 .disable_core = wm_adsp2v2_disable_core,
4545 .start_core = wm_adsp2_start_core,
4546 .stop_core = wm_adsp2_stop_core,
4549 .sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr),
4550 .parse_sizes = wm_adsp2_parse_sizes,
4551 .validate_version = wm_adsp_validate_version,
4552 .setup_algs = wm_adsp2_setup_algs,
4553 .region_to_reg = wm_adsp_region_to_reg,
4555 .show_fw_status = wm_adsp2v2_show_fw_status,
4556 .stop_watchdog = wm_adsp_stop_watchdog,
4558 .enable_memory = wm_adsp2_enable_memory,
4559 .disable_memory = wm_adsp2_disable_memory,
4560 .lock_memory = wm_adsp2_lock,
4562 .enable_core = wm_adsp2v2_enable_core,
4563 .disable_core = wm_adsp2v2_disable_core,
4565 .start_core = wm_adsp2_start_core,
4566 .stop_core = wm_adsp2_stop_core,
4570 static struct wm_adsp_ops wm_halo_ops = {
4571 .sys_config_size = sizeof(struct wm_halo_system_config_xm_hdr),
4572 .parse_sizes = wm_adsp2_parse_sizes,
4573 .validate_version = wm_halo_validate_version,
4574 .setup_algs = wm_halo_setup_algs,
4575 .region_to_reg = wm_halo_region_to_reg,
4577 .show_fw_status = wm_halo_show_fw_status,
4578 .stop_watchdog = wm_halo_stop_watchdog,
4580 .lock_memory = wm_halo_configure_mpu,
4582 .start_core = wm_halo_start_core,
4583 .stop_core = wm_halo_stop_core,
4586 MODULE_LICENSE("GPL v2");