2 * wm_adsp.c -- Wolfson ADSP support
4 * Copyright 2012 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/firmware.h>
18 #include <linux/list.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/workqueue.h>
26 #include <linux/debugfs.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/jack.h>
32 #include <sound/initval.h>
33 #include <sound/tlv.h>
35 #include <linux/mfd/arizona/registers.h>
40 #define adsp_crit(_dsp, fmt, ...) \
41 dev_crit(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
42 #define adsp_err(_dsp, fmt, ...) \
43 dev_err(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
44 #define adsp_warn(_dsp, fmt, ...) \
45 dev_warn(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
46 #define adsp_info(_dsp, fmt, ...) \
47 dev_info(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
48 #define adsp_dbg(_dsp, fmt, ...) \
49 dev_dbg(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
51 #define ADSP1_CONTROL_1 0x00
52 #define ADSP1_CONTROL_2 0x02
53 #define ADSP1_CONTROL_3 0x03
54 #define ADSP1_CONTROL_4 0x04
55 #define ADSP1_CONTROL_5 0x06
56 #define ADSP1_CONTROL_6 0x07
57 #define ADSP1_CONTROL_7 0x08
58 #define ADSP1_CONTROL_8 0x09
59 #define ADSP1_CONTROL_9 0x0A
60 #define ADSP1_CONTROL_10 0x0B
61 #define ADSP1_CONTROL_11 0x0C
62 #define ADSP1_CONTROL_12 0x0D
63 #define ADSP1_CONTROL_13 0x0F
64 #define ADSP1_CONTROL_14 0x10
65 #define ADSP1_CONTROL_15 0x11
66 #define ADSP1_CONTROL_16 0x12
67 #define ADSP1_CONTROL_17 0x13
68 #define ADSP1_CONTROL_18 0x14
69 #define ADSP1_CONTROL_19 0x16
70 #define ADSP1_CONTROL_20 0x17
71 #define ADSP1_CONTROL_21 0x18
72 #define ADSP1_CONTROL_22 0x1A
73 #define ADSP1_CONTROL_23 0x1B
74 #define ADSP1_CONTROL_24 0x1C
75 #define ADSP1_CONTROL_25 0x1E
76 #define ADSP1_CONTROL_26 0x20
77 #define ADSP1_CONTROL_27 0x21
78 #define ADSP1_CONTROL_28 0x22
79 #define ADSP1_CONTROL_29 0x23
80 #define ADSP1_CONTROL_30 0x24
81 #define ADSP1_CONTROL_31 0x26
86 #define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
87 #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
88 #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
94 #define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */
95 #define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */
96 #define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */
97 #define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */
98 #define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
99 #define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
100 #define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
101 #define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
102 #define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
103 #define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
104 #define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
105 #define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
106 #define ADSP1_START 0x0001 /* DSP1_START */
107 #define ADSP1_START_MASK 0x0001 /* DSP1_START */
108 #define ADSP1_START_SHIFT 0 /* DSP1_START */
109 #define ADSP1_START_WIDTH 1 /* DSP1_START */
114 #define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
115 #define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
116 #define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
118 #define ADSP2_CONTROL 0x0
119 #define ADSP2_CLOCKING 0x1
120 #define ADSP2_STATUS1 0x4
121 #define ADSP2_WDMA_CONFIG_1 0x30
122 #define ADSP2_WDMA_CONFIG_2 0x31
123 #define ADSP2_RDMA_CONFIG_1 0x34
125 #define ADSP2_SCRATCH0 0x40
126 #define ADSP2_SCRATCH1 0x41
127 #define ADSP2_SCRATCH2 0x42
128 #define ADSP2_SCRATCH3 0x43
134 #define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */
135 #define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */
136 #define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */
137 #define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */
138 #define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
139 #define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
140 #define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
141 #define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
142 #define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
143 #define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
144 #define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
145 #define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
146 #define ADSP2_START 0x0001 /* DSP1_START */
147 #define ADSP2_START_MASK 0x0001 /* DSP1_START */
148 #define ADSP2_START_SHIFT 0 /* DSP1_START */
149 #define ADSP2_START_WIDTH 1 /* DSP1_START */
154 #define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
155 #define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
156 #define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
161 #define ADSP2_RAM_RDY 0x0001
162 #define ADSP2_RAM_RDY_MASK 0x0001
163 #define ADSP2_RAM_RDY_SHIFT 0
164 #define ADSP2_RAM_RDY_WIDTH 1
167 struct list_head list
;
171 static struct wm_adsp_buf
*wm_adsp_buf_alloc(const void *src
, size_t len
,
172 struct list_head
*list
)
174 struct wm_adsp_buf
*buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
179 buf
->buf
= vmalloc(len
);
184 memcpy(buf
->buf
, src
, len
);
187 list_add_tail(&buf
->list
, list
);
192 static void wm_adsp_buf_free(struct list_head
*list
)
194 while (!list_empty(list
)) {
195 struct wm_adsp_buf
*buf
= list_first_entry(list
,
198 list_del(&buf
->list
);
204 #define WM_ADSP_FW_MBC_VSS 0
205 #define WM_ADSP_FW_HIFI 1
206 #define WM_ADSP_FW_TX 2
207 #define WM_ADSP_FW_TX_SPK 3
208 #define WM_ADSP_FW_RX 4
209 #define WM_ADSP_FW_RX_ANC 5
210 #define WM_ADSP_FW_CTRL 6
211 #define WM_ADSP_FW_ASR 7
212 #define WM_ADSP_FW_TRACE 8
213 #define WM_ADSP_FW_SPK_PROT 9
214 #define WM_ADSP_FW_MISC 10
216 #define WM_ADSP_NUM_FW 11
218 static const char *wm_adsp_fw_text
[WM_ADSP_NUM_FW
] = {
219 [WM_ADSP_FW_MBC_VSS
] = "MBC/VSS",
220 [WM_ADSP_FW_HIFI
] = "MasterHiFi",
221 [WM_ADSP_FW_TX
] = "Tx",
222 [WM_ADSP_FW_TX_SPK
] = "Tx Speaker",
223 [WM_ADSP_FW_RX
] = "Rx",
224 [WM_ADSP_FW_RX_ANC
] = "Rx ANC",
225 [WM_ADSP_FW_CTRL
] = "Voice Ctrl",
226 [WM_ADSP_FW_ASR
] = "ASR Assist",
227 [WM_ADSP_FW_TRACE
] = "Dbg Trace",
228 [WM_ADSP_FW_SPK_PROT
] = "Protection",
229 [WM_ADSP_FW_MISC
] = "Misc",
232 struct wm_adsp_system_config_xm_hdr
{
238 __be32 dma_buffer_size
;
241 __be32 build_job_name
[3];
242 __be32 build_job_number
;
245 struct wm_adsp_alg_xm_struct
{
251 __be32 high_water_mark
;
252 __be32 low_water_mark
;
253 __be64 smoothed_power
;
256 struct wm_adsp_buffer
{
257 __be32 X_buf_base
; /* XM base addr of first X area */
258 __be32 X_buf_size
; /* Size of 1st X area in words */
259 __be32 X_buf_base2
; /* XM base addr of 2nd X area */
260 __be32 X_buf_brk
; /* Total X size in words */
261 __be32 Y_buf_base
; /* YM base addr of Y area */
262 __be32 wrap
; /* Total size X and Y in words */
263 __be32 high_water_mark
; /* Point at which IRQ is asserted */
264 __be32 irq_count
; /* bits 1-31 count IRQ assertions */
265 __be32 irq_ack
; /* acked IRQ count, bit 0 enables IRQ */
266 __be32 next_write_index
; /* word index of next write */
267 __be32 next_read_index
; /* word index of next read */
268 __be32 error
; /* error if any */
269 __be32 oldest_block_index
; /* word index of oldest surviving */
270 __be32 requested_rewind
; /* how many blocks rewind was done */
271 __be32 reserved_space
; /* internal */
272 __be32 min_free
; /* min free space since stream start */
273 __be32 blocks_written
[2]; /* total blocks written (64 bit) */
274 __be32 words_written
[2]; /* total words written (64 bit) */
277 struct wm_adsp_compr_buf
{
280 struct wm_adsp_buffer_region
*regions
;
289 struct wm_adsp_compr
{
291 struct wm_adsp_compr_buf
*buf
;
293 struct snd_compr_stream
*stream
;
294 struct snd_compressed_buffer size
;
297 unsigned int copied_total
;
300 #define WM_ADSP_DATA_WORD_SIZE 3
302 #define WM_ADSP_MIN_FRAGMENTS 1
303 #define WM_ADSP_MAX_FRAGMENTS 256
304 #define WM_ADSP_MIN_FRAGMENT_SIZE (64 * WM_ADSP_DATA_WORD_SIZE)
305 #define WM_ADSP_MAX_FRAGMENT_SIZE (4096 * WM_ADSP_DATA_WORD_SIZE)
307 #define WM_ADSP_ALG_XM_STRUCT_MAGIC 0x49aec7
309 #define HOST_BUFFER_FIELD(field) \
310 (offsetof(struct wm_adsp_buffer, field) / sizeof(__be32))
312 #define ALG_XM_FIELD(field) \
313 (offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32))
315 static int wm_adsp_buffer_init(struct wm_adsp
*dsp
);
316 static int wm_adsp_buffer_free(struct wm_adsp
*dsp
);
318 struct wm_adsp_buffer_region
{
320 unsigned int cumulative_size
;
321 unsigned int mem_type
;
322 unsigned int base_addr
;
325 struct wm_adsp_buffer_region_def
{
326 unsigned int mem_type
;
327 unsigned int base_offset
;
328 unsigned int size_offset
;
331 static struct wm_adsp_buffer_region_def ez2control_regions
[] = {
333 .mem_type
= WMFW_ADSP2_XM
,
334 .base_offset
= HOST_BUFFER_FIELD(X_buf_base
),
335 .size_offset
= HOST_BUFFER_FIELD(X_buf_size
),
338 .mem_type
= WMFW_ADSP2_XM
,
339 .base_offset
= HOST_BUFFER_FIELD(X_buf_base2
),
340 .size_offset
= HOST_BUFFER_FIELD(X_buf_brk
),
343 .mem_type
= WMFW_ADSP2_YM
,
344 .base_offset
= HOST_BUFFER_FIELD(Y_buf_base
),
345 .size_offset
= HOST_BUFFER_FIELD(wrap
),
349 struct wm_adsp_fw_caps
{
351 struct snd_codec_desc desc
;
353 struct wm_adsp_buffer_region_def
*region_defs
;
356 static const struct wm_adsp_fw_caps ez2control_caps
[] = {
358 .id
= SND_AUDIOCODEC_BESPOKE
,
361 .sample_rates
= { 16000 },
362 .num_sample_rates
= 1,
363 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
365 .num_regions
= ARRAY_SIZE(ez2control_regions
),
366 .region_defs
= ez2control_regions
,
370 static const struct {
374 const struct wm_adsp_fw_caps
*caps
;
375 } wm_adsp_fw
[WM_ADSP_NUM_FW
] = {
376 [WM_ADSP_FW_MBC_VSS
] = { .file
= "mbc-vss" },
377 [WM_ADSP_FW_HIFI
] = { .file
= "hifi" },
378 [WM_ADSP_FW_TX
] = { .file
= "tx" },
379 [WM_ADSP_FW_TX_SPK
] = { .file
= "tx-spk" },
380 [WM_ADSP_FW_RX
] = { .file
= "rx" },
381 [WM_ADSP_FW_RX_ANC
] = { .file
= "rx-anc" },
382 [WM_ADSP_FW_CTRL
] = {
384 .compr_direction
= SND_COMPRESS_CAPTURE
,
385 .num_caps
= ARRAY_SIZE(ez2control_caps
),
386 .caps
= ez2control_caps
,
388 [WM_ADSP_FW_ASR
] = { .file
= "asr" },
389 [WM_ADSP_FW_TRACE
] = { .file
= "trace" },
390 [WM_ADSP_FW_SPK_PROT
] = { .file
= "spk-prot" },
391 [WM_ADSP_FW_MISC
] = { .file
= "misc" },
394 struct wm_coeff_ctl_ops
{
395 int (*xget
)(struct snd_kcontrol
*kcontrol
,
396 struct snd_ctl_elem_value
*ucontrol
);
397 int (*xput
)(struct snd_kcontrol
*kcontrol
,
398 struct snd_ctl_elem_value
*ucontrol
);
399 int (*xinfo
)(struct snd_kcontrol
*kcontrol
,
400 struct snd_ctl_elem_info
*uinfo
);
403 struct wm_coeff_ctl
{
406 struct wm_adsp_alg_region alg_region
;
407 struct wm_coeff_ctl_ops ops
;
409 unsigned int enabled
:1;
410 struct list_head list
;
415 struct snd_kcontrol
*kcontrol
;
419 #ifdef CONFIG_DEBUG_FS
420 static void wm_adsp_debugfs_save_wmfwname(struct wm_adsp
*dsp
, const char *s
)
422 char *tmp
= kasprintf(GFP_KERNEL
, "%s\n", s
);
424 kfree(dsp
->wmfw_file_name
);
425 dsp
->wmfw_file_name
= tmp
;
428 static void wm_adsp_debugfs_save_binname(struct wm_adsp
*dsp
, const char *s
)
430 char *tmp
= kasprintf(GFP_KERNEL
, "%s\n", s
);
432 kfree(dsp
->bin_file_name
);
433 dsp
->bin_file_name
= tmp
;
436 static void wm_adsp_debugfs_clear(struct wm_adsp
*dsp
)
438 kfree(dsp
->wmfw_file_name
);
439 kfree(dsp
->bin_file_name
);
440 dsp
->wmfw_file_name
= NULL
;
441 dsp
->bin_file_name
= NULL
;
444 static ssize_t
wm_adsp_debugfs_wmfw_read(struct file
*file
,
445 char __user
*user_buf
,
446 size_t count
, loff_t
*ppos
)
448 struct wm_adsp
*dsp
= file
->private_data
;
451 mutex_lock(&dsp
->pwr_lock
);
453 if (!dsp
->wmfw_file_name
|| !dsp
->running
)
456 ret
= simple_read_from_buffer(user_buf
, count
, ppos
,
458 strlen(dsp
->wmfw_file_name
));
460 mutex_unlock(&dsp
->pwr_lock
);
464 static ssize_t
wm_adsp_debugfs_bin_read(struct file
*file
,
465 char __user
*user_buf
,
466 size_t count
, loff_t
*ppos
)
468 struct wm_adsp
*dsp
= file
->private_data
;
471 mutex_lock(&dsp
->pwr_lock
);
473 if (!dsp
->bin_file_name
|| !dsp
->running
)
476 ret
= simple_read_from_buffer(user_buf
, count
, ppos
,
478 strlen(dsp
->bin_file_name
));
480 mutex_unlock(&dsp
->pwr_lock
);
484 static const struct {
486 const struct file_operations fops
;
487 } wm_adsp_debugfs_fops
[] = {
489 .name
= "wmfw_file_name",
492 .read
= wm_adsp_debugfs_wmfw_read
,
496 .name
= "bin_file_name",
499 .read
= wm_adsp_debugfs_bin_read
,
504 static void wm_adsp2_init_debugfs(struct wm_adsp
*dsp
,
505 struct snd_soc_codec
*codec
)
507 struct dentry
*root
= NULL
;
511 if (!codec
->component
.debugfs_root
) {
512 adsp_err(dsp
, "No codec debugfs root\n");
516 root_name
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
520 snprintf(root_name
, PAGE_SIZE
, "dsp%d", dsp
->num
);
521 root
= debugfs_create_dir(root_name
, codec
->component
.debugfs_root
);
527 if (!debugfs_create_bool("running", S_IRUGO
, root
, &dsp
->running
))
530 if (!debugfs_create_x32("fw_id", S_IRUGO
, root
, &dsp
->fw_id
))
533 if (!debugfs_create_x32("fw_version", S_IRUGO
, root
,
534 &dsp
->fw_id_version
))
537 for (i
= 0; i
< ARRAY_SIZE(wm_adsp_debugfs_fops
); ++i
) {
538 if (!debugfs_create_file(wm_adsp_debugfs_fops
[i
].name
,
540 &wm_adsp_debugfs_fops
[i
].fops
))
544 dsp
->debugfs_root
= root
;
548 debugfs_remove_recursive(root
);
549 adsp_err(dsp
, "Failed to create debugfs\n");
552 static void wm_adsp2_cleanup_debugfs(struct wm_adsp
*dsp
)
554 wm_adsp_debugfs_clear(dsp
);
555 debugfs_remove_recursive(dsp
->debugfs_root
);
558 static inline void wm_adsp2_init_debugfs(struct wm_adsp
*dsp
,
559 struct snd_soc_codec
*codec
)
563 static inline void wm_adsp2_cleanup_debugfs(struct wm_adsp
*dsp
)
567 static inline void wm_adsp_debugfs_save_wmfwname(struct wm_adsp
*dsp
,
572 static inline void wm_adsp_debugfs_save_binname(struct wm_adsp
*dsp
,
577 static inline void wm_adsp_debugfs_clear(struct wm_adsp
*dsp
)
582 static int wm_adsp_fw_get(struct snd_kcontrol
*kcontrol
,
583 struct snd_ctl_elem_value
*ucontrol
)
585 struct snd_soc_codec
*codec
= snd_soc_kcontrol_codec(kcontrol
);
586 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
587 struct wm_adsp
*dsp
= snd_soc_codec_get_drvdata(codec
);
589 ucontrol
->value
.integer
.value
[0] = dsp
[e
->shift_l
].fw
;
594 static int wm_adsp_fw_put(struct snd_kcontrol
*kcontrol
,
595 struct snd_ctl_elem_value
*ucontrol
)
597 struct snd_soc_codec
*codec
= snd_soc_kcontrol_codec(kcontrol
);
598 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
599 struct wm_adsp
*dsp
= snd_soc_codec_get_drvdata(codec
);
602 if (ucontrol
->value
.integer
.value
[0] == dsp
[e
->shift_l
].fw
)
605 if (ucontrol
->value
.integer
.value
[0] >= WM_ADSP_NUM_FW
)
608 mutex_lock(&dsp
[e
->shift_l
].pwr_lock
);
610 if (dsp
[e
->shift_l
].running
|| dsp
[e
->shift_l
].compr
)
613 dsp
[e
->shift_l
].fw
= ucontrol
->value
.integer
.value
[0];
615 mutex_unlock(&dsp
[e
->shift_l
].pwr_lock
);
620 static const struct soc_enum wm_adsp_fw_enum
[] = {
621 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text
), wm_adsp_fw_text
),
622 SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text
), wm_adsp_fw_text
),
623 SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text
), wm_adsp_fw_text
),
624 SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text
), wm_adsp_fw_text
),
627 const struct snd_kcontrol_new wm_adsp_fw_controls
[] = {
628 SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum
[0],
629 wm_adsp_fw_get
, wm_adsp_fw_put
),
630 SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum
[1],
631 wm_adsp_fw_get
, wm_adsp_fw_put
),
632 SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum
[2],
633 wm_adsp_fw_get
, wm_adsp_fw_put
),
634 SOC_ENUM_EXT("DSP4 Firmware", wm_adsp_fw_enum
[3],
635 wm_adsp_fw_get
, wm_adsp_fw_put
),
637 EXPORT_SYMBOL_GPL(wm_adsp_fw_controls
);
639 static struct wm_adsp_region
const *wm_adsp_find_region(struct wm_adsp
*dsp
,
644 for (i
= 0; i
< dsp
->num_mems
; i
++)
645 if (dsp
->mem
[i
].type
== type
)
651 static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region
const *mem
,
658 return mem
->base
+ (offset
* 3);
660 return mem
->base
+ (offset
* 2);
662 return mem
->base
+ (offset
* 2);
664 return mem
->base
+ (offset
* 2);
666 return mem
->base
+ (offset
* 2);
668 WARN(1, "Unknown memory region type");
673 static void wm_adsp2_show_fw_status(struct wm_adsp
*dsp
)
678 ret
= regmap_raw_read(dsp
->regmap
, dsp
->base
+ ADSP2_SCRATCH0
,
679 scratch
, sizeof(scratch
));
681 adsp_err(dsp
, "Failed to read SCRATCH regs: %d\n", ret
);
685 adsp_dbg(dsp
, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
686 be16_to_cpu(scratch
[0]),
687 be16_to_cpu(scratch
[1]),
688 be16_to_cpu(scratch
[2]),
689 be16_to_cpu(scratch
[3]));
692 static int wm_coeff_info(struct snd_kcontrol
*kctl
,
693 struct snd_ctl_elem_info
*uinfo
)
695 struct wm_coeff_ctl
*ctl
= (struct wm_coeff_ctl
*)kctl
->private_value
;
697 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BYTES
;
698 uinfo
->count
= ctl
->len
;
702 static int wm_coeff_write_control(struct wm_coeff_ctl
*ctl
,
703 const void *buf
, size_t len
)
705 struct wm_adsp_alg_region
*alg_region
= &ctl
->alg_region
;
706 const struct wm_adsp_region
*mem
;
707 struct wm_adsp
*dsp
= ctl
->dsp
;
712 mem
= wm_adsp_find_region(dsp
, alg_region
->type
);
714 adsp_err(dsp
, "No base for region %x\n",
719 reg
= ctl
->alg_region
.base
+ ctl
->offset
;
720 reg
= wm_adsp_region_to_reg(mem
, reg
);
722 scratch
= kmemdup(buf
, ctl
->len
, GFP_KERNEL
| GFP_DMA
);
726 ret
= regmap_raw_write(dsp
->regmap
, reg
, scratch
,
729 adsp_err(dsp
, "Failed to write %zu bytes to %x: %d\n",
734 adsp_dbg(dsp
, "Wrote %zu bytes to %x\n", ctl
->len
, reg
);
741 static int wm_coeff_put(struct snd_kcontrol
*kctl
,
742 struct snd_ctl_elem_value
*ucontrol
)
744 struct wm_coeff_ctl
*ctl
= (struct wm_coeff_ctl
*)kctl
->private_value
;
745 char *p
= ucontrol
->value
.bytes
.data
;
748 mutex_lock(&ctl
->dsp
->pwr_lock
);
750 memcpy(ctl
->cache
, p
, ctl
->len
);
754 ret
= wm_coeff_write_control(ctl
, p
, ctl
->len
);
756 mutex_unlock(&ctl
->dsp
->pwr_lock
);
761 static int wm_coeff_read_control(struct wm_coeff_ctl
*ctl
,
762 void *buf
, size_t len
)
764 struct wm_adsp_alg_region
*alg_region
= &ctl
->alg_region
;
765 const struct wm_adsp_region
*mem
;
766 struct wm_adsp
*dsp
= ctl
->dsp
;
771 mem
= wm_adsp_find_region(dsp
, alg_region
->type
);
773 adsp_err(dsp
, "No base for region %x\n",
778 reg
= ctl
->alg_region
.base
+ ctl
->offset
;
779 reg
= wm_adsp_region_to_reg(mem
, reg
);
781 scratch
= kmalloc(ctl
->len
, GFP_KERNEL
| GFP_DMA
);
785 ret
= regmap_raw_read(dsp
->regmap
, reg
, scratch
, ctl
->len
);
787 adsp_err(dsp
, "Failed to read %zu bytes from %x: %d\n",
792 adsp_dbg(dsp
, "Read %zu bytes from %x\n", ctl
->len
, reg
);
794 memcpy(buf
, scratch
, ctl
->len
);
800 static int wm_coeff_get(struct snd_kcontrol
*kctl
,
801 struct snd_ctl_elem_value
*ucontrol
)
803 struct wm_coeff_ctl
*ctl
= (struct wm_coeff_ctl
*)kctl
->private_value
;
804 char *p
= ucontrol
->value
.bytes
.data
;
807 mutex_lock(&ctl
->dsp
->pwr_lock
);
809 if (ctl
->flags
& WMFW_CTL_FLAG_VOLATILE
) {
811 ret
= wm_coeff_read_control(ctl
, p
, ctl
->len
);
815 if (!ctl
->flags
&& ctl
->enabled
)
816 ret
= wm_coeff_read_control(ctl
, ctl
->cache
, ctl
->len
);
818 memcpy(p
, ctl
->cache
, ctl
->len
);
821 mutex_unlock(&ctl
->dsp
->pwr_lock
);
826 struct wmfw_ctl_work
{
828 struct wm_coeff_ctl
*ctl
;
829 struct work_struct work
;
832 static int wmfw_add_ctl(struct wm_adsp
*dsp
, struct wm_coeff_ctl
*ctl
)
834 struct snd_kcontrol_new
*kcontrol
;
837 if (!ctl
|| !ctl
->name
)
840 kcontrol
= kzalloc(sizeof(*kcontrol
), GFP_KERNEL
);
843 kcontrol
->iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
845 kcontrol
->name
= ctl
->name
;
846 kcontrol
->info
= wm_coeff_info
;
847 kcontrol
->get
= wm_coeff_get
;
848 kcontrol
->put
= wm_coeff_put
;
849 kcontrol
->private_value
= (unsigned long)ctl
;
852 if (ctl
->flags
& WMFW_CTL_FLAG_WRITEABLE
)
853 kcontrol
->access
|= SNDRV_CTL_ELEM_ACCESS_WRITE
;
854 if (ctl
->flags
& WMFW_CTL_FLAG_READABLE
)
855 kcontrol
->access
|= SNDRV_CTL_ELEM_ACCESS_READ
;
856 if (ctl
->flags
& WMFW_CTL_FLAG_VOLATILE
)
857 kcontrol
->access
|= SNDRV_CTL_ELEM_ACCESS_VOLATILE
;
860 ret
= snd_soc_add_card_controls(dsp
->card
,
867 ctl
->kcontrol
= snd_soc_card_get_kcontrol(dsp
->card
,
877 static int wm_coeff_init_control_caches(struct wm_adsp
*dsp
)
879 struct wm_coeff_ctl
*ctl
;
882 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
883 if (!ctl
->enabled
|| ctl
->set
)
885 if (ctl
->flags
& WMFW_CTL_FLAG_VOLATILE
)
888 ret
= wm_coeff_read_control(ctl
,
898 static int wm_coeff_sync_controls(struct wm_adsp
*dsp
)
900 struct wm_coeff_ctl
*ctl
;
903 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
906 if (ctl
->set
&& !(ctl
->flags
& WMFW_CTL_FLAG_VOLATILE
)) {
907 ret
= wm_coeff_write_control(ctl
,
918 static void wm_adsp_ctl_work(struct work_struct
*work
)
920 struct wmfw_ctl_work
*ctl_work
= container_of(work
,
921 struct wmfw_ctl_work
,
924 wmfw_add_ctl(ctl_work
->dsp
, ctl_work
->ctl
);
928 static int wm_adsp_create_control(struct wm_adsp
*dsp
,
929 const struct wm_adsp_alg_region
*alg_region
,
930 unsigned int offset
, unsigned int len
,
931 const char *subname
, unsigned int subname_len
,
934 struct wm_coeff_ctl
*ctl
;
935 struct wmfw_ctl_work
*ctl_work
;
936 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
940 if (flags
& WMFW_CTL_FLAG_SYS
)
943 switch (alg_region
->type
) {
960 adsp_err(dsp
, "Unknown region type: %d\n", alg_region
->type
);
964 switch (dsp
->fw_ver
) {
967 snprintf(name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
, "DSP%d %s %x",
968 dsp
->num
, region_name
, alg_region
->alg
);
971 ret
= snprintf(name
, SNDRV_CTL_ELEM_ID_NAME_MAXLEN
,
972 "DSP%d%c %.12s %x", dsp
->num
, *region_name
,
973 wm_adsp_fw_text
[dsp
->fw
], alg_region
->alg
);
975 /* Truncate the subname from the start if it is too long */
977 int avail
= SNDRV_CTL_ELEM_ID_NAME_MAXLEN
- ret
- 2;
980 if (subname_len
> avail
)
981 skip
= subname_len
- avail
;
984 SNDRV_CTL_ELEM_ID_NAME_MAXLEN
- ret
, " %.*s",
985 subname_len
- skip
, subname
+ skip
);
990 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
991 if (!strcmp(ctl
->name
, name
)) {
998 ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
1001 ctl
->fw_name
= wm_adsp_fw_text
[dsp
->fw
];
1002 ctl
->alg_region
= *alg_region
;
1003 ctl
->name
= kmemdup(name
, strlen(name
) + 1, GFP_KERNEL
);
1010 ctl
->ops
.xget
= wm_coeff_get
;
1011 ctl
->ops
.xput
= wm_coeff_put
;
1015 ctl
->offset
= offset
;
1017 adsp_warn(dsp
, "Truncating control %s from %d\n",
1022 ctl
->cache
= kzalloc(ctl
->len
, GFP_KERNEL
);
1028 list_add(&ctl
->list
, &dsp
->ctl_list
);
1030 ctl_work
= kzalloc(sizeof(*ctl_work
), GFP_KERNEL
);
1036 ctl_work
->dsp
= dsp
;
1037 ctl_work
->ctl
= ctl
;
1038 INIT_WORK(&ctl_work
->work
, wm_adsp_ctl_work
);
1039 schedule_work(&ctl_work
->work
);
1053 struct wm_coeff_parsed_alg
{
1060 struct wm_coeff_parsed_coeff
{
1070 static int wm_coeff_parse_string(int bytes
, const u8
**pos
, const u8
**str
)
1079 length
= le16_to_cpu(*((__le16
*)*pos
));
1086 *str
= *pos
+ bytes
;
1088 *pos
+= ((length
+ bytes
) + 3) & ~0x03;
1093 static int wm_coeff_parse_int(int bytes
, const u8
**pos
)
1099 val
= le16_to_cpu(*((__le16
*)*pos
));
1102 val
= le32_to_cpu(*((__le32
*)*pos
));
1113 static inline void wm_coeff_parse_alg(struct wm_adsp
*dsp
, const u8
**data
,
1114 struct wm_coeff_parsed_alg
*blk
)
1116 const struct wmfw_adsp_alg_data
*raw
;
1118 switch (dsp
->fw_ver
) {
1121 raw
= (const struct wmfw_adsp_alg_data
*)*data
;
1124 blk
->id
= le32_to_cpu(raw
->id
);
1125 blk
->name
= raw
->name
;
1126 blk
->name_len
= strlen(raw
->name
);
1127 blk
->ncoeff
= le32_to_cpu(raw
->ncoeff
);
1130 blk
->id
= wm_coeff_parse_int(sizeof(raw
->id
), data
);
1131 blk
->name_len
= wm_coeff_parse_string(sizeof(u8
), data
,
1133 wm_coeff_parse_string(sizeof(u16
), data
, NULL
);
1134 blk
->ncoeff
= wm_coeff_parse_int(sizeof(raw
->ncoeff
), data
);
1138 adsp_dbg(dsp
, "Algorithm ID: %#x\n", blk
->id
);
1139 adsp_dbg(dsp
, "Algorithm name: %.*s\n", blk
->name_len
, blk
->name
);
1140 adsp_dbg(dsp
, "# of coefficient descriptors: %#x\n", blk
->ncoeff
);
1143 static inline void wm_coeff_parse_coeff(struct wm_adsp
*dsp
, const u8
**data
,
1144 struct wm_coeff_parsed_coeff
*blk
)
1146 const struct wmfw_adsp_coeff_data
*raw
;
1150 switch (dsp
->fw_ver
) {
1153 raw
= (const struct wmfw_adsp_coeff_data
*)*data
;
1154 *data
= *data
+ sizeof(raw
->hdr
) + le32_to_cpu(raw
->hdr
.size
);
1156 blk
->offset
= le16_to_cpu(raw
->hdr
.offset
);
1157 blk
->mem_type
= le16_to_cpu(raw
->hdr
.type
);
1158 blk
->name
= raw
->name
;
1159 blk
->name_len
= strlen(raw
->name
);
1160 blk
->ctl_type
= le16_to_cpu(raw
->ctl_type
);
1161 blk
->flags
= le16_to_cpu(raw
->flags
);
1162 blk
->len
= le32_to_cpu(raw
->len
);
1166 blk
->offset
= wm_coeff_parse_int(sizeof(raw
->hdr
.offset
), &tmp
);
1167 blk
->mem_type
= wm_coeff_parse_int(sizeof(raw
->hdr
.type
), &tmp
);
1168 length
= wm_coeff_parse_int(sizeof(raw
->hdr
.size
), &tmp
);
1169 blk
->name_len
= wm_coeff_parse_string(sizeof(u8
), &tmp
,
1171 wm_coeff_parse_string(sizeof(u8
), &tmp
, NULL
);
1172 wm_coeff_parse_string(sizeof(u16
), &tmp
, NULL
);
1173 blk
->ctl_type
= wm_coeff_parse_int(sizeof(raw
->ctl_type
), &tmp
);
1174 blk
->flags
= wm_coeff_parse_int(sizeof(raw
->flags
), &tmp
);
1175 blk
->len
= wm_coeff_parse_int(sizeof(raw
->len
), &tmp
);
1177 *data
= *data
+ sizeof(raw
->hdr
) + length
;
1181 adsp_dbg(dsp
, "\tCoefficient type: %#x\n", blk
->mem_type
);
1182 adsp_dbg(dsp
, "\tCoefficient offset: %#x\n", blk
->offset
);
1183 adsp_dbg(dsp
, "\tCoefficient name: %.*s\n", blk
->name_len
, blk
->name
);
1184 adsp_dbg(dsp
, "\tCoefficient flags: %#x\n", blk
->flags
);
1185 adsp_dbg(dsp
, "\tALSA control type: %#x\n", blk
->ctl_type
);
1186 adsp_dbg(dsp
, "\tALSA control len: %#x\n", blk
->len
);
1189 static int wm_adsp_parse_coeff(struct wm_adsp
*dsp
,
1190 const struct wmfw_region
*region
)
1192 struct wm_adsp_alg_region alg_region
= {};
1193 struct wm_coeff_parsed_alg alg_blk
;
1194 struct wm_coeff_parsed_coeff coeff_blk
;
1195 const u8
*data
= region
->data
;
1198 wm_coeff_parse_alg(dsp
, &data
, &alg_blk
);
1199 for (i
= 0; i
< alg_blk
.ncoeff
; i
++) {
1200 wm_coeff_parse_coeff(dsp
, &data
, &coeff_blk
);
1202 switch (coeff_blk
.ctl_type
) {
1203 case SNDRV_CTL_ELEM_TYPE_BYTES
:
1206 adsp_err(dsp
, "Unknown control type: %d\n",
1207 coeff_blk
.ctl_type
);
1211 alg_region
.type
= coeff_blk
.mem_type
;
1212 alg_region
.alg
= alg_blk
.id
;
1214 ret
= wm_adsp_create_control(dsp
, &alg_region
,
1221 adsp_err(dsp
, "Failed to create control: %.*s, %d\n",
1222 coeff_blk
.name_len
, coeff_blk
.name
, ret
);
1228 static int wm_adsp_load(struct wm_adsp
*dsp
)
1230 LIST_HEAD(buf_list
);
1231 const struct firmware
*firmware
;
1232 struct regmap
*regmap
= dsp
->regmap
;
1233 unsigned int pos
= 0;
1234 const struct wmfw_header
*header
;
1235 const struct wmfw_adsp1_sizes
*adsp1_sizes
;
1236 const struct wmfw_adsp2_sizes
*adsp2_sizes
;
1237 const struct wmfw_footer
*footer
;
1238 const struct wmfw_region
*region
;
1239 const struct wm_adsp_region
*mem
;
1240 const char *region_name
;
1242 struct wm_adsp_buf
*buf
;
1245 int ret
, offset
, type
, sizes
;
1247 file
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
1251 snprintf(file
, PAGE_SIZE
, "%s-dsp%d-%s.wmfw", dsp
->part
, dsp
->num
,
1252 wm_adsp_fw
[dsp
->fw
].file
);
1253 file
[PAGE_SIZE
- 1] = '\0';
1255 ret
= request_firmware(&firmware
, file
, dsp
->dev
);
1257 adsp_err(dsp
, "Failed to request '%s'\n", file
);
1262 pos
= sizeof(*header
) + sizeof(*adsp1_sizes
) + sizeof(*footer
);
1263 if (pos
>= firmware
->size
) {
1264 adsp_err(dsp
, "%s: file too short, %zu bytes\n",
1265 file
, firmware
->size
);
1269 header
= (void *)&firmware
->data
[0];
1271 if (memcmp(&header
->magic
[0], "WMFW", 4) != 0) {
1272 adsp_err(dsp
, "%s: invalid magic\n", file
);
1276 switch (header
->ver
) {
1278 adsp_warn(dsp
, "%s: Depreciated file format %d\n",
1285 adsp_err(dsp
, "%s: unknown file format %d\n",
1290 adsp_info(dsp
, "Firmware version: %d\n", header
->ver
);
1291 dsp
->fw_ver
= header
->ver
;
1293 if (header
->core
!= dsp
->type
) {
1294 adsp_err(dsp
, "%s: invalid core %d != %d\n",
1295 file
, header
->core
, dsp
->type
);
1299 switch (dsp
->type
) {
1301 pos
= sizeof(*header
) + sizeof(*adsp1_sizes
) + sizeof(*footer
);
1302 adsp1_sizes
= (void *)&(header
[1]);
1303 footer
= (void *)&(adsp1_sizes
[1]);
1304 sizes
= sizeof(*adsp1_sizes
);
1306 adsp_dbg(dsp
, "%s: %d DM, %d PM, %d ZM\n",
1307 file
, le32_to_cpu(adsp1_sizes
->dm
),
1308 le32_to_cpu(adsp1_sizes
->pm
),
1309 le32_to_cpu(adsp1_sizes
->zm
));
1313 pos
= sizeof(*header
) + sizeof(*adsp2_sizes
) + sizeof(*footer
);
1314 adsp2_sizes
= (void *)&(header
[1]);
1315 footer
= (void *)&(adsp2_sizes
[1]);
1316 sizes
= sizeof(*adsp2_sizes
);
1318 adsp_dbg(dsp
, "%s: %d XM, %d YM %d PM, %d ZM\n",
1319 file
, le32_to_cpu(adsp2_sizes
->xm
),
1320 le32_to_cpu(adsp2_sizes
->ym
),
1321 le32_to_cpu(adsp2_sizes
->pm
),
1322 le32_to_cpu(adsp2_sizes
->zm
));
1326 WARN(1, "Unknown DSP type");
1330 if (le32_to_cpu(header
->len
) != sizeof(*header
) +
1331 sizes
+ sizeof(*footer
)) {
1332 adsp_err(dsp
, "%s: unexpected header length %d\n",
1333 file
, le32_to_cpu(header
->len
));
1337 adsp_dbg(dsp
, "%s: timestamp %llu\n", file
,
1338 le64_to_cpu(footer
->timestamp
));
1340 while (pos
< firmware
->size
&&
1341 pos
- firmware
->size
> sizeof(*region
)) {
1342 region
= (void *)&(firmware
->data
[pos
]);
1343 region_name
= "Unknown";
1346 offset
= le32_to_cpu(region
->offset
) & 0xffffff;
1347 type
= be32_to_cpu(region
->type
) & 0xff;
1348 mem
= wm_adsp_find_region(dsp
, type
);
1351 case WMFW_NAME_TEXT
:
1352 region_name
= "Firmware name";
1353 text
= kzalloc(le32_to_cpu(region
->len
) + 1,
1356 case WMFW_ALGORITHM_DATA
:
1357 region_name
= "Algorithm";
1358 ret
= wm_adsp_parse_coeff(dsp
, region
);
1362 case WMFW_INFO_TEXT
:
1363 region_name
= "Information";
1364 text
= kzalloc(le32_to_cpu(region
->len
) + 1,
1368 region_name
= "Absolute";
1373 reg
= wm_adsp_region_to_reg(mem
, offset
);
1377 reg
= wm_adsp_region_to_reg(mem
, offset
);
1381 reg
= wm_adsp_region_to_reg(mem
, offset
);
1385 reg
= wm_adsp_region_to_reg(mem
, offset
);
1389 reg
= wm_adsp_region_to_reg(mem
, offset
);
1393 "%s.%d: Unknown region type %x at %d(%x)\n",
1394 file
, regions
, type
, pos
, pos
);
1398 adsp_dbg(dsp
, "%s.%d: %d bytes at %d in %s\n", file
,
1399 regions
, le32_to_cpu(region
->len
), offset
,
1403 memcpy(text
, region
->data
, le32_to_cpu(region
->len
));
1404 adsp_info(dsp
, "%s: %s\n", file
, text
);
1409 buf
= wm_adsp_buf_alloc(region
->data
,
1410 le32_to_cpu(region
->len
),
1413 adsp_err(dsp
, "Out of memory\n");
1418 ret
= regmap_raw_write_async(regmap
, reg
, buf
->buf
,
1419 le32_to_cpu(region
->len
));
1422 "%s.%d: Failed to write %d bytes at %d in %s: %d\n",
1424 le32_to_cpu(region
->len
), offset
,
1430 pos
+= le32_to_cpu(region
->len
) + sizeof(*region
);
1434 ret
= regmap_async_complete(regmap
);
1436 adsp_err(dsp
, "Failed to complete async write: %d\n", ret
);
1440 if (pos
> firmware
->size
)
1441 adsp_warn(dsp
, "%s.%d: %zu bytes at end of file\n",
1442 file
, regions
, pos
- firmware
->size
);
1444 wm_adsp_debugfs_save_wmfwname(dsp
, file
);
1447 regmap_async_complete(regmap
);
1448 wm_adsp_buf_free(&buf_list
);
1449 release_firmware(firmware
);
1456 static void wm_adsp_ctl_fixup_base(struct wm_adsp
*dsp
,
1457 const struct wm_adsp_alg_region
*alg_region
)
1459 struct wm_coeff_ctl
*ctl
;
1461 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
1462 if (ctl
->fw_name
== wm_adsp_fw_text
[dsp
->fw
] &&
1463 alg_region
->alg
== ctl
->alg_region
.alg
&&
1464 alg_region
->type
== ctl
->alg_region
.type
) {
1465 ctl
->alg_region
.base
= alg_region
->base
;
1470 static void *wm_adsp_read_algs(struct wm_adsp
*dsp
, size_t n_algs
,
1471 unsigned int pos
, unsigned int len
)
1478 adsp_err(dsp
, "No algorithms\n");
1479 return ERR_PTR(-EINVAL
);
1482 if (n_algs
> 1024) {
1483 adsp_err(dsp
, "Algorithm count %zx excessive\n", n_algs
);
1484 return ERR_PTR(-EINVAL
);
1487 /* Read the terminator first to validate the length */
1488 ret
= regmap_raw_read(dsp
->regmap
, pos
+ len
, &val
, sizeof(val
));
1490 adsp_err(dsp
, "Failed to read algorithm list end: %d\n",
1492 return ERR_PTR(ret
);
1495 if (be32_to_cpu(val
) != 0xbedead)
1496 adsp_warn(dsp
, "Algorithm list end %x 0x%x != 0xbeadead\n",
1497 pos
+ len
, be32_to_cpu(val
));
1499 alg
= kzalloc(len
* 2, GFP_KERNEL
| GFP_DMA
);
1501 return ERR_PTR(-ENOMEM
);
1503 ret
= regmap_raw_read(dsp
->regmap
, pos
, alg
, len
* 2);
1505 adsp_err(dsp
, "Failed to read algorithm list: %d\n",
1508 return ERR_PTR(ret
);
1514 static struct wm_adsp_alg_region
*
1515 wm_adsp_find_alg_region(struct wm_adsp
*dsp
, int type
, unsigned int id
)
1517 struct wm_adsp_alg_region
*alg_region
;
1519 list_for_each_entry(alg_region
, &dsp
->alg_regions
, list
) {
1520 if (id
== alg_region
->alg
&& type
== alg_region
->type
)
1527 static struct wm_adsp_alg_region
*wm_adsp_create_region(struct wm_adsp
*dsp
,
1528 int type
, __be32 id
,
1531 struct wm_adsp_alg_region
*alg_region
;
1533 alg_region
= kzalloc(sizeof(*alg_region
), GFP_KERNEL
);
1535 return ERR_PTR(-ENOMEM
);
1537 alg_region
->type
= type
;
1538 alg_region
->alg
= be32_to_cpu(id
);
1539 alg_region
->base
= be32_to_cpu(base
);
1541 list_add_tail(&alg_region
->list
, &dsp
->alg_regions
);
1543 if (dsp
->fw_ver
> 0)
1544 wm_adsp_ctl_fixup_base(dsp
, alg_region
);
1549 static int wm_adsp1_setup_algs(struct wm_adsp
*dsp
)
1551 struct wmfw_adsp1_id_hdr adsp1_id
;
1552 struct wmfw_adsp1_alg_hdr
*adsp1_alg
;
1553 struct wm_adsp_alg_region
*alg_region
;
1554 const struct wm_adsp_region
*mem
;
1555 unsigned int pos
, len
;
1559 mem
= wm_adsp_find_region(dsp
, WMFW_ADSP1_DM
);
1563 ret
= regmap_raw_read(dsp
->regmap
, mem
->base
, &adsp1_id
,
1566 adsp_err(dsp
, "Failed to read algorithm info: %d\n",
1571 n_algs
= be32_to_cpu(adsp1_id
.n_algs
);
1572 dsp
->fw_id
= be32_to_cpu(adsp1_id
.fw
.id
);
1573 adsp_info(dsp
, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
1575 (be32_to_cpu(adsp1_id
.fw
.ver
) & 0xff0000) >> 16,
1576 (be32_to_cpu(adsp1_id
.fw
.ver
) & 0xff00) >> 8,
1577 be32_to_cpu(adsp1_id
.fw
.ver
) & 0xff,
1580 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP1_ZM
,
1581 adsp1_id
.fw
.id
, adsp1_id
.zm
);
1582 if (IS_ERR(alg_region
))
1583 return PTR_ERR(alg_region
);
1585 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP1_DM
,
1586 adsp1_id
.fw
.id
, adsp1_id
.dm
);
1587 if (IS_ERR(alg_region
))
1588 return PTR_ERR(alg_region
);
1590 pos
= sizeof(adsp1_id
) / 2;
1591 len
= (sizeof(*adsp1_alg
) * n_algs
) / 2;
1593 adsp1_alg
= wm_adsp_read_algs(dsp
, n_algs
, mem
->base
+ pos
, len
);
1594 if (IS_ERR(adsp1_alg
))
1595 return PTR_ERR(adsp1_alg
);
1597 for (i
= 0; i
< n_algs
; i
++) {
1598 adsp_info(dsp
, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
1599 i
, be32_to_cpu(adsp1_alg
[i
].alg
.id
),
1600 (be32_to_cpu(adsp1_alg
[i
].alg
.ver
) & 0xff0000) >> 16,
1601 (be32_to_cpu(adsp1_alg
[i
].alg
.ver
) & 0xff00) >> 8,
1602 be32_to_cpu(adsp1_alg
[i
].alg
.ver
) & 0xff,
1603 be32_to_cpu(adsp1_alg
[i
].dm
),
1604 be32_to_cpu(adsp1_alg
[i
].zm
));
1606 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP1_DM
,
1607 adsp1_alg
[i
].alg
.id
,
1609 if (IS_ERR(alg_region
)) {
1610 ret
= PTR_ERR(alg_region
);
1613 if (dsp
->fw_ver
== 0) {
1614 if (i
+ 1 < n_algs
) {
1615 len
= be32_to_cpu(adsp1_alg
[i
+ 1].dm
);
1616 len
-= be32_to_cpu(adsp1_alg
[i
].dm
);
1618 wm_adsp_create_control(dsp
, alg_region
, 0,
1621 adsp_warn(dsp
, "Missing length info for region DM with ID %x\n",
1622 be32_to_cpu(adsp1_alg
[i
].alg
.id
));
1626 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP1_ZM
,
1627 adsp1_alg
[i
].alg
.id
,
1629 if (IS_ERR(alg_region
)) {
1630 ret
= PTR_ERR(alg_region
);
1633 if (dsp
->fw_ver
== 0) {
1634 if (i
+ 1 < n_algs
) {
1635 len
= be32_to_cpu(adsp1_alg
[i
+ 1].zm
);
1636 len
-= be32_to_cpu(adsp1_alg
[i
].zm
);
1638 wm_adsp_create_control(dsp
, alg_region
, 0,
1641 adsp_warn(dsp
, "Missing length info for region ZM with ID %x\n",
1642 be32_to_cpu(adsp1_alg
[i
].alg
.id
));
1652 static int wm_adsp2_setup_algs(struct wm_adsp
*dsp
)
1654 struct wmfw_adsp2_id_hdr adsp2_id
;
1655 struct wmfw_adsp2_alg_hdr
*adsp2_alg
;
1656 struct wm_adsp_alg_region
*alg_region
;
1657 const struct wm_adsp_region
*mem
;
1658 unsigned int pos
, len
;
1662 mem
= wm_adsp_find_region(dsp
, WMFW_ADSP2_XM
);
1666 ret
= regmap_raw_read(dsp
->regmap
, mem
->base
, &adsp2_id
,
1669 adsp_err(dsp
, "Failed to read algorithm info: %d\n",
1674 n_algs
= be32_to_cpu(adsp2_id
.n_algs
);
1675 dsp
->fw_id
= be32_to_cpu(adsp2_id
.fw
.id
);
1676 dsp
->fw_id_version
= be32_to_cpu(adsp2_id
.fw
.ver
);
1677 adsp_info(dsp
, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
1679 (dsp
->fw_id_version
& 0xff0000) >> 16,
1680 (dsp
->fw_id_version
& 0xff00) >> 8,
1681 dsp
->fw_id_version
& 0xff,
1684 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP2_XM
,
1685 adsp2_id
.fw
.id
, adsp2_id
.xm
);
1686 if (IS_ERR(alg_region
))
1687 return PTR_ERR(alg_region
);
1689 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP2_YM
,
1690 adsp2_id
.fw
.id
, adsp2_id
.ym
);
1691 if (IS_ERR(alg_region
))
1692 return PTR_ERR(alg_region
);
1694 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP2_ZM
,
1695 adsp2_id
.fw
.id
, adsp2_id
.zm
);
1696 if (IS_ERR(alg_region
))
1697 return PTR_ERR(alg_region
);
1699 pos
= sizeof(adsp2_id
) / 2;
1700 len
= (sizeof(*adsp2_alg
) * n_algs
) / 2;
1702 adsp2_alg
= wm_adsp_read_algs(dsp
, n_algs
, mem
->base
+ pos
, len
);
1703 if (IS_ERR(adsp2_alg
))
1704 return PTR_ERR(adsp2_alg
);
1706 for (i
= 0; i
< n_algs
; i
++) {
1708 "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
1709 i
, be32_to_cpu(adsp2_alg
[i
].alg
.id
),
1710 (be32_to_cpu(adsp2_alg
[i
].alg
.ver
) & 0xff0000) >> 16,
1711 (be32_to_cpu(adsp2_alg
[i
].alg
.ver
) & 0xff00) >> 8,
1712 be32_to_cpu(adsp2_alg
[i
].alg
.ver
) & 0xff,
1713 be32_to_cpu(adsp2_alg
[i
].xm
),
1714 be32_to_cpu(adsp2_alg
[i
].ym
),
1715 be32_to_cpu(adsp2_alg
[i
].zm
));
1717 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP2_XM
,
1718 adsp2_alg
[i
].alg
.id
,
1720 if (IS_ERR(alg_region
)) {
1721 ret
= PTR_ERR(alg_region
);
1724 if (dsp
->fw_ver
== 0) {
1725 if (i
+ 1 < n_algs
) {
1726 len
= be32_to_cpu(adsp2_alg
[i
+ 1].xm
);
1727 len
-= be32_to_cpu(adsp2_alg
[i
].xm
);
1729 wm_adsp_create_control(dsp
, alg_region
, 0,
1732 adsp_warn(dsp
, "Missing length info for region XM with ID %x\n",
1733 be32_to_cpu(adsp2_alg
[i
].alg
.id
));
1737 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP2_YM
,
1738 adsp2_alg
[i
].alg
.id
,
1740 if (IS_ERR(alg_region
)) {
1741 ret
= PTR_ERR(alg_region
);
1744 if (dsp
->fw_ver
== 0) {
1745 if (i
+ 1 < n_algs
) {
1746 len
= be32_to_cpu(adsp2_alg
[i
+ 1].ym
);
1747 len
-= be32_to_cpu(adsp2_alg
[i
].ym
);
1749 wm_adsp_create_control(dsp
, alg_region
, 0,
1752 adsp_warn(dsp
, "Missing length info for region YM with ID %x\n",
1753 be32_to_cpu(adsp2_alg
[i
].alg
.id
));
1757 alg_region
= wm_adsp_create_region(dsp
, WMFW_ADSP2_ZM
,
1758 adsp2_alg
[i
].alg
.id
,
1760 if (IS_ERR(alg_region
)) {
1761 ret
= PTR_ERR(alg_region
);
1764 if (dsp
->fw_ver
== 0) {
1765 if (i
+ 1 < n_algs
) {
1766 len
= be32_to_cpu(adsp2_alg
[i
+ 1].zm
);
1767 len
-= be32_to_cpu(adsp2_alg
[i
].zm
);
1769 wm_adsp_create_control(dsp
, alg_region
, 0,
1772 adsp_warn(dsp
, "Missing length info for region ZM with ID %x\n",
1773 be32_to_cpu(adsp2_alg
[i
].alg
.id
));
1783 static int wm_adsp_load_coeff(struct wm_adsp
*dsp
)
1785 LIST_HEAD(buf_list
);
1786 struct regmap
*regmap
= dsp
->regmap
;
1787 struct wmfw_coeff_hdr
*hdr
;
1788 struct wmfw_coeff_item
*blk
;
1789 const struct firmware
*firmware
;
1790 const struct wm_adsp_region
*mem
;
1791 struct wm_adsp_alg_region
*alg_region
;
1792 const char *region_name
;
1793 int ret
, pos
, blocks
, type
, offset
, reg
;
1795 struct wm_adsp_buf
*buf
;
1797 file
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
1801 snprintf(file
, PAGE_SIZE
, "%s-dsp%d-%s.bin", dsp
->part
, dsp
->num
,
1802 wm_adsp_fw
[dsp
->fw
].file
);
1803 file
[PAGE_SIZE
- 1] = '\0';
1805 ret
= request_firmware(&firmware
, file
, dsp
->dev
);
1807 adsp_warn(dsp
, "Failed to request '%s'\n", file
);
1813 if (sizeof(*hdr
) >= firmware
->size
) {
1814 adsp_err(dsp
, "%s: file too short, %zu bytes\n",
1815 file
, firmware
->size
);
1819 hdr
= (void *)&firmware
->data
[0];
1820 if (memcmp(hdr
->magic
, "WMDR", 4) != 0) {
1821 adsp_err(dsp
, "%s: invalid magic\n", file
);
1825 switch (be32_to_cpu(hdr
->rev
) & 0xff) {
1829 adsp_err(dsp
, "%s: Unsupported coefficient file format %d\n",
1830 file
, be32_to_cpu(hdr
->rev
) & 0xff);
1835 adsp_dbg(dsp
, "%s: v%d.%d.%d\n", file
,
1836 (le32_to_cpu(hdr
->ver
) >> 16) & 0xff,
1837 (le32_to_cpu(hdr
->ver
) >> 8) & 0xff,
1838 le32_to_cpu(hdr
->ver
) & 0xff);
1840 pos
= le32_to_cpu(hdr
->len
);
1843 while (pos
< firmware
->size
&&
1844 pos
- firmware
->size
> sizeof(*blk
)) {
1845 blk
= (void *)(&firmware
->data
[pos
]);
1847 type
= le16_to_cpu(blk
->type
);
1848 offset
= le16_to_cpu(blk
->offset
);
1850 adsp_dbg(dsp
, "%s.%d: %x v%d.%d.%d\n",
1851 file
, blocks
, le32_to_cpu(blk
->id
),
1852 (le32_to_cpu(blk
->ver
) >> 16) & 0xff,
1853 (le32_to_cpu(blk
->ver
) >> 8) & 0xff,
1854 le32_to_cpu(blk
->ver
) & 0xff);
1855 adsp_dbg(dsp
, "%s.%d: %d bytes at 0x%x in %x\n",
1856 file
, blocks
, le32_to_cpu(blk
->len
), offset
, type
);
1859 region_name
= "Unknown";
1861 case (WMFW_NAME_TEXT
<< 8):
1862 case (WMFW_INFO_TEXT
<< 8):
1864 case (WMFW_ABSOLUTE
<< 8):
1866 * Old files may use this for global
1869 if (le32_to_cpu(blk
->id
) == dsp
->fw_id
&&
1871 region_name
= "global coefficients";
1872 mem
= wm_adsp_find_region(dsp
, type
);
1874 adsp_err(dsp
, "No ZM\n");
1877 reg
= wm_adsp_region_to_reg(mem
, 0);
1880 region_name
= "register";
1889 adsp_dbg(dsp
, "%s.%d: %d bytes in %x for %x\n",
1890 file
, blocks
, le32_to_cpu(blk
->len
),
1891 type
, le32_to_cpu(blk
->id
));
1893 mem
= wm_adsp_find_region(dsp
, type
);
1895 adsp_err(dsp
, "No base for region %x\n", type
);
1899 alg_region
= wm_adsp_find_alg_region(dsp
, type
,
1900 le32_to_cpu(blk
->id
));
1902 reg
= alg_region
->base
;
1903 reg
= wm_adsp_region_to_reg(mem
, reg
);
1906 adsp_err(dsp
, "No %x for algorithm %x\n",
1907 type
, le32_to_cpu(blk
->id
));
1912 adsp_err(dsp
, "%s.%d: Unknown region type %x at %d\n",
1913 file
, blocks
, type
, pos
);
1918 buf
= wm_adsp_buf_alloc(blk
->data
,
1919 le32_to_cpu(blk
->len
),
1922 adsp_err(dsp
, "Out of memory\n");
1927 adsp_dbg(dsp
, "%s.%d: Writing %d bytes at %x\n",
1928 file
, blocks
, le32_to_cpu(blk
->len
),
1930 ret
= regmap_raw_write_async(regmap
, reg
, buf
->buf
,
1931 le32_to_cpu(blk
->len
));
1934 "%s.%d: Failed to write to %x in %s: %d\n",
1935 file
, blocks
, reg
, region_name
, ret
);
1939 pos
+= (le32_to_cpu(blk
->len
) + sizeof(*blk
) + 3) & ~0x03;
1943 ret
= regmap_async_complete(regmap
);
1945 adsp_err(dsp
, "Failed to complete async write: %d\n", ret
);
1947 if (pos
> firmware
->size
)
1948 adsp_warn(dsp
, "%s.%d: %zu bytes at end of file\n",
1949 file
, blocks
, pos
- firmware
->size
);
1951 wm_adsp_debugfs_save_binname(dsp
, file
);
1954 regmap_async_complete(regmap
);
1955 release_firmware(firmware
);
1956 wm_adsp_buf_free(&buf_list
);
1962 int wm_adsp1_init(struct wm_adsp
*dsp
)
1964 INIT_LIST_HEAD(&dsp
->alg_regions
);
1966 mutex_init(&dsp
->pwr_lock
);
1970 EXPORT_SYMBOL_GPL(wm_adsp1_init
);
1972 int wm_adsp1_event(struct snd_soc_dapm_widget
*w
,
1973 struct snd_kcontrol
*kcontrol
,
1976 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(w
->dapm
);
1977 struct wm_adsp
*dsps
= snd_soc_codec_get_drvdata(codec
);
1978 struct wm_adsp
*dsp
= &dsps
[w
->shift
];
1979 struct wm_adsp_alg_region
*alg_region
;
1980 struct wm_coeff_ctl
*ctl
;
1984 dsp
->card
= codec
->component
.card
;
1986 mutex_lock(&dsp
->pwr_lock
);
1989 case SND_SOC_DAPM_POST_PMU
:
1990 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
1991 ADSP1_SYS_ENA
, ADSP1_SYS_ENA
);
1994 * For simplicity set the DSP clock rate to be the
1995 * SYSCLK rate rather than making it configurable.
1997 if (dsp
->sysclk_reg
) {
1998 ret
= regmap_read(dsp
->regmap
, dsp
->sysclk_reg
, &val
);
2000 adsp_err(dsp
, "Failed to read SYSCLK state: %d\n",
2005 val
= (val
& dsp
->sysclk_mask
)
2006 >> dsp
->sysclk_shift
;
2008 ret
= regmap_update_bits(dsp
->regmap
,
2009 dsp
->base
+ ADSP1_CONTROL_31
,
2010 ADSP1_CLK_SEL_MASK
, val
);
2012 adsp_err(dsp
, "Failed to set clock rate: %d\n",
2018 ret
= wm_adsp_load(dsp
);
2022 ret
= wm_adsp1_setup_algs(dsp
);
2026 ret
= wm_adsp_load_coeff(dsp
);
2030 /* Initialize caches for enabled and unset controls */
2031 ret
= wm_coeff_init_control_caches(dsp
);
2035 /* Sync set controls */
2036 ret
= wm_coeff_sync_controls(dsp
);
2040 /* Start the core running */
2041 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
2042 ADSP1_CORE_ENA
| ADSP1_START
,
2043 ADSP1_CORE_ENA
| ADSP1_START
);
2046 case SND_SOC_DAPM_PRE_PMD
:
2048 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
2049 ADSP1_CORE_ENA
| ADSP1_START
, 0);
2051 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_19
,
2052 ADSP1_WDMA_BUFFER_LENGTH_MASK
, 0);
2054 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
2057 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
)
2060 while (!list_empty(&dsp
->alg_regions
)) {
2061 alg_region
= list_first_entry(&dsp
->alg_regions
,
2062 struct wm_adsp_alg_region
,
2064 list_del(&alg_region
->list
);
2073 mutex_unlock(&dsp
->pwr_lock
);
2078 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
2081 mutex_unlock(&dsp
->pwr_lock
);
2085 EXPORT_SYMBOL_GPL(wm_adsp1_event
);
2087 static int wm_adsp2_ena(struct wm_adsp
*dsp
)
2092 ret
= regmap_update_bits_async(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2093 ADSP2_SYS_ENA
, ADSP2_SYS_ENA
);
2097 /* Wait for the RAM to start, should be near instantaneous */
2098 for (count
= 0; count
< 10; ++count
) {
2099 ret
= regmap_read(dsp
->regmap
, dsp
->base
+ ADSP2_STATUS1
,
2104 if (val
& ADSP2_RAM_RDY
)
2110 if (!(val
& ADSP2_RAM_RDY
)) {
2111 adsp_err(dsp
, "Failed to start DSP RAM\n");
2115 adsp_dbg(dsp
, "RAM ready after %d polls\n", count
);
2120 static void wm_adsp2_boot_work(struct work_struct
*work
)
2122 struct wm_adsp
*dsp
= container_of(work
,
2128 mutex_lock(&dsp
->pwr_lock
);
2131 * For simplicity set the DSP clock rate to be the
2132 * SYSCLK rate rather than making it configurable.
2134 ret
= regmap_read(dsp
->regmap
, ARIZONA_SYSTEM_CLOCK_1
, &val
);
2136 adsp_err(dsp
, "Failed to read SYSCLK state: %d\n", ret
);
2139 val
= (val
& ARIZONA_SYSCLK_FREQ_MASK
)
2140 >> ARIZONA_SYSCLK_FREQ_SHIFT
;
2142 ret
= regmap_update_bits_async(dsp
->regmap
,
2143 dsp
->base
+ ADSP2_CLOCKING
,
2144 ADSP2_CLK_SEL_MASK
, val
);
2146 adsp_err(dsp
, "Failed to set clock rate: %d\n", ret
);
2150 ret
= wm_adsp2_ena(dsp
);
2154 ret
= wm_adsp_load(dsp
);
2158 ret
= wm_adsp2_setup_algs(dsp
);
2162 ret
= wm_adsp_load_coeff(dsp
);
2166 /* Initialize caches for enabled and unset controls */
2167 ret
= wm_coeff_init_control_caches(dsp
);
2171 /* Sync set controls */
2172 ret
= wm_coeff_sync_controls(dsp
);
2176 dsp
->running
= true;
2178 mutex_unlock(&dsp
->pwr_lock
);
2183 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2184 ADSP2_SYS_ENA
| ADSP2_CORE_ENA
| ADSP2_START
, 0);
2186 mutex_unlock(&dsp
->pwr_lock
);
2189 int wm_adsp2_early_event(struct snd_soc_dapm_widget
*w
,
2190 struct snd_kcontrol
*kcontrol
, int event
)
2192 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(w
->dapm
);
2193 struct wm_adsp
*dsps
= snd_soc_codec_get_drvdata(codec
);
2194 struct wm_adsp
*dsp
= &dsps
[w
->shift
];
2196 dsp
->card
= codec
->component
.card
;
2199 case SND_SOC_DAPM_PRE_PMU
:
2200 queue_work(system_unbound_wq
, &dsp
->boot_work
);
2208 EXPORT_SYMBOL_GPL(wm_adsp2_early_event
);
2210 int wm_adsp2_event(struct snd_soc_dapm_widget
*w
,
2211 struct snd_kcontrol
*kcontrol
, int event
)
2213 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(w
->dapm
);
2214 struct wm_adsp
*dsps
= snd_soc_codec_get_drvdata(codec
);
2215 struct wm_adsp
*dsp
= &dsps
[w
->shift
];
2216 struct wm_adsp_alg_region
*alg_region
;
2217 struct wm_coeff_ctl
*ctl
;
2221 case SND_SOC_DAPM_POST_PMU
:
2222 flush_work(&dsp
->boot_work
);
2227 ret
= regmap_update_bits(dsp
->regmap
,
2228 dsp
->base
+ ADSP2_CONTROL
,
2229 ADSP2_CORE_ENA
| ADSP2_START
,
2230 ADSP2_CORE_ENA
| ADSP2_START
);
2234 if (wm_adsp_fw
[dsp
->fw
].num_caps
!= 0)
2235 ret
= wm_adsp_buffer_init(dsp
);
2239 case SND_SOC_DAPM_PRE_PMD
:
2240 /* Log firmware state, it can be useful for analysis */
2241 wm_adsp2_show_fw_status(dsp
);
2243 mutex_lock(&dsp
->pwr_lock
);
2245 wm_adsp_debugfs_clear(dsp
);
2248 dsp
->fw_id_version
= 0;
2249 dsp
->running
= false;
2251 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2252 ADSP2_SYS_ENA
| ADSP2_CORE_ENA
|
2255 /* Make sure DMAs are quiesced */
2256 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_WDMA_CONFIG_1
, 0);
2257 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_WDMA_CONFIG_2
, 0);
2258 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_RDMA_CONFIG_1
, 0);
2260 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
)
2263 while (!list_empty(&dsp
->alg_regions
)) {
2264 alg_region
= list_first_entry(&dsp
->alg_regions
,
2265 struct wm_adsp_alg_region
,
2267 list_del(&alg_region
->list
);
2271 if (wm_adsp_fw
[dsp
->fw
].num_caps
!= 0)
2272 wm_adsp_buffer_free(dsp
);
2274 mutex_unlock(&dsp
->pwr_lock
);
2276 adsp_dbg(dsp
, "Shutdown complete\n");
2285 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2286 ADSP2_SYS_ENA
| ADSP2_CORE_ENA
| ADSP2_START
, 0);
2289 EXPORT_SYMBOL_GPL(wm_adsp2_event
);
2291 int wm_adsp2_codec_probe(struct wm_adsp
*dsp
, struct snd_soc_codec
*codec
)
2293 wm_adsp2_init_debugfs(dsp
, codec
);
2295 return snd_soc_add_codec_controls(codec
,
2296 &wm_adsp_fw_controls
[dsp
->num
- 1],
2299 EXPORT_SYMBOL_GPL(wm_adsp2_codec_probe
);
2301 int wm_adsp2_codec_remove(struct wm_adsp
*dsp
, struct snd_soc_codec
*codec
)
2303 wm_adsp2_cleanup_debugfs(dsp
);
2307 EXPORT_SYMBOL_GPL(wm_adsp2_codec_remove
);
2309 int wm_adsp2_init(struct wm_adsp
*dsp
)
2314 * Disable the DSP memory by default when in reset for a small
2317 ret
= regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2320 adsp_err(dsp
, "Failed to clear memory retention: %d\n", ret
);
2324 INIT_LIST_HEAD(&dsp
->alg_regions
);
2325 INIT_LIST_HEAD(&dsp
->ctl_list
);
2326 INIT_WORK(&dsp
->boot_work
, wm_adsp2_boot_work
);
2328 mutex_init(&dsp
->pwr_lock
);
2332 EXPORT_SYMBOL_GPL(wm_adsp2_init
);
2334 int wm_adsp_compr_open(struct wm_adsp
*dsp
, struct snd_compr_stream
*stream
)
2336 struct wm_adsp_compr
*compr
;
2339 mutex_lock(&dsp
->pwr_lock
);
2341 if (wm_adsp_fw
[dsp
->fw
].num_caps
== 0) {
2342 adsp_err(dsp
, "Firmware does not support compressed API\n");
2347 if (wm_adsp_fw
[dsp
->fw
].compr_direction
!= stream
->direction
) {
2348 adsp_err(dsp
, "Firmware does not support stream direction\n");
2354 /* It is expect this limitation will be removed in future */
2355 adsp_err(dsp
, "Only a single stream supported per DSP\n");
2360 compr
= kzalloc(sizeof(*compr
), GFP_KERNEL
);
2367 compr
->stream
= stream
;
2371 stream
->runtime
->private_data
= compr
;
2374 mutex_unlock(&dsp
->pwr_lock
);
2378 EXPORT_SYMBOL_GPL(wm_adsp_compr_open
);
2380 int wm_adsp_compr_free(struct snd_compr_stream
*stream
)
2382 struct wm_adsp_compr
*compr
= stream
->runtime
->private_data
;
2383 struct wm_adsp
*dsp
= compr
->dsp
;
2385 mutex_lock(&dsp
->pwr_lock
);
2389 kfree(compr
->raw_buf
);
2392 mutex_unlock(&dsp
->pwr_lock
);
2396 EXPORT_SYMBOL_GPL(wm_adsp_compr_free
);
2398 static int wm_adsp_compr_check_params(struct snd_compr_stream
*stream
,
2399 struct snd_compr_params
*params
)
2401 struct wm_adsp_compr
*compr
= stream
->runtime
->private_data
;
2402 struct wm_adsp
*dsp
= compr
->dsp
;
2403 const struct wm_adsp_fw_caps
*caps
;
2404 const struct snd_codec_desc
*desc
;
2407 if (params
->buffer
.fragment_size
< WM_ADSP_MIN_FRAGMENT_SIZE
||
2408 params
->buffer
.fragment_size
> WM_ADSP_MAX_FRAGMENT_SIZE
||
2409 params
->buffer
.fragments
< WM_ADSP_MIN_FRAGMENTS
||
2410 params
->buffer
.fragments
> WM_ADSP_MAX_FRAGMENTS
||
2411 params
->buffer
.fragment_size
% WM_ADSP_DATA_WORD_SIZE
) {
2412 adsp_err(dsp
, "Invalid buffer fragsize=%d fragments=%d\n",
2413 params
->buffer
.fragment_size
,
2414 params
->buffer
.fragments
);
2419 for (i
= 0; i
< wm_adsp_fw
[dsp
->fw
].num_caps
; i
++) {
2420 caps
= &wm_adsp_fw
[dsp
->fw
].caps
[i
];
2423 if (caps
->id
!= params
->codec
.id
)
2426 if (stream
->direction
== SND_COMPRESS_PLAYBACK
) {
2427 if (desc
->max_ch
< params
->codec
.ch_out
)
2430 if (desc
->max_ch
< params
->codec
.ch_in
)
2434 if (!(desc
->formats
& (1 << params
->codec
.format
)))
2437 for (j
= 0; j
< desc
->num_sample_rates
; ++j
)
2438 if (desc
->sample_rates
[j
] == params
->codec
.sample_rate
)
2442 adsp_err(dsp
, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n",
2443 params
->codec
.id
, params
->codec
.ch_in
, params
->codec
.ch_out
,
2444 params
->codec
.sample_rate
, params
->codec
.format
);
2448 static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr
*compr
)
2450 return compr
->size
.fragment_size
/ WM_ADSP_DATA_WORD_SIZE
;
2453 int wm_adsp_compr_set_params(struct snd_compr_stream
*stream
,
2454 struct snd_compr_params
*params
)
2456 struct wm_adsp_compr
*compr
= stream
->runtime
->private_data
;
2460 ret
= wm_adsp_compr_check_params(stream
, params
);
2464 compr
->size
= params
->buffer
;
2466 adsp_dbg(compr
->dsp
, "fragment_size=%d fragments=%d\n",
2467 compr
->size
.fragment_size
, compr
->size
.fragments
);
2469 size
= wm_adsp_compr_frag_words(compr
) * sizeof(*compr
->raw_buf
);
2470 compr
->raw_buf
= kmalloc(size
, GFP_DMA
| GFP_KERNEL
);
2471 if (!compr
->raw_buf
)
2476 EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params
);
2478 int wm_adsp_compr_get_caps(struct snd_compr_stream
*stream
,
2479 struct snd_compr_caps
*caps
)
2481 struct wm_adsp_compr
*compr
= stream
->runtime
->private_data
;
2482 int fw
= compr
->dsp
->fw
;
2485 if (wm_adsp_fw
[fw
].caps
) {
2486 for (i
= 0; i
< wm_adsp_fw
[fw
].num_caps
; i
++)
2487 caps
->codecs
[i
] = wm_adsp_fw
[fw
].caps
[i
].id
;
2489 caps
->num_codecs
= i
;
2490 caps
->direction
= wm_adsp_fw
[fw
].compr_direction
;
2492 caps
->min_fragment_size
= WM_ADSP_MIN_FRAGMENT_SIZE
;
2493 caps
->max_fragment_size
= WM_ADSP_MAX_FRAGMENT_SIZE
;
2494 caps
->min_fragments
= WM_ADSP_MIN_FRAGMENTS
;
2495 caps
->max_fragments
= WM_ADSP_MAX_FRAGMENTS
;
2500 EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps
);
2502 static int wm_adsp_read_data_block(struct wm_adsp
*dsp
, int mem_type
,
2503 unsigned int mem_addr
,
2504 unsigned int num_words
, u32
*data
)
2506 struct wm_adsp_region
const *mem
= wm_adsp_find_region(dsp
, mem_type
);
2507 unsigned int i
, reg
;
2513 reg
= wm_adsp_region_to_reg(mem
, mem_addr
);
2515 ret
= regmap_raw_read(dsp
->regmap
, reg
, data
,
2516 sizeof(*data
) * num_words
);
2520 for (i
= 0; i
< num_words
; ++i
)
2521 data
[i
] = be32_to_cpu(data
[i
]) & 0x00ffffffu
;
2526 static inline int wm_adsp_read_data_word(struct wm_adsp
*dsp
, int mem_type
,
2527 unsigned int mem_addr
, u32
*data
)
2529 return wm_adsp_read_data_block(dsp
, mem_type
, mem_addr
, 1, data
);
2532 static int wm_adsp_write_data_word(struct wm_adsp
*dsp
, int mem_type
,
2533 unsigned int mem_addr
, u32 data
)
2535 struct wm_adsp_region
const *mem
= wm_adsp_find_region(dsp
, mem_type
);
2541 reg
= wm_adsp_region_to_reg(mem
, mem_addr
);
2543 data
= cpu_to_be32(data
& 0x00ffffffu
);
2545 return regmap_raw_write(dsp
->regmap
, reg
, &data
, sizeof(data
));
2548 static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf
*buf
,
2549 unsigned int field_offset
, u32
*data
)
2551 return wm_adsp_read_data_word(buf
->dsp
, WMFW_ADSP2_XM
,
2552 buf
->host_buf_ptr
+ field_offset
, data
);
2555 static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf
*buf
,
2556 unsigned int field_offset
, u32 data
)
2558 return wm_adsp_write_data_word(buf
->dsp
, WMFW_ADSP2_XM
,
2559 buf
->host_buf_ptr
+ field_offset
, data
);
2562 static int wm_adsp_buffer_locate(struct wm_adsp_compr_buf
*buf
)
2564 struct wm_adsp_alg_region
*alg_region
;
2565 struct wm_adsp
*dsp
= buf
->dsp
;
2566 u32 xmalg
, addr
, magic
;
2569 alg_region
= wm_adsp_find_alg_region(dsp
, WMFW_ADSP2_XM
, dsp
->fw_id
);
2570 xmalg
= sizeof(struct wm_adsp_system_config_xm_hdr
) / sizeof(__be32
);
2572 addr
= alg_region
->base
+ xmalg
+ ALG_XM_FIELD(magic
);
2573 ret
= wm_adsp_read_data_word(dsp
, WMFW_ADSP2_XM
, addr
, &magic
);
2577 if (magic
!= WM_ADSP_ALG_XM_STRUCT_MAGIC
)
2580 addr
= alg_region
->base
+ xmalg
+ ALG_XM_FIELD(host_buf_ptr
);
2581 for (i
= 0; i
< 5; ++i
) {
2582 ret
= wm_adsp_read_data_word(dsp
, WMFW_ADSP2_XM
, addr
,
2583 &buf
->host_buf_ptr
);
2587 if (buf
->host_buf_ptr
)
2590 usleep_range(1000, 2000);
2593 if (!buf
->host_buf_ptr
)
2596 adsp_dbg(dsp
, "host_buf_ptr=%x\n", buf
->host_buf_ptr
);
2601 static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf
*buf
)
2603 const struct wm_adsp_fw_caps
*caps
= wm_adsp_fw
[buf
->dsp
->fw
].caps
;
2604 struct wm_adsp_buffer_region
*region
;
2608 for (i
= 0; i
< caps
->num_regions
; ++i
) {
2609 region
= &buf
->regions
[i
];
2611 region
->offset
= offset
;
2612 region
->mem_type
= caps
->region_defs
[i
].mem_type
;
2614 ret
= wm_adsp_buffer_read(buf
, caps
->region_defs
[i
].base_offset
,
2615 ®ion
->base_addr
);
2619 ret
= wm_adsp_buffer_read(buf
, caps
->region_defs
[i
].size_offset
,
2624 region
->cumulative_size
= offset
;
2627 "region=%d type=%d base=%04x off=%04x size=%04x\n",
2628 i
, region
->mem_type
, region
->base_addr
,
2629 region
->offset
, region
->cumulative_size
);
2635 static int wm_adsp_buffer_init(struct wm_adsp
*dsp
)
2637 struct wm_adsp_compr_buf
*buf
;
2640 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
2645 buf
->read_index
= -1;
2646 buf
->irq_count
= 0xFFFFFFFF;
2648 ret
= wm_adsp_buffer_locate(buf
);
2650 adsp_err(dsp
, "Failed to acquire host buffer: %d\n", ret
);
2654 buf
->regions
= kcalloc(wm_adsp_fw
[dsp
->fw
].caps
->num_regions
,
2655 sizeof(*buf
->regions
), GFP_KERNEL
);
2656 if (!buf
->regions
) {
2661 ret
= wm_adsp_buffer_populate(buf
);
2663 adsp_err(dsp
, "Failed to populate host buffer: %d\n", ret
);
2672 kfree(buf
->regions
);
2678 static int wm_adsp_buffer_free(struct wm_adsp
*dsp
)
2681 kfree(dsp
->buffer
->regions
);
2690 static inline int wm_adsp_compr_attached(struct wm_adsp_compr
*compr
)
2692 return compr
->buf
!= NULL
;
2695 static int wm_adsp_compr_attach(struct wm_adsp_compr
*compr
)
2698 * Note this will be more complex once each DSP can support multiple
2701 if (!compr
->dsp
->buffer
)
2704 compr
->buf
= compr
->dsp
->buffer
;
2709 int wm_adsp_compr_trigger(struct snd_compr_stream
*stream
, int cmd
)
2711 struct wm_adsp_compr
*compr
= stream
->runtime
->private_data
;
2712 struct wm_adsp
*dsp
= compr
->dsp
;
2715 adsp_dbg(dsp
, "Trigger: %d\n", cmd
);
2717 mutex_lock(&dsp
->pwr_lock
);
2720 case SNDRV_PCM_TRIGGER_START
:
2721 if (wm_adsp_compr_attached(compr
))
2724 ret
= wm_adsp_compr_attach(compr
);
2726 adsp_err(dsp
, "Failed to link buffer and stream: %d\n",
2731 /* Trigger the IRQ at one fragment of data */
2732 ret
= wm_adsp_buffer_write(compr
->buf
,
2733 HOST_BUFFER_FIELD(high_water_mark
),
2734 wm_adsp_compr_frag_words(compr
));
2736 adsp_err(dsp
, "Failed to set high water mark: %d\n",
2741 case SNDRV_PCM_TRIGGER_STOP
:
2748 mutex_unlock(&dsp
->pwr_lock
);
2752 EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger
);
2754 static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf
*buf
)
2756 int last_region
= wm_adsp_fw
[buf
->dsp
->fw
].caps
->num_regions
- 1;
2758 return buf
->regions
[last_region
].cumulative_size
;
2761 static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf
*buf
)
2763 u32 next_read_index
, next_write_index
;
2764 int write_index
, read_index
, avail
;
2767 /* Only sync read index if we haven't already read a valid index */
2768 if (buf
->read_index
< 0) {
2769 ret
= wm_adsp_buffer_read(buf
,
2770 HOST_BUFFER_FIELD(next_read_index
),
2775 read_index
= sign_extend32(next_read_index
, 23);
2777 if (read_index
< 0) {
2778 adsp_dbg(buf
->dsp
, "Avail check on unstarted stream\n");
2782 buf
->read_index
= read_index
;
2785 ret
= wm_adsp_buffer_read(buf
, HOST_BUFFER_FIELD(next_write_index
),
2790 write_index
= sign_extend32(next_write_index
, 23);
2792 avail
= write_index
- buf
->read_index
;
2794 avail
+= wm_adsp_buffer_size(buf
);
2796 adsp_dbg(buf
->dsp
, "readindex=0x%x, writeindex=0x%x, avail=%d\n",
2797 buf
->read_index
, write_index
, avail
);
2804 int wm_adsp_compr_handle_irq(struct wm_adsp
*dsp
)
2806 struct wm_adsp_compr_buf
*buf
= dsp
->buffer
;
2807 struct wm_adsp_compr
*compr
= dsp
->compr
;
2810 mutex_lock(&dsp
->pwr_lock
);
2813 adsp_err(dsp
, "Spurious buffer IRQ\n");
2818 adsp_dbg(dsp
, "Handling buffer IRQ\n");
2820 ret
= wm_adsp_buffer_read(buf
, HOST_BUFFER_FIELD(error
), &buf
->error
);
2822 adsp_err(dsp
, "Failed to check buffer error: %d\n", ret
);
2825 if (buf
->error
!= 0) {
2826 adsp_err(dsp
, "Buffer error occurred: %d\n", buf
->error
);
2831 ret
= wm_adsp_buffer_read(buf
, HOST_BUFFER_FIELD(irq_count
),
2834 adsp_err(dsp
, "Failed to get irq_count: %d\n", ret
);
2838 ret
= wm_adsp_buffer_update_avail(buf
);
2840 adsp_err(dsp
, "Error reading avail: %d\n", ret
);
2845 snd_compr_fragment_elapsed(compr
->stream
);
2848 mutex_unlock(&dsp
->pwr_lock
);
2852 EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq
);
2854 static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf
*buf
)
2856 if (buf
->irq_count
& 0x01)
2859 adsp_dbg(buf
->dsp
, "Enable IRQ(0x%x) for next fragment\n",
2862 buf
->irq_count
|= 0x01;
2864 return wm_adsp_buffer_write(buf
, HOST_BUFFER_FIELD(irq_ack
),
2868 int wm_adsp_compr_pointer(struct snd_compr_stream
*stream
,
2869 struct snd_compr_tstamp
*tstamp
)
2871 struct wm_adsp_compr
*compr
= stream
->runtime
->private_data
;
2872 struct wm_adsp_compr_buf
*buf
= compr
->buf
;
2873 struct wm_adsp
*dsp
= compr
->dsp
;
2876 adsp_dbg(dsp
, "Pointer request\n");
2878 mutex_lock(&dsp
->pwr_lock
);
2885 if (compr
->buf
->error
) {
2890 if (buf
->avail
< wm_adsp_compr_frag_words(compr
)) {
2891 ret
= wm_adsp_buffer_update_avail(buf
);
2893 adsp_err(dsp
, "Error reading avail: %d\n", ret
);
2898 * If we really have less than 1 fragment available tell the
2899 * DSP to inform us once a whole fragment is available.
2901 if (buf
->avail
< wm_adsp_compr_frag_words(compr
)) {
2902 ret
= wm_adsp_buffer_reenable_irq(buf
);
2905 "Failed to re-enable buffer IRQ: %d\n",
2912 tstamp
->copied_total
= compr
->copied_total
;
2913 tstamp
->copied_total
+= buf
->avail
* WM_ADSP_DATA_WORD_SIZE
;
2916 mutex_unlock(&dsp
->pwr_lock
);
2920 EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer
);
2922 static int wm_adsp_buffer_capture_block(struct wm_adsp_compr
*compr
, int target
)
2924 struct wm_adsp_compr_buf
*buf
= compr
->buf
;
2925 u8
*pack_in
= (u8
*)compr
->raw_buf
;
2926 u8
*pack_out
= (u8
*)compr
->raw_buf
;
2927 unsigned int adsp_addr
;
2928 int mem_type
, nwords
, max_read
;
2931 /* Calculate read parameters */
2932 for (i
= 0; i
< wm_adsp_fw
[buf
->dsp
->fw
].caps
->num_regions
; ++i
)
2933 if (buf
->read_index
< buf
->regions
[i
].cumulative_size
)
2936 if (i
== wm_adsp_fw
[buf
->dsp
->fw
].caps
->num_regions
)
2939 mem_type
= buf
->regions
[i
].mem_type
;
2940 adsp_addr
= buf
->regions
[i
].base_addr
+
2941 (buf
->read_index
- buf
->regions
[i
].offset
);
2943 max_read
= wm_adsp_compr_frag_words(compr
);
2944 nwords
= buf
->regions
[i
].cumulative_size
- buf
->read_index
;
2946 if (nwords
> target
)
2948 if (nwords
> buf
->avail
)
2949 nwords
= buf
->avail
;
2950 if (nwords
> max_read
)
2955 /* Read data from DSP */
2956 ret
= wm_adsp_read_data_block(buf
->dsp
, mem_type
, adsp_addr
,
2957 nwords
, compr
->raw_buf
);
2961 /* Remove the padding bytes from the data read from the DSP */
2962 for (i
= 0; i
< nwords
; i
++) {
2963 for (j
= 0; j
< WM_ADSP_DATA_WORD_SIZE
; j
++)
2964 *pack_out
++ = *pack_in
++;
2966 pack_in
+= sizeof(*(compr
->raw_buf
)) - WM_ADSP_DATA_WORD_SIZE
;
2969 /* update read index to account for words read */
2970 buf
->read_index
+= nwords
;
2971 if (buf
->read_index
== wm_adsp_buffer_size(buf
))
2972 buf
->read_index
= 0;
2974 ret
= wm_adsp_buffer_write(buf
, HOST_BUFFER_FIELD(next_read_index
),
2979 /* update avail to account for words read */
2980 buf
->avail
-= nwords
;
2985 static int wm_adsp_compr_read(struct wm_adsp_compr
*compr
,
2986 char __user
*buf
, size_t count
)
2988 struct wm_adsp
*dsp
= compr
->dsp
;
2992 adsp_dbg(dsp
, "Requested read of %zu bytes\n", count
);
2997 if (compr
->buf
->error
)
3000 count
/= WM_ADSP_DATA_WORD_SIZE
;
3003 nwords
= wm_adsp_buffer_capture_block(compr
, count
);
3005 adsp_err(dsp
, "Failed to capture block: %d\n", nwords
);
3009 nbytes
= nwords
* WM_ADSP_DATA_WORD_SIZE
;
3011 adsp_dbg(dsp
, "Read %d bytes\n", nbytes
);
3013 if (copy_to_user(buf
+ ntotal
, compr
->raw_buf
, nbytes
)) {
3014 adsp_err(dsp
, "Failed to copy data to user: %d, %d\n",
3021 } while (nwords
> 0 && count
> 0);
3023 compr
->copied_total
+= ntotal
;
3028 int wm_adsp_compr_copy(struct snd_compr_stream
*stream
, char __user
*buf
,
3031 struct wm_adsp_compr
*compr
= stream
->runtime
->private_data
;
3032 struct wm_adsp
*dsp
= compr
->dsp
;
3035 mutex_lock(&dsp
->pwr_lock
);
3037 if (stream
->direction
== SND_COMPRESS_CAPTURE
)
3038 ret
= wm_adsp_compr_read(compr
, buf
, count
);
3042 mutex_unlock(&dsp
->pwr_lock
);
3046 EXPORT_SYMBOL_GPL(wm_adsp_compr_copy
);
3048 MODULE_LICENSE("GPL v2");