1 /*
2 * wm_adsp.c -- Wolfson ADSP support
3 *
4 * Copyright 2012 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
11 */
12
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>
19 #include <linux/pm.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>
34
35 #include "wm_adsp.h"
36
37 #define adsp_crit(_dsp, fmt, ...) \
38 dev_crit(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
39 #define adsp_err(_dsp, fmt, ...) \
40 dev_err(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
41 #define adsp_warn(_dsp, fmt, ...) \
42 dev_warn(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
43 #define adsp_info(_dsp, fmt, ...) \
44 dev_info(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
45 #define adsp_dbg(_dsp, fmt, ...) \
46 dev_dbg(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
47
48 #define ADSP1_CONTROL_1 0x00
49 #define ADSP1_CONTROL_2 0x02
50 #define ADSP1_CONTROL_3 0x03
51 #define ADSP1_CONTROL_4 0x04
52 #define ADSP1_CONTROL_5 0x06
53 #define ADSP1_CONTROL_6 0x07
54 #define ADSP1_CONTROL_7 0x08
55 #define ADSP1_CONTROL_8 0x09
56 #define ADSP1_CONTROL_9 0x0A
57 #define ADSP1_CONTROL_10 0x0B
58 #define ADSP1_CONTROL_11 0x0C
59 #define ADSP1_CONTROL_12 0x0D
60 #define ADSP1_CONTROL_13 0x0F
61 #define ADSP1_CONTROL_14 0x10
62 #define ADSP1_CONTROL_15 0x11
63 #define ADSP1_CONTROL_16 0x12
64 #define ADSP1_CONTROL_17 0x13
65 #define ADSP1_CONTROL_18 0x14
66 #define ADSP1_CONTROL_19 0x16
67 #define ADSP1_CONTROL_20 0x17
68 #define ADSP1_CONTROL_21 0x18
69 #define ADSP1_CONTROL_22 0x1A
70 #define ADSP1_CONTROL_23 0x1B
71 #define ADSP1_CONTROL_24 0x1C
72 #define ADSP1_CONTROL_25 0x1E
73 #define ADSP1_CONTROL_26 0x20
74 #define ADSP1_CONTROL_27 0x21
75 #define ADSP1_CONTROL_28 0x22
76 #define ADSP1_CONTROL_29 0x23
77 #define ADSP1_CONTROL_30 0x24
78 #define ADSP1_CONTROL_31 0x26
79
80 /*
81 * ADSP1 Control 19
82 */
83 #define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
84 #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
85 #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
86
87
88 /*
89 * ADSP1 Control 30
90 */
91 #define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */
92 #define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */
93 #define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */
94 #define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */
95 #define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
96 #define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
97 #define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
98 #define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
99 #define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
100 #define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
101 #define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
102 #define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
103 #define ADSP1_START 0x0001 /* DSP1_START */
104 #define ADSP1_START_MASK 0x0001 /* DSP1_START */
105 #define ADSP1_START_SHIFT 0 /* DSP1_START */
106 #define ADSP1_START_WIDTH 1 /* DSP1_START */
107
108 /*
109 * ADSP1 Control 31
110 */
111 #define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
112 #define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
113 #define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
114
115 #define ADSP2_CONTROL 0x0
116 #define ADSP2_CLOCKING 0x1
117 #define ADSP2V2_CLOCKING 0x2
118 #define ADSP2_STATUS1 0x4
119 #define ADSP2_WDMA_CONFIG_1 0x30
120 #define ADSP2_WDMA_CONFIG_2 0x31
121 #define ADSP2V2_WDMA_CONFIG_2 0x32
122 #define ADSP2_RDMA_CONFIG_1 0x34
123
124 #define ADSP2_SCRATCH0 0x40
125 #define ADSP2_SCRATCH1 0x41
126 #define ADSP2_SCRATCH2 0x42
127 #define ADSP2_SCRATCH3 0x43
128
129 #define ADSP2V2_SCRATCH0_1 0x40
130 #define ADSP2V2_SCRATCH2_3 0x42
131
132 /*
133 * ADSP2 Control
134 */
135
136 #define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */
137 #define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */
138 #define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */
139 #define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */
140 #define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
141 #define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
142 #define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
143 #define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
144 #define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
145 #define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
146 #define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
147 #define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
148 #define ADSP2_START 0x0001 /* DSP1_START */
149 #define ADSP2_START_MASK 0x0001 /* DSP1_START */
150 #define ADSP2_START_SHIFT 0 /* DSP1_START */
151 #define ADSP2_START_WIDTH 1 /* DSP1_START */
152
153 /*
154 * ADSP2 clocking
155 */
156 #define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
157 #define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
158 #define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
159
160 /*
161 * ADSP2V2 clocking
162 */
163 #define ADSP2V2_CLK_SEL_MASK 0x70000 /* CLK_SEL_ENA */
164 #define ADSP2V2_CLK_SEL_SHIFT 16 /* CLK_SEL_ENA */
165 #define ADSP2V2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
166
167 #define ADSP2V2_RATE_MASK 0x7800 /* DSP_RATE */
168 #define ADSP2V2_RATE_SHIFT 11 /* DSP_RATE */
169 #define ADSP2V2_RATE_WIDTH 4 /* DSP_RATE */
170
171 /*
172 * ADSP2 Status 1
173 */
174 #define ADSP2_RAM_RDY 0x0001
175 #define ADSP2_RAM_RDY_MASK 0x0001
176 #define ADSP2_RAM_RDY_SHIFT 0
177 #define ADSP2_RAM_RDY_WIDTH 1
178
179 /*
180 * ADSP2 Lock support
181 */
182 #define ADSP2_LOCK_CODE_0 0x5555
183 #define ADSP2_LOCK_CODE_1 0xAAAA
184
185 #define ADSP2_WATCHDOG 0x0A
186 #define ADSP2_BUS_ERR_ADDR 0x52
187 #define ADSP2_REGION_LOCK_STATUS 0x64
188 #define ADSP2_LOCK_REGION_1_LOCK_REGION_0 0x66
189 #define ADSP2_LOCK_REGION_3_LOCK_REGION_2 0x68
190 #define ADSP2_LOCK_REGION_5_LOCK_REGION_4 0x6A
191 #define ADSP2_LOCK_REGION_7_LOCK_REGION_6 0x6C
192 #define ADSP2_LOCK_REGION_9_LOCK_REGION_8 0x6E
193 #define ADSP2_LOCK_REGION_CTRL 0x7A
194 #define ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR 0x7C
195
196 #define ADSP2_REGION_LOCK_ERR_MASK 0x8000
197 #define ADSP2_SLAVE_ERR_MASK 0x4000
198 #define ADSP2_WDT_TIMEOUT_STS_MASK 0x2000
199 #define ADSP2_CTRL_ERR_PAUSE_ENA 0x0002
200 #define ADSP2_CTRL_ERR_EINT 0x0001
201
202 #define ADSP2_BUS_ERR_ADDR_MASK 0x00FFFFFF
203 #define ADSP2_XMEM_ERR_ADDR_MASK 0x0000FFFF
204 #define ADSP2_PMEM_ERR_ADDR_MASK 0x7FFF0000
205 #define ADSP2_PMEM_ERR_ADDR_SHIFT 16
206 #define ADSP2_WDT_ENA_MASK 0xFFFFFFFD
207
208 #define ADSP2_LOCK_REGION_SHIFT 16
209
210 #define ADSP_MAX_STD_CTRL_SIZE 512
211
212 #define WM_ADSP_ACKED_CTL_TIMEOUT_MS 100
213 #define WM_ADSP_ACKED_CTL_N_QUICKPOLLS 10
214 #define WM_ADSP_ACKED_CTL_MIN_VALUE 0
215 #define WM_ADSP_ACKED_CTL_MAX_VALUE 0xFFFFFF
216
217 /*
218 * Event control messages
219 */
220 #define WM_ADSP_FW_EVENT_SHUTDOWN 0x000001
221
222 struct wm_adsp_buf {
223 struct list_head list;
224 void *buf;
225 };
226
wm_adsp_buf_alloc(const void * src,size_t len,struct list_head * list)227 static struct wm_adsp_buf *wm_adsp_buf_alloc(const void *src, size_t len,
228 struct list_head *list)
229 {
230 struct wm_adsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL);
231
232 if (buf == NULL)
233 return NULL;
234
235 buf->buf = vmalloc(len);
236 if (!buf->buf) {
237 kfree(buf);
238 return NULL;
239 }
240 memcpy(buf->buf, src, len);
241
242 if (list)
243 list_add_tail(&buf->list, list);
244
245 return buf;
246 }
247
wm_adsp_buf_free(struct list_head * list)248 static void wm_adsp_buf_free(struct list_head *list)
249 {
250 while (!list_empty(list)) {
251 struct wm_adsp_buf *buf = list_first_entry(list,
252 struct wm_adsp_buf,
253 list);
254 list_del(&buf->list);
255 vfree(buf->buf);
256 kfree(buf);
257 }
258 }
259
260 #define WM_ADSP_FW_MBC_VSS 0
261 #define WM_ADSP_FW_HIFI 1
262 #define WM_ADSP_FW_TX 2
263 #define WM_ADSP_FW_TX_SPK 3
264 #define WM_ADSP_FW_RX 4
265 #define WM_ADSP_FW_RX_ANC 5
266 #define WM_ADSP_FW_CTRL 6
267 #define WM_ADSP_FW_ASR 7
268 #define WM_ADSP_FW_TRACE 8
269 #define WM_ADSP_FW_SPK_PROT 9
270 #define WM_ADSP_FW_MISC 10
271
272 #define WM_ADSP_NUM_FW 11
273
274 static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = {
275 [WM_ADSP_FW_MBC_VSS] = "MBC/VSS",
276 [WM_ADSP_FW_HIFI] = "MasterHiFi",
277 [WM_ADSP_FW_TX] = "Tx",
278 [WM_ADSP_FW_TX_SPK] = "Tx Speaker",
279 [WM_ADSP_FW_RX] = "Rx",
280 [WM_ADSP_FW_RX_ANC] = "Rx ANC",
281 [WM_ADSP_FW_CTRL] = "Voice Ctrl",
282 [WM_ADSP_FW_ASR] = "ASR Assist",
283 [WM_ADSP_FW_TRACE] = "Dbg Trace",
284 [WM_ADSP_FW_SPK_PROT] = "Protection",
285 [WM_ADSP_FW_MISC] = "Misc",
286 };
287
288 struct wm_adsp_system_config_xm_hdr {
289 __be32 sys_enable;
290 __be32 fw_id;
291 __be32 fw_rev;
292 __be32 boot_status;
293 __be32 watchdog;
294 __be32 dma_buffer_size;
295 __be32 rdma[6];
296 __be32 wdma[8];
297 __be32 build_job_name[3];
298 __be32 build_job_number;
299 };
300
301 struct wm_adsp_alg_xm_struct {
302 __be32 magic;
303 __be32 smoothing;
304 __be32 threshold;
305 __be32 host_buf_ptr;
306 __be32 start_seq;
307 __be32 high_water_mark;
308 __be32 low_water_mark;
309 __be64 smoothed_power;
310 };
311
312 struct wm_adsp_buffer {
313 __be32 X_buf_base; /* XM base addr of first X area */
314 __be32 X_buf_size; /* Size of 1st X area in words */
315 __be32 X_buf_base2; /* XM base addr of 2nd X area */
316 __be32 X_buf_brk; /* Total X size in words */
317 __be32 Y_buf_base; /* YM base addr of Y area */
318 __be32 wrap; /* Total size X and Y in words */
319 __be32 high_water_mark; /* Point at which IRQ is asserted */
320 __be32 irq_count; /* bits 1-31 count IRQ assertions */
321 __be32 irq_ack; /* acked IRQ count, bit 0 enables IRQ */
322 __be32 next_write_index; /* word index of next write */
323 __be32 next_read_index; /* word index of next read */
324 __be32 error; /* error if any */
325 __be32 oldest_block_index; /* word index of oldest surviving */
326 __be32 requested_rewind; /* how many blocks rewind was done */
327 __be32 reserved_space; /* internal */
328 __be32 min_free; /* min free space since stream start */
329 __be32 blocks_written[2]; /* total blocks written (64 bit) */
330 __be32 words_written[2]; /* total words written (64 bit) */
331 };
332
333 struct wm_adsp_compr;
334
335 struct wm_adsp_compr_buf {
336 struct wm_adsp *dsp;
337 struct wm_adsp_compr *compr;
338
339 struct wm_adsp_buffer_region *regions;
340 u32 host_buf_ptr;
341
342 u32 error;
343 u32 irq_count;
344 int read_index;
345 int avail;
346 };
347
348 struct wm_adsp_compr {
349 struct wm_adsp *dsp;
350 struct wm_adsp_compr_buf *buf;
351
352 struct snd_compr_stream *stream;
353 struct snd_compressed_buffer size;
354
355 u32 *raw_buf;
356 unsigned int copied_total;
357
358 unsigned int sample_rate;
359 };
360
361 #define WM_ADSP_DATA_WORD_SIZE 3
362
363 #define WM_ADSP_MIN_FRAGMENTS 1
364 #define WM_ADSP_MAX_FRAGMENTS 256
365 #define WM_ADSP_MIN_FRAGMENT_SIZE (64 * WM_ADSP_DATA_WORD_SIZE)
366 #define WM_ADSP_MAX_FRAGMENT_SIZE (4096 * WM_ADSP_DATA_WORD_SIZE)
367
368 #define WM_ADSP_ALG_XM_STRUCT_MAGIC 0x49aec7
369
370 #define HOST_BUFFER_FIELD(field) \
371 (offsetof(struct wm_adsp_buffer, field) / sizeof(__be32))
372
373 #define ALG_XM_FIELD(field) \
374 (offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32))
375
376 static int wm_adsp_buffer_init(struct wm_adsp *dsp);
377 static int wm_adsp_buffer_free(struct wm_adsp *dsp);
378
379 struct wm_adsp_buffer_region {
380 unsigned int offset;
381 unsigned int cumulative_size;
382 unsigned int mem_type;
383 unsigned int base_addr;
384 };
385
386 struct wm_adsp_buffer_region_def {
387 unsigned int mem_type;
388 unsigned int base_offset;
389 unsigned int size_offset;
390 };
391
392 static const struct wm_adsp_buffer_region_def default_regions[] = {
393 {
394 .mem_type = WMFW_ADSP2_XM,
395 .base_offset = HOST_BUFFER_FIELD(X_buf_base),
396 .size_offset = HOST_BUFFER_FIELD(X_buf_size),
397 },
398 {
399 .mem_type = WMFW_ADSP2_XM,
400 .base_offset = HOST_BUFFER_FIELD(X_buf_base2),
401 .size_offset = HOST_BUFFER_FIELD(X_buf_brk),
402 },
403 {
404 .mem_type = WMFW_ADSP2_YM,
405 .base_offset = HOST_BUFFER_FIELD(Y_buf_base),
406 .size_offset = HOST_BUFFER_FIELD(wrap),
407 },
408 };
409
410 struct wm_adsp_fw_caps {
411 u32 id;
412 struct snd_codec_desc desc;
413 int num_regions;
414 const struct wm_adsp_buffer_region_def *region_defs;
415 };
416
417 static const struct wm_adsp_fw_caps ctrl_caps[] = {
418 {
419 .id = SND_AUDIOCODEC_BESPOKE,
420 .desc = {
421 .max_ch = 1,
422 .sample_rates = { 16000 },
423 .num_sample_rates = 1,
424 .formats = SNDRV_PCM_FMTBIT_S16_LE,
425 },
426 .num_regions = ARRAY_SIZE(default_regions),
427 .region_defs = default_regions,
428 },
429 };
430
431 static const struct wm_adsp_fw_caps trace_caps[] = {
432 {
433 .id = SND_AUDIOCODEC_BESPOKE,
434 .desc = {
435 .max_ch = 8,
436 .sample_rates = {
437 4000, 8000, 11025, 12000, 16000, 22050,
438 24000, 32000, 44100, 48000, 64000, 88200,
439 96000, 176400, 192000
440 },
441 .num_sample_rates = 15,
442 .formats = SNDRV_PCM_FMTBIT_S16_LE,
443 },
444 .num_regions = ARRAY_SIZE(default_regions),
445 .region_defs = default_regions,
446 },
447 };
448
449 static const struct {
450 const char *file;
451 int compr_direction;
452 int num_caps;
453 const struct wm_adsp_fw_caps *caps;
454 bool voice_trigger;
455 } wm_adsp_fw[WM_ADSP_NUM_FW] = {
456 [WM_ADSP_FW_MBC_VSS] = { .file = "mbc-vss" },
457 [WM_ADSP_FW_HIFI] = { .file = "hifi" },
458 [WM_ADSP_FW_TX] = { .file = "tx" },
459 [WM_ADSP_FW_TX_SPK] = { .file = "tx-spk" },
460 [WM_ADSP_FW_RX] = { .file = "rx" },
461 [WM_ADSP_FW_RX_ANC] = { .file = "rx-anc" },
462 [WM_ADSP_FW_CTRL] = {
463 .file = "ctrl",
464 .compr_direction = SND_COMPRESS_CAPTURE,
465 .num_caps = ARRAY_SIZE(ctrl_caps),
466 .caps = ctrl_caps,
467 .voice_trigger = true,
468 },
469 [WM_ADSP_FW_ASR] = { .file = "asr" },
470 [WM_ADSP_FW_TRACE] = {
471 .file = "trace",
472 .compr_direction = SND_COMPRESS_CAPTURE,
473 .num_caps = ARRAY_SIZE(trace_caps),
474 .caps = trace_caps,
475 },
476 [WM_ADSP_FW_SPK_PROT] = { .file = "spk-prot" },
477 [WM_ADSP_FW_MISC] = { .file = "misc" },
478 };
479
480 struct wm_coeff_ctl_ops {
481 int (*xget)(struct snd_kcontrol *kcontrol,
482 struct snd_ctl_elem_value *ucontrol);
483 int (*xput)(struct snd_kcontrol *kcontrol,
484 struct snd_ctl_elem_value *ucontrol);
485 };
486
487 struct wm_coeff_ctl {
488 const char *name;
489 const char *fw_name;
490 struct wm_adsp_alg_region alg_region;
491 struct wm_coeff_ctl_ops ops;
492 struct wm_adsp *dsp;
493 unsigned int enabled:1;
494 struct list_head list;
495 void *cache;
496 unsigned int offset;
497 size_t len;
498 unsigned int set:1;
499 struct soc_bytes_ext bytes_ext;
500 unsigned int flags;
501 unsigned int type;
502 };
503
wm_adsp_mem_region_name(unsigned int type)504 static const char *wm_adsp_mem_region_name(unsigned int type)
505 {
506 switch (type) {
507 case WMFW_ADSP1_PM:
508 return "PM";
509 case WMFW_ADSP1_DM:
510 return "DM";
511 case WMFW_ADSP2_XM:
512 return "XM";
513 case WMFW_ADSP2_YM:
514 return "YM";
515 case WMFW_ADSP1_ZM:
516 return "ZM";
517 default:
518 return NULL;
519 }
520 }
521
522 #ifdef CONFIG_DEBUG_FS
wm_adsp_debugfs_save_wmfwname(struct wm_adsp * dsp,const char * s)523 static void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, const char *s)
524 {
525 char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
526
527 kfree(dsp->wmfw_file_name);
528 dsp->wmfw_file_name = tmp;
529 }
530
wm_adsp_debugfs_save_binname(struct wm_adsp * dsp,const char * s)531 static void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, const char *s)
532 {
533 char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
534
535 kfree(dsp->bin_file_name);
536 dsp->bin_file_name = tmp;
537 }
538
wm_adsp_debugfs_clear(struct wm_adsp * dsp)539 static void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
540 {
541 kfree(dsp->wmfw_file_name);
542 kfree(dsp->bin_file_name);
543 dsp->wmfw_file_name = NULL;
544 dsp->bin_file_name = NULL;
545 }
546
wm_adsp_debugfs_wmfw_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)547 static ssize_t wm_adsp_debugfs_wmfw_read(struct file *file,
548 char __user *user_buf,
549 size_t count, loff_t *ppos)
550 {
551 struct wm_adsp *dsp = file->private_data;
552 ssize_t ret;
553
554 mutex_lock(&dsp->pwr_lock);
555
556 if (!dsp->wmfw_file_name || !dsp->booted)
557 ret = 0;
558 else
559 ret = simple_read_from_buffer(user_buf, count, ppos,
560 dsp->wmfw_file_name,
561 strlen(dsp->wmfw_file_name));
562
563 mutex_unlock(&dsp->pwr_lock);
564 return ret;
565 }
566
wm_adsp_debugfs_bin_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)567 static ssize_t wm_adsp_debugfs_bin_read(struct file *file,
568 char __user *user_buf,
569 size_t count, loff_t *ppos)
570 {
571 struct wm_adsp *dsp = file->private_data;
572 ssize_t ret;
573
574 mutex_lock(&dsp->pwr_lock);
575
576 if (!dsp->bin_file_name || !dsp->booted)
577 ret = 0;
578 else
579 ret = simple_read_from_buffer(user_buf, count, ppos,
580 dsp->bin_file_name,
581 strlen(dsp->bin_file_name));
582
583 mutex_unlock(&dsp->pwr_lock);
584 return ret;
585 }
586
587 static const struct {
588 const char *name;
589 const struct file_operations fops;
590 } wm_adsp_debugfs_fops[] = {
591 {
592 .name = "wmfw_file_name",
593 .fops = {
594 .open = simple_open,
595 .read = wm_adsp_debugfs_wmfw_read,
596 },
597 },
598 {
599 .name = "bin_file_name",
600 .fops = {
601 .open = simple_open,
602 .read = wm_adsp_debugfs_bin_read,
603 },
604 },
605 };
606
wm_adsp2_init_debugfs(struct wm_adsp * dsp,struct snd_soc_codec * codec)607 static void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
608 struct snd_soc_codec *codec)
609 {
610 struct dentry *root = NULL;
611 char *root_name;
612 int i;
613
614 if (!codec->component.debugfs_root) {
615 adsp_err(dsp, "No codec debugfs root\n");
616 goto err;
617 }
618
619 root_name = kmalloc(PAGE_SIZE, GFP_KERNEL);
620 if (!root_name)
621 goto err;
622
623 snprintf(root_name, PAGE_SIZE, "dsp%d", dsp->num);
624 root = debugfs_create_dir(root_name, codec->component.debugfs_root);
625 kfree(root_name);
626
627 if (!root)
628 goto err;
629
630 if (!debugfs_create_bool("booted", S_IRUGO, root, &dsp->booted))
631 goto err;
632
633 if (!debugfs_create_bool("running", S_IRUGO, root, &dsp->running))
634 goto err;
635
636 if (!debugfs_create_x32("fw_id", S_IRUGO, root, &dsp->fw_id))
637 goto err;
638
639 if (!debugfs_create_x32("fw_version", S_IRUGO, root,
640 &dsp->fw_id_version))
641 goto err;
642
643 for (i = 0; i < ARRAY_SIZE(wm_adsp_debugfs_fops); ++i) {
644 if (!debugfs_create_file(wm_adsp_debugfs_fops[i].name,
645 S_IRUGO, root, dsp,
646 &wm_adsp_debugfs_fops[i].fops))
647 goto err;
648 }
649
650 dsp->debugfs_root = root;
651 return;
652
653 err:
654 debugfs_remove_recursive(root);
655 adsp_err(dsp, "Failed to create debugfs\n");
656 }
657
wm_adsp2_cleanup_debugfs(struct wm_adsp * dsp)658 static void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
659 {
660 wm_adsp_debugfs_clear(dsp);
661 debugfs_remove_recursive(dsp->debugfs_root);
662 }
663 #else
wm_adsp2_init_debugfs(struct wm_adsp * dsp,struct snd_soc_codec * codec)664 static inline void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
665 struct snd_soc_codec *codec)
666 {
667 }
668
wm_adsp2_cleanup_debugfs(struct wm_adsp * dsp)669 static inline void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
670 {
671 }
672
wm_adsp_debugfs_save_wmfwname(struct wm_adsp * dsp,const char * s)673 static inline void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp,
674 const char *s)
675 {
676 }
677
wm_adsp_debugfs_save_binname(struct wm_adsp * dsp,const char * s)678 static inline void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp,
679 const char *s)
680 {
681 }
682
wm_adsp_debugfs_clear(struct wm_adsp * dsp)683 static inline void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
684 {
685 }
686 #endif
687
wm_adsp_fw_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)688 static int wm_adsp_fw_get(struct snd_kcontrol *kcontrol,
689 struct snd_ctl_elem_value *ucontrol)
690 {
691 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
692 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
693 struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
694
695 ucontrol->value.enumerated.item[0] = dsp[e->shift_l].fw;
696
697 return 0;
698 }
699
wm_adsp_fw_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)700 static int wm_adsp_fw_put(struct snd_kcontrol *kcontrol,
701 struct snd_ctl_elem_value *ucontrol)
702 {
703 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
704 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
705 struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
706 int ret = 0;
707
708 if (ucontrol->value.enumerated.item[0] == dsp[e->shift_l].fw)
709 return 0;
710
711 if (ucontrol->value.enumerated.item[0] >= WM_ADSP_NUM_FW)
712 return -EINVAL;
713
714 mutex_lock(&dsp[e->shift_l].pwr_lock);
715
716 if (dsp[e->shift_l].booted || dsp[e->shift_l].compr)
717 ret = -EBUSY;
718 else
719 dsp[e->shift_l].fw = ucontrol->value.enumerated.item[0];
720
721 mutex_unlock(&dsp[e->shift_l].pwr_lock);
722
723 return ret;
724 }
725
726 static const struct soc_enum wm_adsp_fw_enum[] = {
727 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
728 SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
729 SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
730 SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
731 SOC_ENUM_SINGLE(0, 4, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
732 SOC_ENUM_SINGLE(0, 5, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
733 SOC_ENUM_SINGLE(0, 6, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
734 };
735
736 const struct snd_kcontrol_new wm_adsp_fw_controls[] = {
737 SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum[0],
738 wm_adsp_fw_get, wm_adsp_fw_put),
739 SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum[1],
740 wm_adsp_fw_get, wm_adsp_fw_put),
741 SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum[2],
742 wm_adsp_fw_get, wm_adsp_fw_put),
743 SOC_ENUM_EXT("DSP4 Firmware", wm_adsp_fw_enum[3],
744 wm_adsp_fw_get, wm_adsp_fw_put),
745 SOC_ENUM_EXT("DSP5 Firmware", wm_adsp_fw_enum[4],
746 wm_adsp_fw_get, wm_adsp_fw_put),
747 SOC_ENUM_EXT("DSP6 Firmware", wm_adsp_fw_enum[5],
748 wm_adsp_fw_get, wm_adsp_fw_put),
749 SOC_ENUM_EXT("DSP7 Firmware", wm_adsp_fw_enum[6],
750 wm_adsp_fw_get, wm_adsp_fw_put),
751 };
752 EXPORT_SYMBOL_GPL(wm_adsp_fw_controls);
753
wm_adsp_find_region(struct wm_adsp * dsp,int type)754 static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp,
755 int type)
756 {
757 int i;
758
759 for (i = 0; i < dsp->num_mems; i++)
760 if (dsp->mem[i].type == type)
761 return &dsp->mem[i];
762
763 return NULL;
764 }
765
wm_adsp_region_to_reg(struct wm_adsp_region const * mem,unsigned int offset)766 static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *mem,
767 unsigned int offset)
768 {
769 if (WARN_ON(!mem))
770 return offset;
771 switch (mem->type) {
772 case WMFW_ADSP1_PM:
773 return mem->base + (offset * 3);
774 case WMFW_ADSP1_DM:
775 return mem->base + (offset * 2);
776 case WMFW_ADSP2_XM:
777 return mem->base + (offset * 2);
778 case WMFW_ADSP2_YM:
779 return mem->base + (offset * 2);
780 case WMFW_ADSP1_ZM:
781 return mem->base + (offset * 2);
782 default:
783 WARN(1, "Unknown memory region type");
784 return offset;
785 }
786 }
787
wm_adsp2_show_fw_status(struct wm_adsp * dsp)788 static void wm_adsp2_show_fw_status(struct wm_adsp *dsp)
789 {
790 unsigned int scratch[4];
791 unsigned int addr = dsp->base + ADSP2_SCRATCH0;
792 unsigned int i;
793 int ret;
794
795 for (i = 0; i < ARRAY_SIZE(scratch); ++i) {
796 ret = regmap_read(dsp->regmap, addr + i, &scratch[i]);
797 if (ret) {
798 adsp_err(dsp, "Failed to read SCRATCH%u: %d\n", i, ret);
799 return;
800 }
801 }
802
803 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
804 scratch[0], scratch[1], scratch[2], scratch[3]);
805 }
806
wm_adsp2v2_show_fw_status(struct wm_adsp * dsp)807 static void wm_adsp2v2_show_fw_status(struct wm_adsp *dsp)
808 {
809 unsigned int scratch[2];
810 int ret;
811
812 ret = regmap_read(dsp->regmap, dsp->base + ADSP2V2_SCRATCH0_1,
813 &scratch[0]);
814 if (ret) {
815 adsp_err(dsp, "Failed to read SCRATCH0_1: %d\n", ret);
816 return;
817 }
818
819 ret = regmap_read(dsp->regmap, dsp->base + ADSP2V2_SCRATCH2_3,
820 &scratch[1]);
821 if (ret) {
822 adsp_err(dsp, "Failed to read SCRATCH2_3: %d\n", ret);
823 return;
824 }
825
826 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
827 scratch[0] & 0xFFFF,
828 scratch[0] >> 16,
829 scratch[1] & 0xFFFF,
830 scratch[1] >> 16);
831 }
832
bytes_ext_to_ctl(struct soc_bytes_ext * ext)833 static inline struct wm_coeff_ctl *bytes_ext_to_ctl(struct soc_bytes_ext *ext)
834 {
835 return container_of(ext, struct wm_coeff_ctl, bytes_ext);
836 }
837
wm_coeff_base_reg(struct wm_coeff_ctl * ctl,unsigned int * reg)838 static int wm_coeff_base_reg(struct wm_coeff_ctl *ctl, unsigned int *reg)
839 {
840 const struct wm_adsp_alg_region *alg_region = &ctl->alg_region;
841 struct wm_adsp *dsp = ctl->dsp;
842 const struct wm_adsp_region *mem;
843
844 mem = wm_adsp_find_region(dsp, alg_region->type);
845 if (!mem) {
846 adsp_err(dsp, "No base for region %x\n",
847 alg_region->type);
848 return -EINVAL;
849 }
850
851 *reg = wm_adsp_region_to_reg(mem, ctl->alg_region.base + ctl->offset);
852
853 return 0;
854 }
855
wm_coeff_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)856 static int wm_coeff_info(struct snd_kcontrol *kctl,
857 struct snd_ctl_elem_info *uinfo)
858 {
859 struct soc_bytes_ext *bytes_ext =
860 (struct soc_bytes_ext *)kctl->private_value;
861 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
862
863 switch (ctl->type) {
864 case WMFW_CTL_TYPE_ACKED:
865 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
866 uinfo->value.integer.min = WM_ADSP_ACKED_CTL_MIN_VALUE;
867 uinfo->value.integer.max = WM_ADSP_ACKED_CTL_MAX_VALUE;
868 uinfo->value.integer.step = 1;
869 uinfo->count = 1;
870 break;
871 default:
872 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
873 uinfo->count = ctl->len;
874 break;
875 }
876
877 return 0;
878 }
879
wm_coeff_write_acked_control(struct wm_coeff_ctl * ctl,unsigned int event_id)880 static int wm_coeff_write_acked_control(struct wm_coeff_ctl *ctl,
881 unsigned int event_id)
882 {
883 struct wm_adsp *dsp = ctl->dsp;
884 u32 val = cpu_to_be32(event_id);
885 unsigned int reg;
886 int i, ret;
887
888 ret = wm_coeff_base_reg(ctl, ®);
889 if (ret)
890 return ret;
891
892 adsp_dbg(dsp, "Sending 0x%x to acked control alg 0x%x %s:0x%x\n",
893 event_id, ctl->alg_region.alg,
894 wm_adsp_mem_region_name(ctl->alg_region.type), ctl->offset);
895
896 ret = regmap_raw_write(dsp->regmap, reg, &val, sizeof(val));
897 if (ret) {
898 adsp_err(dsp, "Failed to write %x: %d\n", reg, ret);
899 return ret;
900 }
901
902 /*
903 * Poll for ack, we initially poll at ~1ms intervals for firmwares
904 * that respond quickly, then go to ~10ms polls. A firmware is unlikely
905 * to ack instantly so we do the first 1ms delay before reading the
906 * control to avoid a pointless bus transaction
907 */
908 for (i = 0; i < WM_ADSP_ACKED_CTL_TIMEOUT_MS;) {
909 switch (i) {
910 case 0 ... WM_ADSP_ACKED_CTL_N_QUICKPOLLS - 1:
911 usleep_range(1000, 2000);
912 i++;
913 break;
914 default:
915 usleep_range(10000, 20000);
916 i += 10;
917 break;
918 }
919
920 ret = regmap_raw_read(dsp->regmap, reg, &val, sizeof(val));
921 if (ret) {
922 adsp_err(dsp, "Failed to read %x: %d\n", reg, ret);
923 return ret;
924 }
925
926 if (val == 0) {
927 adsp_dbg(dsp, "Acked control ACKED at poll %u\n", i);
928 return 0;
929 }
930 }
931
932 adsp_warn(dsp, "Acked control @0x%x alg:0x%x %s:0x%x timed out\n",
933 reg, ctl->alg_region.alg,
934 wm_adsp_mem_region_name(ctl->alg_region.type),
935 ctl->offset);
936
937 return -ETIMEDOUT;
938 }
939
wm_coeff_write_control(struct wm_coeff_ctl * ctl,const void * buf,size_t len)940 static int wm_coeff_write_control(struct wm_coeff_ctl *ctl,
941 const void *buf, size_t len)
942 {
943 struct wm_adsp *dsp = ctl->dsp;
944 void *scratch;
945 int ret;
946 unsigned int reg;
947
948 ret = wm_coeff_base_reg(ctl, ®);
949 if (ret)
950 return ret;
951
952 scratch = kmemdup(buf, len, GFP_KERNEL | GFP_DMA);
953 if (!scratch)
954 return -ENOMEM;
955
956 ret = regmap_raw_write(dsp->regmap, reg, scratch,
957 len);
958 if (ret) {
959 adsp_err(dsp, "Failed to write %zu bytes to %x: %d\n",
960 len, reg, ret);
961 kfree(scratch);
962 return ret;
963 }
964 adsp_dbg(dsp, "Wrote %zu bytes to %x\n", len, reg);
965
966 kfree(scratch);
967
968 return 0;
969 }
970
wm_coeff_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)971 static int wm_coeff_put(struct snd_kcontrol *kctl,
972 struct snd_ctl_elem_value *ucontrol)
973 {
974 struct soc_bytes_ext *bytes_ext =
975 (struct soc_bytes_ext *)kctl->private_value;
976 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
977 char *p = ucontrol->value.bytes.data;
978 int ret = 0;
979
980 mutex_lock(&ctl->dsp->pwr_lock);
981
982 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
983 ret = -EPERM;
984 else
985 memcpy(ctl->cache, p, ctl->len);
986
987 ctl->set = 1;
988 if (ctl->enabled && ctl->dsp->running)
989 ret = wm_coeff_write_control(ctl, p, ctl->len);
990
991 mutex_unlock(&ctl->dsp->pwr_lock);
992
993 return ret;
994 }
995
wm_coeff_tlv_put(struct snd_kcontrol * kctl,const unsigned int __user * bytes,unsigned int size)996 static int wm_coeff_tlv_put(struct snd_kcontrol *kctl,
997 const unsigned int __user *bytes, unsigned int size)
998 {
999 struct soc_bytes_ext *bytes_ext =
1000 (struct soc_bytes_ext *)kctl->private_value;
1001 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1002 int ret = 0;
1003
1004 mutex_lock(&ctl->dsp->pwr_lock);
1005
1006 if (copy_from_user(ctl->cache, bytes, size)) {
1007 ret = -EFAULT;
1008 } else {
1009 ctl->set = 1;
1010 if (ctl->enabled && ctl->dsp->running)
1011 ret = wm_coeff_write_control(ctl, ctl->cache, size);
1012 else if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
1013 ret = -EPERM;
1014 }
1015
1016 mutex_unlock(&ctl->dsp->pwr_lock);
1017
1018 return ret;
1019 }
1020
wm_coeff_put_acked(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)1021 static int wm_coeff_put_acked(struct snd_kcontrol *kctl,
1022 struct snd_ctl_elem_value *ucontrol)
1023 {
1024 struct soc_bytes_ext *bytes_ext =
1025 (struct soc_bytes_ext *)kctl->private_value;
1026 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1027 unsigned int val = ucontrol->value.integer.value[0];
1028 int ret;
1029
1030 if (val == 0)
1031 return 0; /* 0 means no event */
1032
1033 mutex_lock(&ctl->dsp->pwr_lock);
1034
1035 if (ctl->enabled && ctl->dsp->running)
1036 ret = wm_coeff_write_acked_control(ctl, val);
1037 else
1038 ret = -EPERM;
1039
1040 mutex_unlock(&ctl->dsp->pwr_lock);
1041
1042 return ret;
1043 }
1044
wm_coeff_read_control(struct wm_coeff_ctl * ctl,void * buf,size_t len)1045 static int wm_coeff_read_control(struct wm_coeff_ctl *ctl,
1046 void *buf, size_t len)
1047 {
1048 struct wm_adsp *dsp = ctl->dsp;
1049 void *scratch;
1050 int ret;
1051 unsigned int reg;
1052
1053 ret = wm_coeff_base_reg(ctl, ®);
1054 if (ret)
1055 return ret;
1056
1057 scratch = kmalloc(len, GFP_KERNEL | GFP_DMA);
1058 if (!scratch)
1059 return -ENOMEM;
1060
1061 ret = regmap_raw_read(dsp->regmap, reg, scratch, len);
1062 if (ret) {
1063 adsp_err(dsp, "Failed to read %zu bytes from %x: %d\n",
1064 len, reg, ret);
1065 kfree(scratch);
1066 return ret;
1067 }
1068 adsp_dbg(dsp, "Read %zu bytes from %x\n", len, reg);
1069
1070 memcpy(buf, scratch, len);
1071 kfree(scratch);
1072
1073 return 0;
1074 }
1075
wm_coeff_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)1076 static int wm_coeff_get(struct snd_kcontrol *kctl,
1077 struct snd_ctl_elem_value *ucontrol)
1078 {
1079 struct soc_bytes_ext *bytes_ext =
1080 (struct soc_bytes_ext *)kctl->private_value;
1081 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1082 char *p = ucontrol->value.bytes.data;
1083 int ret = 0;
1084
1085 mutex_lock(&ctl->dsp->pwr_lock);
1086
1087 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
1088 if (ctl->enabled && ctl->dsp->running)
1089 ret = wm_coeff_read_control(ctl, p, ctl->len);
1090 else
1091 ret = -EPERM;
1092 } else {
1093 if (!ctl->flags && ctl->enabled && ctl->dsp->running)
1094 ret = wm_coeff_read_control(ctl, ctl->cache, ctl->len);
1095
1096 memcpy(p, ctl->cache, ctl->len);
1097 }
1098
1099 mutex_unlock(&ctl->dsp->pwr_lock);
1100
1101 return ret;
1102 }
1103
wm_coeff_tlv_get(struct snd_kcontrol * kctl,unsigned int __user * bytes,unsigned int size)1104 static int wm_coeff_tlv_get(struct snd_kcontrol *kctl,
1105 unsigned int __user *bytes, unsigned int size)
1106 {
1107 struct soc_bytes_ext *bytes_ext =
1108 (struct soc_bytes_ext *)kctl->private_value;
1109 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1110 int ret = 0;
1111
1112 mutex_lock(&ctl->dsp->pwr_lock);
1113
1114 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
1115 if (ctl->enabled && ctl->dsp->running)
1116 ret = wm_coeff_read_control(ctl, ctl->cache, size);
1117 else
1118 ret = -EPERM;
1119 } else {
1120 if (!ctl->flags && ctl->enabled && ctl->dsp->running)
1121 ret = wm_coeff_read_control(ctl, ctl->cache, size);
1122 }
1123
1124 if (!ret && copy_to_user(bytes, ctl->cache, size))
1125 ret = -EFAULT;
1126
1127 mutex_unlock(&ctl->dsp->pwr_lock);
1128
1129 return ret;
1130 }
1131
wm_coeff_get_acked(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1132 static int wm_coeff_get_acked(struct snd_kcontrol *kcontrol,
1133 struct snd_ctl_elem_value *ucontrol)
1134 {
1135 /*
1136 * Although it's not useful to read an acked control, we must satisfy
1137 * user-side assumptions that all controls are readable and that a
1138 * write of the same value should be filtered out (it's valid to send
1139 * the same event number again to the firmware). We therefore return 0,
1140 * meaning "no event" so valid event numbers will always be a change
1141 */
1142 ucontrol->value.integer.value[0] = 0;
1143
1144 return 0;
1145 }
1146
1147 struct wmfw_ctl_work {
1148 struct wm_adsp *dsp;
1149 struct wm_coeff_ctl *ctl;
1150 struct work_struct work;
1151 };
1152
wmfw_convert_flags(unsigned int in,unsigned int len)1153 static unsigned int wmfw_convert_flags(unsigned int in, unsigned int len)
1154 {
1155 unsigned int out, rd, wr, vol;
1156
1157 if (len > ADSP_MAX_STD_CTRL_SIZE) {
1158 rd = SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1159 wr = SNDRV_CTL_ELEM_ACCESS_TLV_WRITE;
1160 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
1161
1162 out = SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1163 } else {
1164 rd = SNDRV_CTL_ELEM_ACCESS_READ;
1165 wr = SNDRV_CTL_ELEM_ACCESS_WRITE;
1166 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
1167
1168 out = 0;
1169 }
1170
1171 if (in) {
1172 out |= rd;
1173 if (in & WMFW_CTL_FLAG_WRITEABLE)
1174 out |= wr;
1175 if (in & WMFW_CTL_FLAG_VOLATILE)
1176 out |= vol;
1177 } else {
1178 out |= rd | wr | vol;
1179 }
1180
1181 return out;
1182 }
1183
wmfw_add_ctl(struct wm_adsp * dsp,struct wm_coeff_ctl * ctl)1184 static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl)
1185 {
1186 struct snd_kcontrol_new *kcontrol;
1187 int ret;
1188
1189 if (!ctl || !ctl->name)
1190 return -EINVAL;
1191
1192 kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL);
1193 if (!kcontrol)
1194 return -ENOMEM;
1195
1196 kcontrol->name = ctl->name;
1197 kcontrol->info = wm_coeff_info;
1198 kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1199 kcontrol->tlv.c = snd_soc_bytes_tlv_callback;
1200 kcontrol->private_value = (unsigned long)&ctl->bytes_ext;
1201 kcontrol->access = wmfw_convert_flags(ctl->flags, ctl->len);
1202
1203 switch (ctl->type) {
1204 case WMFW_CTL_TYPE_ACKED:
1205 kcontrol->get = wm_coeff_get_acked;
1206 kcontrol->put = wm_coeff_put_acked;
1207 break;
1208 default:
1209 if (kcontrol->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1210 ctl->bytes_ext.max = ctl->len;
1211 ctl->bytes_ext.get = wm_coeff_tlv_get;
1212 ctl->bytes_ext.put = wm_coeff_tlv_put;
1213 } else {
1214 kcontrol->get = wm_coeff_get;
1215 kcontrol->put = wm_coeff_put;
1216 }
1217 break;
1218 }
1219
1220 ret = snd_soc_add_codec_controls(dsp->codec, kcontrol, 1);
1221 if (ret < 0)
1222 goto err_kcontrol;
1223
1224 kfree(kcontrol);
1225
1226 return 0;
1227
1228 err_kcontrol:
1229 kfree(kcontrol);
1230 return ret;
1231 }
1232
wm_coeff_init_control_caches(struct wm_adsp * dsp)1233 static int wm_coeff_init_control_caches(struct wm_adsp *dsp)
1234 {
1235 struct wm_coeff_ctl *ctl;
1236 int ret;
1237
1238 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1239 if (!ctl->enabled || ctl->set)
1240 continue;
1241 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
1242 continue;
1243
1244 ret = wm_coeff_read_control(ctl, ctl->cache, ctl->len);
1245 if (ret < 0)
1246 return ret;
1247 }
1248
1249 return 0;
1250 }
1251
wm_coeff_sync_controls(struct wm_adsp * dsp)1252 static int wm_coeff_sync_controls(struct wm_adsp *dsp)
1253 {
1254 struct wm_coeff_ctl *ctl;
1255 int ret;
1256
1257 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1258 if (!ctl->enabled)
1259 continue;
1260 if (ctl->set && !(ctl->flags & WMFW_CTL_FLAG_VOLATILE)) {
1261 ret = wm_coeff_write_control(ctl, ctl->cache, ctl->len);
1262 if (ret < 0)
1263 return ret;
1264 }
1265 }
1266
1267 return 0;
1268 }
1269
wm_adsp_signal_event_controls(struct wm_adsp * dsp,unsigned int event)1270 static void wm_adsp_signal_event_controls(struct wm_adsp *dsp,
1271 unsigned int event)
1272 {
1273 struct wm_coeff_ctl *ctl;
1274 int ret;
1275
1276 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1277 if (ctl->type != WMFW_CTL_TYPE_HOSTEVENT)
1278 continue;
1279
1280 if (!ctl->enabled)
1281 continue;
1282
1283 ret = wm_coeff_write_acked_control(ctl, event);
1284 if (ret)
1285 adsp_warn(dsp,
1286 "Failed to send 0x%x event to alg 0x%x (%d)\n",
1287 event, ctl->alg_region.alg, ret);
1288 }
1289 }
1290
wm_adsp_ctl_work(struct work_struct * work)1291 static void wm_adsp_ctl_work(struct work_struct *work)
1292 {
1293 struct wmfw_ctl_work *ctl_work = container_of(work,
1294 struct wmfw_ctl_work,
1295 work);
1296
1297 wmfw_add_ctl(ctl_work->dsp, ctl_work->ctl);
1298 kfree(ctl_work);
1299 }
1300
wm_adsp_free_ctl_blk(struct wm_coeff_ctl * ctl)1301 static void wm_adsp_free_ctl_blk(struct wm_coeff_ctl *ctl)
1302 {
1303 kfree(ctl->cache);
1304 kfree(ctl->name);
1305 kfree(ctl);
1306 }
1307
wm_adsp_create_control(struct wm_adsp * dsp,const struct wm_adsp_alg_region * alg_region,unsigned int offset,unsigned int len,const char * subname,unsigned int subname_len,unsigned int flags,unsigned int type)1308 static int wm_adsp_create_control(struct wm_adsp *dsp,
1309 const struct wm_adsp_alg_region *alg_region,
1310 unsigned int offset, unsigned int len,
1311 const char *subname, unsigned int subname_len,
1312 unsigned int flags, unsigned int type)
1313 {
1314 struct wm_coeff_ctl *ctl;
1315 struct wmfw_ctl_work *ctl_work;
1316 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1317 const char *region_name;
1318 int ret;
1319
1320 region_name = wm_adsp_mem_region_name(alg_region->type);
1321 if (!region_name) {
1322 adsp_err(dsp, "Unknown region type: %d\n", alg_region->type);
1323 return -EINVAL;
1324 }
1325
1326 switch (dsp->fw_ver) {
1327 case 0:
1328 case 1:
1329 snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "DSP%d %s %x",
1330 dsp->num, region_name, alg_region->alg);
1331 break;
1332 default:
1333 ret = snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
1334 "DSP%d%c %.12s %x", dsp->num, *region_name,
1335 wm_adsp_fw_text[dsp->fw], alg_region->alg);
1336
1337 /* Truncate the subname from the start if it is too long */
1338 if (subname) {
1339 int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
1340 int skip = 0;
1341
1342 if (subname_len > avail)
1343 skip = subname_len - avail;
1344
1345 snprintf(name + ret,
1346 SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret, " %.*s",
1347 subname_len - skip, subname + skip);
1348 }
1349 break;
1350 }
1351
1352 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1353 if (!strcmp(ctl->name, name)) {
1354 if (!ctl->enabled)
1355 ctl->enabled = 1;
1356 return 0;
1357 }
1358 }
1359
1360 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1361 if (!ctl)
1362 return -ENOMEM;
1363 ctl->fw_name = wm_adsp_fw_text[dsp->fw];
1364 ctl->alg_region = *alg_region;
1365 ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
1366 if (!ctl->name) {
1367 ret = -ENOMEM;
1368 goto err_ctl;
1369 }
1370 ctl->enabled = 1;
1371 ctl->set = 0;
1372 ctl->ops.xget = wm_coeff_get;
1373 ctl->ops.xput = wm_coeff_put;
1374 ctl->dsp = dsp;
1375
1376 ctl->flags = flags;
1377 ctl->type = type;
1378 ctl->offset = offset;
1379 ctl->len = len;
1380 ctl->cache = kzalloc(ctl->len, GFP_KERNEL);
1381 if (!ctl->cache) {
1382 ret = -ENOMEM;
1383 goto err_ctl_name;
1384 }
1385
1386 list_add(&ctl->list, &dsp->ctl_list);
1387
1388 if (flags & WMFW_CTL_FLAG_SYS)
1389 return 0;
1390
1391 ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL);
1392 if (!ctl_work) {
1393 ret = -ENOMEM;
1394 goto err_ctl_cache;
1395 }
1396
1397 ctl_work->dsp = dsp;
1398 ctl_work->ctl = ctl;
1399 INIT_WORK(&ctl_work->work, wm_adsp_ctl_work);
1400 schedule_work(&ctl_work->work);
1401
1402 return 0;
1403
1404 err_ctl_cache:
1405 kfree(ctl->cache);
1406 err_ctl_name:
1407 kfree(ctl->name);
1408 err_ctl:
1409 kfree(ctl);
1410
1411 return ret;
1412 }
1413
1414 struct wm_coeff_parsed_alg {
1415 int id;
1416 const u8 *name;
1417 int name_len;
1418 int ncoeff;
1419 };
1420
1421 struct wm_coeff_parsed_coeff {
1422 int offset;
1423 int mem_type;
1424 const u8 *name;
1425 int name_len;
1426 int ctl_type;
1427 int flags;
1428 int len;
1429 };
1430
wm_coeff_parse_string(int bytes,const u8 ** pos,const u8 ** str)1431 static int wm_coeff_parse_string(int bytes, const u8 **pos, const u8 **str)
1432 {
1433 int length;
1434
1435 switch (bytes) {
1436 case 1:
1437 length = **pos;
1438 break;
1439 case 2:
1440 length = le16_to_cpu(*((__le16 *)*pos));
1441 break;
1442 default:
1443 return 0;
1444 }
1445
1446 if (str)
1447 *str = *pos + bytes;
1448
1449 *pos += ((length + bytes) + 3) & ~0x03;
1450
1451 return length;
1452 }
1453
wm_coeff_parse_int(int bytes,const u8 ** pos)1454 static int wm_coeff_parse_int(int bytes, const u8 **pos)
1455 {
1456 int val = 0;
1457
1458 switch (bytes) {
1459 case 2:
1460 val = le16_to_cpu(*((__le16 *)*pos));
1461 break;
1462 case 4:
1463 val = le32_to_cpu(*((__le32 *)*pos));
1464 break;
1465 default:
1466 break;
1467 }
1468
1469 *pos += bytes;
1470
1471 return val;
1472 }
1473
wm_coeff_parse_alg(struct wm_adsp * dsp,const u8 ** data,struct wm_coeff_parsed_alg * blk)1474 static inline void wm_coeff_parse_alg(struct wm_adsp *dsp, const u8 **data,
1475 struct wm_coeff_parsed_alg *blk)
1476 {
1477 const struct wmfw_adsp_alg_data *raw;
1478
1479 switch (dsp->fw_ver) {
1480 case 0:
1481 case 1:
1482 raw = (const struct wmfw_adsp_alg_data *)*data;
1483 *data = raw->data;
1484
1485 blk->id = le32_to_cpu(raw->id);
1486 blk->name = raw->name;
1487 blk->name_len = strlen(raw->name);
1488 blk->ncoeff = le32_to_cpu(raw->ncoeff);
1489 break;
1490 default:
1491 blk->id = wm_coeff_parse_int(sizeof(raw->id), data);
1492 blk->name_len = wm_coeff_parse_string(sizeof(u8), data,
1493 &blk->name);
1494 wm_coeff_parse_string(sizeof(u16), data, NULL);
1495 blk->ncoeff = wm_coeff_parse_int(sizeof(raw->ncoeff), data);
1496 break;
1497 }
1498
1499 adsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id);
1500 adsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name);
1501 adsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff);
1502 }
1503
wm_coeff_parse_coeff(struct wm_adsp * dsp,const u8 ** data,struct wm_coeff_parsed_coeff * blk)1504 static inline void wm_coeff_parse_coeff(struct wm_adsp *dsp, const u8 **data,
1505 struct wm_coeff_parsed_coeff *blk)
1506 {
1507 const struct wmfw_adsp_coeff_data *raw;
1508 const u8 *tmp;
1509 int length;
1510
1511 switch (dsp->fw_ver) {
1512 case 0:
1513 case 1:
1514 raw = (const struct wmfw_adsp_coeff_data *)*data;
1515 *data = *data + sizeof(raw->hdr) + le32_to_cpu(raw->hdr.size);
1516
1517 blk->offset = le16_to_cpu(raw->hdr.offset);
1518 blk->mem_type = le16_to_cpu(raw->hdr.type);
1519 blk->name = raw->name;
1520 blk->name_len = strlen(raw->name);
1521 blk->ctl_type = le16_to_cpu(raw->ctl_type);
1522 blk->flags = le16_to_cpu(raw->flags);
1523 blk->len = le32_to_cpu(raw->len);
1524 break;
1525 default:
1526 tmp = *data;
1527 blk->offset = wm_coeff_parse_int(sizeof(raw->hdr.offset), &tmp);
1528 blk->mem_type = wm_coeff_parse_int(sizeof(raw->hdr.type), &tmp);
1529 length = wm_coeff_parse_int(sizeof(raw->hdr.size), &tmp);
1530 blk->name_len = wm_coeff_parse_string(sizeof(u8), &tmp,
1531 &blk->name);
1532 wm_coeff_parse_string(sizeof(u8), &tmp, NULL);
1533 wm_coeff_parse_string(sizeof(u16), &tmp, NULL);
1534 blk->ctl_type = wm_coeff_parse_int(sizeof(raw->ctl_type), &tmp);
1535 blk->flags = wm_coeff_parse_int(sizeof(raw->flags), &tmp);
1536 blk->len = wm_coeff_parse_int(sizeof(raw->len), &tmp);
1537
1538 *data = *data + sizeof(raw->hdr) + length;
1539 break;
1540 }
1541
1542 adsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type);
1543 adsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset);
1544 adsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name);
1545 adsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags);
1546 adsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type);
1547 adsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len);
1548 }
1549
wm_adsp_check_coeff_flags(struct wm_adsp * dsp,const struct wm_coeff_parsed_coeff * coeff_blk,unsigned int f_required,unsigned int f_illegal)1550 static int wm_adsp_check_coeff_flags(struct wm_adsp *dsp,
1551 const struct wm_coeff_parsed_coeff *coeff_blk,
1552 unsigned int f_required,
1553 unsigned int f_illegal)
1554 {
1555 if ((coeff_blk->flags & f_illegal) ||
1556 ((coeff_blk->flags & f_required) != f_required)) {
1557 adsp_err(dsp, "Illegal flags 0x%x for control type 0x%x\n",
1558 coeff_blk->flags, coeff_blk->ctl_type);
1559 return -EINVAL;
1560 }
1561
1562 return 0;
1563 }
1564
wm_adsp_parse_coeff(struct wm_adsp * dsp,const struct wmfw_region * region)1565 static int wm_adsp_parse_coeff(struct wm_adsp *dsp,
1566 const struct wmfw_region *region)
1567 {
1568 struct wm_adsp_alg_region alg_region = {};
1569 struct wm_coeff_parsed_alg alg_blk;
1570 struct wm_coeff_parsed_coeff coeff_blk;
1571 const u8 *data = region->data;
1572 int i, ret;
1573
1574 wm_coeff_parse_alg(dsp, &data, &alg_blk);
1575 for (i = 0; i < alg_blk.ncoeff; i++) {
1576 wm_coeff_parse_coeff(dsp, &data, &coeff_blk);
1577
1578 switch (coeff_blk.ctl_type) {
1579 case SNDRV_CTL_ELEM_TYPE_BYTES:
1580 break;
1581 case WMFW_CTL_TYPE_ACKED:
1582 if (coeff_blk.flags & WMFW_CTL_FLAG_SYS)
1583 continue; /* ignore */
1584
1585 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk,
1586 WMFW_CTL_FLAG_VOLATILE |
1587 WMFW_CTL_FLAG_WRITEABLE |
1588 WMFW_CTL_FLAG_READABLE,
1589 0);
1590 if (ret)
1591 return -EINVAL;
1592 break;
1593 case WMFW_CTL_TYPE_HOSTEVENT:
1594 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk,
1595 WMFW_CTL_FLAG_SYS |
1596 WMFW_CTL_FLAG_VOLATILE |
1597 WMFW_CTL_FLAG_WRITEABLE |
1598 WMFW_CTL_FLAG_READABLE,
1599 0);
1600 if (ret)
1601 return -EINVAL;
1602 break;
1603 default:
1604 adsp_err(dsp, "Unknown control type: %d\n",
1605 coeff_blk.ctl_type);
1606 return -EINVAL;
1607 }
1608
1609 alg_region.type = coeff_blk.mem_type;
1610 alg_region.alg = alg_blk.id;
1611
1612 ret = wm_adsp_create_control(dsp, &alg_region,
1613 coeff_blk.offset,
1614 coeff_blk.len,
1615 coeff_blk.name,
1616 coeff_blk.name_len,
1617 coeff_blk.flags,
1618 coeff_blk.ctl_type);
1619 if (ret < 0)
1620 adsp_err(dsp, "Failed to create control: %.*s, %d\n",
1621 coeff_blk.name_len, coeff_blk.name, ret);
1622 }
1623
1624 return 0;
1625 }
1626
wm_adsp_load(struct wm_adsp * dsp)1627 static int wm_adsp_load(struct wm_adsp *dsp)
1628 {
1629 LIST_HEAD(buf_list);
1630 const struct firmware *firmware;
1631 struct regmap *regmap = dsp->regmap;
1632 unsigned int pos = 0;
1633 const struct wmfw_header *header;
1634 const struct wmfw_adsp1_sizes *adsp1_sizes;
1635 const struct wmfw_adsp2_sizes *adsp2_sizes;
1636 const struct wmfw_footer *footer;
1637 const struct wmfw_region *region;
1638 const struct wm_adsp_region *mem;
1639 const char *region_name;
1640 char *file, *text = NULL;
1641 struct wm_adsp_buf *buf;
1642 unsigned int reg;
1643 int regions = 0;
1644 int ret, offset, type, sizes;
1645
1646 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
1647 if (file == NULL)
1648 return -ENOMEM;
1649
1650 snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.wmfw", dsp->part, dsp->num,
1651 wm_adsp_fw[dsp->fw].file);
1652 file[PAGE_SIZE - 1] = '\0';
1653
1654 ret = request_firmware(&firmware, file, dsp->dev);
1655 if (ret != 0) {
1656 adsp_err(dsp, "Failed to request '%s'\n", file);
1657 goto out;
1658 }
1659 ret = -EINVAL;
1660
1661 pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
1662 if (pos >= firmware->size) {
1663 adsp_err(dsp, "%s: file too short, %zu bytes\n",
1664 file, firmware->size);
1665 goto out_fw;
1666 }
1667
1668 header = (void *)&firmware->data[0];
1669
1670 if (memcmp(&header->magic[0], "WMFW", 4) != 0) {
1671 adsp_err(dsp, "%s: invalid magic\n", file);
1672 goto out_fw;
1673 }
1674
1675 switch (header->ver) {
1676 case 0:
1677 adsp_warn(dsp, "%s: Depreciated file format %d\n",
1678 file, header->ver);
1679 break;
1680 case 1:
1681 case 2:
1682 break;
1683 default:
1684 adsp_err(dsp, "%s: unknown file format %d\n",
1685 file, header->ver);
1686 goto out_fw;
1687 }
1688
1689 adsp_info(dsp, "Firmware version: %d\n", header->ver);
1690 dsp->fw_ver = header->ver;
1691
1692 if (header->core != dsp->type) {
1693 adsp_err(dsp, "%s: invalid core %d != %d\n",
1694 file, header->core, dsp->type);
1695 goto out_fw;
1696 }
1697
1698 switch (dsp->type) {
1699 case WMFW_ADSP1:
1700 pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
1701 adsp1_sizes = (void *)&(header[1]);
1702 footer = (void *)&(adsp1_sizes[1]);
1703 sizes = sizeof(*adsp1_sizes);
1704
1705 adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n",
1706 file, le32_to_cpu(adsp1_sizes->dm),
1707 le32_to_cpu(adsp1_sizes->pm),
1708 le32_to_cpu(adsp1_sizes->zm));
1709 break;
1710
1711 case WMFW_ADSP2:
1712 pos = sizeof(*header) + sizeof(*adsp2_sizes) + sizeof(*footer);
1713 adsp2_sizes = (void *)&(header[1]);
1714 footer = (void *)&(adsp2_sizes[1]);
1715 sizes = sizeof(*adsp2_sizes);
1716
1717 adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n",
1718 file, le32_to_cpu(adsp2_sizes->xm),
1719 le32_to_cpu(adsp2_sizes->ym),
1720 le32_to_cpu(adsp2_sizes->pm),
1721 le32_to_cpu(adsp2_sizes->zm));
1722 break;
1723
1724 default:
1725 WARN(1, "Unknown DSP type");
1726 goto out_fw;
1727 }
1728
1729 if (le32_to_cpu(header->len) != sizeof(*header) +
1730 sizes + sizeof(*footer)) {
1731 adsp_err(dsp, "%s: unexpected header length %d\n",
1732 file, le32_to_cpu(header->len));
1733 goto out_fw;
1734 }
1735
1736 adsp_dbg(dsp, "%s: timestamp %llu\n", file,
1737 le64_to_cpu(footer->timestamp));
1738
1739 while (pos < firmware->size &&
1740 sizeof(*region) < firmware->size - pos) {
1741 region = (void *)&(firmware->data[pos]);
1742 region_name = "Unknown";
1743 reg = 0;
1744 text = NULL;
1745 offset = le32_to_cpu(region->offset) & 0xffffff;
1746 type = be32_to_cpu(region->type) & 0xff;
1747 mem = wm_adsp_find_region(dsp, type);
1748
1749 switch (type) {
1750 case WMFW_NAME_TEXT:
1751 region_name = "Firmware name";
1752 text = kzalloc(le32_to_cpu(region->len) + 1,
1753 GFP_KERNEL);
1754 break;
1755 case WMFW_ALGORITHM_DATA:
1756 region_name = "Algorithm";
1757 ret = wm_adsp_parse_coeff(dsp, region);
1758 if (ret != 0)
1759 goto out_fw;
1760 break;
1761 case WMFW_INFO_TEXT:
1762 region_name = "Information";
1763 text = kzalloc(le32_to_cpu(region->len) + 1,
1764 GFP_KERNEL);
1765 break;
1766 case WMFW_ABSOLUTE:
1767 region_name = "Absolute";
1768 reg = offset;
1769 break;
1770 case WMFW_ADSP1_PM:
1771 case WMFW_ADSP1_DM:
1772 case WMFW_ADSP2_XM:
1773 case WMFW_ADSP2_YM:
1774 case WMFW_ADSP1_ZM:
1775 region_name = wm_adsp_mem_region_name(type);
1776 reg = wm_adsp_region_to_reg(mem, offset);
1777 break;
1778 default:
1779 adsp_warn(dsp,
1780 "%s.%d: Unknown region type %x at %d(%x)\n",
1781 file, regions, type, pos, pos);
1782 break;
1783 }
1784
1785 adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file,
1786 regions, le32_to_cpu(region->len), offset,
1787 region_name);
1788
1789 if (le32_to_cpu(region->len) >
1790 firmware->size - pos - sizeof(*region)) {
1791 adsp_err(dsp,
1792 "%s.%d: %s region len %d bytes exceeds file length %zu\n",
1793 file, regions, region_name,
1794 le32_to_cpu(region->len), firmware->size);
1795 ret = -EINVAL;
1796 goto out_fw;
1797 }
1798
1799 if (text) {
1800 memcpy(text, region->data, le32_to_cpu(region->len));
1801 adsp_info(dsp, "%s: %s\n", file, text);
1802 kfree(text);
1803 text = NULL;
1804 }
1805
1806 if (reg) {
1807 buf = wm_adsp_buf_alloc(region->data,
1808 le32_to_cpu(region->len),
1809 &buf_list);
1810 if (!buf) {
1811 adsp_err(dsp, "Out of memory\n");
1812 ret = -ENOMEM;
1813 goto out_fw;
1814 }
1815
1816 ret = regmap_raw_write_async(regmap, reg, buf->buf,
1817 le32_to_cpu(region->len));
1818 if (ret != 0) {
1819 adsp_err(dsp,
1820 "%s.%d: Failed to write %d bytes at %d in %s: %d\n",
1821 file, regions,
1822 le32_to_cpu(region->len), offset,
1823 region_name, ret);
1824 goto out_fw;
1825 }
1826 }
1827
1828 pos += le32_to_cpu(region->len) + sizeof(*region);
1829 regions++;
1830 }
1831
1832 ret = regmap_async_complete(regmap);
1833 if (ret != 0) {
1834 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
1835 goto out_fw;
1836 }
1837
1838 if (pos > firmware->size)
1839 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
1840 file, regions, pos - firmware->size);
1841
1842 wm_adsp_debugfs_save_wmfwname(dsp, file);
1843
1844 out_fw:
1845 regmap_async_complete(regmap);
1846 wm_adsp_buf_free(&buf_list);
1847 release_firmware(firmware);
1848 kfree(text);
1849 out:
1850 kfree(file);
1851
1852 return ret;
1853 }
1854
wm_adsp_ctl_fixup_base(struct wm_adsp * dsp,const struct wm_adsp_alg_region * alg_region)1855 static void wm_adsp_ctl_fixup_base(struct wm_adsp *dsp,
1856 const struct wm_adsp_alg_region *alg_region)
1857 {
1858 struct wm_coeff_ctl *ctl;
1859
1860 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1861 if (ctl->fw_name == wm_adsp_fw_text[dsp->fw] &&
1862 alg_region->alg == ctl->alg_region.alg &&
1863 alg_region->type == ctl->alg_region.type) {
1864 ctl->alg_region.base = alg_region->base;
1865 }
1866 }
1867 }
1868
wm_adsp_read_algs(struct wm_adsp * dsp,size_t n_algs,unsigned int pos,unsigned int len)1869 static void *wm_adsp_read_algs(struct wm_adsp *dsp, size_t n_algs,
1870 unsigned int pos, unsigned int len)
1871 {
1872 void *alg;
1873 int ret;
1874 __be32 val;
1875
1876 if (n_algs == 0) {
1877 adsp_err(dsp, "No algorithms\n");
1878 return ERR_PTR(-EINVAL);
1879 }
1880
1881 if (n_algs > 1024) {
1882 adsp_err(dsp, "Algorithm count %zx excessive\n", n_algs);
1883 return ERR_PTR(-EINVAL);
1884 }
1885
1886 /* Read the terminator first to validate the length */
1887 ret = regmap_raw_read(dsp->regmap, pos + len, &val, sizeof(val));
1888 if (ret != 0) {
1889 adsp_err(dsp, "Failed to read algorithm list end: %d\n",
1890 ret);
1891 return ERR_PTR(ret);
1892 }
1893
1894 if (be32_to_cpu(val) != 0xbedead)
1895 adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbedead\n",
1896 pos + len, be32_to_cpu(val));
1897
1898 alg = kzalloc(len * 2, GFP_KERNEL | GFP_DMA);
1899 if (!alg)
1900 return ERR_PTR(-ENOMEM);
1901
1902 ret = regmap_raw_read(dsp->regmap, pos, alg, len * 2);
1903 if (ret != 0) {
1904 adsp_err(dsp, "Failed to read algorithm list: %d\n", ret);
1905 kfree(alg);
1906 return ERR_PTR(ret);
1907 }
1908
1909 return alg;
1910 }
1911
1912 static struct wm_adsp_alg_region *
wm_adsp_find_alg_region(struct wm_adsp * dsp,int type,unsigned int id)1913 wm_adsp_find_alg_region(struct wm_adsp *dsp, int type, unsigned int id)
1914 {
1915 struct wm_adsp_alg_region *alg_region;
1916
1917 list_for_each_entry(alg_region, &dsp->alg_regions, list) {
1918 if (id == alg_region->alg && type == alg_region->type)
1919 return alg_region;
1920 }
1921
1922 return NULL;
1923 }
1924
wm_adsp_create_region(struct wm_adsp * dsp,int type,__be32 id,__be32 base)1925 static struct wm_adsp_alg_region *wm_adsp_create_region(struct wm_adsp *dsp,
1926 int type, __be32 id,
1927 __be32 base)
1928 {
1929 struct wm_adsp_alg_region *alg_region;
1930
1931 alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL);
1932 if (!alg_region)
1933 return ERR_PTR(-ENOMEM);
1934
1935 alg_region->type = type;
1936 alg_region->alg = be32_to_cpu(id);
1937 alg_region->base = be32_to_cpu(base);
1938
1939 list_add_tail(&alg_region->list, &dsp->alg_regions);
1940
1941 if (dsp->fw_ver > 0)
1942 wm_adsp_ctl_fixup_base(dsp, alg_region);
1943
1944 return alg_region;
1945 }
1946
wm_adsp_free_alg_regions(struct wm_adsp * dsp)1947 static void wm_adsp_free_alg_regions(struct wm_adsp *dsp)
1948 {
1949 struct wm_adsp_alg_region *alg_region;
1950
1951 while (!list_empty(&dsp->alg_regions)) {
1952 alg_region = list_first_entry(&dsp->alg_regions,
1953 struct wm_adsp_alg_region,
1954 list);
1955 list_del(&alg_region->list);
1956 kfree(alg_region);
1957 }
1958 }
1959
wm_adsp1_setup_algs(struct wm_adsp * dsp)1960 static int wm_adsp1_setup_algs(struct wm_adsp *dsp)
1961 {
1962 struct wmfw_adsp1_id_hdr adsp1_id;
1963 struct wmfw_adsp1_alg_hdr *adsp1_alg;
1964 struct wm_adsp_alg_region *alg_region;
1965 const struct wm_adsp_region *mem;
1966 unsigned int pos, len;
1967 size_t n_algs;
1968 int i, ret;
1969
1970 mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM);
1971 if (WARN_ON(!mem))
1972 return -EINVAL;
1973
1974 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id,
1975 sizeof(adsp1_id));
1976 if (ret != 0) {
1977 adsp_err(dsp, "Failed to read algorithm info: %d\n",
1978 ret);
1979 return ret;
1980 }
1981
1982 n_algs = be32_to_cpu(adsp1_id.n_algs);
1983 dsp->fw_id = be32_to_cpu(adsp1_id.fw.id);
1984 adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
1985 dsp->fw_id,
1986 (be32_to_cpu(adsp1_id.fw.ver) & 0xff0000) >> 16,
1987 (be32_to_cpu(adsp1_id.fw.ver) & 0xff00) >> 8,
1988 be32_to_cpu(adsp1_id.fw.ver) & 0xff,
1989 n_algs);
1990
1991 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
1992 adsp1_id.fw.id, adsp1_id.zm);
1993 if (IS_ERR(alg_region))
1994 return PTR_ERR(alg_region);
1995
1996 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
1997 adsp1_id.fw.id, adsp1_id.dm);
1998 if (IS_ERR(alg_region))
1999 return PTR_ERR(alg_region);
2000
2001 pos = sizeof(adsp1_id) / 2;
2002 len = (sizeof(*adsp1_alg) * n_algs) / 2;
2003
2004 adsp1_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len);
2005 if (IS_ERR(adsp1_alg))
2006 return PTR_ERR(adsp1_alg);
2007
2008 for (i = 0; i < n_algs; i++) {
2009 adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
2010 i, be32_to_cpu(adsp1_alg[i].alg.id),
2011 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16,
2012 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8,
2013 be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff,
2014 be32_to_cpu(adsp1_alg[i].dm),
2015 be32_to_cpu(adsp1_alg[i].zm));
2016
2017 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
2018 adsp1_alg[i].alg.id,
2019 adsp1_alg[i].dm);
2020 if (IS_ERR(alg_region)) {
2021 ret = PTR_ERR(alg_region);
2022 goto out;
2023 }
2024 if (dsp->fw_ver == 0) {
2025 if (i + 1 < n_algs) {
2026 len = be32_to_cpu(adsp1_alg[i + 1].dm);
2027 len -= be32_to_cpu(adsp1_alg[i].dm);
2028 len *= 4;
2029 wm_adsp_create_control(dsp, alg_region, 0,
2030 len, NULL, 0, 0,
2031 SNDRV_CTL_ELEM_TYPE_BYTES);
2032 } else {
2033 adsp_warn(dsp, "Missing length info for region DM with ID %x\n",
2034 be32_to_cpu(adsp1_alg[i].alg.id));
2035 }
2036 }
2037
2038 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
2039 adsp1_alg[i].alg.id,
2040 adsp1_alg[i].zm);
2041 if (IS_ERR(alg_region)) {
2042 ret = PTR_ERR(alg_region);
2043 goto out;
2044 }
2045 if (dsp->fw_ver == 0) {
2046 if (i + 1 < n_algs) {
2047 len = be32_to_cpu(adsp1_alg[i + 1].zm);
2048 len -= be32_to_cpu(adsp1_alg[i].zm);
2049 len *= 4;
2050 wm_adsp_create_control(dsp, alg_region, 0,
2051 len, NULL, 0, 0,
2052 SNDRV_CTL_ELEM_TYPE_BYTES);
2053 } else {
2054 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
2055 be32_to_cpu(adsp1_alg[i].alg.id));
2056 }
2057 }
2058 }
2059
2060 out:
2061 kfree(adsp1_alg);
2062 return ret;
2063 }
2064
wm_adsp2_setup_algs(struct wm_adsp * dsp)2065 static int wm_adsp2_setup_algs(struct wm_adsp *dsp)
2066 {
2067 struct wmfw_adsp2_id_hdr adsp2_id;
2068 struct wmfw_adsp2_alg_hdr *adsp2_alg;
2069 struct wm_adsp_alg_region *alg_region;
2070 const struct wm_adsp_region *mem;
2071 unsigned int pos, len;
2072 size_t n_algs;
2073 int i, ret;
2074
2075 mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM);
2076 if (WARN_ON(!mem))
2077 return -EINVAL;
2078
2079 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id,
2080 sizeof(adsp2_id));
2081 if (ret != 0) {
2082 adsp_err(dsp, "Failed to read algorithm info: %d\n",
2083 ret);
2084 return ret;
2085 }
2086
2087 n_algs = be32_to_cpu(adsp2_id.n_algs);
2088 dsp->fw_id = be32_to_cpu(adsp2_id.fw.id);
2089 dsp->fw_id_version = be32_to_cpu(adsp2_id.fw.ver);
2090 adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
2091 dsp->fw_id,
2092 (dsp->fw_id_version & 0xff0000) >> 16,
2093 (dsp->fw_id_version & 0xff00) >> 8,
2094 dsp->fw_id_version & 0xff,
2095 n_algs);
2096
2097 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
2098 adsp2_id.fw.id, adsp2_id.xm);
2099 if (IS_ERR(alg_region))
2100 return PTR_ERR(alg_region);
2101
2102 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
2103 adsp2_id.fw.id, adsp2_id.ym);
2104 if (IS_ERR(alg_region))
2105 return PTR_ERR(alg_region);
2106
2107 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
2108 adsp2_id.fw.id, adsp2_id.zm);
2109 if (IS_ERR(alg_region))
2110 return PTR_ERR(alg_region);
2111
2112 pos = sizeof(adsp2_id) / 2;
2113 len = (sizeof(*adsp2_alg) * n_algs) / 2;
2114
2115 adsp2_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len);
2116 if (IS_ERR(adsp2_alg))
2117 return PTR_ERR(adsp2_alg);
2118
2119 for (i = 0; i < n_algs; i++) {
2120 adsp_info(dsp,
2121 "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
2122 i, be32_to_cpu(adsp2_alg[i].alg.id),
2123 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16,
2124 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8,
2125 be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff,
2126 be32_to_cpu(adsp2_alg[i].xm),
2127 be32_to_cpu(adsp2_alg[i].ym),
2128 be32_to_cpu(adsp2_alg[i].zm));
2129
2130 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
2131 adsp2_alg[i].alg.id,
2132 adsp2_alg[i].xm);
2133 if (IS_ERR(alg_region)) {
2134 ret = PTR_ERR(alg_region);
2135 goto out;
2136 }
2137 if (dsp->fw_ver == 0) {
2138 if (i + 1 < n_algs) {
2139 len = be32_to_cpu(adsp2_alg[i + 1].xm);
2140 len -= be32_to_cpu(adsp2_alg[i].xm);
2141 len *= 4;
2142 wm_adsp_create_control(dsp, alg_region, 0,
2143 len, NULL, 0, 0,
2144 SNDRV_CTL_ELEM_TYPE_BYTES);
2145 } else {
2146 adsp_warn(dsp, "Missing length info for region XM with ID %x\n",
2147 be32_to_cpu(adsp2_alg[i].alg.id));
2148 }
2149 }
2150
2151 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
2152 adsp2_alg[i].alg.id,
2153 adsp2_alg[i].ym);
2154 if (IS_ERR(alg_region)) {
2155 ret = PTR_ERR(alg_region);
2156 goto out;
2157 }
2158 if (dsp->fw_ver == 0) {
2159 if (i + 1 < n_algs) {
2160 len = be32_to_cpu(adsp2_alg[i + 1].ym);
2161 len -= be32_to_cpu(adsp2_alg[i].ym);
2162 len *= 4;
2163 wm_adsp_create_control(dsp, alg_region, 0,
2164 len, NULL, 0, 0,
2165 SNDRV_CTL_ELEM_TYPE_BYTES);
2166 } else {
2167 adsp_warn(dsp, "Missing length info for region YM with ID %x\n",
2168 be32_to_cpu(adsp2_alg[i].alg.id));
2169 }
2170 }
2171
2172 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
2173 adsp2_alg[i].alg.id,
2174 adsp2_alg[i].zm);
2175 if (IS_ERR(alg_region)) {
2176 ret = PTR_ERR(alg_region);
2177 goto out;
2178 }
2179 if (dsp->fw_ver == 0) {
2180 if (i + 1 < n_algs) {
2181 len = be32_to_cpu(adsp2_alg[i + 1].zm);
2182 len -= be32_to_cpu(adsp2_alg[i].zm);
2183 len *= 4;
2184 wm_adsp_create_control(dsp, alg_region, 0,
2185 len, NULL, 0, 0,
2186 SNDRV_CTL_ELEM_TYPE_BYTES);
2187 } else {
2188 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
2189 be32_to_cpu(adsp2_alg[i].alg.id));
2190 }
2191 }
2192 }
2193
2194 out:
2195 kfree(adsp2_alg);
2196 return ret;
2197 }
2198
wm_adsp_load_coeff(struct wm_adsp * dsp)2199 static int wm_adsp_load_coeff(struct wm_adsp *dsp)
2200 {
2201 LIST_HEAD(buf_list);
2202 struct regmap *regmap = dsp->regmap;
2203 struct wmfw_coeff_hdr *hdr;
2204 struct wmfw_coeff_item *blk;
2205 const struct firmware *firmware;
2206 const struct wm_adsp_region *mem;
2207 struct wm_adsp_alg_region *alg_region;
2208 const char *region_name;
2209 int ret, pos, blocks, type, offset, reg;
2210 char *file;
2211 struct wm_adsp_buf *buf;
2212
2213 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
2214 if (file == NULL)
2215 return -ENOMEM;
2216
2217 snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.bin", dsp->part, dsp->num,
2218 wm_adsp_fw[dsp->fw].file);
2219 file[PAGE_SIZE - 1] = '\0';
2220
2221 ret = request_firmware(&firmware, file, dsp->dev);
2222 if (ret != 0) {
2223 adsp_warn(dsp, "Failed to request '%s'\n", file);
2224 ret = 0;
2225 goto out;
2226 }
2227 ret = -EINVAL;
2228
2229 if (sizeof(*hdr) >= firmware->size) {
2230 adsp_err(dsp, "%s: file too short, %zu bytes\n",
2231 file, firmware->size);
2232 goto out_fw;
2233 }
2234
2235 hdr = (void *)&firmware->data[0];
2236 if (memcmp(hdr->magic, "WMDR", 4) != 0) {
2237 adsp_err(dsp, "%s: invalid magic\n", file);
2238 goto out_fw;
2239 }
2240
2241 switch (be32_to_cpu(hdr->rev) & 0xff) {
2242 case 1:
2243 break;
2244 default:
2245 adsp_err(dsp, "%s: Unsupported coefficient file format %d\n",
2246 file, be32_to_cpu(hdr->rev) & 0xff);
2247 ret = -EINVAL;
2248 goto out_fw;
2249 }
2250
2251 adsp_dbg(dsp, "%s: v%d.%d.%d\n", file,
2252 (le32_to_cpu(hdr->ver) >> 16) & 0xff,
2253 (le32_to_cpu(hdr->ver) >> 8) & 0xff,
2254 le32_to_cpu(hdr->ver) & 0xff);
2255
2256 pos = le32_to_cpu(hdr->len);
2257
2258 blocks = 0;
2259 while (pos < firmware->size &&
2260 sizeof(*blk) < firmware->size - pos) {
2261 blk = (void *)(&firmware->data[pos]);
2262
2263 type = le16_to_cpu(blk->type);
2264 offset = le16_to_cpu(blk->offset);
2265
2266 adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n",
2267 file, blocks, le32_to_cpu(blk->id),
2268 (le32_to_cpu(blk->ver) >> 16) & 0xff,
2269 (le32_to_cpu(blk->ver) >> 8) & 0xff,
2270 le32_to_cpu(blk->ver) & 0xff);
2271 adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n",
2272 file, blocks, le32_to_cpu(blk->len), offset, type);
2273
2274 reg = 0;
2275 region_name = "Unknown";
2276 switch (type) {
2277 case (WMFW_NAME_TEXT << 8):
2278 case (WMFW_INFO_TEXT << 8):
2279 break;
2280 case (WMFW_ABSOLUTE << 8):
2281 /*
2282 * Old files may use this for global
2283 * coefficients.
2284 */
2285 if (le32_to_cpu(blk->id) == dsp->fw_id &&
2286 offset == 0) {
2287 region_name = "global coefficients";
2288 mem = wm_adsp_find_region(dsp, type);
2289 if (!mem) {
2290 adsp_err(dsp, "No ZM\n");
2291 break;
2292 }
2293 reg = wm_adsp_region_to_reg(mem, 0);
2294
2295 } else {
2296 region_name = "register";
2297 reg = offset;
2298 }
2299 break;
2300
2301 case WMFW_ADSP1_DM:
2302 case WMFW_ADSP1_ZM:
2303 case WMFW_ADSP2_XM:
2304 case WMFW_ADSP2_YM:
2305 adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n",
2306 file, blocks, le32_to_cpu(blk->len),
2307 type, le32_to_cpu(blk->id));
2308
2309 mem = wm_adsp_find_region(dsp, type);
2310 if (!mem) {
2311 adsp_err(dsp, "No base for region %x\n", type);
2312 break;
2313 }
2314
2315 alg_region = wm_adsp_find_alg_region(dsp, type,
2316 le32_to_cpu(blk->id));
2317 if (alg_region) {
2318 reg = alg_region->base;
2319 reg = wm_adsp_region_to_reg(mem, reg);
2320 reg += offset;
2321 } else {
2322 adsp_err(dsp, "No %x for algorithm %x\n",
2323 type, le32_to_cpu(blk->id));
2324 }
2325 break;
2326
2327 default:
2328 adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n",
2329 file, blocks, type, pos);
2330 break;
2331 }
2332
2333 if (reg) {
2334 if (le32_to_cpu(blk->len) >
2335 firmware->size - pos - sizeof(*blk)) {
2336 adsp_err(dsp,
2337 "%s.%d: %s region len %d bytes exceeds file length %zu\n",
2338 file, blocks, region_name,
2339 le32_to_cpu(blk->len),
2340 firmware->size);
2341 ret = -EINVAL;
2342 goto out_fw;
2343 }
2344
2345 buf = wm_adsp_buf_alloc(blk->data,
2346 le32_to_cpu(blk->len),
2347 &buf_list);
2348 if (!buf) {
2349 adsp_err(dsp, "Out of memory\n");
2350 ret = -ENOMEM;
2351 goto out_fw;
2352 }
2353
2354 adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n",
2355 file, blocks, le32_to_cpu(blk->len),
2356 reg);
2357 ret = regmap_raw_write_async(regmap, reg, buf->buf,
2358 le32_to_cpu(blk->len));
2359 if (ret != 0) {
2360 adsp_err(dsp,
2361 "%s.%d: Failed to write to %x in %s: %d\n",
2362 file, blocks, reg, region_name, ret);
2363 }
2364 }
2365
2366 pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03;
2367 blocks++;
2368 }
2369
2370 ret = regmap_async_complete(regmap);
2371 if (ret != 0)
2372 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
2373
2374 if (pos > firmware->size)
2375 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
2376 file, blocks, pos - firmware->size);
2377
2378 wm_adsp_debugfs_save_binname(dsp, file);
2379
2380 out_fw:
2381 regmap_async_complete(regmap);
2382 release_firmware(firmware);
2383 wm_adsp_buf_free(&buf_list);
2384 out:
2385 kfree(file);
2386 return ret;
2387 }
2388
wm_adsp1_init(struct wm_adsp * dsp)2389 int wm_adsp1_init(struct wm_adsp *dsp)
2390 {
2391 INIT_LIST_HEAD(&dsp->alg_regions);
2392
2393 mutex_init(&dsp->pwr_lock);
2394
2395 return 0;
2396 }
2397 EXPORT_SYMBOL_GPL(wm_adsp1_init);
2398
wm_adsp1_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)2399 int wm_adsp1_event(struct snd_soc_dapm_widget *w,
2400 struct snd_kcontrol *kcontrol,
2401 int event)
2402 {
2403 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2404 struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
2405 struct wm_adsp *dsp = &dsps[w->shift];
2406 struct wm_coeff_ctl *ctl;
2407 int ret;
2408 unsigned int val;
2409
2410 dsp->codec = codec;
2411
2412 mutex_lock(&dsp->pwr_lock);
2413
2414 switch (event) {
2415 case SND_SOC_DAPM_POST_PMU:
2416 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2417 ADSP1_SYS_ENA, ADSP1_SYS_ENA);
2418
2419 /*
2420 * For simplicity set the DSP clock rate to be the
2421 * SYSCLK rate rather than making it configurable.
2422 */
2423 if (dsp->sysclk_reg) {
2424 ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val);
2425 if (ret != 0) {
2426 adsp_err(dsp, "Failed to read SYSCLK state: %d\n",
2427 ret);
2428 goto err_mutex;
2429 }
2430
2431 val = (val & dsp->sysclk_mask) >> dsp->sysclk_shift;
2432
2433 ret = regmap_update_bits(dsp->regmap,
2434 dsp->base + ADSP1_CONTROL_31,
2435 ADSP1_CLK_SEL_MASK, val);
2436 if (ret != 0) {
2437 adsp_err(dsp, "Failed to set clock rate: %d\n",
2438 ret);
2439 goto err_mutex;
2440 }
2441 }
2442
2443 ret = wm_adsp_load(dsp);
2444 if (ret != 0)
2445 goto err_ena;
2446
2447 ret = wm_adsp1_setup_algs(dsp);
2448 if (ret != 0)
2449 goto err_ena;
2450
2451 ret = wm_adsp_load_coeff(dsp);
2452 if (ret != 0)
2453 goto err_ena;
2454
2455 /* Initialize caches for enabled and unset controls */
2456 ret = wm_coeff_init_control_caches(dsp);
2457 if (ret != 0)
2458 goto err_ena;
2459
2460 /* Sync set controls */
2461 ret = wm_coeff_sync_controls(dsp);
2462 if (ret != 0)
2463 goto err_ena;
2464
2465 dsp->booted = true;
2466
2467 /* Start the core running */
2468 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2469 ADSP1_CORE_ENA | ADSP1_START,
2470 ADSP1_CORE_ENA | ADSP1_START);
2471
2472 dsp->running = true;
2473 break;
2474
2475 case SND_SOC_DAPM_PRE_PMD:
2476 dsp->running = false;
2477 dsp->booted = false;
2478
2479 /* Halt the core */
2480 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2481 ADSP1_CORE_ENA | ADSP1_START, 0);
2482
2483 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19,
2484 ADSP1_WDMA_BUFFER_LENGTH_MASK, 0);
2485
2486 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2487 ADSP1_SYS_ENA, 0);
2488
2489 list_for_each_entry(ctl, &dsp->ctl_list, list)
2490 ctl->enabled = 0;
2491
2492
2493 wm_adsp_free_alg_regions(dsp);
2494 break;
2495
2496 default:
2497 break;
2498 }
2499
2500 mutex_unlock(&dsp->pwr_lock);
2501
2502 return 0;
2503
2504 err_ena:
2505 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2506 ADSP1_SYS_ENA, 0);
2507 err_mutex:
2508 mutex_unlock(&dsp->pwr_lock);
2509
2510 return ret;
2511 }
2512 EXPORT_SYMBOL_GPL(wm_adsp1_event);
2513
wm_adsp2_ena(struct wm_adsp * dsp)2514 static int wm_adsp2_ena(struct wm_adsp *dsp)
2515 {
2516 unsigned int val;
2517 int ret, count;
2518
2519 switch (dsp->rev) {
2520 case 0:
2521 ret = regmap_update_bits_async(dsp->regmap,
2522 dsp->base + ADSP2_CONTROL,
2523 ADSP2_SYS_ENA, ADSP2_SYS_ENA);
2524 if (ret != 0)
2525 return ret;
2526 break;
2527 default:
2528 break;
2529 }
2530
2531 /* Wait for the RAM to start, should be near instantaneous */
2532 for (count = 0; count < 10; ++count) {
2533 ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1, &val);
2534 if (ret != 0)
2535 return ret;
2536
2537 if (val & ADSP2_RAM_RDY)
2538 break;
2539
2540 usleep_range(250, 500);
2541 }
2542
2543 if (!(val & ADSP2_RAM_RDY)) {
2544 adsp_err(dsp, "Failed to start DSP RAM\n");
2545 return -EBUSY;
2546 }
2547
2548 adsp_dbg(dsp, "RAM ready after %d polls\n", count);
2549
2550 return 0;
2551 }
2552
wm_adsp2_boot_work(struct work_struct * work)2553 static void wm_adsp2_boot_work(struct work_struct *work)
2554 {
2555 struct wm_adsp *dsp = container_of(work,
2556 struct wm_adsp,
2557 boot_work);
2558 int ret;
2559
2560 mutex_lock(&dsp->pwr_lock);
2561
2562 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2563 ADSP2_MEM_ENA, ADSP2_MEM_ENA);
2564 if (ret != 0)
2565 goto err_mutex;
2566
2567 ret = wm_adsp2_ena(dsp);
2568 if (ret != 0)
2569 goto err_mem;
2570
2571 ret = wm_adsp_load(dsp);
2572 if (ret != 0)
2573 goto err_ena;
2574
2575 ret = wm_adsp2_setup_algs(dsp);
2576 if (ret != 0)
2577 goto err_ena;
2578
2579 ret = wm_adsp_load_coeff(dsp);
2580 if (ret != 0)
2581 goto err_ena;
2582
2583 /* Initialize caches for enabled and unset controls */
2584 ret = wm_coeff_init_control_caches(dsp);
2585 if (ret != 0)
2586 goto err_ena;
2587
2588 switch (dsp->rev) {
2589 case 0:
2590 /* Turn DSP back off until we are ready to run */
2591 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2592 ADSP2_SYS_ENA, 0);
2593 if (ret != 0)
2594 goto err_ena;
2595 break;
2596 default:
2597 break;
2598 }
2599
2600 dsp->booted = true;
2601
2602 mutex_unlock(&dsp->pwr_lock);
2603
2604 return;
2605
2606 err_ena:
2607 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2608 ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
2609 err_mem:
2610 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2611 ADSP2_MEM_ENA, 0);
2612 err_mutex:
2613 mutex_unlock(&dsp->pwr_lock);
2614 }
2615
wm_adsp2_set_dspclk(struct wm_adsp * dsp,unsigned int freq)2616 static void wm_adsp2_set_dspclk(struct wm_adsp *dsp, unsigned int freq)
2617 {
2618 int ret;
2619
2620 switch (dsp->rev) {
2621 case 0:
2622 ret = regmap_update_bits_async(dsp->regmap,
2623 dsp->base + ADSP2_CLOCKING,
2624 ADSP2_CLK_SEL_MASK,
2625 freq << ADSP2_CLK_SEL_SHIFT);
2626 if (ret) {
2627 adsp_err(dsp, "Failed to set clock rate: %d\n", ret);
2628 return;
2629 }
2630 break;
2631 default:
2632 /* clock is handled by parent codec driver */
2633 break;
2634 }
2635 }
2636
wm_adsp2_preloader_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2637 int wm_adsp2_preloader_get(struct snd_kcontrol *kcontrol,
2638 struct snd_ctl_elem_value *ucontrol)
2639 {
2640 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
2641 struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
2642
2643 ucontrol->value.integer.value[0] = dsp->preloaded;
2644
2645 return 0;
2646 }
2647 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_get);
2648
wm_adsp2_preloader_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2649 int wm_adsp2_preloader_put(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
2651 {
2652 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
2653 struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
2654 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
2655 struct soc_mixer_control *mc =
2656 (struct soc_mixer_control *)kcontrol->private_value;
2657 char preload[32];
2658
2659 snprintf(preload, ARRAY_SIZE(preload), "DSP%u Preload", mc->shift);
2660
2661 dsp->preloaded = ucontrol->value.integer.value[0];
2662
2663 if (ucontrol->value.integer.value[0])
2664 snd_soc_dapm_force_enable_pin(dapm, preload);
2665 else
2666 snd_soc_dapm_disable_pin(dapm, preload);
2667
2668 snd_soc_dapm_sync(dapm);
2669
2670 return 0;
2671 }
2672 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_put);
2673
wm_adsp_stop_watchdog(struct wm_adsp * dsp)2674 static void wm_adsp_stop_watchdog(struct wm_adsp *dsp)
2675 {
2676 switch (dsp->rev) {
2677 case 0:
2678 case 1:
2679 return;
2680 default:
2681 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_WATCHDOG,
2682 ADSP2_WDT_ENA_MASK, 0);
2683 }
2684 }
2685
wm_adsp2_early_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event,unsigned int freq)2686 int wm_adsp2_early_event(struct snd_soc_dapm_widget *w,
2687 struct snd_kcontrol *kcontrol, int event,
2688 unsigned int freq)
2689 {
2690 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2691 struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
2692 struct wm_adsp *dsp = &dsps[w->shift];
2693 struct wm_coeff_ctl *ctl;
2694
2695 switch (event) {
2696 case SND_SOC_DAPM_PRE_PMU:
2697 wm_adsp2_set_dspclk(dsp, freq);
2698 queue_work(system_unbound_wq, &dsp->boot_work);
2699 break;
2700 case SND_SOC_DAPM_PRE_PMD:
2701 mutex_lock(&dsp->pwr_lock);
2702
2703 wm_adsp_debugfs_clear(dsp);
2704
2705 dsp->fw_id = 0;
2706 dsp->fw_id_version = 0;
2707
2708 dsp->booted = false;
2709
2710 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2711 ADSP2_MEM_ENA, 0);
2712
2713 list_for_each_entry(ctl, &dsp->ctl_list, list)
2714 ctl->enabled = 0;
2715
2716 wm_adsp_free_alg_regions(dsp);
2717
2718 mutex_unlock(&dsp->pwr_lock);
2719
2720 adsp_dbg(dsp, "Shutdown complete\n");
2721 break;
2722 default:
2723 break;
2724 }
2725
2726 return 0;
2727 }
2728 EXPORT_SYMBOL_GPL(wm_adsp2_early_event);
2729
wm_adsp2_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)2730 int wm_adsp2_event(struct snd_soc_dapm_widget *w,
2731 struct snd_kcontrol *kcontrol, int event)
2732 {
2733 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2734 struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
2735 struct wm_adsp *dsp = &dsps[w->shift];
2736 int ret;
2737
2738 switch (event) {
2739 case SND_SOC_DAPM_POST_PMU:
2740 flush_work(&dsp->boot_work);
2741
2742 mutex_lock(&dsp->pwr_lock);
2743
2744 if (!dsp->booted) {
2745 ret = -EIO;
2746 goto err;
2747 }
2748
2749 ret = wm_adsp2_ena(dsp);
2750 if (ret != 0)
2751 goto err;
2752
2753 /* Sync set controls */
2754 ret = wm_coeff_sync_controls(dsp);
2755 if (ret != 0)
2756 goto err;
2757
2758 wm_adsp2_lock(dsp, dsp->lock_regions);
2759
2760 ret = regmap_update_bits(dsp->regmap,
2761 dsp->base + ADSP2_CONTROL,
2762 ADSP2_CORE_ENA | ADSP2_START,
2763 ADSP2_CORE_ENA | ADSP2_START);
2764 if (ret != 0)
2765 goto err;
2766
2767 if (wm_adsp_fw[dsp->fw].num_caps != 0) {
2768 ret = wm_adsp_buffer_init(dsp);
2769 if (ret < 0)
2770 goto err;
2771 }
2772
2773 dsp->running = true;
2774
2775 mutex_unlock(&dsp->pwr_lock);
2776
2777 break;
2778
2779 case SND_SOC_DAPM_PRE_PMD:
2780 /* Tell the firmware to cleanup */
2781 wm_adsp_signal_event_controls(dsp, WM_ADSP_FW_EVENT_SHUTDOWN);
2782
2783 wm_adsp_stop_watchdog(dsp);
2784
2785 /* Log firmware state, it can be useful for analysis */
2786 switch (dsp->rev) {
2787 case 0:
2788 wm_adsp2_show_fw_status(dsp);
2789 break;
2790 default:
2791 wm_adsp2v2_show_fw_status(dsp);
2792 break;
2793 }
2794
2795 mutex_lock(&dsp->pwr_lock);
2796
2797 dsp->running = false;
2798
2799 regmap_update_bits(dsp->regmap,
2800 dsp->base + ADSP2_CONTROL,
2801 ADSP2_CORE_ENA | ADSP2_START, 0);
2802
2803 /* Make sure DMAs are quiesced */
2804 switch (dsp->rev) {
2805 case 0:
2806 regmap_write(dsp->regmap,
2807 dsp->base + ADSP2_RDMA_CONFIG_1, 0);
2808 regmap_write(dsp->regmap,
2809 dsp->base + ADSP2_WDMA_CONFIG_1, 0);
2810 regmap_write(dsp->regmap,
2811 dsp->base + ADSP2_WDMA_CONFIG_2, 0);
2812
2813 regmap_update_bits(dsp->regmap,
2814 dsp->base + ADSP2_CONTROL,
2815 ADSP2_SYS_ENA, 0);
2816 break;
2817 default:
2818 regmap_write(dsp->regmap,
2819 dsp->base + ADSP2_RDMA_CONFIG_1, 0);
2820 regmap_write(dsp->regmap,
2821 dsp->base + ADSP2_WDMA_CONFIG_1, 0);
2822 regmap_write(dsp->regmap,
2823 dsp->base + ADSP2V2_WDMA_CONFIG_2, 0);
2824 break;
2825 }
2826
2827 if (wm_adsp_fw[dsp->fw].num_caps != 0)
2828 wm_adsp_buffer_free(dsp);
2829
2830 mutex_unlock(&dsp->pwr_lock);
2831
2832 adsp_dbg(dsp, "Execution stopped\n");
2833 break;
2834
2835 default:
2836 break;
2837 }
2838
2839 return 0;
2840 err:
2841 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2842 ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
2843 mutex_unlock(&dsp->pwr_lock);
2844 return ret;
2845 }
2846 EXPORT_SYMBOL_GPL(wm_adsp2_event);
2847
wm_adsp2_codec_probe(struct wm_adsp * dsp,struct snd_soc_codec * codec)2848 int wm_adsp2_codec_probe(struct wm_adsp *dsp, struct snd_soc_codec *codec)
2849 {
2850 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
2851 char preload[32];
2852
2853 snprintf(preload, ARRAY_SIZE(preload), "DSP%d Preload", dsp->num);
2854 snd_soc_dapm_disable_pin(dapm, preload);
2855
2856 wm_adsp2_init_debugfs(dsp, codec);
2857
2858 dsp->codec = codec;
2859
2860 return snd_soc_add_codec_controls(codec,
2861 &wm_adsp_fw_controls[dsp->num - 1],
2862 1);
2863 }
2864 EXPORT_SYMBOL_GPL(wm_adsp2_codec_probe);
2865
wm_adsp2_codec_remove(struct wm_adsp * dsp,struct snd_soc_codec * codec)2866 int wm_adsp2_codec_remove(struct wm_adsp *dsp, struct snd_soc_codec *codec)
2867 {
2868 wm_adsp2_cleanup_debugfs(dsp);
2869
2870 return 0;
2871 }
2872 EXPORT_SYMBOL_GPL(wm_adsp2_codec_remove);
2873
wm_adsp2_init(struct wm_adsp * dsp)2874 int wm_adsp2_init(struct wm_adsp *dsp)
2875 {
2876 int ret;
2877
2878 switch (dsp->rev) {
2879 case 0:
2880 /*
2881 * Disable the DSP memory by default when in reset for a small
2882 * power saving.
2883 */
2884 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2885 ADSP2_MEM_ENA, 0);
2886 if (ret) {
2887 adsp_err(dsp,
2888 "Failed to clear memory retention: %d\n", ret);
2889 return ret;
2890 }
2891 break;
2892 default:
2893 break;
2894 }
2895
2896 INIT_LIST_HEAD(&dsp->alg_regions);
2897 INIT_LIST_HEAD(&dsp->ctl_list);
2898 INIT_WORK(&dsp->boot_work, wm_adsp2_boot_work);
2899
2900 mutex_init(&dsp->pwr_lock);
2901
2902 return 0;
2903 }
2904 EXPORT_SYMBOL_GPL(wm_adsp2_init);
2905
wm_adsp2_remove(struct wm_adsp * dsp)2906 void wm_adsp2_remove(struct wm_adsp *dsp)
2907 {
2908 struct wm_coeff_ctl *ctl;
2909
2910 while (!list_empty(&dsp->ctl_list)) {
2911 ctl = list_first_entry(&dsp->ctl_list, struct wm_coeff_ctl,
2912 list);
2913 list_del(&ctl->list);
2914 wm_adsp_free_ctl_blk(ctl);
2915 }
2916 }
2917 EXPORT_SYMBOL_GPL(wm_adsp2_remove);
2918
wm_adsp_compr_attached(struct wm_adsp_compr * compr)2919 static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr)
2920 {
2921 return compr->buf != NULL;
2922 }
2923
wm_adsp_compr_attach(struct wm_adsp_compr * compr)2924 static int wm_adsp_compr_attach(struct wm_adsp_compr *compr)
2925 {
2926 /*
2927 * Note this will be more complex once each DSP can support multiple
2928 * streams
2929 */
2930 if (!compr->dsp->buffer)
2931 return -EINVAL;
2932
2933 compr->buf = compr->dsp->buffer;
2934 compr->buf->compr = compr;
2935
2936 return 0;
2937 }
2938
wm_adsp_compr_detach(struct wm_adsp_compr * compr)2939 static void wm_adsp_compr_detach(struct wm_adsp_compr *compr)
2940 {
2941 if (!compr)
2942 return;
2943
2944 /* Wake the poll so it can see buffer is no longer attached */
2945 if (compr->stream)
2946 snd_compr_fragment_elapsed(compr->stream);
2947
2948 if (wm_adsp_compr_attached(compr)) {
2949 compr->buf->compr = NULL;
2950 compr->buf = NULL;
2951 }
2952 }
2953
wm_adsp_compr_open(struct wm_adsp * dsp,struct snd_compr_stream * stream)2954 int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream)
2955 {
2956 struct wm_adsp_compr *compr;
2957 int ret = 0;
2958
2959 mutex_lock(&dsp->pwr_lock);
2960
2961 if (wm_adsp_fw[dsp->fw].num_caps == 0) {
2962 adsp_err(dsp, "Firmware does not support compressed API\n");
2963 ret = -ENXIO;
2964 goto out;
2965 }
2966
2967 if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) {
2968 adsp_err(dsp, "Firmware does not support stream direction\n");
2969 ret = -EINVAL;
2970 goto out;
2971 }
2972
2973 if (dsp->compr) {
2974 /* It is expect this limitation will be removed in future */
2975 adsp_err(dsp, "Only a single stream supported per DSP\n");
2976 ret = -EBUSY;
2977 goto out;
2978 }
2979
2980 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
2981 if (!compr) {
2982 ret = -ENOMEM;
2983 goto out;
2984 }
2985
2986 compr->dsp = dsp;
2987 compr->stream = stream;
2988
2989 dsp->compr = compr;
2990
2991 stream->runtime->private_data = compr;
2992
2993 out:
2994 mutex_unlock(&dsp->pwr_lock);
2995
2996 return ret;
2997 }
2998 EXPORT_SYMBOL_GPL(wm_adsp_compr_open);
2999
wm_adsp_compr_free(struct snd_compr_stream * stream)3000 int wm_adsp_compr_free(struct snd_compr_stream *stream)
3001 {
3002 struct wm_adsp_compr *compr = stream->runtime->private_data;
3003 struct wm_adsp *dsp = compr->dsp;
3004
3005 mutex_lock(&dsp->pwr_lock);
3006
3007 wm_adsp_compr_detach(compr);
3008 dsp->compr = NULL;
3009
3010 kfree(compr->raw_buf);
3011 kfree(compr);
3012
3013 mutex_unlock(&dsp->pwr_lock);
3014
3015 return 0;
3016 }
3017 EXPORT_SYMBOL_GPL(wm_adsp_compr_free);
3018
wm_adsp_compr_check_params(struct snd_compr_stream * stream,struct snd_compr_params * params)3019 static int wm_adsp_compr_check_params(struct snd_compr_stream *stream,
3020 struct snd_compr_params *params)
3021 {
3022 struct wm_adsp_compr *compr = stream->runtime->private_data;
3023 struct wm_adsp *dsp = compr->dsp;
3024 const struct wm_adsp_fw_caps *caps;
3025 const struct snd_codec_desc *desc;
3026 int i, j;
3027
3028 if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE ||
3029 params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE ||
3030 params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS ||
3031 params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS ||
3032 params->buffer.fragment_size % WM_ADSP_DATA_WORD_SIZE) {
3033 adsp_err(dsp, "Invalid buffer fragsize=%d fragments=%d\n",
3034 params->buffer.fragment_size,
3035 params->buffer.fragments);
3036
3037 return -EINVAL;
3038 }
3039
3040 for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) {
3041 caps = &wm_adsp_fw[dsp->fw].caps[i];
3042 desc = &caps->desc;
3043
3044 if (caps->id != params->codec.id)
3045 continue;
3046
3047 if (stream->direction == SND_COMPRESS_PLAYBACK) {
3048 if (desc->max_ch < params->codec.ch_out)
3049 continue;
3050 } else {
3051 if (desc->max_ch < params->codec.ch_in)
3052 continue;
3053 }
3054
3055 if (!(desc->formats & (1 << params->codec.format)))
3056 continue;
3057
3058 for (j = 0; j < desc->num_sample_rates; ++j)
3059 if (desc->sample_rates[j] == params->codec.sample_rate)
3060 return 0;
3061 }
3062
3063 adsp_err(dsp, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n",
3064 params->codec.id, params->codec.ch_in, params->codec.ch_out,
3065 params->codec.sample_rate, params->codec.format);
3066 return -EINVAL;
3067 }
3068
wm_adsp_compr_frag_words(struct wm_adsp_compr * compr)3069 static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr)
3070 {
3071 return compr->size.fragment_size / WM_ADSP_DATA_WORD_SIZE;
3072 }
3073
wm_adsp_compr_set_params(struct snd_compr_stream * stream,struct snd_compr_params * params)3074 int wm_adsp_compr_set_params(struct snd_compr_stream *stream,
3075 struct snd_compr_params *params)
3076 {
3077 struct wm_adsp_compr *compr = stream->runtime->private_data;
3078 unsigned int size;
3079 int ret;
3080
3081 ret = wm_adsp_compr_check_params(stream, params);
3082 if (ret)
3083 return ret;
3084
3085 compr->size = params->buffer;
3086
3087 adsp_dbg(compr->dsp, "fragment_size=%d fragments=%d\n",
3088 compr->size.fragment_size, compr->size.fragments);
3089
3090 size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf);
3091 compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL);
3092 if (!compr->raw_buf)
3093 return -ENOMEM;
3094
3095 compr->sample_rate = params->codec.sample_rate;
3096
3097 return 0;
3098 }
3099 EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params);
3100
wm_adsp_compr_get_caps(struct snd_compr_stream * stream,struct snd_compr_caps * caps)3101 int wm_adsp_compr_get_caps(struct snd_compr_stream *stream,
3102 struct snd_compr_caps *caps)
3103 {
3104 struct wm_adsp_compr *compr = stream->runtime->private_data;
3105 int fw = compr->dsp->fw;
3106 int i;
3107
3108 if (wm_adsp_fw[fw].caps) {
3109 for (i = 0; i < wm_adsp_fw[fw].num_caps; i++)
3110 caps->codecs[i] = wm_adsp_fw[fw].caps[i].id;
3111
3112 caps->num_codecs = i;
3113 caps->direction = wm_adsp_fw[fw].compr_direction;
3114
3115 caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE;
3116 caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE;
3117 caps->min_fragments = WM_ADSP_MIN_FRAGMENTS;
3118 caps->max_fragments = WM_ADSP_MAX_FRAGMENTS;
3119 }
3120
3121 return 0;
3122 }
3123 EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps);
3124
wm_adsp_read_data_block(struct wm_adsp * dsp,int mem_type,unsigned int mem_addr,unsigned int num_words,u32 * data)3125 static int wm_adsp_read_data_block(struct wm_adsp *dsp, int mem_type,
3126 unsigned int mem_addr,
3127 unsigned int num_words, u32 *data)
3128 {
3129 struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
3130 unsigned int i, reg;
3131 int ret;
3132
3133 if (!mem)
3134 return -EINVAL;
3135
3136 reg = wm_adsp_region_to_reg(mem, mem_addr);
3137
3138 ret = regmap_raw_read(dsp->regmap, reg, data,
3139 sizeof(*data) * num_words);
3140 if (ret < 0)
3141 return ret;
3142
3143 for (i = 0; i < num_words; ++i)
3144 data[i] = be32_to_cpu(data[i]) & 0x00ffffffu;
3145
3146 return 0;
3147 }
3148
wm_adsp_read_data_word(struct wm_adsp * dsp,int mem_type,unsigned int mem_addr,u32 * data)3149 static inline int wm_adsp_read_data_word(struct wm_adsp *dsp, int mem_type,
3150 unsigned int mem_addr, u32 *data)
3151 {
3152 return wm_adsp_read_data_block(dsp, mem_type, mem_addr, 1, data);
3153 }
3154
wm_adsp_write_data_word(struct wm_adsp * dsp,int mem_type,unsigned int mem_addr,u32 data)3155 static int wm_adsp_write_data_word(struct wm_adsp *dsp, int mem_type,
3156 unsigned int mem_addr, u32 data)
3157 {
3158 struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
3159 unsigned int reg;
3160
3161 if (!mem)
3162 return -EINVAL;
3163
3164 reg = wm_adsp_region_to_reg(mem, mem_addr);
3165
3166 data = cpu_to_be32(data & 0x00ffffffu);
3167
3168 return regmap_raw_write(dsp->regmap, reg, &data, sizeof(data));
3169 }
3170
wm_adsp_buffer_read(struct wm_adsp_compr_buf * buf,unsigned int field_offset,u32 * data)3171 static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf,
3172 unsigned int field_offset, u32 *data)
3173 {
3174 return wm_adsp_read_data_word(buf->dsp, WMFW_ADSP2_XM,
3175 buf->host_buf_ptr + field_offset, data);
3176 }
3177
wm_adsp_buffer_write(struct wm_adsp_compr_buf * buf,unsigned int field_offset,u32 data)3178 static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf,
3179 unsigned int field_offset, u32 data)
3180 {
3181 return wm_adsp_write_data_word(buf->dsp, WMFW_ADSP2_XM,
3182 buf->host_buf_ptr + field_offset, data);
3183 }
3184
wm_adsp_buffer_locate(struct wm_adsp_compr_buf * buf)3185 static int wm_adsp_buffer_locate(struct wm_adsp_compr_buf *buf)
3186 {
3187 struct wm_adsp_alg_region *alg_region;
3188 struct wm_adsp *dsp = buf->dsp;
3189 u32 xmalg, addr, magic;
3190 int i, ret;
3191
3192 alg_region = wm_adsp_find_alg_region(dsp, WMFW_ADSP2_XM, dsp->fw_id);
3193 xmalg = sizeof(struct wm_adsp_system_config_xm_hdr) / sizeof(__be32);
3194
3195 addr = alg_region->base + xmalg + ALG_XM_FIELD(magic);
3196 ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr, &magic);
3197 if (ret < 0)
3198 return ret;
3199
3200 if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC)
3201 return -EINVAL;
3202
3203 addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr);
3204 for (i = 0; i < 5; ++i) {
3205 ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr,
3206 &buf->host_buf_ptr);
3207 if (ret < 0)
3208 return ret;
3209
3210 if (buf->host_buf_ptr)
3211 break;
3212
3213 usleep_range(1000, 2000);
3214 }
3215
3216 if (!buf->host_buf_ptr)
3217 return -EIO;
3218
3219 adsp_dbg(dsp, "host_buf_ptr=%x\n", buf->host_buf_ptr);
3220
3221 return 0;
3222 }
3223
wm_adsp_buffer_populate(struct wm_adsp_compr_buf * buf)3224 static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf)
3225 {
3226 const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps;
3227 struct wm_adsp_buffer_region *region;
3228 u32 offset = 0;
3229 int i, ret;
3230
3231 for (i = 0; i < caps->num_regions; ++i) {
3232 region = &buf->regions[i];
3233
3234 region->offset = offset;
3235 region->mem_type = caps->region_defs[i].mem_type;
3236
3237 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset,
3238 ®ion->base_addr);
3239 if (ret < 0)
3240 return ret;
3241
3242 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset,
3243 &offset);
3244 if (ret < 0)
3245 return ret;
3246
3247 region->cumulative_size = offset;
3248
3249 adsp_dbg(buf->dsp,
3250 "region=%d type=%d base=%04x off=%04x size=%04x\n",
3251 i, region->mem_type, region->base_addr,
3252 region->offset, region->cumulative_size);
3253 }
3254
3255 return 0;
3256 }
3257
wm_adsp_buffer_init(struct wm_adsp * dsp)3258 static int wm_adsp_buffer_init(struct wm_adsp *dsp)
3259 {
3260 struct wm_adsp_compr_buf *buf;
3261 int ret;
3262
3263 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
3264 if (!buf)
3265 return -ENOMEM;
3266
3267 buf->dsp = dsp;
3268 buf->read_index = -1;
3269 buf->irq_count = 0xFFFFFFFF;
3270
3271 ret = wm_adsp_buffer_locate(buf);
3272 if (ret < 0) {
3273 adsp_err(dsp, "Failed to acquire host buffer: %d\n", ret);
3274 goto err_buffer;
3275 }
3276
3277 buf->regions = kcalloc(wm_adsp_fw[dsp->fw].caps->num_regions,
3278 sizeof(*buf->regions), GFP_KERNEL);
3279 if (!buf->regions) {
3280 ret = -ENOMEM;
3281 goto err_buffer;
3282 }
3283
3284 ret = wm_adsp_buffer_populate(buf);
3285 if (ret < 0) {
3286 adsp_err(dsp, "Failed to populate host buffer: %d\n", ret);
3287 goto err_regions;
3288 }
3289
3290 dsp->buffer = buf;
3291
3292 return 0;
3293
3294 err_regions:
3295 kfree(buf->regions);
3296 err_buffer:
3297 kfree(buf);
3298 return ret;
3299 }
3300
wm_adsp_buffer_free(struct wm_adsp * dsp)3301 static int wm_adsp_buffer_free(struct wm_adsp *dsp)
3302 {
3303 if (dsp->buffer) {
3304 wm_adsp_compr_detach(dsp->buffer->compr);
3305
3306 kfree(dsp->buffer->regions);
3307 kfree(dsp->buffer);
3308
3309 dsp->buffer = NULL;
3310 }
3311
3312 return 0;
3313 }
3314
wm_adsp_compr_trigger(struct snd_compr_stream * stream,int cmd)3315 int wm_adsp_compr_trigger(struct snd_compr_stream *stream, int cmd)
3316 {
3317 struct wm_adsp_compr *compr = stream->runtime->private_data;
3318 struct wm_adsp *dsp = compr->dsp;
3319 int ret = 0;
3320
3321 adsp_dbg(dsp, "Trigger: %d\n", cmd);
3322
3323 mutex_lock(&dsp->pwr_lock);
3324
3325 switch (cmd) {
3326 case SNDRV_PCM_TRIGGER_START:
3327 if (wm_adsp_compr_attached(compr))
3328 break;
3329
3330 ret = wm_adsp_compr_attach(compr);
3331 if (ret < 0) {
3332 adsp_err(dsp, "Failed to link buffer and stream: %d\n",
3333 ret);
3334 break;
3335 }
3336
3337 /* Trigger the IRQ at one fragment of data */
3338 ret = wm_adsp_buffer_write(compr->buf,
3339 HOST_BUFFER_FIELD(high_water_mark),
3340 wm_adsp_compr_frag_words(compr));
3341 if (ret < 0) {
3342 adsp_err(dsp, "Failed to set high water mark: %d\n",
3343 ret);
3344 break;
3345 }
3346 break;
3347 case SNDRV_PCM_TRIGGER_STOP:
3348 break;
3349 default:
3350 ret = -EINVAL;
3351 break;
3352 }
3353
3354 mutex_unlock(&dsp->pwr_lock);
3355
3356 return ret;
3357 }
3358 EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger);
3359
wm_adsp_buffer_size(struct wm_adsp_compr_buf * buf)3360 static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf)
3361 {
3362 int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1;
3363
3364 return buf->regions[last_region].cumulative_size;
3365 }
3366
wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf * buf)3367 static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf)
3368 {
3369 u32 next_read_index, next_write_index;
3370 int write_index, read_index, avail;
3371 int ret;
3372
3373 /* Only sync read index if we haven't already read a valid index */
3374 if (buf->read_index < 0) {
3375 ret = wm_adsp_buffer_read(buf,
3376 HOST_BUFFER_FIELD(next_read_index),
3377 &next_read_index);
3378 if (ret < 0)
3379 return ret;
3380
3381 read_index = sign_extend32(next_read_index, 23);
3382
3383 if (read_index < 0) {
3384 adsp_dbg(buf->dsp, "Avail check on unstarted stream\n");
3385 return 0;
3386 }
3387
3388 buf->read_index = read_index;
3389 }
3390
3391 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index),
3392 &next_write_index);
3393 if (ret < 0)
3394 return ret;
3395
3396 write_index = sign_extend32(next_write_index, 23);
3397
3398 avail = write_index - buf->read_index;
3399 if (avail < 0)
3400 avail += wm_adsp_buffer_size(buf);
3401
3402 adsp_dbg(buf->dsp, "readindex=0x%x, writeindex=0x%x, avail=%d\n",
3403 buf->read_index, write_index, avail * WM_ADSP_DATA_WORD_SIZE);
3404
3405 buf->avail = avail;
3406
3407 return 0;
3408 }
3409
wm_adsp_buffer_get_error(struct wm_adsp_compr_buf * buf)3410 static int wm_adsp_buffer_get_error(struct wm_adsp_compr_buf *buf)
3411 {
3412 int ret;
3413
3414 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error);
3415 if (ret < 0) {
3416 adsp_err(buf->dsp, "Failed to check buffer error: %d\n", ret);
3417 return ret;
3418 }
3419 if (buf->error != 0) {
3420 adsp_err(buf->dsp, "Buffer error occurred: %d\n", buf->error);
3421 return -EIO;
3422 }
3423
3424 return 0;
3425 }
3426
wm_adsp_compr_handle_irq(struct wm_adsp * dsp)3427 int wm_adsp_compr_handle_irq(struct wm_adsp *dsp)
3428 {
3429 struct wm_adsp_compr_buf *buf;
3430 struct wm_adsp_compr *compr;
3431 int ret = 0;
3432
3433 mutex_lock(&dsp->pwr_lock);
3434
3435 buf = dsp->buffer;
3436 compr = dsp->compr;
3437
3438 if (!buf) {
3439 ret = -ENODEV;
3440 goto out;
3441 }
3442
3443 adsp_dbg(dsp, "Handling buffer IRQ\n");
3444
3445 ret = wm_adsp_buffer_get_error(buf);
3446 if (ret < 0)
3447 goto out_notify; /* Wake poll to report error */
3448
3449 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count),
3450 &buf->irq_count);
3451 if (ret < 0) {
3452 adsp_err(dsp, "Failed to get irq_count: %d\n", ret);
3453 goto out;
3454 }
3455
3456 ret = wm_adsp_buffer_update_avail(buf);
3457 if (ret < 0) {
3458 adsp_err(dsp, "Error reading avail: %d\n", ret);
3459 goto out;
3460 }
3461
3462 if (wm_adsp_fw[dsp->fw].voice_trigger && buf->irq_count == 2)
3463 ret = WM_ADSP_COMPR_VOICE_TRIGGER;
3464
3465 out_notify:
3466 if (compr && compr->stream)
3467 snd_compr_fragment_elapsed(compr->stream);
3468
3469 out:
3470 mutex_unlock(&dsp->pwr_lock);
3471
3472 return ret;
3473 }
3474 EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq);
3475
wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf * buf)3476 static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf)
3477 {
3478 if (buf->irq_count & 0x01)
3479 return 0;
3480
3481 adsp_dbg(buf->dsp, "Enable IRQ(0x%x) for next fragment\n",
3482 buf->irq_count);
3483
3484 buf->irq_count |= 0x01;
3485
3486 return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack),
3487 buf->irq_count);
3488 }
3489
wm_adsp_compr_pointer(struct snd_compr_stream * stream,struct snd_compr_tstamp * tstamp)3490 int wm_adsp_compr_pointer(struct snd_compr_stream *stream,
3491 struct snd_compr_tstamp *tstamp)
3492 {
3493 struct wm_adsp_compr *compr = stream->runtime->private_data;
3494 struct wm_adsp *dsp = compr->dsp;
3495 struct wm_adsp_compr_buf *buf;
3496 int ret = 0;
3497
3498 adsp_dbg(dsp, "Pointer request\n");
3499
3500 mutex_lock(&dsp->pwr_lock);
3501
3502 buf = compr->buf;
3503
3504 if (!compr->buf || compr->buf->error) {
3505 snd_compr_stop_error(stream, SNDRV_PCM_STATE_XRUN);
3506 ret = -EIO;
3507 goto out;
3508 }
3509
3510 if (buf->avail < wm_adsp_compr_frag_words(compr)) {
3511 ret = wm_adsp_buffer_update_avail(buf);
3512 if (ret < 0) {
3513 adsp_err(dsp, "Error reading avail: %d\n", ret);
3514 goto out;
3515 }
3516
3517 /*
3518 * If we really have less than 1 fragment available tell the
3519 * DSP to inform us once a whole fragment is available.
3520 */
3521 if (buf->avail < wm_adsp_compr_frag_words(compr)) {
3522 ret = wm_adsp_buffer_get_error(buf);
3523 if (ret < 0) {
3524 if (compr->buf->error)
3525 snd_compr_stop_error(stream,
3526 SNDRV_PCM_STATE_XRUN);
3527 goto out;
3528 }
3529
3530 ret = wm_adsp_buffer_reenable_irq(buf);
3531 if (ret < 0) {
3532 adsp_err(dsp,
3533 "Failed to re-enable buffer IRQ: %d\n",
3534 ret);
3535 goto out;
3536 }
3537 }
3538 }
3539
3540 tstamp->copied_total = compr->copied_total;
3541 tstamp->copied_total += buf->avail * WM_ADSP_DATA_WORD_SIZE;
3542 tstamp->sampling_rate = compr->sample_rate;
3543
3544 out:
3545 mutex_unlock(&dsp->pwr_lock);
3546
3547 return ret;
3548 }
3549 EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer);
3550
wm_adsp_buffer_capture_block(struct wm_adsp_compr * compr,int target)3551 static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target)
3552 {
3553 struct wm_adsp_compr_buf *buf = compr->buf;
3554 u8 *pack_in = (u8 *)compr->raw_buf;
3555 u8 *pack_out = (u8 *)compr->raw_buf;
3556 unsigned int adsp_addr;
3557 int mem_type, nwords, max_read;
3558 int i, j, ret;
3559
3560 /* Calculate read parameters */
3561 for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i)
3562 if (buf->read_index < buf->regions[i].cumulative_size)
3563 break;
3564
3565 if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions)
3566 return -EINVAL;
3567
3568 mem_type = buf->regions[i].mem_type;
3569 adsp_addr = buf->regions[i].base_addr +
3570 (buf->read_index - buf->regions[i].offset);
3571
3572 max_read = wm_adsp_compr_frag_words(compr);
3573 nwords = buf->regions[i].cumulative_size - buf->read_index;
3574
3575 if (nwords > target)
3576 nwords = target;
3577 if (nwords > buf->avail)
3578 nwords = buf->avail;
3579 if (nwords > max_read)
3580 nwords = max_read;
3581 if (!nwords)
3582 return 0;
3583
3584 /* Read data from DSP */
3585 ret = wm_adsp_read_data_block(buf->dsp, mem_type, adsp_addr,
3586 nwords, compr->raw_buf);
3587 if (ret < 0)
3588 return ret;
3589
3590 /* Remove the padding bytes from the data read from the DSP */
3591 for (i = 0; i < nwords; i++) {
3592 for (j = 0; j < WM_ADSP_DATA_WORD_SIZE; j++)
3593 *pack_out++ = *pack_in++;
3594
3595 pack_in += sizeof(*(compr->raw_buf)) - WM_ADSP_DATA_WORD_SIZE;
3596 }
3597
3598 /* update read index to account for words read */
3599 buf->read_index += nwords;
3600 if (buf->read_index == wm_adsp_buffer_size(buf))
3601 buf->read_index = 0;
3602
3603 ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index),
3604 buf->read_index);
3605 if (ret < 0)
3606 return ret;
3607
3608 /* update avail to account for words read */
3609 buf->avail -= nwords;
3610
3611 return nwords;
3612 }
3613
wm_adsp_compr_read(struct wm_adsp_compr * compr,char __user * buf,size_t count)3614 static int wm_adsp_compr_read(struct wm_adsp_compr *compr,
3615 char __user *buf, size_t count)
3616 {
3617 struct wm_adsp *dsp = compr->dsp;
3618 int ntotal = 0;
3619 int nwords, nbytes;
3620
3621 adsp_dbg(dsp, "Requested read of %zu bytes\n", count);
3622
3623 if (!compr->buf || compr->buf->error) {
3624 snd_compr_stop_error(compr->stream, SNDRV_PCM_STATE_XRUN);
3625 return -EIO;
3626 }
3627
3628 count /= WM_ADSP_DATA_WORD_SIZE;
3629
3630 do {
3631 nwords = wm_adsp_buffer_capture_block(compr, count);
3632 if (nwords < 0) {
3633 adsp_err(dsp, "Failed to capture block: %d\n", nwords);
3634 return nwords;
3635 }
3636
3637 nbytes = nwords * WM_ADSP_DATA_WORD_SIZE;
3638
3639 adsp_dbg(dsp, "Read %d bytes\n", nbytes);
3640
3641 if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) {
3642 adsp_err(dsp, "Failed to copy data to user: %d, %d\n",
3643 ntotal, nbytes);
3644 return -EFAULT;
3645 }
3646
3647 count -= nwords;
3648 ntotal += nbytes;
3649 } while (nwords > 0 && count > 0);
3650
3651 compr->copied_total += ntotal;
3652
3653 return ntotal;
3654 }
3655
wm_adsp_compr_copy(struct snd_compr_stream * stream,char __user * buf,size_t count)3656 int wm_adsp_compr_copy(struct snd_compr_stream *stream, char __user *buf,
3657 size_t count)
3658 {
3659 struct wm_adsp_compr *compr = stream->runtime->private_data;
3660 struct wm_adsp *dsp = compr->dsp;
3661 int ret;
3662
3663 mutex_lock(&dsp->pwr_lock);
3664
3665 if (stream->direction == SND_COMPRESS_CAPTURE)
3666 ret = wm_adsp_compr_read(compr, buf, count);
3667 else
3668 ret = -ENOTSUPP;
3669
3670 mutex_unlock(&dsp->pwr_lock);
3671
3672 return ret;
3673 }
3674 EXPORT_SYMBOL_GPL(wm_adsp_compr_copy);
3675
wm_adsp2_lock(struct wm_adsp * dsp,unsigned int lock_regions)3676 int wm_adsp2_lock(struct wm_adsp *dsp, unsigned int lock_regions)
3677 {
3678 struct regmap *regmap = dsp->regmap;
3679 unsigned int code0, code1, lock_reg;
3680
3681 if (!(lock_regions & WM_ADSP2_REGION_ALL))
3682 return 0;
3683
3684 lock_regions &= WM_ADSP2_REGION_ALL;
3685 lock_reg = dsp->base + ADSP2_LOCK_REGION_1_LOCK_REGION_0;
3686
3687 while (lock_regions) {
3688 code0 = code1 = 0;
3689 if (lock_regions & BIT(0)) {
3690 code0 = ADSP2_LOCK_CODE_0;
3691 code1 = ADSP2_LOCK_CODE_1;
3692 }
3693 if (lock_regions & BIT(1)) {
3694 code0 |= ADSP2_LOCK_CODE_0 << ADSP2_LOCK_REGION_SHIFT;
3695 code1 |= ADSP2_LOCK_CODE_1 << ADSP2_LOCK_REGION_SHIFT;
3696 }
3697 regmap_write(regmap, lock_reg, code0);
3698 regmap_write(regmap, lock_reg, code1);
3699 lock_regions >>= 2;
3700 lock_reg += 2;
3701 }
3702
3703 return 0;
3704 }
3705 EXPORT_SYMBOL_GPL(wm_adsp2_lock);
3706
wm_adsp2_bus_error(struct wm_adsp * dsp)3707 irqreturn_t wm_adsp2_bus_error(struct wm_adsp *dsp)
3708 {
3709 unsigned int val;
3710 struct regmap *regmap = dsp->regmap;
3711 int ret = 0;
3712
3713 mutex_lock(&dsp->pwr_lock);
3714
3715 ret = regmap_read(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL, &val);
3716 if (ret) {
3717 adsp_err(dsp,
3718 "Failed to read Region Lock Ctrl register: %d\n", ret);
3719 goto error;
3720 }
3721
3722 if (val & ADSP2_WDT_TIMEOUT_STS_MASK) {
3723 adsp_err(dsp, "watchdog timeout error\n");
3724 wm_adsp_stop_watchdog(dsp);
3725 }
3726
3727 if (val & (ADSP2_SLAVE_ERR_MASK | ADSP2_REGION_LOCK_ERR_MASK)) {
3728 if (val & ADSP2_SLAVE_ERR_MASK)
3729 adsp_err(dsp, "bus error: slave error\n");
3730 else
3731 adsp_err(dsp, "bus error: region lock error\n");
3732
3733 ret = regmap_read(regmap, dsp->base + ADSP2_BUS_ERR_ADDR, &val);
3734 if (ret) {
3735 adsp_err(dsp,
3736 "Failed to read Bus Err Addr register: %d\n",
3737 ret);
3738 goto error;
3739 }
3740
3741 adsp_err(dsp, "bus error address = 0x%x\n",
3742 val & ADSP2_BUS_ERR_ADDR_MASK);
3743
3744 ret = regmap_read(regmap,
3745 dsp->base + ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR,
3746 &val);
3747 if (ret) {
3748 adsp_err(dsp,
3749 "Failed to read Pmem Xmem Err Addr register: %d\n",
3750 ret);
3751 goto error;
3752 }
3753
3754 adsp_err(dsp, "xmem error address = 0x%x\n",
3755 val & ADSP2_XMEM_ERR_ADDR_MASK);
3756 adsp_err(dsp, "pmem error address = 0x%x\n",
3757 (val & ADSP2_PMEM_ERR_ADDR_MASK) >>
3758 ADSP2_PMEM_ERR_ADDR_SHIFT);
3759 }
3760
3761 regmap_update_bits(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL,
3762 ADSP2_CTRL_ERR_EINT, ADSP2_CTRL_ERR_EINT);
3763
3764 error:
3765 mutex_unlock(&dsp->pwr_lock);
3766
3767 return IRQ_HANDLED;
3768 }
3769 EXPORT_SYMBOL_GPL(wm_adsp2_bus_error);
3770
3771 MODULE_LICENSE("GPL v2");
3772