blob: d3d32b501acae75da07334b6b3ff527433970292 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mark Brown2159ad932012-10-11 11:54:02 +09002/*
3 * wm_adsp.c -- Wolfson ADSP support
4 *
5 * Copyright 2012 Wolfson Microelectronics plc
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brown2159ad932012-10-11 11:54:02 +09008 */
9
Richard Fitzgerald605391d2018-08-08 17:13:39 +010010#include <linux/ctype.h>
Mark Brown2159ad932012-10-11 11:54:02 +090011#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/init.h>
14#include <linux/delay.h>
15#include <linux/firmware.h>
Mark Browncf17c832013-01-30 14:37:23 +080016#include <linux/list.h>
Mark Brown2159ad932012-10-11 11:54:02 +090017#include <linux/pm.h>
18#include <linux/pm_runtime.h>
19#include <linux/regmap.h>
Mark Brown973838a2012-11-28 17:20:32 +000020#include <linux/regulator/consumer.h>
Mark Brown2159ad932012-10-11 11:54:02 +090021#include <linux/slab.h>
Charles Keepaxcdcd7f72014-11-14 15:40:45 +000022#include <linux/vmalloc.h>
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +010023#include <linux/workqueue.h>
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +010024#include <linux/debugfs.h>
Mark Brown2159ad932012-10-11 11:54:02 +090025#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
29#include <sound/jack.h>
30#include <sound/initval.h>
31#include <sound/tlv.h>
32
Mark Brown2159ad932012-10-11 11:54:02 +090033#include "wm_adsp.h"
34
35#define adsp_crit(_dsp, fmt, ...) \
Richard Fitzgerald605391d2018-08-08 17:13:39 +010036 dev_crit(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
Mark Brown2159ad932012-10-11 11:54:02 +090037#define adsp_err(_dsp, fmt, ...) \
Richard Fitzgerald605391d2018-08-08 17:13:39 +010038 dev_err(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
Mark Brown2159ad932012-10-11 11:54:02 +090039#define adsp_warn(_dsp, fmt, ...) \
Richard Fitzgerald605391d2018-08-08 17:13:39 +010040 dev_warn(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
Mark Brown2159ad932012-10-11 11:54:02 +090041#define adsp_info(_dsp, fmt, ...) \
Richard Fitzgerald605391d2018-08-08 17:13:39 +010042 dev_info(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
Mark Brown2159ad932012-10-11 11:54:02 +090043#define adsp_dbg(_dsp, fmt, ...) \
Richard Fitzgerald605391d2018-08-08 17:13:39 +010044 dev_dbg(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
Mark Brown2159ad932012-10-11 11:54:02 +090045
Charles Keepax0d3fba32019-02-22 10:04:21 +000046#define compr_err(_obj, fmt, ...) \
47 adsp_err(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \
48 ##__VA_ARGS__)
49#define compr_dbg(_obj, fmt, ...) \
50 adsp_dbg(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \
51 ##__VA_ARGS__)
52
Mark Brown2159ad932012-10-11 11:54:02 +090053#define ADSP1_CONTROL_1 0x00
54#define ADSP1_CONTROL_2 0x02
55#define ADSP1_CONTROL_3 0x03
56#define ADSP1_CONTROL_4 0x04
57#define ADSP1_CONTROL_5 0x06
58#define ADSP1_CONTROL_6 0x07
59#define ADSP1_CONTROL_7 0x08
60#define ADSP1_CONTROL_8 0x09
61#define ADSP1_CONTROL_9 0x0A
62#define ADSP1_CONTROL_10 0x0B
63#define ADSP1_CONTROL_11 0x0C
64#define ADSP1_CONTROL_12 0x0D
65#define ADSP1_CONTROL_13 0x0F
66#define ADSP1_CONTROL_14 0x10
67#define ADSP1_CONTROL_15 0x11
68#define ADSP1_CONTROL_16 0x12
69#define ADSP1_CONTROL_17 0x13
70#define ADSP1_CONTROL_18 0x14
71#define ADSP1_CONTROL_19 0x16
72#define ADSP1_CONTROL_20 0x17
73#define ADSP1_CONTROL_21 0x18
74#define ADSP1_CONTROL_22 0x1A
75#define ADSP1_CONTROL_23 0x1B
76#define ADSP1_CONTROL_24 0x1C
77#define ADSP1_CONTROL_25 0x1E
78#define ADSP1_CONTROL_26 0x20
79#define ADSP1_CONTROL_27 0x21
80#define ADSP1_CONTROL_28 0x22
81#define ADSP1_CONTROL_29 0x23
82#define ADSP1_CONTROL_30 0x24
83#define ADSP1_CONTROL_31 0x26
84
85/*
86 * ADSP1 Control 19
87 */
88#define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
89#define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
90#define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
91
92
93/*
94 * ADSP1 Control 30
95 */
96#define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */
97#define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */
98#define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */
99#define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */
100#define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
101#define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
102#define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
103#define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
104#define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
105#define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
106#define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
107#define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
108#define ADSP1_START 0x0001 /* DSP1_START */
109#define ADSP1_START_MASK 0x0001 /* DSP1_START */
110#define ADSP1_START_SHIFT 0 /* DSP1_START */
111#define ADSP1_START_WIDTH 1 /* DSP1_START */
112
Chris Rattray94e205b2013-01-18 08:43:09 +0000113/*
114 * ADSP1 Control 31
115 */
116#define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
117#define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
118#define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
119
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +0100120#define ADSP2_CONTROL 0x0
121#define ADSP2_CLOCKING 0x1
122#define ADSP2V2_CLOCKING 0x2
123#define ADSP2_STATUS1 0x4
124#define ADSP2_WDMA_CONFIG_1 0x30
125#define ADSP2_WDMA_CONFIG_2 0x31
126#define ADSP2V2_WDMA_CONFIG_2 0x32
127#define ADSP2_RDMA_CONFIG_1 0x34
Mark Brown2159ad932012-10-11 11:54:02 +0900128
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +0100129#define ADSP2_SCRATCH0 0x40
130#define ADSP2_SCRATCH1 0x41
131#define ADSP2_SCRATCH2 0x42
132#define ADSP2_SCRATCH3 0x43
133
134#define ADSP2V2_SCRATCH0_1 0x40
135#define ADSP2V2_SCRATCH2_3 0x42
Richard Fitzgerald10337b02015-05-29 10:23:07 +0100136
Mark Brown2159ad932012-10-11 11:54:02 +0900137/*
138 * ADSP2 Control
139 */
140
141#define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */
142#define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */
143#define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */
144#define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */
145#define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
146#define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
147#define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
148#define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
149#define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
150#define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
151#define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
152#define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
153#define ADSP2_START 0x0001 /* DSP1_START */
154#define ADSP2_START_MASK 0x0001 /* DSP1_START */
155#define ADSP2_START_SHIFT 0 /* DSP1_START */
156#define ADSP2_START_WIDTH 1 /* DSP1_START */
157
158/*
Mark Brown973838a2012-11-28 17:20:32 +0000159 * ADSP2 clocking
160 */
161#define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
162#define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
163#define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
164
165/*
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +0100166 * ADSP2V2 clocking
167 */
168#define ADSP2V2_CLK_SEL_MASK 0x70000 /* CLK_SEL_ENA */
169#define ADSP2V2_CLK_SEL_SHIFT 16 /* CLK_SEL_ENA */
170#define ADSP2V2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
171
172#define ADSP2V2_RATE_MASK 0x7800 /* DSP_RATE */
173#define ADSP2V2_RATE_SHIFT 11 /* DSP_RATE */
174#define ADSP2V2_RATE_WIDTH 4 /* DSP_RATE */
175
176/*
Mark Brown2159ad932012-10-11 11:54:02 +0900177 * ADSP2 Status 1
178 */
179#define ADSP2_RAM_RDY 0x0001
180#define ADSP2_RAM_RDY_MASK 0x0001
181#define ADSP2_RAM_RDY_SHIFT 0
182#define ADSP2_RAM_RDY_WIDTH 1
183
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +0100184/*
185 * ADSP2 Lock support
186 */
187#define ADSP2_LOCK_CODE_0 0x5555
188#define ADSP2_LOCK_CODE_1 0xAAAA
189
190#define ADSP2_WATCHDOG 0x0A
191#define ADSP2_BUS_ERR_ADDR 0x52
192#define ADSP2_REGION_LOCK_STATUS 0x64
193#define ADSP2_LOCK_REGION_1_LOCK_REGION_0 0x66
194#define ADSP2_LOCK_REGION_3_LOCK_REGION_2 0x68
195#define ADSP2_LOCK_REGION_5_LOCK_REGION_4 0x6A
196#define ADSP2_LOCK_REGION_7_LOCK_REGION_6 0x6C
197#define ADSP2_LOCK_REGION_9_LOCK_REGION_8 0x6E
198#define ADSP2_LOCK_REGION_CTRL 0x7A
199#define ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR 0x7C
200
201#define ADSP2_REGION_LOCK_ERR_MASK 0x8000
202#define ADSP2_SLAVE_ERR_MASK 0x4000
203#define ADSP2_WDT_TIMEOUT_STS_MASK 0x2000
204#define ADSP2_CTRL_ERR_PAUSE_ENA 0x0002
205#define ADSP2_CTRL_ERR_EINT 0x0001
206
207#define ADSP2_BUS_ERR_ADDR_MASK 0x00FFFFFF
208#define ADSP2_XMEM_ERR_ADDR_MASK 0x0000FFFF
209#define ADSP2_PMEM_ERR_ADDR_MASK 0x7FFF0000
210#define ADSP2_PMEM_ERR_ADDR_SHIFT 16
211#define ADSP2_WDT_ENA_MASK 0xFFFFFFFD
212
213#define ADSP2_LOCK_REGION_SHIFT 16
214
Charles Keepax9ee78752016-05-02 13:57:36 +0100215#define ADSP_MAX_STD_CTRL_SIZE 512
216
Richard Fitzgeraldf4f0c4c2016-11-09 17:14:17 +0000217#define WM_ADSP_ACKED_CTL_TIMEOUT_MS 100
218#define WM_ADSP_ACKED_CTL_N_QUICKPOLLS 10
Richard Fitzgeralda23ebba2016-11-09 17:14:18 +0000219#define WM_ADSP_ACKED_CTL_MIN_VALUE 0
220#define WM_ADSP_ACKED_CTL_MAX_VALUE 0xFFFFFF
Richard Fitzgeraldf4f0c4c2016-11-09 17:14:17 +0000221
222/*
223 * Event control messages
224 */
225#define WM_ADSP_FW_EVENT_SHUTDOWN 0x000001
226
Wen Shi170b1e12019-03-19 11:52:13 +0000227/*
Richard Fitzgerald2ae58132019-03-19 11:52:14 +0000228 * HALO system info
229 */
230#define HALO_AHBM_WINDOW_DEBUG_0 0x02040
231#define HALO_AHBM_WINDOW_DEBUG_1 0x02044
232
233/*
Wen Shi170b1e12019-03-19 11:52:13 +0000234 * HALO core
235 */
236#define HALO_SCRATCH1 0x005c0
237#define HALO_SCRATCH2 0x005c8
238#define HALO_SCRATCH3 0x005d0
239#define HALO_SCRATCH4 0x005d8
240#define HALO_CCM_CORE_CONTROL 0x41000
241#define HALO_CORE_SOFT_RESET 0x00010
Stuart Henderson8bc144f2019-03-19 11:52:15 +0000242#define HALO_WDT_CONTROL 0x47000
Wen Shi170b1e12019-03-19 11:52:13 +0000243
244/*
245 * HALO MPU banks
246 */
247#define HALO_MPU_XMEM_ACCESS_0 0x43000
248#define HALO_MPU_YMEM_ACCESS_0 0x43004
249#define HALO_MPU_WINDOW_ACCESS_0 0x43008
250#define HALO_MPU_XREG_ACCESS_0 0x4300C
251#define HALO_MPU_YREG_ACCESS_0 0x43014
252#define HALO_MPU_XMEM_ACCESS_1 0x43018
253#define HALO_MPU_YMEM_ACCESS_1 0x4301C
254#define HALO_MPU_WINDOW_ACCESS_1 0x43020
255#define HALO_MPU_XREG_ACCESS_1 0x43024
256#define HALO_MPU_YREG_ACCESS_1 0x4302C
257#define HALO_MPU_XMEM_ACCESS_2 0x43030
258#define HALO_MPU_YMEM_ACCESS_2 0x43034
259#define HALO_MPU_WINDOW_ACCESS_2 0x43038
260#define HALO_MPU_XREG_ACCESS_2 0x4303C
261#define HALO_MPU_YREG_ACCESS_2 0x43044
262#define HALO_MPU_XMEM_ACCESS_3 0x43048
263#define HALO_MPU_YMEM_ACCESS_3 0x4304C
264#define HALO_MPU_WINDOW_ACCESS_3 0x43050
265#define HALO_MPU_XREG_ACCESS_3 0x43054
266#define HALO_MPU_YREG_ACCESS_3 0x4305C
Richard Fitzgerald2ae58132019-03-19 11:52:14 +0000267#define HALO_MPU_XM_VIO_ADDR 0x43100
268#define HALO_MPU_XM_VIO_STATUS 0x43104
269#define HALO_MPU_YM_VIO_ADDR 0x43108
270#define HALO_MPU_YM_VIO_STATUS 0x4310C
271#define HALO_MPU_PM_VIO_ADDR 0x43110
272#define HALO_MPU_PM_VIO_STATUS 0x43114
Wen Shi170b1e12019-03-19 11:52:13 +0000273#define HALO_MPU_LOCK_CONFIG 0x43140
274
275/*
Richard Fitzgerald2ae58132019-03-19 11:52:14 +0000276 * HALO_AHBM_WINDOW_DEBUG_1
277 */
278#define HALO_AHBM_CORE_ERR_ADDR_MASK 0x0fffff00
279#define HALO_AHBM_CORE_ERR_ADDR_SHIFT 8
280#define HALO_AHBM_FLAGS_ERR_MASK 0x000000ff
281
282/*
Wen Shi170b1e12019-03-19 11:52:13 +0000283 * HALO_CCM_CORE_CONTROL
284 */
285#define HALO_CORE_EN 0x00000001
286
287/*
288 * HALO_CORE_SOFT_RESET
289 */
290#define HALO_CORE_SOFT_RESET_MASK 0x00000001
291
Richard Fitzgerald2ae58132019-03-19 11:52:14 +0000292/*
Stuart Henderson8bc144f2019-03-19 11:52:15 +0000293 * HALO_WDT_CONTROL
294 */
295#define HALO_WDT_EN_MASK 0x00000001
296
297/*
Richard Fitzgerald2ae58132019-03-19 11:52:14 +0000298 * HALO_MPU_?M_VIO_STATUS
299 */
300#define HALO_MPU_VIO_STS_MASK 0x007e0000
301#define HALO_MPU_VIO_STS_SHIFT 17
302#define HALO_MPU_VIO_ERR_WR_MASK 0x00008000
303#define HALO_MPU_VIO_ERR_SRC_MASK 0x00007fff
304#define HALO_MPU_VIO_ERR_SRC_SHIFT 0
305
Charles Keepaxcd537872019-03-19 17:43:09 +0000306static struct wm_adsp_ops wm_adsp1_ops;
307static struct wm_adsp_ops wm_adsp2_ops[];
308static struct wm_adsp_ops wm_halo_ops;
Charles Keepax4e08d502019-03-19 11:52:12 +0000309
Mark Browncf17c832013-01-30 14:37:23 +0800310struct wm_adsp_buf {
311 struct list_head list;
312 void *buf;
313};
314
315static struct wm_adsp_buf *wm_adsp_buf_alloc(const void *src, size_t len,
316 struct list_head *list)
317{
318 struct wm_adsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL);
319
320 if (buf == NULL)
321 return NULL;
322
Charles Keepaxcdcd7f72014-11-14 15:40:45 +0000323 buf->buf = vmalloc(len);
Mark Browncf17c832013-01-30 14:37:23 +0800324 if (!buf->buf) {
Richard Fitzgerald4d41c742016-12-09 09:57:41 +0000325 kfree(buf);
Mark Browncf17c832013-01-30 14:37:23 +0800326 return NULL;
327 }
Charles Keepaxcdcd7f72014-11-14 15:40:45 +0000328 memcpy(buf->buf, src, len);
Mark Browncf17c832013-01-30 14:37:23 +0800329
330 if (list)
331 list_add_tail(&buf->list, list);
332
333 return buf;
334}
335
336static void wm_adsp_buf_free(struct list_head *list)
337{
338 while (!list_empty(list)) {
339 struct wm_adsp_buf *buf = list_first_entry(list,
340 struct wm_adsp_buf,
341 list);
342 list_del(&buf->list);
Charles Keepaxcdcd7f72014-11-14 15:40:45 +0000343 vfree(buf->buf);
Mark Browncf17c832013-01-30 14:37:23 +0800344 kfree(buf);
345 }
346}
347
Charles Keepax04d13002015-11-26 14:01:52 +0000348#define WM_ADSP_FW_MBC_VSS 0
349#define WM_ADSP_FW_HIFI 1
350#define WM_ADSP_FW_TX 2
351#define WM_ADSP_FW_TX_SPK 3
352#define WM_ADSP_FW_RX 4
353#define WM_ADSP_FW_RX_ANC 5
354#define WM_ADSP_FW_CTRL 6
355#define WM_ADSP_FW_ASR 7
356#define WM_ADSP_FW_TRACE 8
357#define WM_ADSP_FW_SPK_PROT 9
358#define WM_ADSP_FW_MISC 10
Mark Brown1023dbd2013-01-11 22:58:28 +0000359
Charles Keepax04d13002015-11-26 14:01:52 +0000360#define WM_ADSP_NUM_FW 11
Mark Browndd84f922013-03-08 15:25:58 +0800361
Mark Brown1023dbd2013-01-11 22:58:28 +0000362static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = {
Charles Keepax04d13002015-11-26 14:01:52 +0000363 [WM_ADSP_FW_MBC_VSS] = "MBC/VSS",
364 [WM_ADSP_FW_HIFI] = "MasterHiFi",
365 [WM_ADSP_FW_TX] = "Tx",
366 [WM_ADSP_FW_TX_SPK] = "Tx Speaker",
367 [WM_ADSP_FW_RX] = "Rx",
368 [WM_ADSP_FW_RX_ANC] = "Rx ANC",
369 [WM_ADSP_FW_CTRL] = "Voice Ctrl",
370 [WM_ADSP_FW_ASR] = "ASR Assist",
371 [WM_ADSP_FW_TRACE] = "Dbg Trace",
372 [WM_ADSP_FW_SPK_PROT] = "Protection",
373 [WM_ADSP_FW_MISC] = "Misc",
Mark Brown1023dbd2013-01-11 22:58:28 +0000374};
375
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000376struct wm_adsp_system_config_xm_hdr {
377 __be32 sys_enable;
378 __be32 fw_id;
379 __be32 fw_rev;
380 __be32 boot_status;
381 __be32 watchdog;
382 __be32 dma_buffer_size;
383 __be32 rdma[6];
384 __be32 wdma[8];
385 __be32 build_job_name[3];
386 __be32 build_job_number;
387};
388
Wen Shi170b1e12019-03-19 11:52:13 +0000389struct wm_halo_system_config_xm_hdr {
390 __be32 halo_heartbeat;
391 __be32 build_job_name[3];
392 __be32 build_job_number;
393};
394
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000395struct wm_adsp_alg_xm_struct {
396 __be32 magic;
397 __be32 smoothing;
398 __be32 threshold;
399 __be32 host_buf_ptr;
400 __be32 start_seq;
401 __be32 high_water_mark;
402 __be32 low_water_mark;
403 __be64 smoothed_power;
404};
405
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +0000406struct wm_adsp_host_buf_coeff_v1 {
407 __be32 host_buf_ptr; /* Host buffer pointer */
408 __be32 versions; /* Version numbers */
409 __be32 name[4]; /* The buffer name */
410};
411
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000412struct wm_adsp_buffer {
Richard Fitzgerald2a2aefa2018-10-19 13:25:15 +0100413 __be32 buf1_base; /* Base addr of first buffer area */
414 __be32 buf1_size; /* Size of buf1 area in DSP words */
415 __be32 buf2_base; /* Base addr of 2nd buffer area */
416 __be32 buf1_buf2_size; /* Size of buf1+buf2 in DSP words */
417 __be32 buf3_base; /* Base addr of buf3 area */
418 __be32 buf_total_size; /* Size of buf1+buf2+buf3 in DSP words */
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000419 __be32 high_water_mark; /* Point at which IRQ is asserted */
420 __be32 irq_count; /* bits 1-31 count IRQ assertions */
421 __be32 irq_ack; /* acked IRQ count, bit 0 enables IRQ */
422 __be32 next_write_index; /* word index of next write */
423 __be32 next_read_index; /* word index of next read */
424 __be32 error; /* error if any */
425 __be32 oldest_block_index; /* word index of oldest surviving */
426 __be32 requested_rewind; /* how many blocks rewind was done */
427 __be32 reserved_space; /* internal */
428 __be32 min_free; /* min free space since stream start */
429 __be32 blocks_written[2]; /* total blocks written (64 bit) */
430 __be32 words_written[2]; /* total words written (64 bit) */
431};
432
Charles Keepax721be3b2016-05-04 17:11:56 +0100433struct wm_adsp_compr;
434
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000435struct wm_adsp_compr_buf {
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +0000436 struct list_head list;
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000437 struct wm_adsp *dsp;
Charles Keepax721be3b2016-05-04 17:11:56 +0100438 struct wm_adsp_compr *compr;
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000439
440 struct wm_adsp_buffer_region *regions;
441 u32 host_buf_ptr;
Charles Keepax565ace42016-01-06 12:33:18 +0000442
443 u32 error;
444 u32 irq_count;
445 int read_index;
446 int avail;
Andrew Fordfb13f192019-02-19 17:31:56 +0000447 int host_buf_mem_type;
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +0000448
449 char *name;
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000450};
451
Charles Keepax406abc92015-12-15 11:29:45 +0000452struct wm_adsp_compr {
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +0000453 struct list_head list;
Charles Keepax406abc92015-12-15 11:29:45 +0000454 struct wm_adsp *dsp;
Charles Keepax95fe9592015-12-15 11:29:47 +0000455 struct wm_adsp_compr_buf *buf;
Charles Keepax406abc92015-12-15 11:29:45 +0000456
457 struct snd_compr_stream *stream;
458 struct snd_compressed_buffer size;
Charles Keepax565ace42016-01-06 12:33:18 +0000459
Charles Keepax83a40ce2016-01-06 12:33:19 +0000460 u32 *raw_buf;
Charles Keepax565ace42016-01-06 12:33:18 +0000461 unsigned int copied_total;
Charles Keepaxda2b3352016-02-02 16:41:36 +0000462
463 unsigned int sample_rate;
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +0000464
465 const char *name;
Charles Keepax406abc92015-12-15 11:29:45 +0000466};
467
468#define WM_ADSP_DATA_WORD_SIZE 3
469
470#define WM_ADSP_MIN_FRAGMENTS 1
471#define WM_ADSP_MAX_FRAGMENTS 256
472#define WM_ADSP_MIN_FRAGMENT_SIZE (64 * WM_ADSP_DATA_WORD_SIZE)
473#define WM_ADSP_MAX_FRAGMENT_SIZE (4096 * WM_ADSP_DATA_WORD_SIZE)
474
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000475#define WM_ADSP_ALG_XM_STRUCT_MAGIC 0x49aec7
476
477#define HOST_BUFFER_FIELD(field) \
478 (offsetof(struct wm_adsp_buffer, field) / sizeof(__be32))
479
480#define ALG_XM_FIELD(field) \
481 (offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32))
482
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +0000483#define HOST_BUF_COEFF_SUPPORTED_COMPAT_VER 1
484
485#define HOST_BUF_COEFF_COMPAT_VER_MASK 0xFF00
486#define HOST_BUF_COEFF_COMPAT_VER_SHIFT 8
487
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000488static int wm_adsp_buffer_init(struct wm_adsp *dsp);
489static int wm_adsp_buffer_free(struct wm_adsp *dsp);
490
491struct wm_adsp_buffer_region {
492 unsigned int offset;
493 unsigned int cumulative_size;
494 unsigned int mem_type;
495 unsigned int base_addr;
496};
497
498struct wm_adsp_buffer_region_def {
499 unsigned int mem_type;
500 unsigned int base_offset;
501 unsigned int size_offset;
502};
503
Charles Keepax3a9686c2016-02-01 15:22:34 +0000504static const struct wm_adsp_buffer_region_def default_regions[] = {
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000505 {
506 .mem_type = WMFW_ADSP2_XM,
Richard Fitzgerald2a2aefa2018-10-19 13:25:15 +0100507 .base_offset = HOST_BUFFER_FIELD(buf1_base),
508 .size_offset = HOST_BUFFER_FIELD(buf1_size),
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000509 },
510 {
511 .mem_type = WMFW_ADSP2_XM,
Richard Fitzgerald2a2aefa2018-10-19 13:25:15 +0100512 .base_offset = HOST_BUFFER_FIELD(buf2_base),
513 .size_offset = HOST_BUFFER_FIELD(buf1_buf2_size),
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000514 },
515 {
516 .mem_type = WMFW_ADSP2_YM,
Richard Fitzgerald2a2aefa2018-10-19 13:25:15 +0100517 .base_offset = HOST_BUFFER_FIELD(buf3_base),
518 .size_offset = HOST_BUFFER_FIELD(buf_total_size),
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000519 },
520};
521
Charles Keepax406abc92015-12-15 11:29:45 +0000522struct wm_adsp_fw_caps {
523 u32 id;
524 struct snd_codec_desc desc;
Charles Keepax2cd19bd2015-12-15 11:29:46 +0000525 int num_regions;
Charles Keepax3a9686c2016-02-01 15:22:34 +0000526 const struct wm_adsp_buffer_region_def *region_defs;
Charles Keepax406abc92015-12-15 11:29:45 +0000527};
528
Charles Keepaxe6d00f32016-01-21 17:52:58 +0000529static const struct wm_adsp_fw_caps ctrl_caps[] = {
Charles Keepax406abc92015-12-15 11:29:45 +0000530 {
531 .id = SND_AUDIOCODEC_BESPOKE,
532 .desc = {
Richard Fitzgerald3bbc2702018-07-19 11:50:38 +0100533 .max_ch = 8,
Charles Keepax406abc92015-12-15 11:29:45 +0000534 .sample_rates = { 16000 },
535 .num_sample_rates = 1,
536 .formats = SNDRV_PCM_FMTBIT_S16_LE,
537 },
Charles Keepaxe6d00f32016-01-21 17:52:58 +0000538 .num_regions = ARRAY_SIZE(default_regions),
539 .region_defs = default_regions,
Charles Keepax406abc92015-12-15 11:29:45 +0000540 },
541};
542
Charles Keepax7ce42832016-01-21 17:52:59 +0000543static const struct wm_adsp_fw_caps trace_caps[] = {
544 {
545 .id = SND_AUDIOCODEC_BESPOKE,
546 .desc = {
547 .max_ch = 8,
548 .sample_rates = {
549 4000, 8000, 11025, 12000, 16000, 22050,
550 24000, 32000, 44100, 48000, 64000, 88200,
551 96000, 176400, 192000
552 },
553 .num_sample_rates = 15,
554 .formats = SNDRV_PCM_FMTBIT_S16_LE,
555 },
556 .num_regions = ARRAY_SIZE(default_regions),
557 .region_defs = default_regions,
Charles Keepax406abc92015-12-15 11:29:45 +0000558 },
559};
560
561static const struct {
Mark Brown1023dbd2013-01-11 22:58:28 +0000562 const char *file;
Charles Keepax406abc92015-12-15 11:29:45 +0000563 int compr_direction;
564 int num_caps;
565 const struct wm_adsp_fw_caps *caps;
Charles Keepax20b7f7c2016-05-13 16:45:17 +0100566 bool voice_trigger;
Mark Brown1023dbd2013-01-11 22:58:28 +0000567} wm_adsp_fw[WM_ADSP_NUM_FW] = {
Charles Keepax04d13002015-11-26 14:01:52 +0000568 [WM_ADSP_FW_MBC_VSS] = { .file = "mbc-vss" },
569 [WM_ADSP_FW_HIFI] = { .file = "hifi" },
570 [WM_ADSP_FW_TX] = { .file = "tx" },
571 [WM_ADSP_FW_TX_SPK] = { .file = "tx-spk" },
572 [WM_ADSP_FW_RX] = { .file = "rx" },
573 [WM_ADSP_FW_RX_ANC] = { .file = "rx-anc" },
Charles Keepax406abc92015-12-15 11:29:45 +0000574 [WM_ADSP_FW_CTRL] = {
575 .file = "ctrl",
576 .compr_direction = SND_COMPRESS_CAPTURE,
Charles Keepaxe6d00f32016-01-21 17:52:58 +0000577 .num_caps = ARRAY_SIZE(ctrl_caps),
578 .caps = ctrl_caps,
Charles Keepax20b7f7c2016-05-13 16:45:17 +0100579 .voice_trigger = true,
Charles Keepax406abc92015-12-15 11:29:45 +0000580 },
Charles Keepax04d13002015-11-26 14:01:52 +0000581 [WM_ADSP_FW_ASR] = { .file = "asr" },
Charles Keepax7ce42832016-01-21 17:52:59 +0000582 [WM_ADSP_FW_TRACE] = {
583 .file = "trace",
584 .compr_direction = SND_COMPRESS_CAPTURE,
585 .num_caps = ARRAY_SIZE(trace_caps),
586 .caps = trace_caps,
587 },
Charles Keepax04d13002015-11-26 14:01:52 +0000588 [WM_ADSP_FW_SPK_PROT] = { .file = "spk-prot" },
589 [WM_ADSP_FW_MISC] = { .file = "misc" },
Mark Brown1023dbd2013-01-11 22:58:28 +0000590};
591
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100592struct wm_coeff_ctl_ops {
593 int (*xget)(struct snd_kcontrol *kcontrol,
594 struct snd_ctl_elem_value *ucontrol);
595 int (*xput)(struct snd_kcontrol *kcontrol,
596 struct snd_ctl_elem_value *ucontrol);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100597};
598
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100599struct wm_coeff_ctl {
600 const char *name;
Charles Keepax23237362015-04-13 13:28:02 +0100601 const char *fw_name;
Li Xueb65ccd2019-11-15 13:54:13 -0600602 /* Subname is needed to match with firmware */
603 const char *subname;
604 unsigned int subname_len;
Charles Keepax3809f002015-04-13 13:27:54 +0100605 struct wm_adsp_alg_region alg_region;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100606 struct wm_coeff_ctl_ops ops;
Charles Keepax3809f002015-04-13 13:27:54 +0100607 struct wm_adsp *dsp;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100608 unsigned int enabled:1;
609 struct list_head list;
610 void *cache;
Charles Keepax23237362015-04-13 13:28:02 +0100611 unsigned int offset;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100612 size_t len;
Dimitris Papastamos0c2e3f32013-05-28 12:01:50 +0100613 unsigned int set:1;
Charles Keepax9ee78752016-05-02 13:57:36 +0100614 struct soc_bytes_ext bytes_ext;
Charles Keepax26c22a12015-04-20 13:52:45 +0100615 unsigned int flags;
Stuart Henderson8eb084d2016-11-09 17:14:16 +0000616 unsigned int type;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100617};
618
Richard Fitzgerald9ce5e6e2016-11-09 17:14:15 +0000619static const char *wm_adsp_mem_region_name(unsigned int type)
620{
621 switch (type) {
622 case WMFW_ADSP1_PM:
623 return "PM";
Wen Shi170b1e12019-03-19 11:52:13 +0000624 case WMFW_HALO_PM_PACKED:
625 return "PM_PACKED";
Richard Fitzgerald9ce5e6e2016-11-09 17:14:15 +0000626 case WMFW_ADSP1_DM:
627 return "DM";
628 case WMFW_ADSP2_XM:
629 return "XM";
Wen Shi170b1e12019-03-19 11:52:13 +0000630 case WMFW_HALO_XM_PACKED:
631 return "XM_PACKED";
Richard Fitzgerald9ce5e6e2016-11-09 17:14:15 +0000632 case WMFW_ADSP2_YM:
633 return "YM";
Wen Shi170b1e12019-03-19 11:52:13 +0000634 case WMFW_HALO_YM_PACKED:
635 return "YM_PACKED";
Richard Fitzgerald9ce5e6e2016-11-09 17:14:15 +0000636 case WMFW_ADSP1_ZM:
637 return "ZM";
638 default:
639 return NULL;
640 }
641}
642
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100643#ifdef CONFIG_DEBUG_FS
644static void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, const char *s)
645{
646 char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
647
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100648 kfree(dsp->wmfw_file_name);
649 dsp->wmfw_file_name = tmp;
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100650}
651
652static void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, const char *s)
653{
654 char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
655
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100656 kfree(dsp->bin_file_name);
657 dsp->bin_file_name = tmp;
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100658}
659
660static void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
661{
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100662 kfree(dsp->wmfw_file_name);
663 kfree(dsp->bin_file_name);
664 dsp->wmfw_file_name = NULL;
665 dsp->bin_file_name = NULL;
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100666}
667
668static ssize_t wm_adsp_debugfs_wmfw_read(struct file *file,
669 char __user *user_buf,
670 size_t count, loff_t *ppos)
671{
672 struct wm_adsp *dsp = file->private_data;
673 ssize_t ret;
674
Charles Keepax078e7182015-12-08 16:08:26 +0000675 mutex_lock(&dsp->pwr_lock);
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100676
Charles Keepax28823eb2016-09-20 13:52:32 +0100677 if (!dsp->wmfw_file_name || !dsp->booted)
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100678 ret = 0;
679 else
680 ret = simple_read_from_buffer(user_buf, count, ppos,
681 dsp->wmfw_file_name,
682 strlen(dsp->wmfw_file_name));
683
Charles Keepax078e7182015-12-08 16:08:26 +0000684 mutex_unlock(&dsp->pwr_lock);
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100685 return ret;
686}
687
688static ssize_t wm_adsp_debugfs_bin_read(struct file *file,
689 char __user *user_buf,
690 size_t count, loff_t *ppos)
691{
692 struct wm_adsp *dsp = file->private_data;
693 ssize_t ret;
694
Charles Keepax078e7182015-12-08 16:08:26 +0000695 mutex_lock(&dsp->pwr_lock);
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100696
Charles Keepax28823eb2016-09-20 13:52:32 +0100697 if (!dsp->bin_file_name || !dsp->booted)
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100698 ret = 0;
699 else
700 ret = simple_read_from_buffer(user_buf, count, ppos,
701 dsp->bin_file_name,
702 strlen(dsp->bin_file_name));
703
Charles Keepax078e7182015-12-08 16:08:26 +0000704 mutex_unlock(&dsp->pwr_lock);
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100705 return ret;
706}
707
708static const struct {
709 const char *name;
710 const struct file_operations fops;
711} wm_adsp_debugfs_fops[] = {
712 {
713 .name = "wmfw_file_name",
714 .fops = {
715 .open = simple_open,
716 .read = wm_adsp_debugfs_wmfw_read,
717 },
718 },
719 {
720 .name = "bin_file_name",
721 .fops = {
722 .open = simple_open,
723 .read = wm_adsp_debugfs_bin_read,
724 },
725 },
726};
727
728static void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000729 struct snd_soc_component *component)
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100730{
731 struct dentry *root = NULL;
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100732 int i;
733
Richard Fitzgerald605391d2018-08-08 17:13:39 +0100734 root = debugfs_create_dir(dsp->name, component->debugfs_root);
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100735
Greg Kroah-Hartman7f807f22019-06-14 11:47:54 +0200736 debugfs_create_bool("booted", 0444, root, &dsp->booted);
737 debugfs_create_bool("running", 0444, root, &dsp->running);
738 debugfs_create_x32("fw_id", 0444, root, &dsp->fw_id);
739 debugfs_create_x32("fw_version", 0444, root, &dsp->fw_id_version);
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100740
Greg Kroah-Hartman7f807f22019-06-14 11:47:54 +0200741 for (i = 0; i < ARRAY_SIZE(wm_adsp_debugfs_fops); ++i)
742 debugfs_create_file(wm_adsp_debugfs_fops[i].name, 0444, root,
743 dsp, &wm_adsp_debugfs_fops[i].fops);
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100744
745 dsp->debugfs_root = root;
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100746}
747
748static void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
749{
750 wm_adsp_debugfs_clear(dsp);
751 debugfs_remove_recursive(dsp->debugfs_root);
752}
753#else
754static inline void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000755 struct snd_soc_component *component)
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +0100756{
757}
758
759static inline void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
760{
761}
762
763static inline void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp,
764 const char *s)
765{
766}
767
768static inline void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp,
769 const char *s)
770{
771}
772
773static inline void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
774{
775}
776#endif
777
Richard Fitzgerald0a047f02018-08-08 17:13:38 +0100778int wm_adsp_fw_get(struct snd_kcontrol *kcontrol,
779 struct snd_ctl_elem_value *ucontrol)
Mark Brown1023dbd2013-01-11 22:58:28 +0000780{
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000781 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
Mark Brown1023dbd2013-01-11 22:58:28 +0000782 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000783 struct wm_adsp *dsp = snd_soc_component_get_drvdata(component);
Mark Brown1023dbd2013-01-11 22:58:28 +0000784
Takashi Iwai15c66572016-02-29 18:01:18 +0100785 ucontrol->value.enumerated.item[0] = dsp[e->shift_l].fw;
Mark Brown1023dbd2013-01-11 22:58:28 +0000786
787 return 0;
788}
Richard Fitzgerald0a047f02018-08-08 17:13:38 +0100789EXPORT_SYMBOL_GPL(wm_adsp_fw_get);
Mark Brown1023dbd2013-01-11 22:58:28 +0000790
Richard Fitzgerald0a047f02018-08-08 17:13:38 +0100791int wm_adsp_fw_put(struct snd_kcontrol *kcontrol,
792 struct snd_ctl_elem_value *ucontrol)
Mark Brown1023dbd2013-01-11 22:58:28 +0000793{
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000794 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
Mark Brown1023dbd2013-01-11 22:58:28 +0000795 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000796 struct wm_adsp *dsp = snd_soc_component_get_drvdata(component);
Charles Keepaxd27c5e12015-12-08 16:08:28 +0000797 int ret = 0;
Mark Brown1023dbd2013-01-11 22:58:28 +0000798
Takashi Iwai15c66572016-02-29 18:01:18 +0100799 if (ucontrol->value.enumerated.item[0] == dsp[e->shift_l].fw)
Mark Brown1023dbd2013-01-11 22:58:28 +0000800 return 0;
801
Takashi Iwai15c66572016-02-29 18:01:18 +0100802 if (ucontrol->value.enumerated.item[0] >= WM_ADSP_NUM_FW)
Mark Brown1023dbd2013-01-11 22:58:28 +0000803 return -EINVAL;
804
Charles Keepaxd27c5e12015-12-08 16:08:28 +0000805 mutex_lock(&dsp[e->shift_l].pwr_lock);
806
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +0000807 if (dsp[e->shift_l].booted || !list_empty(&dsp[e->shift_l].compr_list))
Charles Keepaxd27c5e12015-12-08 16:08:28 +0000808 ret = -EBUSY;
809 else
Takashi Iwai15c66572016-02-29 18:01:18 +0100810 dsp[e->shift_l].fw = ucontrol->value.enumerated.item[0];
Mark Brown1023dbd2013-01-11 22:58:28 +0000811
Charles Keepaxd27c5e12015-12-08 16:08:28 +0000812 mutex_unlock(&dsp[e->shift_l].pwr_lock);
Mark Brown1023dbd2013-01-11 22:58:28 +0000813
Charles Keepaxd27c5e12015-12-08 16:08:28 +0000814 return ret;
Mark Brown1023dbd2013-01-11 22:58:28 +0000815}
Richard Fitzgerald0a047f02018-08-08 17:13:38 +0100816EXPORT_SYMBOL_GPL(wm_adsp_fw_put);
Mark Brown1023dbd2013-01-11 22:58:28 +0000817
Richard Fitzgerald0a047f02018-08-08 17:13:38 +0100818const struct soc_enum wm_adsp_fw_enum[] = {
Mark Brown1023dbd2013-01-11 22:58:28 +0000819 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
820 SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
821 SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
822 SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +0100823 SOC_ENUM_SINGLE(0, 4, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
824 SOC_ENUM_SINGLE(0, 5, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
825 SOC_ENUM_SINGLE(0, 6, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
Mark Brown1023dbd2013-01-11 22:58:28 +0000826};
Richard Fitzgerald0a047f02018-08-08 17:13:38 +0100827EXPORT_SYMBOL_GPL(wm_adsp_fw_enum);
Mark Brown2159ad932012-10-11 11:54:02 +0900828
829static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp,
830 int type)
831{
832 int i;
833
834 for (i = 0; i < dsp->num_mems; i++)
835 if (dsp->mem[i].type == type)
836 return &dsp->mem[i];
837
838 return NULL;
839}
840
Charles Keepax3809f002015-04-13 13:27:54 +0100841static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *mem,
Mark Brown45b9ee72013-01-08 16:02:06 +0000842 unsigned int offset)
843{
Charles Keepax3809f002015-04-13 13:27:54 +0100844 switch (mem->type) {
Mark Brown45b9ee72013-01-08 16:02:06 +0000845 case WMFW_ADSP1_PM:
Charles Keepax3809f002015-04-13 13:27:54 +0100846 return mem->base + (offset * 3);
Mark Brown45b9ee72013-01-08 16:02:06 +0000847 case WMFW_ADSP1_DM:
Mark Brown45b9ee72013-01-08 16:02:06 +0000848 case WMFW_ADSP2_XM:
Mark Brown45b9ee72013-01-08 16:02:06 +0000849 case WMFW_ADSP2_YM:
Mark Brown45b9ee72013-01-08 16:02:06 +0000850 case WMFW_ADSP1_ZM:
Charles Keepax3809f002015-04-13 13:27:54 +0100851 return mem->base + (offset * 2);
Mark Brown45b9ee72013-01-08 16:02:06 +0000852 default:
Takashi Iwai6c452bd2013-11-05 18:40:00 +0100853 WARN(1, "Unknown memory region type");
Mark Brown45b9ee72013-01-08 16:02:06 +0000854 return offset;
855 }
856}
857
Wen Shi170b1e12019-03-19 11:52:13 +0000858static unsigned int wm_halo_region_to_reg(struct wm_adsp_region const *mem,
859 unsigned int offset)
860{
861 switch (mem->type) {
862 case WMFW_ADSP2_XM:
863 case WMFW_ADSP2_YM:
864 return mem->base + (offset * 4);
865 case WMFW_HALO_XM_PACKED:
866 case WMFW_HALO_YM_PACKED:
867 return (mem->base + (offset * 3)) & ~0x3;
868 case WMFW_HALO_PM_PACKED:
869 return mem->base + (offset * 5);
870 default:
871 WARN(1, "Unknown memory region type");
872 return offset;
873 }
874}
875
Charles Keepax4049ce82019-03-19 11:52:10 +0000876static void wm_adsp_read_fw_status(struct wm_adsp *dsp,
877 int noffs, unsigned int *offs)
Richard Fitzgerald10337b02015-05-29 10:23:07 +0100878{
Richard Fitzgerald20e00db2018-11-12 13:36:38 +0000879 unsigned int i;
Richard Fitzgerald10337b02015-05-29 10:23:07 +0100880 int ret;
881
Charles Keepax4049ce82019-03-19 11:52:10 +0000882 for (i = 0; i < noffs; ++i) {
883 ret = regmap_read(dsp->regmap, dsp->base + offs[i], &offs[i]);
Richard Fitzgerald20e00db2018-11-12 13:36:38 +0000884 if (ret) {
885 adsp_err(dsp, "Failed to read SCRATCH%u: %d\n", i, ret);
886 return;
887 }
Richard Fitzgerald10337b02015-05-29 10:23:07 +0100888 }
Charles Keepax4049ce82019-03-19 11:52:10 +0000889}
890
891static void wm_adsp2_show_fw_status(struct wm_adsp *dsp)
892{
893 unsigned int offs[] = {
894 ADSP2_SCRATCH0, ADSP2_SCRATCH1, ADSP2_SCRATCH2, ADSP2_SCRATCH3,
895 };
896
897 wm_adsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs);
Richard Fitzgerald10337b02015-05-29 10:23:07 +0100898
899 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
Charles Keepax4049ce82019-03-19 11:52:10 +0000900 offs[0], offs[1], offs[2], offs[3]);
Richard Fitzgerald10337b02015-05-29 10:23:07 +0100901}
902
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +0100903static void wm_adsp2v2_show_fw_status(struct wm_adsp *dsp)
904{
Charles Keepax4049ce82019-03-19 11:52:10 +0000905 unsigned int offs[] = { ADSP2V2_SCRATCH0_1, ADSP2V2_SCRATCH2_3 };
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +0100906
Charles Keepax4049ce82019-03-19 11:52:10 +0000907 wm_adsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs);
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +0100908
909 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
Charles Keepax4049ce82019-03-19 11:52:10 +0000910 offs[0] & 0xFFFF, offs[0] >> 16,
911 offs[1] & 0xFFFF, offs[1] >> 16);
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +0100912}
913
Wen Shi170b1e12019-03-19 11:52:13 +0000914static void wm_halo_show_fw_status(struct wm_adsp *dsp)
915{
916 unsigned int offs[] = {
917 HALO_SCRATCH1, HALO_SCRATCH2, HALO_SCRATCH3, HALO_SCRATCH4,
918 };
919
920 wm_adsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs);
921
922 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
923 offs[0], offs[1], offs[2], offs[3]);
924}
925
Charles Keepax9ee78752016-05-02 13:57:36 +0100926static inline struct wm_coeff_ctl *bytes_ext_to_ctl(struct soc_bytes_ext *ext)
927{
928 return container_of(ext, struct wm_coeff_ctl, bytes_ext);
929}
930
Richard Fitzgeraldb396ebc2016-11-09 17:14:14 +0000931static int wm_coeff_base_reg(struct wm_coeff_ctl *ctl, unsigned int *reg)
932{
933 const struct wm_adsp_alg_region *alg_region = &ctl->alg_region;
934 struct wm_adsp *dsp = ctl->dsp;
935 const struct wm_adsp_region *mem;
936
937 mem = wm_adsp_find_region(dsp, alg_region->type);
938 if (!mem) {
939 adsp_err(dsp, "No base for region %x\n",
940 alg_region->type);
941 return -EINVAL;
942 }
943
Wen Shi170b1e12019-03-19 11:52:13 +0000944 *reg = dsp->ops->region_to_reg(mem, ctl->alg_region.base + ctl->offset);
Richard Fitzgeraldb396ebc2016-11-09 17:14:14 +0000945
946 return 0;
947}
948
Charles Keepax7585a5b2015-12-08 16:08:25 +0000949static int wm_coeff_info(struct snd_kcontrol *kctl,
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100950 struct snd_ctl_elem_info *uinfo)
951{
Charles Keepax9ee78752016-05-02 13:57:36 +0100952 struct soc_bytes_ext *bytes_ext =
953 (struct soc_bytes_ext *)kctl->private_value;
954 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100955
Richard Fitzgeralda23ebba2016-11-09 17:14:18 +0000956 switch (ctl->type) {
957 case WMFW_CTL_TYPE_ACKED:
958 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
959 uinfo->value.integer.min = WM_ADSP_ACKED_CTL_MIN_VALUE;
960 uinfo->value.integer.max = WM_ADSP_ACKED_CTL_MAX_VALUE;
961 uinfo->value.integer.step = 1;
962 uinfo->count = 1;
963 break;
964 default:
965 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
966 uinfo->count = ctl->len;
967 break;
968 }
969
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +0100970 return 0;
971}
972
Richard Fitzgeraldf4f0c4c2016-11-09 17:14:17 +0000973static int wm_coeff_write_acked_control(struct wm_coeff_ctl *ctl,
974 unsigned int event_id)
975{
976 struct wm_adsp *dsp = ctl->dsp;
977 u32 val = cpu_to_be32(event_id);
978 unsigned int reg;
979 int i, ret;
980
981 ret = wm_coeff_base_reg(ctl, &reg);
982 if (ret)
983 return ret;
984
985 adsp_dbg(dsp, "Sending 0x%x to acked control alg 0x%x %s:0x%x\n",
986 event_id, ctl->alg_region.alg,
987 wm_adsp_mem_region_name(ctl->alg_region.type), ctl->offset);
988
989 ret = regmap_raw_write(dsp->regmap, reg, &val, sizeof(val));
990 if (ret) {
991 adsp_err(dsp, "Failed to write %x: %d\n", reg, ret);
992 return ret;
993 }
994
995 /*
996 * Poll for ack, we initially poll at ~1ms intervals for firmwares
997 * that respond quickly, then go to ~10ms polls. A firmware is unlikely
998 * to ack instantly so we do the first 1ms delay before reading the
999 * control to avoid a pointless bus transaction
1000 */
1001 for (i = 0; i < WM_ADSP_ACKED_CTL_TIMEOUT_MS;) {
1002 switch (i) {
1003 case 0 ... WM_ADSP_ACKED_CTL_N_QUICKPOLLS - 1:
1004 usleep_range(1000, 2000);
1005 i++;
1006 break;
1007 default:
1008 usleep_range(10000, 20000);
1009 i += 10;
1010 break;
1011 }
1012
1013 ret = regmap_raw_read(dsp->regmap, reg, &val, sizeof(val));
1014 if (ret) {
1015 adsp_err(dsp, "Failed to read %x: %d\n", reg, ret);
1016 return ret;
1017 }
1018
1019 if (val == 0) {
1020 adsp_dbg(dsp, "Acked control ACKED at poll %u\n", i);
1021 return 0;
1022 }
1023 }
1024
1025 adsp_warn(dsp, "Acked control @0x%x alg:0x%x %s:0x%x timed out\n",
1026 reg, ctl->alg_region.alg,
1027 wm_adsp_mem_region_name(ctl->alg_region.type),
1028 ctl->offset);
1029
1030 return -ETIMEDOUT;
1031}
1032
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001033static int wm_coeff_write_ctrl_raw(struct wm_coeff_ctl *ctl,
1034 const void *buf, size_t len)
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001035{
Charles Keepax3809f002015-04-13 13:27:54 +01001036 struct wm_adsp *dsp = ctl->dsp;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001037 void *scratch;
1038 int ret;
1039 unsigned int reg;
1040
Richard Fitzgeraldb396ebc2016-11-09 17:14:14 +00001041 ret = wm_coeff_base_reg(ctl, &reg);
1042 if (ret)
1043 return ret;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001044
Charles Keepax4f8ea6d2016-02-19 14:44:44 +00001045 scratch = kmemdup(buf, len, GFP_KERNEL | GFP_DMA);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001046 if (!scratch)
1047 return -ENOMEM;
1048
Charles Keepax3809f002015-04-13 13:27:54 +01001049 ret = regmap_raw_write(dsp->regmap, reg, scratch,
Charles Keepax4f8ea6d2016-02-19 14:44:44 +00001050 len);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001051 if (ret) {
Charles Keepax3809f002015-04-13 13:27:54 +01001052 adsp_err(dsp, "Failed to write %zu bytes to %x: %d\n",
Charles Keepax4f8ea6d2016-02-19 14:44:44 +00001053 len, reg, ret);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001054 kfree(scratch);
1055 return ret;
1056 }
Charles Keepax4f8ea6d2016-02-19 14:44:44 +00001057 adsp_dbg(dsp, "Wrote %zu bytes to %x\n", len, reg);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001058
1059 kfree(scratch);
1060
1061 return 0;
1062}
1063
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001064static int wm_coeff_write_ctrl(struct wm_coeff_ctl *ctl,
1065 const void *buf, size_t len)
1066{
1067 int ret = 0;
1068
1069 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
1070 ret = -EPERM;
1071 else if (buf != ctl->cache)
1072 memcpy(ctl->cache, buf, len);
1073
1074 ctl->set = 1;
1075 if (ctl->enabled && ctl->dsp->running)
1076 ret = wm_coeff_write_ctrl_raw(ctl, buf, len);
1077
1078 return ret;
1079}
1080
Charles Keepax7585a5b2015-12-08 16:08:25 +00001081static int wm_coeff_put(struct snd_kcontrol *kctl,
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001082 struct snd_ctl_elem_value *ucontrol)
1083{
Charles Keepax9ee78752016-05-02 13:57:36 +01001084 struct soc_bytes_ext *bytes_ext =
1085 (struct soc_bytes_ext *)kctl->private_value;
1086 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001087 char *p = ucontrol->value.bytes.data;
Charles Keepax168d10e2015-12-08 16:08:27 +00001088 int ret = 0;
1089
1090 mutex_lock(&ctl->dsp->pwr_lock);
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001091 ret = wm_coeff_write_ctrl(ctl, p, ctl->len);
Charles Keepax168d10e2015-12-08 16:08:27 +00001092 mutex_unlock(&ctl->dsp->pwr_lock);
1093
1094 return ret;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001095}
1096
Charles Keepax9ee78752016-05-02 13:57:36 +01001097static int wm_coeff_tlv_put(struct snd_kcontrol *kctl,
1098 const unsigned int __user *bytes, unsigned int size)
1099{
1100 struct soc_bytes_ext *bytes_ext =
1101 (struct soc_bytes_ext *)kctl->private_value;
1102 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1103 int ret = 0;
1104
1105 mutex_lock(&ctl->dsp->pwr_lock);
1106
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001107 if (copy_from_user(ctl->cache, bytes, size))
Charles Keepax9ee78752016-05-02 13:57:36 +01001108 ret = -EFAULT;
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001109 else
1110 ret = wm_coeff_write_ctrl(ctl, ctl->cache, size);
Charles Keepax9ee78752016-05-02 13:57:36 +01001111
1112 mutex_unlock(&ctl->dsp->pwr_lock);
1113
1114 return ret;
1115}
1116
Richard Fitzgeralda23ebba2016-11-09 17:14:18 +00001117static int wm_coeff_put_acked(struct snd_kcontrol *kctl,
1118 struct snd_ctl_elem_value *ucontrol)
1119{
1120 struct soc_bytes_ext *bytes_ext =
1121 (struct soc_bytes_ext *)kctl->private_value;
1122 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1123 unsigned int val = ucontrol->value.integer.value[0];
1124 int ret;
1125
1126 if (val == 0)
1127 return 0; /* 0 means no event */
1128
1129 mutex_lock(&ctl->dsp->pwr_lock);
1130
Charles Keepax7b4af792017-03-06 16:54:34 +00001131 if (ctl->enabled && ctl->dsp->running)
Richard Fitzgeralda23ebba2016-11-09 17:14:18 +00001132 ret = wm_coeff_write_acked_control(ctl, val);
1133 else
1134 ret = -EPERM;
1135
1136 mutex_unlock(&ctl->dsp->pwr_lock);
1137
1138 return ret;
1139}
1140
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001141static int wm_coeff_read_ctrl_raw(struct wm_coeff_ctl *ctl,
1142 void *buf, size_t len)
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001143{
Charles Keepax3809f002015-04-13 13:27:54 +01001144 struct wm_adsp *dsp = ctl->dsp;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001145 void *scratch;
1146 int ret;
1147 unsigned int reg;
1148
Richard Fitzgeraldb396ebc2016-11-09 17:14:14 +00001149 ret = wm_coeff_base_reg(ctl, &reg);
1150 if (ret)
1151 return ret;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001152
Charles Keepax4f8ea6d2016-02-19 14:44:44 +00001153 scratch = kmalloc(len, GFP_KERNEL | GFP_DMA);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001154 if (!scratch)
1155 return -ENOMEM;
1156
Charles Keepax4f8ea6d2016-02-19 14:44:44 +00001157 ret = regmap_raw_read(dsp->regmap, reg, scratch, len);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001158 if (ret) {
Charles Keepax3809f002015-04-13 13:27:54 +01001159 adsp_err(dsp, "Failed to read %zu bytes from %x: %d\n",
Charles Keepax5602a642016-03-10 10:46:07 +00001160 len, reg, ret);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001161 kfree(scratch);
1162 return ret;
1163 }
Charles Keepax4f8ea6d2016-02-19 14:44:44 +00001164 adsp_dbg(dsp, "Read %zu bytes from %x\n", len, reg);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001165
Charles Keepax4f8ea6d2016-02-19 14:44:44 +00001166 memcpy(buf, scratch, len);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001167 kfree(scratch);
1168
1169 return 0;
1170}
1171
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001172static int wm_coeff_read_ctrl(struct wm_coeff_ctl *ctl, void *buf, size_t len)
1173{
1174 int ret = 0;
1175
1176 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
1177 if (ctl->enabled && ctl->dsp->running)
1178 return wm_coeff_read_ctrl_raw(ctl, buf, len);
1179 else
1180 return -EPERM;
1181 } else {
1182 if (!ctl->flags && ctl->enabled && ctl->dsp->running)
1183 ret = wm_coeff_read_ctrl_raw(ctl, ctl->cache, ctl->len);
1184
1185 if (buf != ctl->cache)
1186 memcpy(buf, ctl->cache, len);
1187 }
1188
1189 return ret;
1190}
1191
Charles Keepax7585a5b2015-12-08 16:08:25 +00001192static int wm_coeff_get(struct snd_kcontrol *kctl,
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001193 struct snd_ctl_elem_value *ucontrol)
1194{
Charles Keepax9ee78752016-05-02 13:57:36 +01001195 struct soc_bytes_ext *bytes_ext =
1196 (struct soc_bytes_ext *)kctl->private_value;
1197 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001198 char *p = ucontrol->value.bytes.data;
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001199 int ret;
Charles Keepax168d10e2015-12-08 16:08:27 +00001200
1201 mutex_lock(&ctl->dsp->pwr_lock);
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001202 ret = wm_coeff_read_ctrl(ctl, p, ctl->len);
Charles Keepax168d10e2015-12-08 16:08:27 +00001203 mutex_unlock(&ctl->dsp->pwr_lock);
Charles Keepax26c22a12015-04-20 13:52:45 +01001204
Charles Keepax168d10e2015-12-08 16:08:27 +00001205 return ret;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001206}
1207
Charles Keepax9ee78752016-05-02 13:57:36 +01001208static int wm_coeff_tlv_get(struct snd_kcontrol *kctl,
1209 unsigned int __user *bytes, unsigned int size)
1210{
1211 struct soc_bytes_ext *bytes_ext =
1212 (struct soc_bytes_ext *)kctl->private_value;
1213 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
1214 int ret = 0;
1215
1216 mutex_lock(&ctl->dsp->pwr_lock);
1217
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001218 ret = wm_coeff_read_ctrl_raw(ctl, ctl->cache, size);
Charles Keepax9ee78752016-05-02 13:57:36 +01001219
1220 if (!ret && copy_to_user(bytes, ctl->cache, size))
1221 ret = -EFAULT;
1222
1223 mutex_unlock(&ctl->dsp->pwr_lock);
1224
1225 return ret;
1226}
1227
Richard Fitzgeralda23ebba2016-11-09 17:14:18 +00001228static int wm_coeff_get_acked(struct snd_kcontrol *kcontrol,
1229 struct snd_ctl_elem_value *ucontrol)
1230{
1231 /*
1232 * Although it's not useful to read an acked control, we must satisfy
1233 * user-side assumptions that all controls are readable and that a
1234 * write of the same value should be filtered out (it's valid to send
1235 * the same event number again to the firmware). We therefore return 0,
1236 * meaning "no event" so valid event numbers will always be a change
1237 */
1238 ucontrol->value.integer.value[0] = 0;
1239
1240 return 0;
1241}
1242
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001243struct wmfw_ctl_work {
Charles Keepax3809f002015-04-13 13:27:54 +01001244 struct wm_adsp *dsp;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001245 struct wm_coeff_ctl *ctl;
1246 struct work_struct work;
1247};
1248
Charles Keepax9ee78752016-05-02 13:57:36 +01001249static unsigned int wmfw_convert_flags(unsigned int in, unsigned int len)
1250{
1251 unsigned int out, rd, wr, vol;
1252
1253 if (len > ADSP_MAX_STD_CTRL_SIZE) {
1254 rd = SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1255 wr = SNDRV_CTL_ELEM_ACCESS_TLV_WRITE;
1256 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
1257
1258 out = SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1259 } else {
1260 rd = SNDRV_CTL_ELEM_ACCESS_READ;
1261 wr = SNDRV_CTL_ELEM_ACCESS_WRITE;
1262 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
1263
1264 out = 0;
1265 }
1266
1267 if (in) {
Stuart Henderson3ae73592019-10-02 09:42:40 +01001268 out |= rd;
Charles Keepax9ee78752016-05-02 13:57:36 +01001269 if (in & WMFW_CTL_FLAG_WRITEABLE)
1270 out |= wr;
1271 if (in & WMFW_CTL_FLAG_VOLATILE)
1272 out |= vol;
1273 } else {
1274 out |= rd | wr | vol;
1275 }
1276
1277 return out;
1278}
1279
Charles Keepax3809f002015-04-13 13:27:54 +01001280static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl)
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001281{
1282 struct snd_kcontrol_new *kcontrol;
1283 int ret;
1284
Dimitris Papastamos92bb4c32013-08-01 11:11:28 +01001285 if (!ctl || !ctl->name)
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001286 return -EINVAL;
1287
1288 kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL);
1289 if (!kcontrol)
1290 return -ENOMEM;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001291
1292 kcontrol->name = ctl->name;
1293 kcontrol->info = wm_coeff_info;
Charles Keepax9ee78752016-05-02 13:57:36 +01001294 kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1295 kcontrol->tlv.c = snd_soc_bytes_tlv_callback;
1296 kcontrol->private_value = (unsigned long)&ctl->bytes_ext;
Charles Keepax9ee78752016-05-02 13:57:36 +01001297 kcontrol->access = wmfw_convert_flags(ctl->flags, ctl->len);
Charles Keepax26c22a12015-04-20 13:52:45 +01001298
Richard Fitzgeralda23ebba2016-11-09 17:14:18 +00001299 switch (ctl->type) {
1300 case WMFW_CTL_TYPE_ACKED:
1301 kcontrol->get = wm_coeff_get_acked;
1302 kcontrol->put = wm_coeff_put_acked;
1303 break;
1304 default:
Richard Fitzgeraldd7789f52018-02-28 10:31:10 +00001305 if (kcontrol->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1306 ctl->bytes_ext.max = ctl->len;
1307 ctl->bytes_ext.get = wm_coeff_tlv_get;
1308 ctl->bytes_ext.put = wm_coeff_tlv_put;
1309 } else {
1310 kcontrol->get = wm_coeff_get;
1311 kcontrol->put = wm_coeff_put;
1312 }
Richard Fitzgeralda23ebba2016-11-09 17:14:18 +00001313 break;
1314 }
1315
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00001316 ret = snd_soc_add_component_controls(dsp->component, kcontrol, 1);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001317 if (ret < 0)
1318 goto err_kcontrol;
1319
1320 kfree(kcontrol);
1321
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01001322 return 0;
1323
1324err_kcontrol:
1325 kfree(kcontrol);
1326 return ret;
1327}
1328
Charles Keepaxb21acc12015-04-13 13:28:01 +01001329static int wm_coeff_init_control_caches(struct wm_adsp *dsp)
1330{
1331 struct wm_coeff_ctl *ctl;
1332 int ret;
1333
1334 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1335 if (!ctl->enabled || ctl->set)
1336 continue;
Charles Keepax26c22a12015-04-20 13:52:45 +01001337 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
1338 continue;
1339
Richard Fitzgerald04ff40a2018-02-05 11:38:17 +00001340 /*
1341 * For readable controls populate the cache from the DSP memory.
1342 * For non-readable controls the cache was zero-filled when
1343 * created so we don't need to do anything.
1344 */
1345 if (!ctl->flags || (ctl->flags & WMFW_CTL_FLAG_READABLE)) {
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001346 ret = wm_coeff_read_ctrl_raw(ctl, ctl->cache, ctl->len);
Richard Fitzgerald04ff40a2018-02-05 11:38:17 +00001347 if (ret < 0)
1348 return ret;
1349 }
Charles Keepaxb21acc12015-04-13 13:28:01 +01001350 }
1351
1352 return 0;
1353}
1354
1355static int wm_coeff_sync_controls(struct wm_adsp *dsp)
1356{
1357 struct wm_coeff_ctl *ctl;
1358 int ret;
1359
1360 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1361 if (!ctl->enabled)
1362 continue;
Charles Keepax26c22a12015-04-20 13:52:45 +01001363 if (ctl->set && !(ctl->flags & WMFW_CTL_FLAG_VOLATILE)) {
Charles Keepax73ecf1a2020-01-14 16:18:38 +00001364 ret = wm_coeff_write_ctrl_raw(ctl, ctl->cache,
1365 ctl->len);
Charles Keepaxb21acc12015-04-13 13:28:01 +01001366 if (ret < 0)
1367 return ret;
1368 }
1369 }
1370
1371 return 0;
1372}
1373
Richard Fitzgeraldf4f0c4c2016-11-09 17:14:17 +00001374static void wm_adsp_signal_event_controls(struct wm_adsp *dsp,
1375 unsigned int event)
1376{
1377 struct wm_coeff_ctl *ctl;
1378 int ret;
1379
1380 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1381 if (ctl->type != WMFW_CTL_TYPE_HOSTEVENT)
1382 continue;
1383
Charles Keepax87aa6372016-11-21 18:00:02 +00001384 if (!ctl->enabled)
1385 continue;
1386
Richard Fitzgeraldf4f0c4c2016-11-09 17:14:17 +00001387 ret = wm_coeff_write_acked_control(ctl, event);
1388 if (ret)
1389 adsp_warn(dsp,
1390 "Failed to send 0x%x event to alg 0x%x (%d)\n",
1391 event, ctl->alg_region.alg, ret);
1392 }
1393}
1394
Charles Keepaxb21acc12015-04-13 13:28:01 +01001395static void wm_adsp_ctl_work(struct work_struct *work)
1396{
1397 struct wmfw_ctl_work *ctl_work = container_of(work,
1398 struct wmfw_ctl_work,
1399 work);
1400
1401 wmfw_add_ctl(ctl_work->dsp, ctl_work->ctl);
1402 kfree(ctl_work);
1403}
1404
Richard Fitzgerald66225e92016-04-27 14:58:27 +01001405static void wm_adsp_free_ctl_blk(struct wm_coeff_ctl *ctl)
1406{
1407 kfree(ctl->cache);
1408 kfree(ctl->name);
Li Xueb65ccd2019-11-15 13:54:13 -06001409 kfree(ctl->subname);
Richard Fitzgerald66225e92016-04-27 14:58:27 +01001410 kfree(ctl);
1411}
1412
Charles Keepaxb21acc12015-04-13 13:28:01 +01001413static int wm_adsp_create_control(struct wm_adsp *dsp,
1414 const struct wm_adsp_alg_region *alg_region,
Charles Keepax23237362015-04-13 13:28:02 +01001415 unsigned int offset, unsigned int len,
Charles Keepax26c22a12015-04-20 13:52:45 +01001416 const char *subname, unsigned int subname_len,
Stuart Henderson8eb084d2016-11-09 17:14:16 +00001417 unsigned int flags, unsigned int type)
Charles Keepaxb21acc12015-04-13 13:28:01 +01001418{
1419 struct wm_coeff_ctl *ctl;
1420 struct wmfw_ctl_work *ctl_work;
1421 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Richard Fitzgerald9ce5e6e2016-11-09 17:14:15 +00001422 const char *region_name;
Charles Keepaxb21acc12015-04-13 13:28:01 +01001423 int ret;
1424
Richard Fitzgerald9ce5e6e2016-11-09 17:14:15 +00001425 region_name = wm_adsp_mem_region_name(alg_region->type);
1426 if (!region_name) {
Charles Keepax23237362015-04-13 13:28:02 +01001427 adsp_err(dsp, "Unknown region type: %d\n", alg_region->type);
Charles Keepaxb21acc12015-04-13 13:28:01 +01001428 return -EINVAL;
1429 }
1430
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001431 switch (dsp->fw_ver) {
1432 case 0:
1433 case 1:
Richard Fitzgerald605391d2018-08-08 17:13:39 +01001434 snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s %x",
1435 dsp->name, region_name, alg_region->alg);
Wen Shi170b1e12019-03-19 11:52:13 +00001436 subname = NULL; /* don't append subname */
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001437 break;
Wen Shi170b1e12019-03-19 11:52:13 +00001438 case 2:
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001439 ret = snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
Richard Fitzgerald605391d2018-08-08 17:13:39 +01001440 "%s%c %.12s %x", dsp->name, *region_name,
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001441 wm_adsp_fw_text[dsp->fw], alg_region->alg);
Wen Shi170b1e12019-03-19 11:52:13 +00001442 break;
1443 default:
1444 ret = snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
1445 "%s %.12s %x", dsp->name,
1446 wm_adsp_fw_text[dsp->fw], alg_region->alg);
1447 break;
1448 }
1449
1450 if (subname) {
1451 int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
1452 int skip = 0;
1453
1454 if (dsp->component->name_prefix)
1455 avail -= strlen(dsp->component->name_prefix) + 1;
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001456
1457 /* Truncate the subname from the start if it is too long */
Wen Shi170b1e12019-03-19 11:52:13 +00001458 if (subname_len > avail)
1459 skip = subname_len - avail;
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001460
Wen Shi170b1e12019-03-19 11:52:13 +00001461 snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret,
1462 " %.*s", subname_len - skip, subname + skip);
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001463 }
Charles Keepaxb21acc12015-04-13 13:28:01 +01001464
Charles Keepax7585a5b2015-12-08 16:08:25 +00001465 list_for_each_entry(ctl, &dsp->ctl_list, list) {
Charles Keepaxb21acc12015-04-13 13:28:01 +01001466 if (!strcmp(ctl->name, name)) {
1467 if (!ctl->enabled)
1468 ctl->enabled = 1;
1469 return 0;
1470 }
1471 }
1472
1473 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1474 if (!ctl)
1475 return -ENOMEM;
Charles Keepax23237362015-04-13 13:28:02 +01001476 ctl->fw_name = wm_adsp_fw_text[dsp->fw];
Charles Keepaxb21acc12015-04-13 13:28:01 +01001477 ctl->alg_region = *alg_region;
1478 ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
1479 if (!ctl->name) {
1480 ret = -ENOMEM;
1481 goto err_ctl;
1482 }
Li Xueb65ccd2019-11-15 13:54:13 -06001483 if (subname) {
1484 ctl->subname_len = subname_len;
1485 ctl->subname = kmemdup(subname,
1486 strlen(subname) + 1, GFP_KERNEL);
1487 if (!ctl->subname) {
1488 ret = -ENOMEM;
1489 goto err_ctl_name;
1490 }
1491 }
Charles Keepaxb21acc12015-04-13 13:28:01 +01001492 ctl->enabled = 1;
1493 ctl->set = 0;
1494 ctl->ops.xget = wm_coeff_get;
1495 ctl->ops.xput = wm_coeff_put;
1496 ctl->dsp = dsp;
1497
Charles Keepax26c22a12015-04-20 13:52:45 +01001498 ctl->flags = flags;
Stuart Henderson8eb084d2016-11-09 17:14:16 +00001499 ctl->type = type;
Charles Keepax23237362015-04-13 13:28:02 +01001500 ctl->offset = offset;
Charles Keepaxb21acc12015-04-13 13:28:01 +01001501 ctl->len = len;
1502 ctl->cache = kzalloc(ctl->len, GFP_KERNEL);
1503 if (!ctl->cache) {
1504 ret = -ENOMEM;
Li Xueb65ccd2019-11-15 13:54:13 -06001505 goto err_ctl_subname;
Charles Keepaxb21acc12015-04-13 13:28:01 +01001506 }
1507
Charles Keepax23237362015-04-13 13:28:02 +01001508 list_add(&ctl->list, &dsp->ctl_list);
1509
Stuart Henderson8eb084d2016-11-09 17:14:16 +00001510 if (flags & WMFW_CTL_FLAG_SYS)
1511 return 0;
1512
Charles Keepaxb21acc12015-04-13 13:28:01 +01001513 ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL);
1514 if (!ctl_work) {
1515 ret = -ENOMEM;
1516 goto err_ctl_cache;
1517 }
1518
1519 ctl_work->dsp = dsp;
1520 ctl_work->ctl = ctl;
1521 INIT_WORK(&ctl_work->work, wm_adsp_ctl_work);
1522 schedule_work(&ctl_work->work);
1523
1524 return 0;
1525
1526err_ctl_cache:
1527 kfree(ctl->cache);
Li Xueb65ccd2019-11-15 13:54:13 -06001528err_ctl_subname:
1529 kfree(ctl->subname);
Charles Keepaxb21acc12015-04-13 13:28:01 +01001530err_ctl_name:
1531 kfree(ctl->name);
1532err_ctl:
1533 kfree(ctl);
1534
1535 return ret;
1536}
1537
Charles Keepax23237362015-04-13 13:28:02 +01001538struct wm_coeff_parsed_alg {
1539 int id;
1540 const u8 *name;
1541 int name_len;
1542 int ncoeff;
1543};
1544
1545struct wm_coeff_parsed_coeff {
1546 int offset;
1547 int mem_type;
1548 const u8 *name;
1549 int name_len;
1550 int ctl_type;
1551 int flags;
1552 int len;
1553};
1554
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001555static int wm_coeff_parse_string(int bytes, const u8 **pos, const u8 **str)
1556{
1557 int length;
1558
1559 switch (bytes) {
1560 case 1:
1561 length = **pos;
1562 break;
1563 case 2:
Charles Keepax8299ee82015-04-20 13:52:44 +01001564 length = le16_to_cpu(*((__le16 *)*pos));
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001565 break;
1566 default:
1567 return 0;
1568 }
1569
1570 if (str)
1571 *str = *pos + bytes;
1572
1573 *pos += ((length + bytes) + 3) & ~0x03;
1574
1575 return length;
1576}
1577
1578static int wm_coeff_parse_int(int bytes, const u8 **pos)
1579{
1580 int val = 0;
1581
1582 switch (bytes) {
1583 case 2:
Charles Keepax8299ee82015-04-20 13:52:44 +01001584 val = le16_to_cpu(*((__le16 *)*pos));
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001585 break;
1586 case 4:
Charles Keepax8299ee82015-04-20 13:52:44 +01001587 val = le32_to_cpu(*((__le32 *)*pos));
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001588 break;
1589 default:
1590 break;
1591 }
1592
1593 *pos += bytes;
1594
1595 return val;
1596}
1597
Charles Keepax23237362015-04-13 13:28:02 +01001598static inline void wm_coeff_parse_alg(struct wm_adsp *dsp, const u8 **data,
1599 struct wm_coeff_parsed_alg *blk)
1600{
1601 const struct wmfw_adsp_alg_data *raw;
1602
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001603 switch (dsp->fw_ver) {
1604 case 0:
1605 case 1:
1606 raw = (const struct wmfw_adsp_alg_data *)*data;
1607 *data = raw->data;
Charles Keepax23237362015-04-13 13:28:02 +01001608
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001609 blk->id = le32_to_cpu(raw->id);
1610 blk->name = raw->name;
1611 blk->name_len = strlen(raw->name);
1612 blk->ncoeff = le32_to_cpu(raw->ncoeff);
1613 break;
1614 default:
1615 blk->id = wm_coeff_parse_int(sizeof(raw->id), data);
1616 blk->name_len = wm_coeff_parse_string(sizeof(u8), data,
1617 &blk->name);
1618 wm_coeff_parse_string(sizeof(u16), data, NULL);
1619 blk->ncoeff = wm_coeff_parse_int(sizeof(raw->ncoeff), data);
1620 break;
1621 }
Charles Keepax23237362015-04-13 13:28:02 +01001622
1623 adsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id);
1624 adsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name);
1625 adsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff);
1626}
1627
1628static inline void wm_coeff_parse_coeff(struct wm_adsp *dsp, const u8 **data,
1629 struct wm_coeff_parsed_coeff *blk)
1630{
1631 const struct wmfw_adsp_coeff_data *raw;
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001632 const u8 *tmp;
1633 int length;
Charles Keepax23237362015-04-13 13:28:02 +01001634
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001635 switch (dsp->fw_ver) {
1636 case 0:
1637 case 1:
1638 raw = (const struct wmfw_adsp_coeff_data *)*data;
1639 *data = *data + sizeof(raw->hdr) + le32_to_cpu(raw->hdr.size);
Charles Keepax23237362015-04-13 13:28:02 +01001640
Charles Keepaxcb5b57a2015-04-13 13:28:04 +01001641 blk->offset = le16_to_cpu(raw->hdr.offset);
1642 blk->mem_type = le16_to_cpu(raw->hdr.type);
1643 blk->name = raw->name;
1644 blk->name_len = strlen(raw->name);
1645 blk->ctl_type = le16_to_cpu(raw->ctl_type);
1646 blk->flags = le16_to_cpu(raw->flags);
1647 blk->len = le32_to_cpu(raw->len);
1648 break;
1649 default:
1650 tmp = *data;
1651 blk->offset = wm_coeff_parse_int(sizeof(raw->hdr.offset), &tmp);
1652 blk->mem_type = wm_coeff_parse_int(sizeof(raw->hdr.type), &tmp);
1653 length = wm_coeff_parse_int(sizeof(raw->hdr.size), &tmp);
1654 blk->name_len = wm_coeff_parse_string(sizeof(u8), &tmp,
1655 &blk->name);
1656 wm_coeff_parse_string(sizeof(u8), &tmp, NULL);
1657 wm_coeff_parse_string(sizeof(u16), &tmp, NULL);
1658 blk->ctl_type = wm_coeff_parse_int(sizeof(raw->ctl_type), &tmp);
1659 blk->flags = wm_coeff_parse_int(sizeof(raw->flags), &tmp);
1660 blk->len = wm_coeff_parse_int(sizeof(raw->len), &tmp);
1661
1662 *data = *data + sizeof(raw->hdr) + length;
1663 break;
1664 }
Charles Keepax23237362015-04-13 13:28:02 +01001665
1666 adsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type);
1667 adsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset);
1668 adsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name);
1669 adsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags);
1670 adsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type);
1671 adsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len);
1672}
1673
Richard Fitzgeraldf4f0c4c2016-11-09 17:14:17 +00001674static int wm_adsp_check_coeff_flags(struct wm_adsp *dsp,
1675 const struct wm_coeff_parsed_coeff *coeff_blk,
1676 unsigned int f_required,
1677 unsigned int f_illegal)
1678{
1679 if ((coeff_blk->flags & f_illegal) ||
1680 ((coeff_blk->flags & f_required) != f_required)) {
1681 adsp_err(dsp, "Illegal flags 0x%x for control type 0x%x\n",
1682 coeff_blk->flags, coeff_blk->ctl_type);
1683 return -EINVAL;
1684 }
1685
1686 return 0;
1687}
1688
Charles Keepax23237362015-04-13 13:28:02 +01001689static int wm_adsp_parse_coeff(struct wm_adsp *dsp,
1690 const struct wmfw_region *region)
1691{
1692 struct wm_adsp_alg_region alg_region = {};
1693 struct wm_coeff_parsed_alg alg_blk;
1694 struct wm_coeff_parsed_coeff coeff_blk;
1695 const u8 *data = region->data;
1696 int i, ret;
1697
1698 wm_coeff_parse_alg(dsp, &data, &alg_blk);
1699 for (i = 0; i < alg_blk.ncoeff; i++) {
1700 wm_coeff_parse_coeff(dsp, &data, &coeff_blk);
1701
1702 switch (coeff_blk.ctl_type) {
1703 case SNDRV_CTL_ELEM_TYPE_BYTES:
1704 break;
Richard Fitzgeralda23ebba2016-11-09 17:14:18 +00001705 case WMFW_CTL_TYPE_ACKED:
1706 if (coeff_blk.flags & WMFW_CTL_FLAG_SYS)
1707 continue; /* ignore */
1708
1709 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk,
1710 WMFW_CTL_FLAG_VOLATILE |
1711 WMFW_CTL_FLAG_WRITEABLE |
1712 WMFW_CTL_FLAG_READABLE,
1713 0);
1714 if (ret)
1715 return -EINVAL;
1716 break;
Richard Fitzgeraldf4f0c4c2016-11-09 17:14:17 +00001717 case WMFW_CTL_TYPE_HOSTEVENT:
1718 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk,
1719 WMFW_CTL_FLAG_SYS |
1720 WMFW_CTL_FLAG_VOLATILE |
1721 WMFW_CTL_FLAG_WRITEABLE |
1722 WMFW_CTL_FLAG_READABLE,
1723 0);
1724 if (ret)
1725 return -EINVAL;
1726 break;
Richard Fitzgeraldd52ed4b2018-07-19 11:50:39 +01001727 case WMFW_CTL_TYPE_HOST_BUFFER:
1728 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk,
1729 WMFW_CTL_FLAG_SYS |
1730 WMFW_CTL_FLAG_VOLATILE |
1731 WMFW_CTL_FLAG_READABLE,
1732 0);
1733 if (ret)
1734 return -EINVAL;
1735 break;
Charles Keepax23237362015-04-13 13:28:02 +01001736 default:
1737 adsp_err(dsp, "Unknown control type: %d\n",
1738 coeff_blk.ctl_type);
1739 return -EINVAL;
1740 }
1741
1742 alg_region.type = coeff_blk.mem_type;
1743 alg_region.alg = alg_blk.id;
1744
1745 ret = wm_adsp_create_control(dsp, &alg_region,
1746 coeff_blk.offset,
1747 coeff_blk.len,
1748 coeff_blk.name,
Charles Keepax26c22a12015-04-20 13:52:45 +01001749 coeff_blk.name_len,
Stuart Henderson8eb084d2016-11-09 17:14:16 +00001750 coeff_blk.flags,
1751 coeff_blk.ctl_type);
Charles Keepax23237362015-04-13 13:28:02 +01001752 if (ret < 0)
1753 adsp_err(dsp, "Failed to create control: %.*s, %d\n",
1754 coeff_blk.name_len, coeff_blk.name, ret);
1755 }
1756
1757 return 0;
1758}
1759
Charles Keepax4e08d502019-03-19 11:52:12 +00001760static unsigned int wm_adsp1_parse_sizes(struct wm_adsp *dsp,
1761 const char * const file,
1762 unsigned int pos,
1763 const struct firmware *firmware)
1764{
1765 const struct wmfw_adsp1_sizes *adsp1_sizes;
1766
1767 adsp1_sizes = (void *)&firmware->data[pos];
1768
1769 adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n", file,
1770 le32_to_cpu(adsp1_sizes->dm), le32_to_cpu(adsp1_sizes->pm),
1771 le32_to_cpu(adsp1_sizes->zm));
1772
1773 return pos + sizeof(*adsp1_sizes);
1774}
1775
1776static unsigned int wm_adsp2_parse_sizes(struct wm_adsp *dsp,
1777 const char * const file,
1778 unsigned int pos,
1779 const struct firmware *firmware)
1780{
1781 const struct wmfw_adsp2_sizes *adsp2_sizes;
1782
1783 adsp2_sizes = (void *)&firmware->data[pos];
1784
1785 adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n", file,
1786 le32_to_cpu(adsp2_sizes->xm), le32_to_cpu(adsp2_sizes->ym),
1787 le32_to_cpu(adsp2_sizes->pm), le32_to_cpu(adsp2_sizes->zm));
1788
1789 return pos + sizeof(*adsp2_sizes);
1790}
1791
1792static bool wm_adsp_validate_version(struct wm_adsp *dsp, unsigned int version)
1793{
1794 switch (version) {
1795 case 0:
1796 adsp_warn(dsp, "Deprecated file format %d\n", version);
1797 return true;
1798 case 1:
1799 case 2:
1800 return true;
1801 default:
1802 return false;
1803 }
1804}
1805
Wen Shi170b1e12019-03-19 11:52:13 +00001806static bool wm_halo_validate_version(struct wm_adsp *dsp, unsigned int version)
1807{
1808 switch (version) {
1809 case 3:
1810 return true;
1811 default:
1812 return false;
1813 }
1814}
1815
Mark Brown2159ad932012-10-11 11:54:02 +09001816static int wm_adsp_load(struct wm_adsp *dsp)
1817{
Mark Browncf17c832013-01-30 14:37:23 +08001818 LIST_HEAD(buf_list);
Mark Brown2159ad932012-10-11 11:54:02 +09001819 const struct firmware *firmware;
1820 struct regmap *regmap = dsp->regmap;
1821 unsigned int pos = 0;
1822 const struct wmfw_header *header;
1823 const struct wmfw_adsp1_sizes *adsp1_sizes;
Mark Brown2159ad932012-10-11 11:54:02 +09001824 const struct wmfw_footer *footer;
1825 const struct wmfw_region *region;
1826 const struct wm_adsp_region *mem;
1827 const char *region_name;
Richard Fitzgerald1cab2a82016-12-20 10:29:12 +00001828 char *file, *text = NULL;
Mark Browncf17c832013-01-30 14:37:23 +08001829 struct wm_adsp_buf *buf;
Mark Brown2159ad932012-10-11 11:54:02 +09001830 unsigned int reg;
1831 int regions = 0;
Charles Keepax4e08d502019-03-19 11:52:12 +00001832 int ret, offset, type;
Mark Brown2159ad932012-10-11 11:54:02 +09001833
1834 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
1835 if (file == NULL)
1836 return -ENOMEM;
1837
Richard Fitzgerald605391d2018-08-08 17:13:39 +01001838 snprintf(file, PAGE_SIZE, "%s-%s-%s.wmfw", dsp->part, dsp->fwf_name,
Mark Brown1023dbd2013-01-11 22:58:28 +00001839 wm_adsp_fw[dsp->fw].file);
Mark Brown2159ad932012-10-11 11:54:02 +09001840 file[PAGE_SIZE - 1] = '\0';
1841
1842 ret = request_firmware(&firmware, file, dsp->dev);
1843 if (ret != 0) {
1844 adsp_err(dsp, "Failed to request '%s'\n", file);
1845 goto out;
1846 }
1847 ret = -EINVAL;
1848
1849 pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
1850 if (pos >= firmware->size) {
1851 adsp_err(dsp, "%s: file too short, %zu bytes\n",
1852 file, firmware->size);
1853 goto out_fw;
1854 }
1855
Charles Keepax7585a5b2015-12-08 16:08:25 +00001856 header = (void *)&firmware->data[0];
Mark Brown2159ad932012-10-11 11:54:02 +09001857
1858 if (memcmp(&header->magic[0], "WMFW", 4) != 0) {
1859 adsp_err(dsp, "%s: invalid magic\n", file);
1860 goto out_fw;
1861 }
1862
Charles Keepax4e08d502019-03-19 11:52:12 +00001863 if (!dsp->ops->validate_version(dsp, header->ver)) {
Mark Brown2159ad932012-10-11 11:54:02 +09001864 adsp_err(dsp, "%s: unknown file format %d\n",
1865 file, header->ver);
1866 goto out_fw;
1867 }
Charles Keepax23237362015-04-13 13:28:02 +01001868
Dimitris Papastamos36269922013-11-01 15:56:57 +00001869 adsp_info(dsp, "Firmware version: %d\n", header->ver);
Charles Keepax23237362015-04-13 13:28:02 +01001870 dsp->fw_ver = header->ver;
Mark Brown2159ad932012-10-11 11:54:02 +09001871
1872 if (header->core != dsp->type) {
1873 adsp_err(dsp, "%s: invalid core %d != %d\n",
1874 file, header->core, dsp->type);
1875 goto out_fw;
1876 }
1877
Charles Keepax4e08d502019-03-19 11:52:12 +00001878 pos = sizeof(*header);
1879 pos = dsp->ops->parse_sizes(dsp, file, pos, firmware);
Mark Brown2159ad932012-10-11 11:54:02 +09001880
Charles Keepax4e08d502019-03-19 11:52:12 +00001881 footer = (void *)&firmware->data[pos];
1882 pos += sizeof(*footer);
Mark Brown2159ad932012-10-11 11:54:02 +09001883
Charles Keepax4e08d502019-03-19 11:52:12 +00001884 if (le32_to_cpu(header->len) != pos) {
Mark Brown2159ad932012-10-11 11:54:02 +09001885 adsp_err(dsp, "%s: unexpected header length %d\n",
1886 file, le32_to_cpu(header->len));
1887 goto out_fw;
1888 }
1889
1890 adsp_dbg(dsp, "%s: timestamp %llu\n", file,
1891 le64_to_cpu(footer->timestamp));
1892
1893 while (pos < firmware->size &&
Ben Hutchings50dd2ea2017-12-08 16:15:20 +00001894 sizeof(*region) < firmware->size - pos) {
Mark Brown2159ad932012-10-11 11:54:02 +09001895 region = (void *)&(firmware->data[pos]);
1896 region_name = "Unknown";
1897 reg = 0;
1898 text = NULL;
1899 offset = le32_to_cpu(region->offset) & 0xffffff;
1900 type = be32_to_cpu(region->type) & 0xff;
Charles Keepax7585a5b2015-12-08 16:08:25 +00001901
Mark Brown2159ad932012-10-11 11:54:02 +09001902 switch (type) {
1903 case WMFW_NAME_TEXT:
1904 region_name = "Firmware name";
1905 text = kzalloc(le32_to_cpu(region->len) + 1,
1906 GFP_KERNEL);
1907 break;
Charles Keepax23237362015-04-13 13:28:02 +01001908 case WMFW_ALGORITHM_DATA:
1909 region_name = "Algorithm";
1910 ret = wm_adsp_parse_coeff(dsp, region);
1911 if (ret != 0)
1912 goto out_fw;
1913 break;
Mark Brown2159ad932012-10-11 11:54:02 +09001914 case WMFW_INFO_TEXT:
1915 region_name = "Information";
1916 text = kzalloc(le32_to_cpu(region->len) + 1,
1917 GFP_KERNEL);
1918 break;
1919 case WMFW_ABSOLUTE:
1920 region_name = "Absolute";
1921 reg = offset;
1922 break;
1923 case WMFW_ADSP1_PM:
Mark Brown2159ad932012-10-11 11:54:02 +09001924 case WMFW_ADSP1_DM:
Mark Brown2159ad932012-10-11 11:54:02 +09001925 case WMFW_ADSP2_XM:
Mark Brown2159ad932012-10-11 11:54:02 +09001926 case WMFW_ADSP2_YM:
Mark Brown2159ad932012-10-11 11:54:02 +09001927 case WMFW_ADSP1_ZM:
Wen Shi170b1e12019-03-19 11:52:13 +00001928 case WMFW_HALO_PM_PACKED:
1929 case WMFW_HALO_XM_PACKED:
1930 case WMFW_HALO_YM_PACKED:
1931 mem = wm_adsp_find_region(dsp, type);
1932 if (!mem) {
1933 adsp_err(dsp, "No region of type: %x\n", type);
1934 goto out_fw;
1935 }
1936
Richard Fitzgerald9ce5e6e2016-11-09 17:14:15 +00001937 region_name = wm_adsp_mem_region_name(type);
Wen Shi170b1e12019-03-19 11:52:13 +00001938 reg = dsp->ops->region_to_reg(mem, offset);
Mark Brown2159ad932012-10-11 11:54:02 +09001939 break;
1940 default:
1941 adsp_warn(dsp,
1942 "%s.%d: Unknown region type %x at %d(%x)\n",
1943 file, regions, type, pos, pos);
1944 break;
1945 }
1946
1947 adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file,
1948 regions, le32_to_cpu(region->len), offset,
1949 region_name);
1950
Ben Hutchings50dd2ea2017-12-08 16:15:20 +00001951 if (le32_to_cpu(region->len) >
1952 firmware->size - pos - sizeof(*region)) {
Richard Fitzgerald1cab2a82016-12-20 10:29:12 +00001953 adsp_err(dsp,
1954 "%s.%d: %s region len %d bytes exceeds file length %zu\n",
1955 file, regions, region_name,
1956 le32_to_cpu(region->len), firmware->size);
1957 ret = -EINVAL;
1958 goto out_fw;
1959 }
1960
Mark Brown2159ad932012-10-11 11:54:02 +09001961 if (text) {
1962 memcpy(text, region->data, le32_to_cpu(region->len));
1963 adsp_info(dsp, "%s: %s\n", file, text);
1964 kfree(text);
Richard Fitzgerald1cab2a82016-12-20 10:29:12 +00001965 text = NULL;
Mark Brown2159ad932012-10-11 11:54:02 +09001966 }
1967
1968 if (reg) {
Charles Keepaxcdcd7f72014-11-14 15:40:45 +00001969 buf = wm_adsp_buf_alloc(region->data,
1970 le32_to_cpu(region->len),
1971 &buf_list);
1972 if (!buf) {
1973 adsp_err(dsp, "Out of memory\n");
1974 ret = -ENOMEM;
1975 goto out_fw;
1976 }
Mark Browna76fefa2013-01-07 19:03:17 +00001977
Charles Keepaxcdcd7f72014-11-14 15:40:45 +00001978 ret = regmap_raw_write_async(regmap, reg, buf->buf,
1979 le32_to_cpu(region->len));
1980 if (ret != 0) {
1981 adsp_err(dsp,
1982 "%s.%d: Failed to write %d bytes at %d in %s: %d\n",
1983 file, regions,
1984 le32_to_cpu(region->len), offset,
1985 region_name, ret);
1986 goto out_fw;
Mark Brown2159ad932012-10-11 11:54:02 +09001987 }
1988 }
1989
1990 pos += le32_to_cpu(region->len) + sizeof(*region);
1991 regions++;
1992 }
Mark Browncf17c832013-01-30 14:37:23 +08001993
1994 ret = regmap_async_complete(regmap);
1995 if (ret != 0) {
1996 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
1997 goto out_fw;
1998 }
1999
Mark Brown2159ad932012-10-11 11:54:02 +09002000 if (pos > firmware->size)
2001 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
2002 file, regions, pos - firmware->size);
2003
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +01002004 wm_adsp_debugfs_save_wmfwname(dsp, file);
2005
Mark Brown2159ad932012-10-11 11:54:02 +09002006out_fw:
Mark Browncf17c832013-01-30 14:37:23 +08002007 regmap_async_complete(regmap);
2008 wm_adsp_buf_free(&buf_list);
Mark Brown2159ad932012-10-11 11:54:02 +09002009 release_firmware(firmware);
Richard Fitzgerald1cab2a82016-12-20 10:29:12 +00002010 kfree(text);
Mark Brown2159ad932012-10-11 11:54:02 +09002011out:
2012 kfree(file);
2013
2014 return ret;
2015}
2016
Li Xueb65ccd2019-11-15 13:54:13 -06002017/*
2018 * Find wm_coeff_ctl with input name as its subname
2019 * If not found, return NULL
2020 */
2021static struct wm_coeff_ctl *wm_adsp_get_ctl(struct wm_adsp *dsp,
2022 const char *name, int type,
2023 unsigned int alg)
2024{
2025 struct wm_coeff_ctl *pos, *rslt = NULL;
2026
2027 list_for_each_entry(pos, &dsp->ctl_list, list) {
2028 if (!pos->subname)
2029 continue;
2030 if (strncmp(pos->subname, name, pos->subname_len) == 0 &&
2031 pos->alg_region.alg == alg &&
2032 pos->alg_region.type == type) {
2033 rslt = pos;
2034 break;
2035 }
2036 }
2037
2038 return rslt;
2039}
2040
2041int wm_adsp_write_ctl(struct wm_adsp *dsp, const char *name, int type,
2042 unsigned int alg, void *buf, size_t len)
2043{
2044 struct wm_coeff_ctl *ctl;
2045 struct snd_kcontrol *kcontrol;
2046 int ret;
2047
2048 ctl = wm_adsp_get_ctl(dsp, name, type, alg);
2049 if (!ctl)
2050 return -EINVAL;
2051
2052 if (len > ctl->len)
2053 return -EINVAL;
2054
Charles Keepax73ecf1a2020-01-14 16:18:38 +00002055 ret = wm_coeff_write_ctrl(ctl, buf, len);
Li Xueb65ccd2019-11-15 13:54:13 -06002056
2057 kcontrol = snd_soc_card_get_kcontrol(dsp->component->card, ctl->name);
2058 snd_ctl_notify(dsp->component->card->snd_card,
2059 SNDRV_CTL_EVENT_MASK_VALUE, &kcontrol->id);
2060
2061 return ret;
2062}
2063EXPORT_SYMBOL_GPL(wm_adsp_write_ctl);
2064
2065int wm_adsp_read_ctl(struct wm_adsp *dsp, const char *name, int type,
2066 unsigned int alg, void *buf, size_t len)
2067{
2068 struct wm_coeff_ctl *ctl;
2069
2070 ctl = wm_adsp_get_ctl(dsp, name, type, alg);
2071 if (!ctl)
2072 return -EINVAL;
2073
2074 if (len > ctl->len)
2075 return -EINVAL;
2076
Charles Keepax73ecf1a2020-01-14 16:18:38 +00002077 return wm_coeff_read_ctrl(ctl, buf, len);
Li Xueb65ccd2019-11-15 13:54:13 -06002078}
2079EXPORT_SYMBOL_GPL(wm_adsp_read_ctl);
2080
Charles Keepax23237362015-04-13 13:28:02 +01002081static void wm_adsp_ctl_fixup_base(struct wm_adsp *dsp,
2082 const struct wm_adsp_alg_region *alg_region)
2083{
2084 struct wm_coeff_ctl *ctl;
2085
2086 list_for_each_entry(ctl, &dsp->ctl_list, list) {
2087 if (ctl->fw_name == wm_adsp_fw_text[dsp->fw] &&
2088 alg_region->alg == ctl->alg_region.alg &&
2089 alg_region->type == ctl->alg_region.type) {
2090 ctl->alg_region.base = alg_region->base;
2091 }
2092 }
2093}
2094
Charles Keepax3809f002015-04-13 13:27:54 +01002095static void *wm_adsp_read_algs(struct wm_adsp *dsp, size_t n_algs,
Charles Keepax7f7cca02018-06-20 11:56:21 +01002096 const struct wm_adsp_region *mem,
Charles Keepaxb618a1852015-04-13 13:27:53 +01002097 unsigned int pos, unsigned int len)
Mark Browndb405172012-10-26 19:30:40 +01002098{
Charles Keepaxb618a1852015-04-13 13:27:53 +01002099 void *alg;
Charles Keepax7f7cca02018-06-20 11:56:21 +01002100 unsigned int reg;
Charles Keepaxb618a1852015-04-13 13:27:53 +01002101 int ret;
Mark Browndb405172012-10-26 19:30:40 +01002102 __be32 val;
Mark Browndb405172012-10-26 19:30:40 +01002103
Charles Keepax3809f002015-04-13 13:27:54 +01002104 if (n_algs == 0) {
Mark Browndb405172012-10-26 19:30:40 +01002105 adsp_err(dsp, "No algorithms\n");
Charles Keepaxb618a1852015-04-13 13:27:53 +01002106 return ERR_PTR(-EINVAL);
Mark Browndb405172012-10-26 19:30:40 +01002107 }
2108
Charles Keepax3809f002015-04-13 13:27:54 +01002109 if (n_algs > 1024) {
2110 adsp_err(dsp, "Algorithm count %zx excessive\n", n_algs);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002111 return ERR_PTR(-EINVAL);
Mark Brownd62f4bc2012-12-19 14:00:30 +00002112 }
2113
Mark Browndb405172012-10-26 19:30:40 +01002114 /* Read the terminator first to validate the length */
Wen Shi170b1e12019-03-19 11:52:13 +00002115 reg = dsp->ops->region_to_reg(mem, pos + len);
Charles Keepax7f7cca02018-06-20 11:56:21 +01002116
2117 ret = regmap_raw_read(dsp->regmap, reg, &val, sizeof(val));
Mark Browndb405172012-10-26 19:30:40 +01002118 if (ret != 0) {
2119 adsp_err(dsp, "Failed to read algorithm list end: %d\n",
2120 ret);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002121 return ERR_PTR(ret);
Mark Browndb405172012-10-26 19:30:40 +01002122 }
2123
2124 if (be32_to_cpu(val) != 0xbedead)
Richard Fitzgerald503ada82017-05-26 10:47:07 +01002125 adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbedead\n",
Charles Keepax7f7cca02018-06-20 11:56:21 +01002126 reg, be32_to_cpu(val));
2127
2128 /* Convert length from DSP words to bytes */
2129 len *= sizeof(u32);
Mark Browndb405172012-10-26 19:30:40 +01002130
Charles Keepax517ee742018-07-19 11:50:35 +01002131 alg = kzalloc(len, GFP_KERNEL | GFP_DMA);
Mark Browndb405172012-10-26 19:30:40 +01002132 if (!alg)
Charles Keepaxb618a1852015-04-13 13:27:53 +01002133 return ERR_PTR(-ENOMEM);
Mark Browndb405172012-10-26 19:30:40 +01002134
Wen Shi170b1e12019-03-19 11:52:13 +00002135 reg = dsp->ops->region_to_reg(mem, pos);
Charles Keepax7f7cca02018-06-20 11:56:21 +01002136
2137 ret = regmap_raw_read(dsp->regmap, reg, alg, len);
Mark Browndb405172012-10-26 19:30:40 +01002138 if (ret != 0) {
Charles Keepax7d00cd92016-02-19 14:44:43 +00002139 adsp_err(dsp, "Failed to read algorithm list: %d\n", ret);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002140 kfree(alg);
2141 return ERR_PTR(ret);
Mark Browndb405172012-10-26 19:30:40 +01002142 }
2143
Charles Keepaxb618a1852015-04-13 13:27:53 +01002144 return alg;
2145}
2146
Charles Keepax14197092015-12-15 11:29:43 +00002147static struct wm_adsp_alg_region *
2148 wm_adsp_find_alg_region(struct wm_adsp *dsp, int type, unsigned int id)
2149{
2150 struct wm_adsp_alg_region *alg_region;
2151
2152 list_for_each_entry(alg_region, &dsp->alg_regions, list) {
2153 if (id == alg_region->alg && type == alg_region->type)
2154 return alg_region;
2155 }
2156
2157 return NULL;
2158}
2159
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002160static struct wm_adsp_alg_region *wm_adsp_create_region(struct wm_adsp *dsp,
2161 int type, __be32 id,
2162 __be32 base)
2163{
2164 struct wm_adsp_alg_region *alg_region;
2165
2166 alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL);
2167 if (!alg_region)
2168 return ERR_PTR(-ENOMEM);
2169
2170 alg_region->type = type;
2171 alg_region->alg = be32_to_cpu(id);
2172 alg_region->base = be32_to_cpu(base);
2173
2174 list_add_tail(&alg_region->list, &dsp->alg_regions);
2175
Charles Keepax23237362015-04-13 13:28:02 +01002176 if (dsp->fw_ver > 0)
2177 wm_adsp_ctl_fixup_base(dsp, alg_region);
2178
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002179 return alg_region;
2180}
2181
Richard Fitzgerald56574d52016-04-27 14:58:29 +01002182static void wm_adsp_free_alg_regions(struct wm_adsp *dsp)
2183{
2184 struct wm_adsp_alg_region *alg_region;
2185
2186 while (!list_empty(&dsp->alg_regions)) {
2187 alg_region = list_first_entry(&dsp->alg_regions,
2188 struct wm_adsp_alg_region,
2189 list);
2190 list_del(&alg_region->list);
2191 kfree(alg_region);
2192 }
2193}
2194
Charles Keepaxa5dcb242019-03-19 11:52:11 +00002195static void wmfw_parse_id_header(struct wm_adsp *dsp,
2196 struct wmfw_id_hdr *fw, int nalgs)
2197{
2198 dsp->fw_id = be32_to_cpu(fw->id);
2199 dsp->fw_id_version = be32_to_cpu(fw->ver);
2200
Charles Keepaxcd537872019-03-19 17:43:09 +00002201 adsp_info(dsp, "Firmware: %x v%d.%d.%d, %d algorithms\n",
Charles Keepaxa5dcb242019-03-19 11:52:11 +00002202 dsp->fw_id, (dsp->fw_id_version & 0xff0000) >> 16,
2203 (dsp->fw_id_version & 0xff00) >> 8, dsp->fw_id_version & 0xff,
2204 nalgs);
2205}
2206
Wen Shi170b1e12019-03-19 11:52:13 +00002207static void wmfw_v3_parse_id_header(struct wm_adsp *dsp,
2208 struct wmfw_v3_id_hdr *fw, int nalgs)
2209{
2210 dsp->fw_id = be32_to_cpu(fw->id);
2211 dsp->fw_id_version = be32_to_cpu(fw->ver);
2212 dsp->fw_vendor_id = be32_to_cpu(fw->vendor_id);
2213
Charles Keepaxcd537872019-03-19 17:43:09 +00002214 adsp_info(dsp, "Firmware: %x vendor: 0x%x v%d.%d.%d, %d algorithms\n",
Wen Shi170b1e12019-03-19 11:52:13 +00002215 dsp->fw_id, dsp->fw_vendor_id,
2216 (dsp->fw_id_version & 0xff0000) >> 16,
2217 (dsp->fw_id_version & 0xff00) >> 8, dsp->fw_id_version & 0xff,
2218 nalgs);
2219}
2220
2221static int wm_adsp_create_regions(struct wm_adsp *dsp, __be32 id, int nregions,
2222 int *type, __be32 *base)
2223{
2224 struct wm_adsp_alg_region *alg_region;
2225 int i;
2226
2227 for (i = 0; i < nregions; i++) {
2228 alg_region = wm_adsp_create_region(dsp, type[i], id, base[i]);
2229 if (IS_ERR(alg_region))
2230 return PTR_ERR(alg_region);
2231 }
2232
2233 return 0;
2234}
2235
Charles Keepaxb618a1852015-04-13 13:27:53 +01002236static int wm_adsp1_setup_algs(struct wm_adsp *dsp)
2237{
2238 struct wmfw_adsp1_id_hdr adsp1_id;
2239 struct wmfw_adsp1_alg_hdr *adsp1_alg;
Charles Keepax3809f002015-04-13 13:27:54 +01002240 struct wm_adsp_alg_region *alg_region;
Charles Keepaxb618a1852015-04-13 13:27:53 +01002241 const struct wm_adsp_region *mem;
2242 unsigned int pos, len;
Charles Keepax3809f002015-04-13 13:27:54 +01002243 size_t n_algs;
Charles Keepaxb618a1852015-04-13 13:27:53 +01002244 int i, ret;
2245
2246 mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM);
2247 if (WARN_ON(!mem))
2248 return -EINVAL;
2249
2250 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id,
2251 sizeof(adsp1_id));
2252 if (ret != 0) {
2253 adsp_err(dsp, "Failed to read algorithm info: %d\n",
2254 ret);
2255 return ret;
2256 }
2257
Charles Keepax3809f002015-04-13 13:27:54 +01002258 n_algs = be32_to_cpu(adsp1_id.n_algs);
Charles Keepaxa5dcb242019-03-19 11:52:11 +00002259
2260 wmfw_parse_id_header(dsp, &adsp1_id.fw, n_algs);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002261
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002262 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
2263 adsp1_id.fw.id, adsp1_id.zm);
2264 if (IS_ERR(alg_region))
2265 return PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002266
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002267 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
2268 adsp1_id.fw.id, adsp1_id.dm);
2269 if (IS_ERR(alg_region))
2270 return PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002271
Charles Keepax7f7cca02018-06-20 11:56:21 +01002272 /* Calculate offset and length in DSP words */
2273 pos = sizeof(adsp1_id) / sizeof(u32);
2274 len = (sizeof(*adsp1_alg) * n_algs) / sizeof(u32);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002275
Charles Keepax7f7cca02018-06-20 11:56:21 +01002276 adsp1_alg = wm_adsp_read_algs(dsp, n_algs, mem, pos, len);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002277 if (IS_ERR(adsp1_alg))
2278 return PTR_ERR(adsp1_alg);
Mark Browndb405172012-10-26 19:30:40 +01002279
Charles Keepax3809f002015-04-13 13:27:54 +01002280 for (i = 0; i < n_algs; i++) {
Charles Keepaxb618a1852015-04-13 13:27:53 +01002281 adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
2282 i, be32_to_cpu(adsp1_alg[i].alg.id),
2283 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16,
2284 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8,
2285 be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff,
2286 be32_to_cpu(adsp1_alg[i].dm),
2287 be32_to_cpu(adsp1_alg[i].zm));
Mark Brown471f4882013-01-08 16:09:31 +00002288
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002289 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
2290 adsp1_alg[i].alg.id,
2291 adsp1_alg[i].dm);
2292 if (IS_ERR(alg_region)) {
2293 ret = PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002294 goto out;
2295 }
Charles Keepax23237362015-04-13 13:28:02 +01002296 if (dsp->fw_ver == 0) {
2297 if (i + 1 < n_algs) {
2298 len = be32_to_cpu(adsp1_alg[i + 1].dm);
2299 len -= be32_to_cpu(adsp1_alg[i].dm);
2300 len *= 4;
2301 wm_adsp_create_control(dsp, alg_region, 0,
Stuart Henderson8eb084d2016-11-09 17:14:16 +00002302 len, NULL, 0, 0,
2303 SNDRV_CTL_ELEM_TYPE_BYTES);
Charles Keepax23237362015-04-13 13:28:02 +01002304 } else {
2305 adsp_warn(dsp, "Missing length info for region DM with ID %x\n",
2306 be32_to_cpu(adsp1_alg[i].alg.id));
2307 }
Charles Keepaxb618a1852015-04-13 13:27:53 +01002308 }
Mark Brown471f4882013-01-08 16:09:31 +00002309
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002310 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
2311 adsp1_alg[i].alg.id,
2312 adsp1_alg[i].zm);
2313 if (IS_ERR(alg_region)) {
2314 ret = PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002315 goto out;
2316 }
Charles Keepax23237362015-04-13 13:28:02 +01002317 if (dsp->fw_ver == 0) {
2318 if (i + 1 < n_algs) {
2319 len = be32_to_cpu(adsp1_alg[i + 1].zm);
2320 len -= be32_to_cpu(adsp1_alg[i].zm);
2321 len *= 4;
2322 wm_adsp_create_control(dsp, alg_region, 0,
Stuart Henderson8eb084d2016-11-09 17:14:16 +00002323 len, NULL, 0, 0,
2324 SNDRV_CTL_ELEM_TYPE_BYTES);
Charles Keepax23237362015-04-13 13:28:02 +01002325 } else {
2326 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
2327 be32_to_cpu(adsp1_alg[i].alg.id));
2328 }
Mark Browndb405172012-10-26 19:30:40 +01002329 }
2330 }
2331
2332out:
Charles Keepaxb618a1852015-04-13 13:27:53 +01002333 kfree(adsp1_alg);
2334 return ret;
2335}
2336
2337static int wm_adsp2_setup_algs(struct wm_adsp *dsp)
2338{
2339 struct wmfw_adsp2_id_hdr adsp2_id;
2340 struct wmfw_adsp2_alg_hdr *adsp2_alg;
Charles Keepax3809f002015-04-13 13:27:54 +01002341 struct wm_adsp_alg_region *alg_region;
Charles Keepaxb618a1852015-04-13 13:27:53 +01002342 const struct wm_adsp_region *mem;
2343 unsigned int pos, len;
Charles Keepax3809f002015-04-13 13:27:54 +01002344 size_t n_algs;
Charles Keepaxb618a1852015-04-13 13:27:53 +01002345 int i, ret;
2346
2347 mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM);
2348 if (WARN_ON(!mem))
2349 return -EINVAL;
2350
2351 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id,
2352 sizeof(adsp2_id));
2353 if (ret != 0) {
2354 adsp_err(dsp, "Failed to read algorithm info: %d\n",
2355 ret);
2356 return ret;
2357 }
2358
Charles Keepax3809f002015-04-13 13:27:54 +01002359 n_algs = be32_to_cpu(adsp2_id.n_algs);
Charles Keepaxa5dcb242019-03-19 11:52:11 +00002360
2361 wmfw_parse_id_header(dsp, &adsp2_id.fw, n_algs);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002362
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002363 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
2364 adsp2_id.fw.id, adsp2_id.xm);
2365 if (IS_ERR(alg_region))
2366 return PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002367
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002368 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
2369 adsp2_id.fw.id, adsp2_id.ym);
2370 if (IS_ERR(alg_region))
2371 return PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002372
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002373 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
2374 adsp2_id.fw.id, adsp2_id.zm);
2375 if (IS_ERR(alg_region))
2376 return PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002377
Charles Keepax7f7cca02018-06-20 11:56:21 +01002378 /* Calculate offset and length in DSP words */
2379 pos = sizeof(adsp2_id) / sizeof(u32);
2380 len = (sizeof(*adsp2_alg) * n_algs) / sizeof(u32);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002381
Charles Keepax7f7cca02018-06-20 11:56:21 +01002382 adsp2_alg = wm_adsp_read_algs(dsp, n_algs, mem, pos, len);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002383 if (IS_ERR(adsp2_alg))
2384 return PTR_ERR(adsp2_alg);
2385
Charles Keepax3809f002015-04-13 13:27:54 +01002386 for (i = 0; i < n_algs; i++) {
Charles Keepaxb618a1852015-04-13 13:27:53 +01002387 adsp_info(dsp,
2388 "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
2389 i, be32_to_cpu(adsp2_alg[i].alg.id),
2390 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16,
2391 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8,
2392 be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff,
2393 be32_to_cpu(adsp2_alg[i].xm),
2394 be32_to_cpu(adsp2_alg[i].ym),
2395 be32_to_cpu(adsp2_alg[i].zm));
2396
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002397 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
2398 adsp2_alg[i].alg.id,
2399 adsp2_alg[i].xm);
2400 if (IS_ERR(alg_region)) {
2401 ret = PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002402 goto out;
2403 }
Charles Keepax23237362015-04-13 13:28:02 +01002404 if (dsp->fw_ver == 0) {
2405 if (i + 1 < n_algs) {
2406 len = be32_to_cpu(adsp2_alg[i + 1].xm);
2407 len -= be32_to_cpu(adsp2_alg[i].xm);
2408 len *= 4;
2409 wm_adsp_create_control(dsp, alg_region, 0,
Stuart Henderson8eb084d2016-11-09 17:14:16 +00002410 len, NULL, 0, 0,
2411 SNDRV_CTL_ELEM_TYPE_BYTES);
Charles Keepax23237362015-04-13 13:28:02 +01002412 } else {
2413 adsp_warn(dsp, "Missing length info for region XM with ID %x\n",
2414 be32_to_cpu(adsp2_alg[i].alg.id));
2415 }
Charles Keepaxb618a1852015-04-13 13:27:53 +01002416 }
2417
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002418 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
2419 adsp2_alg[i].alg.id,
2420 adsp2_alg[i].ym);
2421 if (IS_ERR(alg_region)) {
2422 ret = PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002423 goto out;
2424 }
Charles Keepax23237362015-04-13 13:28:02 +01002425 if (dsp->fw_ver == 0) {
2426 if (i + 1 < n_algs) {
2427 len = be32_to_cpu(adsp2_alg[i + 1].ym);
2428 len -= be32_to_cpu(adsp2_alg[i].ym);
2429 len *= 4;
2430 wm_adsp_create_control(dsp, alg_region, 0,
Stuart Henderson8eb084d2016-11-09 17:14:16 +00002431 len, NULL, 0, 0,
2432 SNDRV_CTL_ELEM_TYPE_BYTES);
Charles Keepax23237362015-04-13 13:28:02 +01002433 } else {
2434 adsp_warn(dsp, "Missing length info for region YM with ID %x\n",
2435 be32_to_cpu(adsp2_alg[i].alg.id));
2436 }
Charles Keepaxb618a1852015-04-13 13:27:53 +01002437 }
2438
Charles Keepaxd9d20e12015-04-13 13:27:59 +01002439 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
2440 adsp2_alg[i].alg.id,
2441 adsp2_alg[i].zm);
2442 if (IS_ERR(alg_region)) {
2443 ret = PTR_ERR(alg_region);
Charles Keepaxb618a1852015-04-13 13:27:53 +01002444 goto out;
2445 }
Charles Keepax23237362015-04-13 13:28:02 +01002446 if (dsp->fw_ver == 0) {
2447 if (i + 1 < n_algs) {
2448 len = be32_to_cpu(adsp2_alg[i + 1].zm);
2449 len -= be32_to_cpu(adsp2_alg[i].zm);
2450 len *= 4;
2451 wm_adsp_create_control(dsp, alg_region, 0,
Stuart Henderson8eb084d2016-11-09 17:14:16 +00002452 len, NULL, 0, 0,
2453 SNDRV_CTL_ELEM_TYPE_BYTES);
Charles Keepax23237362015-04-13 13:28:02 +01002454 } else {
2455 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
2456 be32_to_cpu(adsp2_alg[i].alg.id));
2457 }
Charles Keepaxb618a1852015-04-13 13:27:53 +01002458 }
2459 }
2460
2461out:
2462 kfree(adsp2_alg);
Mark Browndb405172012-10-26 19:30:40 +01002463 return ret;
2464}
2465
Wen Shi170b1e12019-03-19 11:52:13 +00002466static int wm_halo_create_regions(struct wm_adsp *dsp, __be32 id,
2467 __be32 xm_base, __be32 ym_base)
2468{
2469 int types[] = {
2470 WMFW_ADSP2_XM, WMFW_HALO_XM_PACKED,
2471 WMFW_ADSP2_YM, WMFW_HALO_YM_PACKED
2472 };
2473 __be32 bases[] = { xm_base, xm_base, ym_base, ym_base };
2474
2475 return wm_adsp_create_regions(dsp, id, ARRAY_SIZE(types), types, bases);
2476}
2477
2478static int wm_halo_setup_algs(struct wm_adsp *dsp)
2479{
2480 struct wmfw_halo_id_hdr halo_id;
2481 struct wmfw_halo_alg_hdr *halo_alg;
2482 const struct wm_adsp_region *mem;
2483 unsigned int pos, len;
2484 size_t n_algs;
2485 int i, ret;
2486
2487 mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM);
2488 if (WARN_ON(!mem))
2489 return -EINVAL;
2490
2491 ret = regmap_raw_read(dsp->regmap, mem->base, &halo_id,
2492 sizeof(halo_id));
2493 if (ret != 0) {
2494 adsp_err(dsp, "Failed to read algorithm info: %d\n",
2495 ret);
2496 return ret;
2497 }
2498
2499 n_algs = be32_to_cpu(halo_id.n_algs);
2500
2501 wmfw_v3_parse_id_header(dsp, &halo_id.fw, n_algs);
2502
2503 ret = wm_halo_create_regions(dsp, halo_id.fw.id,
Charles Keepaxb75a9792019-05-01 11:13:31 +01002504 halo_id.xm_base, halo_id.ym_base);
Wen Shi170b1e12019-03-19 11:52:13 +00002505 if (ret)
2506 return ret;
2507
2508 /* Calculate offset and length in DSP words */
2509 pos = sizeof(halo_id) / sizeof(u32);
2510 len = (sizeof(*halo_alg) * n_algs) / sizeof(u32);
2511
2512 halo_alg = wm_adsp_read_algs(dsp, n_algs, mem, pos, len);
2513 if (IS_ERR(halo_alg))
2514 return PTR_ERR(halo_alg);
2515
2516 for (i = 0; i < n_algs; i++) {
2517 adsp_info(dsp,
2518 "%d: ID %x v%d.%d.%d XM@%x YM@%x\n",
2519 i, be32_to_cpu(halo_alg[i].alg.id),
2520 (be32_to_cpu(halo_alg[i].alg.ver) & 0xff0000) >> 16,
2521 (be32_to_cpu(halo_alg[i].alg.ver) & 0xff00) >> 8,
2522 be32_to_cpu(halo_alg[i].alg.ver) & 0xff,
2523 be32_to_cpu(halo_alg[i].xm_base),
2524 be32_to_cpu(halo_alg[i].ym_base));
2525
2526 ret = wm_halo_create_regions(dsp, halo_alg[i].alg.id,
2527 halo_alg[i].xm_base,
2528 halo_alg[i].ym_base);
2529 if (ret)
2530 goto out;
2531 }
2532
2533out:
2534 kfree(halo_alg);
2535 return ret;
2536}
2537
Mark Brown2159ad932012-10-11 11:54:02 +09002538static int wm_adsp_load_coeff(struct wm_adsp *dsp)
2539{
Mark Browncf17c832013-01-30 14:37:23 +08002540 LIST_HEAD(buf_list);
Mark Brown2159ad932012-10-11 11:54:02 +09002541 struct regmap *regmap = dsp->regmap;
2542 struct wmfw_coeff_hdr *hdr;
2543 struct wmfw_coeff_item *blk;
2544 const struct firmware *firmware;
Mark Brown471f4882013-01-08 16:09:31 +00002545 const struct wm_adsp_region *mem;
2546 struct wm_adsp_alg_region *alg_region;
Mark Brown2159ad932012-10-11 11:54:02 +09002547 const char *region_name;
2548 int ret, pos, blocks, type, offset, reg;
2549 char *file;
Mark Browncf17c832013-01-30 14:37:23 +08002550 struct wm_adsp_buf *buf;
Mark Brown2159ad932012-10-11 11:54:02 +09002551
2552 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
2553 if (file == NULL)
2554 return -ENOMEM;
2555
Richard Fitzgerald605391d2018-08-08 17:13:39 +01002556 snprintf(file, PAGE_SIZE, "%s-%s-%s.bin", dsp->part, dsp->fwf_name,
Mark Brown1023dbd2013-01-11 22:58:28 +00002557 wm_adsp_fw[dsp->fw].file);
Mark Brown2159ad932012-10-11 11:54:02 +09002558 file[PAGE_SIZE - 1] = '\0';
2559
2560 ret = request_firmware(&firmware, file, dsp->dev);
2561 if (ret != 0) {
2562 adsp_warn(dsp, "Failed to request '%s'\n", file);
2563 ret = 0;
2564 goto out;
2565 }
2566 ret = -EINVAL;
2567
2568 if (sizeof(*hdr) >= firmware->size) {
2569 adsp_err(dsp, "%s: file too short, %zu bytes\n",
2570 file, firmware->size);
2571 goto out_fw;
2572 }
2573
Charles Keepax7585a5b2015-12-08 16:08:25 +00002574 hdr = (void *)&firmware->data[0];
Mark Brown2159ad932012-10-11 11:54:02 +09002575 if (memcmp(hdr->magic, "WMDR", 4) != 0) {
2576 adsp_err(dsp, "%s: invalid magic\n", file);
Charles Keepaxa4cdbec2013-01-21 09:02:31 +00002577 goto out_fw;
Mark Brown2159ad932012-10-11 11:54:02 +09002578 }
2579
Mark Brownc7123262013-01-16 16:59:04 +09002580 switch (be32_to_cpu(hdr->rev) & 0xff) {
2581 case 1:
2582 break;
2583 default:
2584 adsp_err(dsp, "%s: Unsupported coefficient file format %d\n",
2585 file, be32_to_cpu(hdr->rev) & 0xff);
2586 ret = -EINVAL;
2587 goto out_fw;
2588 }
2589
Mark Brown2159ad932012-10-11 11:54:02 +09002590 adsp_dbg(dsp, "%s: v%d.%d.%d\n", file,
2591 (le32_to_cpu(hdr->ver) >> 16) & 0xff,
2592 (le32_to_cpu(hdr->ver) >> 8) & 0xff,
2593 le32_to_cpu(hdr->ver) & 0xff);
2594
2595 pos = le32_to_cpu(hdr->len);
2596
2597 blocks = 0;
2598 while (pos < firmware->size &&
Ben Hutchings50dd2ea2017-12-08 16:15:20 +00002599 sizeof(*blk) < firmware->size - pos) {
Charles Keepax7585a5b2015-12-08 16:08:25 +00002600 blk = (void *)(&firmware->data[pos]);
Mark Brown2159ad932012-10-11 11:54:02 +09002601
Mark Brownc7123262013-01-16 16:59:04 +09002602 type = le16_to_cpu(blk->type);
2603 offset = le16_to_cpu(blk->offset);
Mark Brown2159ad932012-10-11 11:54:02 +09002604
2605 adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n",
2606 file, blocks, le32_to_cpu(blk->id),
2607 (le32_to_cpu(blk->ver) >> 16) & 0xff,
2608 (le32_to_cpu(blk->ver) >> 8) & 0xff,
2609 le32_to_cpu(blk->ver) & 0xff);
2610 adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n",
2611 file, blocks, le32_to_cpu(blk->len), offset, type);
2612
2613 reg = 0;
2614 region_name = "Unknown";
2615 switch (type) {
Mark Brownc7123262013-01-16 16:59:04 +09002616 case (WMFW_NAME_TEXT << 8):
2617 case (WMFW_INFO_TEXT << 8):
Mark Brown2159ad932012-10-11 11:54:02 +09002618 break;
Mark Brownc7123262013-01-16 16:59:04 +09002619 case (WMFW_ABSOLUTE << 8):
Mark Brownf395a212013-03-05 22:39:54 +08002620 /*
2621 * Old files may use this for global
2622 * coefficients.
2623 */
2624 if (le32_to_cpu(blk->id) == dsp->fw_id &&
2625 offset == 0) {
2626 region_name = "global coefficients";
2627 mem = wm_adsp_find_region(dsp, type);
2628 if (!mem) {
2629 adsp_err(dsp, "No ZM\n");
2630 break;
2631 }
Wen Shi170b1e12019-03-19 11:52:13 +00002632 reg = dsp->ops->region_to_reg(mem, 0);
Mark Brownf395a212013-03-05 22:39:54 +08002633
2634 } else {
2635 region_name = "register";
2636 reg = offset;
2637 }
Mark Brown2159ad932012-10-11 11:54:02 +09002638 break;
Mark Brown471f4882013-01-08 16:09:31 +00002639
2640 case WMFW_ADSP1_DM:
2641 case WMFW_ADSP1_ZM:
2642 case WMFW_ADSP2_XM:
2643 case WMFW_ADSP2_YM:
Wen Shi170b1e12019-03-19 11:52:13 +00002644 case WMFW_HALO_XM_PACKED:
2645 case WMFW_HALO_YM_PACKED:
2646 case WMFW_HALO_PM_PACKED:
Mark Brown471f4882013-01-08 16:09:31 +00002647 adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n",
2648 file, blocks, le32_to_cpu(blk->len),
2649 type, le32_to_cpu(blk->id));
2650
2651 mem = wm_adsp_find_region(dsp, type);
2652 if (!mem) {
2653 adsp_err(dsp, "No base for region %x\n", type);
2654 break;
2655 }
2656
Charles Keepax14197092015-12-15 11:29:43 +00002657 alg_region = wm_adsp_find_alg_region(dsp, type,
2658 le32_to_cpu(blk->id));
2659 if (alg_region) {
2660 reg = alg_region->base;
Wen Shi170b1e12019-03-19 11:52:13 +00002661 reg = dsp->ops->region_to_reg(mem, reg);
Charles Keepax14197092015-12-15 11:29:43 +00002662 reg += offset;
2663 } else {
Mark Brown471f4882013-01-08 16:09:31 +00002664 adsp_err(dsp, "No %x for algorithm %x\n",
2665 type, le32_to_cpu(blk->id));
Charles Keepax14197092015-12-15 11:29:43 +00002666 }
Mark Brown471f4882013-01-08 16:09:31 +00002667 break;
2668
Mark Brown2159ad932012-10-11 11:54:02 +09002669 default:
Mark Brown25c62f7e2013-01-20 19:02:19 +09002670 adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n",
2671 file, blocks, type, pos);
Mark Brown2159ad932012-10-11 11:54:02 +09002672 break;
2673 }
2674
2675 if (reg) {
Ben Hutchings50dd2ea2017-12-08 16:15:20 +00002676 if (le32_to_cpu(blk->len) >
2677 firmware->size - pos - sizeof(*blk)) {
Richard Fitzgerald1cab2a82016-12-20 10:29:12 +00002678 adsp_err(dsp,
2679 "%s.%d: %s region len %d bytes exceeds file length %zu\n",
2680 file, blocks, region_name,
2681 le32_to_cpu(blk->len),
2682 firmware->size);
2683 ret = -EINVAL;
2684 goto out_fw;
2685 }
2686
Mark Browncf17c832013-01-30 14:37:23 +08002687 buf = wm_adsp_buf_alloc(blk->data,
2688 le32_to_cpu(blk->len),
2689 &buf_list);
Mark Browna76fefa2013-01-07 19:03:17 +00002690 if (!buf) {
2691 adsp_err(dsp, "Out of memory\n");
Wei Yongjunf4b82812013-03-12 00:23:15 +08002692 ret = -ENOMEM;
2693 goto out_fw;
Mark Browna76fefa2013-01-07 19:03:17 +00002694 }
2695
Mark Brown20da6d52013-01-12 19:58:17 +00002696 adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n",
2697 file, blocks, le32_to_cpu(blk->len),
2698 reg);
Mark Browncf17c832013-01-30 14:37:23 +08002699 ret = regmap_raw_write_async(regmap, reg, buf->buf,
2700 le32_to_cpu(blk->len));
Mark Brown2159ad932012-10-11 11:54:02 +09002701 if (ret != 0) {
2702 adsp_err(dsp,
Dimitris Papastamos43bc3bf2013-11-01 15:56:52 +00002703 "%s.%d: Failed to write to %x in %s: %d\n",
2704 file, blocks, reg, region_name, ret);
Mark Brown2159ad932012-10-11 11:54:02 +09002705 }
2706 }
2707
Charles Keepaxbe951012015-02-16 15:25:49 +00002708 pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03;
Mark Brown2159ad932012-10-11 11:54:02 +09002709 blocks++;
2710 }
2711
Mark Browncf17c832013-01-30 14:37:23 +08002712 ret = regmap_async_complete(regmap);
2713 if (ret != 0)
2714 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
2715
Mark Brown2159ad932012-10-11 11:54:02 +09002716 if (pos > firmware->size)
2717 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
2718 file, blocks, pos - firmware->size);
2719
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +01002720 wm_adsp_debugfs_save_binname(dsp, file);
2721
Mark Brown2159ad932012-10-11 11:54:02 +09002722out_fw:
Charles Keepax9da7a5a2014-11-17 10:48:21 +00002723 regmap_async_complete(regmap);
Mark Brown2159ad932012-10-11 11:54:02 +09002724 release_firmware(firmware);
Mark Browncf17c832013-01-30 14:37:23 +08002725 wm_adsp_buf_free(&buf_list);
Mark Brown2159ad932012-10-11 11:54:02 +09002726out:
2727 kfree(file);
Wei Yongjunf4b82812013-03-12 00:23:15 +08002728 return ret;
Mark Brown2159ad932012-10-11 11:54:02 +09002729}
2730
Richard Fitzgerald605391d2018-08-08 17:13:39 +01002731static int wm_adsp_create_name(struct wm_adsp *dsp)
2732{
2733 char *p;
2734
2735 if (!dsp->name) {
2736 dsp->name = devm_kasprintf(dsp->dev, GFP_KERNEL, "DSP%d",
2737 dsp->num);
2738 if (!dsp->name)
2739 return -ENOMEM;
2740 }
2741
2742 if (!dsp->fwf_name) {
2743 p = devm_kstrdup(dsp->dev, dsp->name, GFP_KERNEL);
2744 if (!p)
2745 return -ENOMEM;
2746
2747 dsp->fwf_name = p;
2748 for (; *p != 0; ++p)
2749 *p = tolower(*p);
2750 }
2751
2752 return 0;
2753}
2754
Richard Fitzgeralddcad34f2018-11-12 13:36:39 +00002755static int wm_adsp_common_init(struct wm_adsp *dsp)
Mark Brown5e7a7a22013-01-16 10:03:56 +09002756{
Richard Fitzgerald605391d2018-08-08 17:13:39 +01002757 int ret;
2758
2759 ret = wm_adsp_create_name(dsp);
2760 if (ret)
2761 return ret;
2762
Charles Keepax3809f002015-04-13 13:27:54 +01002763 INIT_LIST_HEAD(&dsp->alg_regions);
Richard Fitzgeralddcad34f2018-11-12 13:36:39 +00002764 INIT_LIST_HEAD(&dsp->ctl_list);
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00002765 INIT_LIST_HEAD(&dsp->compr_list);
2766 INIT_LIST_HEAD(&dsp->buffer_list);
Mark Brown5e7a7a22013-01-16 10:03:56 +09002767
Charles Keepax078e7182015-12-08 16:08:26 +00002768 mutex_init(&dsp->pwr_lock);
2769
Mark Brown5e7a7a22013-01-16 10:03:56 +09002770 return 0;
2771}
Richard Fitzgeralddcad34f2018-11-12 13:36:39 +00002772
2773int wm_adsp1_init(struct wm_adsp *dsp)
2774{
Charles Keepax4e08d502019-03-19 11:52:12 +00002775 dsp->ops = &wm_adsp1_ops;
2776
Richard Fitzgeralddcad34f2018-11-12 13:36:39 +00002777 return wm_adsp_common_init(dsp);
2778}
Mark Brown5e7a7a22013-01-16 10:03:56 +09002779EXPORT_SYMBOL_GPL(wm_adsp1_init);
2780
Mark Brown2159ad932012-10-11 11:54:02 +09002781int wm_adsp1_event(struct snd_soc_dapm_widget *w,
2782 struct snd_kcontrol *kcontrol,
2783 int event)
2784{
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00002785 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
2786 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
Mark Brown2159ad932012-10-11 11:54:02 +09002787 struct wm_adsp *dsp = &dsps[w->shift];
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01002788 struct wm_coeff_ctl *ctl;
Mark Brown2159ad932012-10-11 11:54:02 +09002789 int ret;
Charles Keepax7585a5b2015-12-08 16:08:25 +00002790 unsigned int val;
Mark Brown2159ad932012-10-11 11:54:02 +09002791
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00002792 dsp->component = component;
Dimitris Papastamos92bb4c32013-08-01 11:11:28 +01002793
Charles Keepax078e7182015-12-08 16:08:26 +00002794 mutex_lock(&dsp->pwr_lock);
2795
Mark Brown2159ad932012-10-11 11:54:02 +09002796 switch (event) {
2797 case SND_SOC_DAPM_POST_PMU:
2798 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2799 ADSP1_SYS_ENA, ADSP1_SYS_ENA);
2800
Chris Rattray94e205b2013-01-18 08:43:09 +00002801 /*
2802 * For simplicity set the DSP clock rate to be the
2803 * SYSCLK rate rather than making it configurable.
2804 */
Charles Keepax7585a5b2015-12-08 16:08:25 +00002805 if (dsp->sysclk_reg) {
Chris Rattray94e205b2013-01-18 08:43:09 +00002806 ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val);
2807 if (ret != 0) {
2808 adsp_err(dsp, "Failed to read SYSCLK state: %d\n",
2809 ret);
Charles Keepax078e7182015-12-08 16:08:26 +00002810 goto err_mutex;
Chris Rattray94e205b2013-01-18 08:43:09 +00002811 }
2812
Charles Keepax7d00cd92016-02-19 14:44:43 +00002813 val = (val & dsp->sysclk_mask) >> dsp->sysclk_shift;
Chris Rattray94e205b2013-01-18 08:43:09 +00002814
2815 ret = regmap_update_bits(dsp->regmap,
2816 dsp->base + ADSP1_CONTROL_31,
2817 ADSP1_CLK_SEL_MASK, val);
2818 if (ret != 0) {
2819 adsp_err(dsp, "Failed to set clock rate: %d\n",
2820 ret);
Charles Keepax078e7182015-12-08 16:08:26 +00002821 goto err_mutex;
Chris Rattray94e205b2013-01-18 08:43:09 +00002822 }
2823 }
2824
Mark Brown2159ad932012-10-11 11:54:02 +09002825 ret = wm_adsp_load(dsp);
2826 if (ret != 0)
Charles Keepax078e7182015-12-08 16:08:26 +00002827 goto err_ena;
Mark Brown2159ad932012-10-11 11:54:02 +09002828
Charles Keepaxb618a1852015-04-13 13:27:53 +01002829 ret = wm_adsp1_setup_algs(dsp);
Mark Browndb405172012-10-26 19:30:40 +01002830 if (ret != 0)
Charles Keepax078e7182015-12-08 16:08:26 +00002831 goto err_ena;
Mark Browndb405172012-10-26 19:30:40 +01002832
Mark Brown2159ad932012-10-11 11:54:02 +09002833 ret = wm_adsp_load_coeff(dsp);
2834 if (ret != 0)
Charles Keepax078e7182015-12-08 16:08:26 +00002835 goto err_ena;
Mark Brown2159ad932012-10-11 11:54:02 +09002836
Dimitris Papastamos0c2e3f32013-05-28 12:01:50 +01002837 /* Initialize caches for enabled and unset controls */
Dimitris Papastamos81ad93e2013-07-29 13:51:59 +01002838 ret = wm_coeff_init_control_caches(dsp);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01002839 if (ret != 0)
Charles Keepax078e7182015-12-08 16:08:26 +00002840 goto err_ena;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01002841
Dimitris Papastamos0c2e3f32013-05-28 12:01:50 +01002842 /* Sync set controls */
Dimitris Papastamos81ad93e2013-07-29 13:51:59 +01002843 ret = wm_coeff_sync_controls(dsp);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01002844 if (ret != 0)
Charles Keepax078e7182015-12-08 16:08:26 +00002845 goto err_ena;
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01002846
Charles Keepax28823eb2016-09-20 13:52:32 +01002847 dsp->booted = true;
2848
Mark Brown2159ad932012-10-11 11:54:02 +09002849 /* Start the core running */
2850 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2851 ADSP1_CORE_ENA | ADSP1_START,
2852 ADSP1_CORE_ENA | ADSP1_START);
Charles Keepax28823eb2016-09-20 13:52:32 +01002853
2854 dsp->running = true;
Mark Brown2159ad932012-10-11 11:54:02 +09002855 break;
2856
2857 case SND_SOC_DAPM_PRE_PMD:
Charles Keepax28823eb2016-09-20 13:52:32 +01002858 dsp->running = false;
2859 dsp->booted = false;
2860
Mark Brown2159ad932012-10-11 11:54:02 +09002861 /* Halt the core */
2862 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2863 ADSP1_CORE_ENA | ADSP1_START, 0);
2864
2865 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19,
2866 ADSP1_WDMA_BUFFER_LENGTH_MASK, 0);
2867
2868 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2869 ADSP1_SYS_ENA, 0);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01002870
Dimitris Papastamos81ad93e2013-07-29 13:51:59 +01002871 list_for_each_entry(ctl, &dsp->ctl_list, list)
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01002872 ctl->enabled = 0;
Dimitris Papastamosb0101b42013-11-01 15:56:56 +00002873
Richard Fitzgerald56574d52016-04-27 14:58:29 +01002874
2875 wm_adsp_free_alg_regions(dsp);
Mark Brown2159ad932012-10-11 11:54:02 +09002876 break;
2877
2878 default:
2879 break;
2880 }
2881
Charles Keepax078e7182015-12-08 16:08:26 +00002882 mutex_unlock(&dsp->pwr_lock);
2883
Mark Brown2159ad932012-10-11 11:54:02 +09002884 return 0;
2885
Charles Keepax078e7182015-12-08 16:08:26 +00002886err_ena:
Mark Brown2159ad932012-10-11 11:54:02 +09002887 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2888 ADSP1_SYS_ENA, 0);
Charles Keepax078e7182015-12-08 16:08:26 +00002889err_mutex:
2890 mutex_unlock(&dsp->pwr_lock);
2891
Mark Brown2159ad932012-10-11 11:54:02 +09002892 return ret;
2893}
2894EXPORT_SYMBOL_GPL(wm_adsp1_event);
2895
Charles Keepax4e08d502019-03-19 11:52:12 +00002896static int wm_adsp2v2_enable_core(struct wm_adsp *dsp)
Mark Brown2159ad932012-10-11 11:54:02 +09002897{
2898 unsigned int val;
2899 int ret, count;
2900
Mark Brown2159ad932012-10-11 11:54:02 +09002901 /* Wait for the RAM to start, should be near instantaneous */
Charles Keepax939fd1e2013-12-18 09:25:49 +00002902 for (count = 0; count < 10; ++count) {
Charles Keepax7d00cd92016-02-19 14:44:43 +00002903 ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1, &val);
Mark Brown2159ad932012-10-11 11:54:02 +09002904 if (ret != 0)
2905 return ret;
Charles Keepax939fd1e2013-12-18 09:25:49 +00002906
2907 if (val & ADSP2_RAM_RDY)
2908 break;
2909
Charles Keepax1fa96f32016-09-26 10:15:22 +01002910 usleep_range(250, 500);
Charles Keepax939fd1e2013-12-18 09:25:49 +00002911 }
Mark Brown2159ad932012-10-11 11:54:02 +09002912
2913 if (!(val & ADSP2_RAM_RDY)) {
2914 adsp_err(dsp, "Failed to start DSP RAM\n");
2915 return -EBUSY;
2916 }
2917
2918 adsp_dbg(dsp, "RAM ready after %d polls\n", count);
Mark Brown2159ad932012-10-11 11:54:02 +09002919
2920 return 0;
2921}
2922
Charles Keepax4e08d502019-03-19 11:52:12 +00002923static int wm_adsp2_enable_core(struct wm_adsp *dsp)
2924{
2925 int ret;
2926
2927 ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL,
2928 ADSP2_SYS_ENA, ADSP2_SYS_ENA);
2929 if (ret != 0)
2930 return ret;
2931
2932 return wm_adsp2v2_enable_core(dsp);
2933}
2934
Charles Keepax2b0ee492019-03-19 11:52:08 +00002935static int wm_adsp2_lock(struct wm_adsp *dsp, unsigned int lock_regions)
2936{
2937 struct regmap *regmap = dsp->regmap;
2938 unsigned int code0, code1, lock_reg;
2939
2940 if (!(lock_regions & WM_ADSP2_REGION_ALL))
2941 return 0;
2942
2943 lock_regions &= WM_ADSP2_REGION_ALL;
2944 lock_reg = dsp->base + ADSP2_LOCK_REGION_1_LOCK_REGION_0;
2945
2946 while (lock_regions) {
2947 code0 = code1 = 0;
2948 if (lock_regions & BIT(0)) {
2949 code0 = ADSP2_LOCK_CODE_0;
2950 code1 = ADSP2_LOCK_CODE_1;
2951 }
2952 if (lock_regions & BIT(1)) {
2953 code0 |= ADSP2_LOCK_CODE_0 << ADSP2_LOCK_REGION_SHIFT;
2954 code1 |= ADSP2_LOCK_CODE_1 << ADSP2_LOCK_REGION_SHIFT;
2955 }
2956 regmap_write(regmap, lock_reg, code0);
2957 regmap_write(regmap, lock_reg, code1);
2958 lock_regions >>= 2;
2959 lock_reg += 2;
2960 }
2961
2962 return 0;
2963}
2964
Charles Keepax4e08d502019-03-19 11:52:12 +00002965static int wm_adsp2_enable_memory(struct wm_adsp *dsp)
2966{
2967 return regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2968 ADSP2_MEM_ENA, ADSP2_MEM_ENA);
2969}
2970
2971static void wm_adsp2_disable_memory(struct wm_adsp *dsp)
2972{
2973 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2974 ADSP2_MEM_ENA, 0);
2975}
2976
2977static void wm_adsp2_disable_core(struct wm_adsp *dsp)
2978{
2979 regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
2980 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
2981 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0);
2982
2983 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2984 ADSP2_SYS_ENA, 0);
2985}
2986
2987static void wm_adsp2v2_disable_core(struct wm_adsp *dsp)
2988{
2989 regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
2990 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
2991 regmap_write(dsp->regmap, dsp->base + ADSP2V2_WDMA_CONFIG_2, 0);
2992}
2993
2994static void wm_adsp_boot_work(struct work_struct *work)
Charles Keepaxd8a64d62014-01-08 17:42:18 +00002995{
2996 struct wm_adsp *dsp = container_of(work,
2997 struct wm_adsp,
2998 boot_work);
2999 int ret;
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003000
Charles Keepax078e7182015-12-08 16:08:26 +00003001 mutex_lock(&dsp->pwr_lock);
3002
Charles Keepax4e08d502019-03-19 11:52:12 +00003003 if (dsp->ops->enable_memory) {
3004 ret = dsp->ops->enable_memory(dsp);
3005 if (ret != 0)
3006 goto err_mutex;
3007 }
Charles Keepax90d19ba2016-09-26 10:15:23 +01003008
Charles Keepax4e08d502019-03-19 11:52:12 +00003009 if (dsp->ops->enable_core) {
3010 ret = dsp->ops->enable_core(dsp);
3011 if (ret != 0)
3012 goto err_mem;
3013 }
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003014
3015 ret = wm_adsp_load(dsp);
3016 if (ret != 0)
Charles Keepax078e7182015-12-08 16:08:26 +00003017 goto err_ena;
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003018
Charles Keepax4e08d502019-03-19 11:52:12 +00003019 ret = dsp->ops->setup_algs(dsp);
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003020 if (ret != 0)
Charles Keepax078e7182015-12-08 16:08:26 +00003021 goto err_ena;
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003022
3023 ret = wm_adsp_load_coeff(dsp);
3024 if (ret != 0)
Charles Keepax078e7182015-12-08 16:08:26 +00003025 goto err_ena;
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003026
3027 /* Initialize caches for enabled and unset controls */
3028 ret = wm_coeff_init_control_caches(dsp);
3029 if (ret != 0)
Charles Keepax078e7182015-12-08 16:08:26 +00003030 goto err_ena;
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003031
Charles Keepax4e08d502019-03-19 11:52:12 +00003032 if (dsp->ops->disable_core)
3033 dsp->ops->disable_core(dsp);
Charles Keepax90d19ba2016-09-26 10:15:23 +01003034
Charles Keepaxe7799742017-01-24 11:44:00 +00003035 dsp->booted = true;
3036
Charles Keepax078e7182015-12-08 16:08:26 +00003037 mutex_unlock(&dsp->pwr_lock);
3038
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003039 return;
3040
Charles Keepax078e7182015-12-08 16:08:26 +00003041err_ena:
Charles Keepax4e08d502019-03-19 11:52:12 +00003042 if (dsp->ops->disable_core)
3043 dsp->ops->disable_core(dsp);
Charles Keepaxd589d8b2017-01-24 11:44:01 +00003044err_mem:
Charles Keepax4e08d502019-03-19 11:52:12 +00003045 if (dsp->ops->disable_memory)
3046 dsp->ops->disable_memory(dsp);
Charles Keepax078e7182015-12-08 16:08:26 +00003047err_mutex:
3048 mutex_unlock(&dsp->pwr_lock);
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003049}
3050
Wen Shi170b1e12019-03-19 11:52:13 +00003051static int wm_halo_configure_mpu(struct wm_adsp *dsp, unsigned int lock_regions)
3052{
3053 struct reg_sequence config[] = {
3054 { dsp->base + HALO_MPU_LOCK_CONFIG, 0x5555 },
3055 { dsp->base + HALO_MPU_LOCK_CONFIG, 0xAAAA },
3056 { dsp->base + HALO_MPU_XMEM_ACCESS_0, 0xFFFFFFFF },
3057 { dsp->base + HALO_MPU_YMEM_ACCESS_0, 0xFFFFFFFF },
3058 { dsp->base + HALO_MPU_WINDOW_ACCESS_0, lock_regions },
3059 { dsp->base + HALO_MPU_XREG_ACCESS_0, lock_regions },
3060 { dsp->base + HALO_MPU_YREG_ACCESS_0, lock_regions },
3061 { dsp->base + HALO_MPU_XMEM_ACCESS_1, 0xFFFFFFFF },
3062 { dsp->base + HALO_MPU_YMEM_ACCESS_1, 0xFFFFFFFF },
3063 { dsp->base + HALO_MPU_WINDOW_ACCESS_1, lock_regions },
3064 { dsp->base + HALO_MPU_XREG_ACCESS_1, lock_regions },
3065 { dsp->base + HALO_MPU_YREG_ACCESS_1, lock_regions },
3066 { dsp->base + HALO_MPU_XMEM_ACCESS_2, 0xFFFFFFFF },
3067 { dsp->base + HALO_MPU_YMEM_ACCESS_2, 0xFFFFFFFF },
3068 { dsp->base + HALO_MPU_WINDOW_ACCESS_2, lock_regions },
3069 { dsp->base + HALO_MPU_XREG_ACCESS_2, lock_regions },
3070 { dsp->base + HALO_MPU_YREG_ACCESS_2, lock_regions },
3071 { dsp->base + HALO_MPU_XMEM_ACCESS_3, 0xFFFFFFFF },
3072 { dsp->base + HALO_MPU_YMEM_ACCESS_3, 0xFFFFFFFF },
3073 { dsp->base + HALO_MPU_WINDOW_ACCESS_3, lock_regions },
3074 { dsp->base + HALO_MPU_XREG_ACCESS_3, lock_regions },
3075 { dsp->base + HALO_MPU_YREG_ACCESS_3, lock_regions },
3076 { dsp->base + HALO_MPU_LOCK_CONFIG, 0 },
3077 };
3078
3079 return regmap_multi_reg_write(dsp->regmap, config, ARRAY_SIZE(config));
3080}
3081
Richard Fitzgeraldb9070df2019-03-19 11:52:09 +00003082int wm_adsp2_set_dspclk(struct snd_soc_dapm_widget *w, unsigned int freq)
Charles Keepaxd82d7672016-01-21 17:53:02 +00003083{
Richard Fitzgeraldb9070df2019-03-19 11:52:09 +00003084 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
3085 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
3086 struct wm_adsp *dsp = &dsps[w->shift];
Charles Keepaxd82d7672016-01-21 17:53:02 +00003087 int ret;
3088
Richard Fitzgeraldb9070df2019-03-19 11:52:09 +00003089 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CLOCKING,
3090 ADSP2_CLK_SEL_MASK,
3091 freq << ADSP2_CLK_SEL_SHIFT);
3092 if (ret)
3093 adsp_err(dsp, "Failed to set clock rate: %d\n", ret);
3094
3095 return ret;
Charles Keepaxd82d7672016-01-21 17:53:02 +00003096}
Richard Fitzgeraldb9070df2019-03-19 11:52:09 +00003097EXPORT_SYMBOL_GPL(wm_adsp2_set_dspclk);
Charles Keepaxd82d7672016-01-21 17:53:02 +00003098
Charles Keepaxaf813a62017-01-06 14:24:41 +00003099int wm_adsp2_preloader_get(struct snd_kcontrol *kcontrol,
3100 struct snd_ctl_elem_value *ucontrol)
3101{
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003102 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
Ajit Pandeyb1470d42018-08-07 18:30:42 +01003103 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
3104 struct soc_mixer_control *mc =
3105 (struct soc_mixer_control *)kcontrol->private_value;
3106 struct wm_adsp *dsp = &dsps[mc->shift - 1];
Charles Keepaxaf813a62017-01-06 14:24:41 +00003107
3108 ucontrol->value.integer.value[0] = dsp->preloaded;
3109
3110 return 0;
3111}
3112EXPORT_SYMBOL_GPL(wm_adsp2_preloader_get);
3113
3114int wm_adsp2_preloader_put(struct snd_kcontrol *kcontrol,
3115 struct snd_ctl_elem_value *ucontrol)
3116{
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003117 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
Ajit Pandeyb1470d42018-08-07 18:30:42 +01003118 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003119 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Charles Keepaxaf813a62017-01-06 14:24:41 +00003120 struct soc_mixer_control *mc =
3121 (struct soc_mixer_control *)kcontrol->private_value;
Ajit Pandeyb1470d42018-08-07 18:30:42 +01003122 struct wm_adsp *dsp = &dsps[mc->shift - 1];
Charles Keepaxaf813a62017-01-06 14:24:41 +00003123 char preload[32];
3124
Richard Fitzgerald605391d2018-08-08 17:13:39 +01003125 snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->name);
Charles Keepaxaf813a62017-01-06 14:24:41 +00003126
3127 dsp->preloaded = ucontrol->value.integer.value[0];
3128
3129 if (ucontrol->value.integer.value[0])
Charles Keepax95a594d2018-04-24 16:53:09 +01003130 snd_soc_component_force_enable_pin(component, preload);
Charles Keepaxaf813a62017-01-06 14:24:41 +00003131 else
Charles Keepax95a594d2018-04-24 16:53:09 +01003132 snd_soc_component_disable_pin(component, preload);
Charles Keepaxaf813a62017-01-06 14:24:41 +00003133
3134 snd_soc_dapm_sync(dapm);
3135
Stuart Henderson868e49a2018-07-19 11:50:37 +01003136 flush_work(&dsp->boot_work);
3137
Charles Keepaxaf813a62017-01-06 14:24:41 +00003138 return 0;
3139}
3140EXPORT_SYMBOL_GPL(wm_adsp2_preloader_put);
3141
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01003142static void wm_adsp_stop_watchdog(struct wm_adsp *dsp)
3143{
Charles Keepax4e08d502019-03-19 11:52:12 +00003144 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_WATCHDOG,
3145 ADSP2_WDT_ENA_MASK, 0);
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01003146}
3147
Stuart Henderson8bc144f2019-03-19 11:52:15 +00003148static void wm_halo_stop_watchdog(struct wm_adsp *dsp)
3149{
3150 regmap_update_bits(dsp->regmap, dsp->base + HALO_WDT_CONTROL,
3151 HALO_WDT_EN_MASK, 0);
3152}
3153
Charles Keepax4e08d502019-03-19 11:52:12 +00003154int wm_adsp_early_event(struct snd_soc_dapm_widget *w,
3155 struct snd_kcontrol *kcontrol, int event)
Charles Keepax12db5ed2014-01-08 17:42:19 +00003156{
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003157 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
3158 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
Charles Keepax12db5ed2014-01-08 17:42:19 +00003159 struct wm_adsp *dsp = &dsps[w->shift];
Charles Keepax57a60cc2016-09-26 10:15:24 +01003160 struct wm_coeff_ctl *ctl;
Charles Keepax12db5ed2014-01-08 17:42:19 +00003161
Charles Keepax12db5ed2014-01-08 17:42:19 +00003162 switch (event) {
3163 case SND_SOC_DAPM_PRE_PMU:
3164 queue_work(system_unbound_wq, &dsp->boot_work);
3165 break;
Charles Keepax57a60cc2016-09-26 10:15:24 +01003166 case SND_SOC_DAPM_PRE_PMD:
Charles Keepaxbb24ee42017-01-24 11:43:59 +00003167 mutex_lock(&dsp->pwr_lock);
3168
Charles Keepax57a60cc2016-09-26 10:15:24 +01003169 wm_adsp_debugfs_clear(dsp);
3170
3171 dsp->fw_id = 0;
3172 dsp->fw_id_version = 0;
3173
3174 dsp->booted = false;
3175
Charles Keepax4e08d502019-03-19 11:52:12 +00003176 if (dsp->ops->disable_memory)
3177 dsp->ops->disable_memory(dsp);
Charles Keepax57a60cc2016-09-26 10:15:24 +01003178
3179 list_for_each_entry(ctl, &dsp->ctl_list, list)
3180 ctl->enabled = 0;
3181
3182 wm_adsp_free_alg_regions(dsp);
3183
Charles Keepaxbb24ee42017-01-24 11:43:59 +00003184 mutex_unlock(&dsp->pwr_lock);
3185
Charles Keepax57a60cc2016-09-26 10:15:24 +01003186 adsp_dbg(dsp, "Shutdown complete\n");
3187 break;
Charles Keepax12db5ed2014-01-08 17:42:19 +00003188 default:
3189 break;
Charles Keepaxcab272582014-04-17 13:42:54 +01003190 }
Charles Keepax12db5ed2014-01-08 17:42:19 +00003191
3192 return 0;
3193}
Charles Keepax4e08d502019-03-19 11:52:12 +00003194EXPORT_SYMBOL_GPL(wm_adsp_early_event);
Charles Keepax12db5ed2014-01-08 17:42:19 +00003195
Charles Keepax4e08d502019-03-19 11:52:12 +00003196static int wm_adsp2_start_core(struct wm_adsp *dsp)
3197{
3198 return regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
3199 ADSP2_CORE_ENA | ADSP2_START,
3200 ADSP2_CORE_ENA | ADSP2_START);
3201}
3202
3203static void wm_adsp2_stop_core(struct wm_adsp *dsp)
3204{
3205 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
3206 ADSP2_CORE_ENA | ADSP2_START, 0);
3207}
3208
3209int wm_adsp_event(struct snd_soc_dapm_widget *w,
3210 struct snd_kcontrol *kcontrol, int event)
Mark Brown2159ad932012-10-11 11:54:02 +09003211{
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003212 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
3213 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
Mark Brown2159ad932012-10-11 11:54:02 +09003214 struct wm_adsp *dsp = &dsps[w->shift];
3215 int ret;
3216
3217 switch (event) {
3218 case SND_SOC_DAPM_POST_PMU:
Charles Keepaxd8a64d62014-01-08 17:42:18 +00003219 flush_work(&dsp->boot_work);
Mark Browndd49e2c2012-12-02 21:50:46 +09003220
Charles Keepaxbb24ee42017-01-24 11:43:59 +00003221 mutex_lock(&dsp->pwr_lock);
3222
3223 if (!dsp->booted) {
3224 ret = -EIO;
3225 goto err;
3226 }
Mark Browndd49e2c2012-12-02 21:50:46 +09003227
Charles Keepax4e08d502019-03-19 11:52:12 +00003228 if (dsp->ops->enable_core) {
3229 ret = dsp->ops->enable_core(dsp);
3230 if (ret != 0)
3231 goto err;
3232 }
Charles Keepax90d19ba2016-09-26 10:15:23 +01003233
Charles Keepaxcef45772016-09-20 13:52:33 +01003234 /* Sync set controls */
3235 ret = wm_coeff_sync_controls(dsp);
3236 if (ret != 0)
3237 goto err;
3238
Charles Keepax4e08d502019-03-19 11:52:12 +00003239 if (dsp->ops->lock_memory) {
3240 ret = dsp->ops->lock_memory(dsp, dsp->lock_regions);
3241 if (ret != 0) {
3242 adsp_err(dsp, "Error configuring MPU: %d\n",
3243 ret);
3244 goto err;
3245 }
3246 }
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01003247
Charles Keepax4e08d502019-03-19 11:52:12 +00003248 if (dsp->ops->start_core) {
3249 ret = dsp->ops->start_core(dsp);
3250 if (ret != 0)
3251 goto err;
3252 }
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003253
Charles Keepax48c2c992016-11-22 15:38:34 +00003254 if (wm_adsp_fw[dsp->fw].num_caps != 0) {
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003255 ret = wm_adsp_buffer_init(dsp);
Charles Keepaxbb24ee42017-01-24 11:43:59 +00003256 if (ret < 0)
Charles Keepax48c2c992016-11-22 15:38:34 +00003257 goto err;
Charles Keepax48c2c992016-11-22 15:38:34 +00003258 }
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003259
Charles Keepaxe7799742017-01-24 11:44:00 +00003260 dsp->running = true;
3261
Charles Keepax612047f2016-03-28 14:29:22 +01003262 mutex_unlock(&dsp->pwr_lock);
Mark Brown2159ad932012-10-11 11:54:02 +09003263 break;
3264
3265 case SND_SOC_DAPM_PRE_PMD:
Richard Fitzgeraldf4f0c4c2016-11-09 17:14:17 +00003266 /* Tell the firmware to cleanup */
3267 wm_adsp_signal_event_controls(dsp, WM_ADSP_FW_EVENT_SHUTDOWN);
3268
Charles Keepax4e08d502019-03-19 11:52:12 +00003269 if (dsp->ops->stop_watchdog)
3270 dsp->ops->stop_watchdog(dsp);
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01003271
Richard Fitzgerald10337b02015-05-29 10:23:07 +01003272 /* Log firmware state, it can be useful for analysis */
Charles Keepax4e08d502019-03-19 11:52:12 +00003273 if (dsp->ops->show_fw_status)
3274 dsp->ops->show_fw_status(dsp);
Richard Fitzgerald10337b02015-05-29 10:23:07 +01003275
Charles Keepax078e7182015-12-08 16:08:26 +00003276 mutex_lock(&dsp->pwr_lock);
3277
Mark Brown1023dbd2013-01-11 22:58:28 +00003278 dsp->running = false;
3279
Charles Keepax4e08d502019-03-19 11:52:12 +00003280 if (dsp->ops->stop_core)
3281 dsp->ops->stop_core(dsp);
3282 if (dsp->ops->disable_core)
3283 dsp->ops->disable_core(dsp);
Mark Brown2d30b572013-01-28 20:18:17 +08003284
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003285 if (wm_adsp_fw[dsp->fw].num_caps != 0)
3286 wm_adsp_buffer_free(dsp);
3287
Charles Keepaxa2bcbc12019-03-19 11:52:07 +00003288 dsp->fatal_error = false;
3289
Charles Keepax078e7182015-12-08 16:08:26 +00003290 mutex_unlock(&dsp->pwr_lock);
3291
Charles Keepax57a60cc2016-09-26 10:15:24 +01003292 adsp_dbg(dsp, "Execution stopped\n");
Mark Brown2159ad932012-10-11 11:54:02 +09003293 break;
3294
3295 default:
3296 break;
3297 }
3298
3299 return 0;
3300err:
Charles Keepax4e08d502019-03-19 11:52:12 +00003301 if (dsp->ops->stop_core)
3302 dsp->ops->stop_core(dsp);
3303 if (dsp->ops->disable_core)
3304 dsp->ops->disable_core(dsp);
Charles Keepaxbb24ee42017-01-24 11:43:59 +00003305 mutex_unlock(&dsp->pwr_lock);
Mark Brown2159ad932012-10-11 11:54:02 +09003306 return ret;
3307}
Charles Keepax4e08d502019-03-19 11:52:12 +00003308EXPORT_SYMBOL_GPL(wm_adsp_event);
Mark Brown973838a2012-11-28 17:20:32 +00003309
Wen Shi170b1e12019-03-19 11:52:13 +00003310static int wm_halo_start_core(struct wm_adsp *dsp)
3311{
3312 return regmap_update_bits(dsp->regmap,
3313 dsp->base + HALO_CCM_CORE_CONTROL,
3314 HALO_CORE_EN, HALO_CORE_EN);
3315}
3316
3317static void wm_halo_stop_core(struct wm_adsp *dsp)
3318{
3319 regmap_update_bits(dsp->regmap, dsp->base + HALO_CCM_CORE_CONTROL,
3320 HALO_CORE_EN, 0);
3321
Charles Keepax809589a2019-03-27 15:24:51 +00003322 /* reset halo core with CORE_SOFT_RESET */
Wen Shi170b1e12019-03-19 11:52:13 +00003323 regmap_update_bits(dsp->regmap, dsp->base + HALO_CORE_SOFT_RESET,
3324 HALO_CORE_SOFT_RESET_MASK, 1);
3325}
3326
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003327int wm_adsp2_component_probe(struct wm_adsp *dsp, struct snd_soc_component *component)
Richard Fitzgeraldf5e2ce92015-06-11 11:32:30 +01003328{
Charles Keepaxaf813a62017-01-06 14:24:41 +00003329 char preload[32];
3330
Richard Fitzgerald605391d2018-08-08 17:13:39 +01003331 snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->name);
Charles Keepax95a594d2018-04-24 16:53:09 +01003332 snd_soc_component_disable_pin(component, preload);
Richard Fitzgerald685f51a2016-11-22 16:58:57 +00003333
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003334 wm_adsp2_init_debugfs(dsp, component);
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +01003335
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003336 dsp->component = component;
Charles Keepaxaf813a62017-01-06 14:24:41 +00003337
Richard Fitzgerald0a047f02018-08-08 17:13:38 +01003338 return 0;
Richard Fitzgeraldf5e2ce92015-06-11 11:32:30 +01003339}
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003340EXPORT_SYMBOL_GPL(wm_adsp2_component_probe);
Richard Fitzgeraldf5e2ce92015-06-11 11:32:30 +01003341
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003342int wm_adsp2_component_remove(struct wm_adsp *dsp, struct snd_soc_component *component)
Richard Fitzgeraldf5e2ce92015-06-11 11:32:30 +01003343{
Richard Fitzgeraldf9f55e32015-06-11 11:32:32 +01003344 wm_adsp2_cleanup_debugfs(dsp);
3345
Richard Fitzgeraldf5e2ce92015-06-11 11:32:30 +01003346 return 0;
3347}
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +00003348EXPORT_SYMBOL_GPL(wm_adsp2_component_remove);
Richard Fitzgeraldf5e2ce92015-06-11 11:32:30 +01003349
Richard Fitzgerald81ac58b2015-06-02 11:53:34 +01003350int wm_adsp2_init(struct wm_adsp *dsp)
Mark Brown973838a2012-11-28 17:20:32 +00003351{
3352 int ret;
3353
Richard Fitzgeralddcad34f2018-11-12 13:36:39 +00003354 ret = wm_adsp_common_init(dsp);
Richard Fitzgerald605391d2018-08-08 17:13:39 +01003355 if (ret)
3356 return ret;
3357
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +01003358 switch (dsp->rev) {
3359 case 0:
3360 /*
3361 * Disable the DSP memory by default when in reset for a small
3362 * power saving.
3363 */
3364 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
3365 ADSP2_MEM_ENA, 0);
3366 if (ret) {
3367 adsp_err(dsp,
3368 "Failed to clear memory retention: %d\n", ret);
3369 return ret;
3370 }
Charles Keepax4e08d502019-03-19 11:52:12 +00003371
3372 dsp->ops = &wm_adsp2_ops[0];
3373 break;
3374 case 1:
3375 dsp->ops = &wm_adsp2_ops[1];
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +01003376 break;
3377 default:
Charles Keepax4e08d502019-03-19 11:52:12 +00003378 dsp->ops = &wm_adsp2_ops[2];
Richard Fitzgeralde1ea1872017-04-05 11:07:59 +01003379 break;
Mark Brown10a2b662012-12-02 21:37:00 +09003380 }
3381
Charles Keepax4e08d502019-03-19 11:52:12 +00003382 INIT_WORK(&dsp->boot_work, wm_adsp_boot_work);
Dimitris Papastamos6ab2b7b2013-05-08 14:15:35 +01003383
Mark Brown973838a2012-11-28 17:20:32 +00003384 return 0;
3385}
3386EXPORT_SYMBOL_GPL(wm_adsp2_init);
Praveen Diwakar0a37c6ef2014-07-04 11:17:41 +05303387
Wen Shi170b1e12019-03-19 11:52:13 +00003388int wm_halo_init(struct wm_adsp *dsp)
3389{
3390 int ret;
3391
3392 ret = wm_adsp_common_init(dsp);
3393 if (ret)
3394 return ret;
3395
3396 dsp->ops = &wm_halo_ops;
3397
3398 INIT_WORK(&dsp->boot_work, wm_adsp_boot_work);
3399
3400 return 0;
3401}
3402EXPORT_SYMBOL_GPL(wm_halo_init);
3403
Richard Fitzgerald66225e92016-04-27 14:58:27 +01003404void wm_adsp2_remove(struct wm_adsp *dsp)
3405{
3406 struct wm_coeff_ctl *ctl;
3407
3408 while (!list_empty(&dsp->ctl_list)) {
3409 ctl = list_first_entry(&dsp->ctl_list, struct wm_coeff_ctl,
3410 list);
3411 list_del(&ctl->list);
3412 wm_adsp_free_ctl_blk(ctl);
3413 }
3414}
3415EXPORT_SYMBOL_GPL(wm_adsp2_remove);
3416
Charles Keepaxedd71352016-05-04 17:11:55 +01003417static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr)
3418{
3419 return compr->buf != NULL;
3420}
3421
3422static int wm_adsp_compr_attach(struct wm_adsp_compr *compr)
3423{
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003424 struct wm_adsp_compr_buf *buf = NULL, *tmp;
3425
Charles Keepaxa2bcbc12019-03-19 11:52:07 +00003426 if (compr->dsp->fatal_error)
3427 return -EINVAL;
3428
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003429 list_for_each_entry(tmp, &compr->dsp->buffer_list, list) {
3430 if (!tmp->name || !strcmp(compr->name, tmp->name)) {
3431 buf = tmp;
3432 break;
3433 }
3434 }
3435
3436 if (!buf)
Charles Keepaxedd71352016-05-04 17:11:55 +01003437 return -EINVAL;
3438
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003439 compr->buf = buf;
Charles Keepax789b9302019-04-02 13:49:16 +01003440 buf->compr = compr;
Charles Keepaxedd71352016-05-04 17:11:55 +01003441
3442 return 0;
3443}
3444
Charles Keepax721be3b2016-05-04 17:11:56 +01003445static void wm_adsp_compr_detach(struct wm_adsp_compr *compr)
3446{
3447 if (!compr)
3448 return;
3449
3450 /* Wake the poll so it can see buffer is no longer attached */
3451 if (compr->stream)
3452 snd_compr_fragment_elapsed(compr->stream);
3453
3454 if (wm_adsp_compr_attached(compr)) {
3455 compr->buf->compr = NULL;
3456 compr->buf = NULL;
3457 }
3458}
3459
Charles Keepax406abc92015-12-15 11:29:45 +00003460int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream)
3461{
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003462 struct wm_adsp_compr *compr, *tmp;
3463 struct snd_soc_pcm_runtime *rtd = stream->private_data;
Charles Keepax406abc92015-12-15 11:29:45 +00003464 int ret = 0;
3465
3466 mutex_lock(&dsp->pwr_lock);
3467
3468 if (wm_adsp_fw[dsp->fw].num_caps == 0) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00003469 adsp_err(dsp, "%s: Firmware does not support compressed API\n",
3470 rtd->codec_dai->name);
Charles Keepax406abc92015-12-15 11:29:45 +00003471 ret = -ENXIO;
3472 goto out;
3473 }
3474
3475 if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00003476 adsp_err(dsp, "%s: Firmware does not support stream direction\n",
3477 rtd->codec_dai->name);
Charles Keepax406abc92015-12-15 11:29:45 +00003478 ret = -EINVAL;
3479 goto out;
3480 }
3481
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003482 list_for_each_entry(tmp, &dsp->compr_list, list) {
3483 if (!strcmp(tmp->name, rtd->codec_dai->name)) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00003484 adsp_err(dsp, "%s: Only a single stream supported per dai\n",
3485 rtd->codec_dai->name);
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003486 ret = -EBUSY;
3487 goto out;
3488 }
Charles Keepax95fe9592015-12-15 11:29:47 +00003489 }
3490
Charles Keepax406abc92015-12-15 11:29:45 +00003491 compr = kzalloc(sizeof(*compr), GFP_KERNEL);
3492 if (!compr) {
3493 ret = -ENOMEM;
3494 goto out;
3495 }
3496
3497 compr->dsp = dsp;
3498 compr->stream = stream;
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003499 compr->name = rtd->codec_dai->name;
Charles Keepax406abc92015-12-15 11:29:45 +00003500
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003501 list_add_tail(&compr->list, &dsp->compr_list);
Charles Keepax406abc92015-12-15 11:29:45 +00003502
3503 stream->runtime->private_data = compr;
3504
3505out:
3506 mutex_unlock(&dsp->pwr_lock);
3507
3508 return ret;
3509}
3510EXPORT_SYMBOL_GPL(wm_adsp_compr_open);
3511
3512int wm_adsp_compr_free(struct snd_compr_stream *stream)
3513{
3514 struct wm_adsp_compr *compr = stream->runtime->private_data;
3515 struct wm_adsp *dsp = compr->dsp;
3516
3517 mutex_lock(&dsp->pwr_lock);
3518
Charles Keepax721be3b2016-05-04 17:11:56 +01003519 wm_adsp_compr_detach(compr);
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003520 list_del(&compr->list);
Charles Keepax406abc92015-12-15 11:29:45 +00003521
Charles Keepax83a40ce2016-01-06 12:33:19 +00003522 kfree(compr->raw_buf);
Charles Keepax406abc92015-12-15 11:29:45 +00003523 kfree(compr);
3524
3525 mutex_unlock(&dsp->pwr_lock);
3526
3527 return 0;
3528}
3529EXPORT_SYMBOL_GPL(wm_adsp_compr_free);
3530
3531static int wm_adsp_compr_check_params(struct snd_compr_stream *stream,
3532 struct snd_compr_params *params)
3533{
3534 struct wm_adsp_compr *compr = stream->runtime->private_data;
3535 struct wm_adsp *dsp = compr->dsp;
3536 const struct wm_adsp_fw_caps *caps;
3537 const struct snd_codec_desc *desc;
3538 int i, j;
3539
3540 if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE ||
3541 params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE ||
3542 params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS ||
3543 params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS ||
3544 params->buffer.fragment_size % WM_ADSP_DATA_WORD_SIZE) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00003545 compr_err(compr, "Invalid buffer fragsize=%d fragments=%d\n",
3546 params->buffer.fragment_size,
3547 params->buffer.fragments);
Charles Keepax406abc92015-12-15 11:29:45 +00003548
3549 return -EINVAL;
3550 }
3551
3552 for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) {
3553 caps = &wm_adsp_fw[dsp->fw].caps[i];
3554 desc = &caps->desc;
3555
3556 if (caps->id != params->codec.id)
3557 continue;
3558
3559 if (stream->direction == SND_COMPRESS_PLAYBACK) {
3560 if (desc->max_ch < params->codec.ch_out)
3561 continue;
3562 } else {
3563 if (desc->max_ch < params->codec.ch_in)
3564 continue;
3565 }
3566
3567 if (!(desc->formats & (1 << params->codec.format)))
3568 continue;
3569
3570 for (j = 0; j < desc->num_sample_rates; ++j)
3571 if (desc->sample_rates[j] == params->codec.sample_rate)
3572 return 0;
3573 }
3574
Charles Keepax0d3fba32019-02-22 10:04:21 +00003575 compr_err(compr, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n",
3576 params->codec.id, params->codec.ch_in, params->codec.ch_out,
3577 params->codec.sample_rate, params->codec.format);
Charles Keepax406abc92015-12-15 11:29:45 +00003578 return -EINVAL;
3579}
3580
Charles Keepax565ace42016-01-06 12:33:18 +00003581static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr)
3582{
3583 return compr->size.fragment_size / WM_ADSP_DATA_WORD_SIZE;
3584}
3585
Charles Keepax406abc92015-12-15 11:29:45 +00003586int wm_adsp_compr_set_params(struct snd_compr_stream *stream,
3587 struct snd_compr_params *params)
3588{
3589 struct wm_adsp_compr *compr = stream->runtime->private_data;
Charles Keepax83a40ce2016-01-06 12:33:19 +00003590 unsigned int size;
Charles Keepax406abc92015-12-15 11:29:45 +00003591 int ret;
3592
3593 ret = wm_adsp_compr_check_params(stream, params);
3594 if (ret)
3595 return ret;
3596
3597 compr->size = params->buffer;
3598
Charles Keepax0d3fba32019-02-22 10:04:21 +00003599 compr_dbg(compr, "fragment_size=%d fragments=%d\n",
3600 compr->size.fragment_size, compr->size.fragments);
Charles Keepax406abc92015-12-15 11:29:45 +00003601
Charles Keepax83a40ce2016-01-06 12:33:19 +00003602 size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf);
3603 compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL);
3604 if (!compr->raw_buf)
3605 return -ENOMEM;
3606
Charles Keepaxda2b3352016-02-02 16:41:36 +00003607 compr->sample_rate = params->codec.sample_rate;
3608
Charles Keepax406abc92015-12-15 11:29:45 +00003609 return 0;
3610}
3611EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params);
3612
3613int wm_adsp_compr_get_caps(struct snd_compr_stream *stream,
3614 struct snd_compr_caps *caps)
3615{
3616 struct wm_adsp_compr *compr = stream->runtime->private_data;
3617 int fw = compr->dsp->fw;
3618 int i;
3619
3620 if (wm_adsp_fw[fw].caps) {
3621 for (i = 0; i < wm_adsp_fw[fw].num_caps; i++)
3622 caps->codecs[i] = wm_adsp_fw[fw].caps[i].id;
3623
3624 caps->num_codecs = i;
3625 caps->direction = wm_adsp_fw[fw].compr_direction;
3626
3627 caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE;
3628 caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE;
3629 caps->min_fragments = WM_ADSP_MIN_FRAGMENTS;
3630 caps->max_fragments = WM_ADSP_MAX_FRAGMENTS;
3631 }
3632
3633 return 0;
3634}
3635EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps);
3636
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003637static int wm_adsp_read_data_block(struct wm_adsp *dsp, int mem_type,
3638 unsigned int mem_addr,
3639 unsigned int num_words, u32 *data)
3640{
3641 struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
3642 unsigned int i, reg;
3643 int ret;
3644
3645 if (!mem)
3646 return -EINVAL;
3647
Wen Shi170b1e12019-03-19 11:52:13 +00003648 reg = dsp->ops->region_to_reg(mem, mem_addr);
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003649
3650 ret = regmap_raw_read(dsp->regmap, reg, data,
3651 sizeof(*data) * num_words);
3652 if (ret < 0)
3653 return ret;
3654
3655 for (i = 0; i < num_words; ++i)
3656 data[i] = be32_to_cpu(data[i]) & 0x00ffffffu;
3657
3658 return 0;
3659}
3660
3661static inline int wm_adsp_read_data_word(struct wm_adsp *dsp, int mem_type,
3662 unsigned int mem_addr, u32 *data)
3663{
3664 return wm_adsp_read_data_block(dsp, mem_type, mem_addr, 1, data);
3665}
3666
3667static int wm_adsp_write_data_word(struct wm_adsp *dsp, int mem_type,
3668 unsigned int mem_addr, u32 data)
3669{
3670 struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
3671 unsigned int reg;
3672
3673 if (!mem)
3674 return -EINVAL;
3675
Wen Shi170b1e12019-03-19 11:52:13 +00003676 reg = dsp->ops->region_to_reg(mem, mem_addr);
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003677
3678 data = cpu_to_be32(data & 0x00ffffffu);
3679
3680 return regmap_raw_write(dsp->regmap, reg, &data, sizeof(data));
3681}
3682
3683static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf,
3684 unsigned int field_offset, u32 *data)
3685{
Andrew Fordfb13f192019-02-19 17:31:56 +00003686 return wm_adsp_read_data_word(buf->dsp, buf->host_buf_mem_type,
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003687 buf->host_buf_ptr + field_offset, data);
3688}
3689
3690static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf,
3691 unsigned int field_offset, u32 data)
3692{
Andrew Fordfb13f192019-02-19 17:31:56 +00003693 return wm_adsp_write_data_word(buf->dsp, buf->host_buf_mem_type,
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003694 buf->host_buf_ptr + field_offset, data);
3695}
3696
Charles Keepaxcc7d6ce2019-02-22 10:04:17 +00003697static void wm_adsp_remove_padding(u32 *buf, int nwords, int data_word_size)
3698{
3699 u8 *pack_in = (u8 *)buf;
3700 u8 *pack_out = (u8 *)buf;
3701 int i, j;
3702
3703 /* Remove the padding bytes from the data read from the DSP */
3704 for (i = 0; i < nwords; i++) {
3705 for (j = 0; j < data_word_size; j++)
3706 *pack_out++ = *pack_in++;
3707
3708 pack_in += sizeof(*buf) - data_word_size;
3709 }
3710}
3711
Charles Keepax1e38f062019-02-22 10:04:18 +00003712static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf)
3713{
3714 const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps;
3715 struct wm_adsp_buffer_region *region;
3716 u32 offset = 0;
3717 int i, ret;
3718
Charles Keepaxa792af62019-02-22 10:04:19 +00003719 buf->regions = kcalloc(caps->num_regions, sizeof(*buf->regions),
3720 GFP_KERNEL);
3721 if (!buf->regions)
3722 return -ENOMEM;
3723
Charles Keepax1e38f062019-02-22 10:04:18 +00003724 for (i = 0; i < caps->num_regions; ++i) {
3725 region = &buf->regions[i];
3726
3727 region->offset = offset;
3728 region->mem_type = caps->region_defs[i].mem_type;
3729
3730 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset,
3731 &region->base_addr);
3732 if (ret < 0)
3733 return ret;
3734
3735 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset,
3736 &offset);
3737 if (ret < 0)
3738 return ret;
3739
3740 region->cumulative_size = offset;
3741
Charles Keepax0d3fba32019-02-22 10:04:21 +00003742 compr_dbg(buf,
3743 "region=%d type=%d base=%08x off=%08x size=%08x\n",
3744 i, region->mem_type, region->base_addr,
3745 region->offset, region->cumulative_size);
Charles Keepax1e38f062019-02-22 10:04:18 +00003746 }
3747
3748 return 0;
3749}
3750
3751static void wm_adsp_buffer_clear(struct wm_adsp_compr_buf *buf)
3752{
3753 buf->irq_count = 0xFFFFFFFF;
3754 buf->read_index = -1;
3755 buf->avail = 0;
3756}
3757
Charles Keepaxa792af62019-02-22 10:04:19 +00003758static struct wm_adsp_compr_buf *wm_adsp_buffer_alloc(struct wm_adsp *dsp)
3759{
3760 struct wm_adsp_compr_buf *buf;
3761
3762 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
3763 if (!buf)
3764 return NULL;
3765
3766 buf->dsp = dsp;
3767
3768 wm_adsp_buffer_clear(buf);
3769
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003770 list_add_tail(&buf->list, &dsp->buffer_list);
Charles Keepaxa792af62019-02-22 10:04:19 +00003771
3772 return buf;
3773}
3774
3775static int wm_adsp_buffer_parse_legacy(struct wm_adsp *dsp)
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003776{
3777 struct wm_adsp_alg_region *alg_region;
Charles Keepaxa792af62019-02-22 10:04:19 +00003778 struct wm_adsp_compr_buf *buf;
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003779 u32 xmalg, addr, magic;
3780 int i, ret;
3781
Li Xu9daf4fd02019-10-01 14:09:11 +01003782 alg_region = wm_adsp_find_alg_region(dsp, WMFW_ADSP2_XM, dsp->fw_id);
3783 if (!alg_region) {
3784 adsp_err(dsp, "No algorithm region found\n");
3785 return -EINVAL;
3786 }
3787
Charles Keepaxa792af62019-02-22 10:04:19 +00003788 buf = wm_adsp_buffer_alloc(dsp);
3789 if (!buf)
3790 return -ENOMEM;
3791
Wen Shi170b1e12019-03-19 11:52:13 +00003792 xmalg = dsp->ops->sys_config_size / sizeof(__be32);
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003793
3794 addr = alg_region->base + xmalg + ALG_XM_FIELD(magic);
3795 ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr, &magic);
3796 if (ret < 0)
3797 return ret;
3798
3799 if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC)
Charles Keepaxa792af62019-02-22 10:04:19 +00003800 return -ENODEV;
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003801
3802 addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr);
3803 for (i = 0; i < 5; ++i) {
3804 ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr,
3805 &buf->host_buf_ptr);
3806 if (ret < 0)
3807 return ret;
3808
3809 if (buf->host_buf_ptr)
3810 break;
3811
3812 usleep_range(1000, 2000);
3813 }
3814
3815 if (!buf->host_buf_ptr)
3816 return -EIO;
3817
Andrew Fordfb13f192019-02-19 17:31:56 +00003818 buf->host_buf_mem_type = WMFW_ADSP2_XM;
3819
Charles Keepaxa792af62019-02-22 10:04:19 +00003820 ret = wm_adsp_buffer_populate(buf);
3821 if (ret < 0)
3822 return ret;
3823
Charles Keepax0d3fba32019-02-22 10:04:21 +00003824 compr_dbg(buf, "legacy host_buf_ptr=%x\n", buf->host_buf_ptr);
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003825
3826 return 0;
3827}
3828
Charles Keepaxa792af62019-02-22 10:04:19 +00003829static int wm_adsp_buffer_parse_coeff(struct wm_coeff_ctl *ctl)
Richard Fitzgeraldd52ed4b2018-07-19 11:50:39 +01003830{
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003831 struct wm_adsp_host_buf_coeff_v1 coeff_v1;
Charles Keepaxa792af62019-02-22 10:04:19 +00003832 struct wm_adsp_compr_buf *buf;
3833 unsigned int val, reg;
3834 int ret, i;
Richard Fitzgeraldd52ed4b2018-07-19 11:50:39 +01003835
3836 ret = wm_coeff_base_reg(ctl, &reg);
3837 if (ret)
3838 return ret;
3839
3840 for (i = 0; i < 5; ++i) {
Charles Keepaxa792af62019-02-22 10:04:19 +00003841 ret = regmap_raw_read(ctl->dsp->regmap, reg, &val, sizeof(val));
Richard Fitzgeraldd52ed4b2018-07-19 11:50:39 +01003842 if (ret < 0)
3843 return ret;
3844
3845 if (val)
3846 break;
3847
3848 usleep_range(1000, 2000);
3849 }
3850
Charles Keepaxa792af62019-02-22 10:04:19 +00003851 if (!val) {
3852 adsp_err(ctl->dsp, "Failed to acquire host buffer\n");
Richard Fitzgeraldd52ed4b2018-07-19 11:50:39 +01003853 return -EIO;
Charles Keepaxa792af62019-02-22 10:04:19 +00003854 }
Richard Fitzgeraldd52ed4b2018-07-19 11:50:39 +01003855
Charles Keepaxa792af62019-02-22 10:04:19 +00003856 buf = wm_adsp_buffer_alloc(ctl->dsp);
3857 if (!buf)
3858 return -ENOMEM;
3859
3860 buf->host_buf_mem_type = ctl->alg_region.type;
Richard Fitzgeraldd52ed4b2018-07-19 11:50:39 +01003861 buf->host_buf_ptr = be32_to_cpu(val);
Charles Keepaxa792af62019-02-22 10:04:19 +00003862
3863 ret = wm_adsp_buffer_populate(buf);
3864 if (ret < 0)
3865 return ret;
3866
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003867 /*
3868 * v0 host_buffer coefficients didn't have versioning, so if the
3869 * control is one word, assume version 0.
3870 */
3871 if (ctl->len == 4) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00003872 compr_dbg(buf, "host_buf_ptr=%x\n", buf->host_buf_ptr);
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003873 return 0;
3874 }
Richard Fitzgeraldd52ed4b2018-07-19 11:50:39 +01003875
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003876 ret = regmap_raw_read(ctl->dsp->regmap, reg, &coeff_v1,
3877 sizeof(coeff_v1));
3878 if (ret < 0)
3879 return ret;
3880
3881 coeff_v1.versions = be32_to_cpu(coeff_v1.versions);
3882 val = coeff_v1.versions & HOST_BUF_COEFF_COMPAT_VER_MASK;
3883 val >>= HOST_BUF_COEFF_COMPAT_VER_SHIFT;
3884
3885 if (val > HOST_BUF_COEFF_SUPPORTED_COMPAT_VER) {
3886 adsp_err(ctl->dsp,
3887 "Host buffer coeff ver %u > supported version %u\n",
3888 val, HOST_BUF_COEFF_SUPPORTED_COMPAT_VER);
3889 return -EINVAL;
3890 }
3891
3892 for (i = 0; i < ARRAY_SIZE(coeff_v1.name); i++)
3893 coeff_v1.name[i] = be32_to_cpu(coeff_v1.name[i]);
3894
3895 wm_adsp_remove_padding((u32 *)&coeff_v1.name,
3896 ARRAY_SIZE(coeff_v1.name),
3897 WM_ADSP_DATA_WORD_SIZE);
3898
3899 buf->name = kasprintf(GFP_KERNEL, "%s-dsp-%s", ctl->dsp->part,
3900 (char *)&coeff_v1.name);
3901
Charles Keepax0d3fba32019-02-22 10:04:21 +00003902 compr_dbg(buf, "host_buf_ptr=%x coeff version %u\n",
3903 buf->host_buf_ptr, val);
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003904
3905 return val;
Richard Fitzgeraldd52ed4b2018-07-19 11:50:39 +01003906}
3907
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003908static int wm_adsp_buffer_init(struct wm_adsp *dsp)
3909{
Charles Keepaxa792af62019-02-22 10:04:19 +00003910 struct wm_coeff_ctl *ctl;
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003911 int ret;
3912
Charles Keepaxa792af62019-02-22 10:04:19 +00003913 list_for_each_entry(ctl, &dsp->ctl_list, list) {
3914 if (ctl->type != WMFW_CTL_TYPE_HOST_BUFFER)
3915 continue;
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003916
Charles Keepaxa792af62019-02-22 10:04:19 +00003917 if (!ctl->enabled)
3918 continue;
Charles Keepax61fc0602018-02-26 10:49:47 +00003919
Charles Keepaxa792af62019-02-22 10:04:19 +00003920 ret = wm_adsp_buffer_parse_coeff(ctl);
3921 if (ret < 0) {
3922 adsp_err(dsp, "Failed to parse coeff: %d\n", ret);
3923 goto error;
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003924 } else if (ret == 0) {
3925 /* Only one buffer supported for version 0 */
3926 return 0;
Charles Keepaxa792af62019-02-22 10:04:19 +00003927 }
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003928 }
3929
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003930 if (list_empty(&dsp->buffer_list)) {
Charles Keepaxa792af62019-02-22 10:04:19 +00003931 /* Fall back to legacy support */
3932 ret = wm_adsp_buffer_parse_legacy(dsp);
3933 if (ret) {
3934 adsp_err(dsp, "Failed to parse legacy: %d\n", ret);
3935 goto error;
3936 }
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003937 }
3938
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003939 return 0;
3940
Charles Keepaxa792af62019-02-22 10:04:19 +00003941error:
3942 wm_adsp_buffer_free(dsp);
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003943 return ret;
3944}
3945
3946static int wm_adsp_buffer_free(struct wm_adsp *dsp)
3947{
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003948 struct wm_adsp_compr_buf *buf, *tmp;
Charles Keepax721be3b2016-05-04 17:11:56 +01003949
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003950 list_for_each_entry_safe(buf, tmp, &dsp->buffer_list, list) {
Charles Keepax26ffa012019-04-02 13:49:15 +01003951 wm_adsp_compr_detach(buf->compr);
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003952
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00003953 kfree(buf->name);
3954 kfree(buf->regions);
3955 list_del(&buf->list);
3956 kfree(buf);
Charles Keepax2cd19bd2015-12-15 11:29:46 +00003957 }
3958
3959 return 0;
3960}
3961
Stuart Hendersonf938f342019-02-19 17:31:57 +00003962static int wm_adsp_buffer_get_error(struct wm_adsp_compr_buf *buf)
3963{
3964 int ret;
3965
3966 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error);
3967 if (ret < 0) {
Charles Keepax48ead312019-03-19 11:52:05 +00003968 compr_err(buf, "Failed to check buffer error: %d\n", ret);
Stuart Hendersonf938f342019-02-19 17:31:57 +00003969 return ret;
3970 }
3971 if (buf->error != 0) {
Charles Keepax48ead312019-03-19 11:52:05 +00003972 compr_err(buf, "Buffer error occurred: %d\n", buf->error);
Stuart Hendersonf938f342019-02-19 17:31:57 +00003973 return -EIO;
3974 }
3975
3976 return 0;
3977}
3978
Charles Keepax95fe9592015-12-15 11:29:47 +00003979int wm_adsp_compr_trigger(struct snd_compr_stream *stream, int cmd)
3980{
3981 struct wm_adsp_compr *compr = stream->runtime->private_data;
3982 struct wm_adsp *dsp = compr->dsp;
3983 int ret = 0;
3984
Charles Keepax0d3fba32019-02-22 10:04:21 +00003985 compr_dbg(compr, "Trigger: %d\n", cmd);
Charles Keepax95fe9592015-12-15 11:29:47 +00003986
3987 mutex_lock(&dsp->pwr_lock);
3988
3989 switch (cmd) {
3990 case SNDRV_PCM_TRIGGER_START:
Charles Keepax61fc0602018-02-26 10:49:47 +00003991 if (!wm_adsp_compr_attached(compr)) {
3992 ret = wm_adsp_compr_attach(compr);
3993 if (ret < 0) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00003994 compr_err(compr, "Failed to link buffer and stream: %d\n",
3995 ret);
Charles Keepax61fc0602018-02-26 10:49:47 +00003996 break;
3997 }
Charles Keepax95fe9592015-12-15 11:29:47 +00003998 }
Charles Keepax565ace42016-01-06 12:33:18 +00003999
Stuart Hendersonf938f342019-02-19 17:31:57 +00004000 ret = wm_adsp_buffer_get_error(compr->buf);
4001 if (ret < 0)
4002 break;
4003
Charles Keepax565ace42016-01-06 12:33:18 +00004004 /* Trigger the IRQ at one fragment of data */
4005 ret = wm_adsp_buffer_write(compr->buf,
4006 HOST_BUFFER_FIELD(high_water_mark),
4007 wm_adsp_compr_frag_words(compr));
4008 if (ret < 0) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00004009 compr_err(compr, "Failed to set high water mark: %d\n",
4010 ret);
Charles Keepax565ace42016-01-06 12:33:18 +00004011 break;
4012 }
Charles Keepax95fe9592015-12-15 11:29:47 +00004013 break;
4014 case SNDRV_PCM_TRIGGER_STOP:
Charles Keepax43d147b2019-04-02 13:49:14 +01004015 if (wm_adsp_compr_attached(compr))
4016 wm_adsp_buffer_clear(compr->buf);
Charles Keepax95fe9592015-12-15 11:29:47 +00004017 break;
4018 default:
4019 ret = -EINVAL;
4020 break;
4021 }
4022
4023 mutex_unlock(&dsp->pwr_lock);
4024
4025 return ret;
4026}
4027EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger);
4028
Charles Keepax565ace42016-01-06 12:33:18 +00004029static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf)
4030{
4031 int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1;
4032
4033 return buf->regions[last_region].cumulative_size;
4034}
4035
4036static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf)
4037{
4038 u32 next_read_index, next_write_index;
4039 int write_index, read_index, avail;
4040 int ret;
4041
4042 /* Only sync read index if we haven't already read a valid index */
4043 if (buf->read_index < 0) {
4044 ret = wm_adsp_buffer_read(buf,
4045 HOST_BUFFER_FIELD(next_read_index),
4046 &next_read_index);
4047 if (ret < 0)
4048 return ret;
4049
4050 read_index = sign_extend32(next_read_index, 23);
4051
4052 if (read_index < 0) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00004053 compr_dbg(buf, "Avail check on unstarted stream\n");
Charles Keepax565ace42016-01-06 12:33:18 +00004054 return 0;
4055 }
4056
4057 buf->read_index = read_index;
4058 }
4059
4060 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index),
4061 &next_write_index);
4062 if (ret < 0)
4063 return ret;
4064
4065 write_index = sign_extend32(next_write_index, 23);
4066
4067 avail = write_index - buf->read_index;
4068 if (avail < 0)
4069 avail += wm_adsp_buffer_size(buf);
4070
Charles Keepax0d3fba32019-02-22 10:04:21 +00004071 compr_dbg(buf, "readindex=0x%x, writeindex=0x%x, avail=%d\n",
4072 buf->read_index, write_index, avail * WM_ADSP_DATA_WORD_SIZE);
Charles Keepax565ace42016-01-06 12:33:18 +00004073
4074 buf->avail = avail;
4075
4076 return 0;
4077}
4078
4079int wm_adsp_compr_handle_irq(struct wm_adsp *dsp)
4080{
Charles Keepax612047f2016-03-28 14:29:22 +01004081 struct wm_adsp_compr_buf *buf;
4082 struct wm_adsp_compr *compr;
Charles Keepax565ace42016-01-06 12:33:18 +00004083 int ret = 0;
4084
4085 mutex_lock(&dsp->pwr_lock);
4086
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00004087 if (list_empty(&dsp->buffer_list)) {
Charles Keepax565ace42016-01-06 12:33:18 +00004088 ret = -ENODEV;
4089 goto out;
4090 }
Charles Keepax0d3fba32019-02-22 10:04:21 +00004091
Charles Keepax565ace42016-01-06 12:33:18 +00004092 adsp_dbg(dsp, "Handling buffer IRQ\n");
4093
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00004094 list_for_each_entry(buf, &dsp->buffer_list, list) {
4095 compr = buf->compr;
Charles Keepax565ace42016-01-06 12:33:18 +00004096
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00004097 ret = wm_adsp_buffer_get_error(buf);
4098 if (ret < 0)
4099 goto out_notify; /* Wake poll to report error */
Charles Keepax565ace42016-01-06 12:33:18 +00004100
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00004101 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count),
4102 &buf->irq_count);
4103 if (ret < 0) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00004104 compr_err(buf, "Failed to get irq_count: %d\n", ret);
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00004105 goto out;
4106 }
Charles Keepax565ace42016-01-06 12:33:18 +00004107
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00004108 ret = wm_adsp_buffer_update_avail(buf);
4109 if (ret < 0) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00004110 compr_err(buf, "Error reading avail: %d\n", ret);
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00004111 goto out;
4112 }
4113
4114 if (wm_adsp_fw[dsp->fw].voice_trigger && buf->irq_count == 2)
4115 ret = WM_ADSP_COMPR_VOICE_TRIGGER;
Charles Keepax20b7f7c2016-05-13 16:45:17 +01004116
Charles Keepax58476092016-04-06 11:21:54 +01004117out_notify:
Stuart Henderson4f2d4ea2019-02-22 10:04:20 +00004118 if (compr && compr->stream)
4119 snd_compr_fragment_elapsed(compr->stream);
4120 }
Charles Keepax83a40ce2016-01-06 12:33:19 +00004121
Charles Keepax565ace42016-01-06 12:33:18 +00004122out:
4123 mutex_unlock(&dsp->pwr_lock);
4124
4125 return ret;
4126}
4127EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq);
4128
4129static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf)
4130{
4131 if (buf->irq_count & 0x01)
4132 return 0;
4133
Charles Keepax0d3fba32019-02-22 10:04:21 +00004134 compr_dbg(buf, "Enable IRQ(0x%x) for next fragment\n", buf->irq_count);
Charles Keepax565ace42016-01-06 12:33:18 +00004135
4136 buf->irq_count |= 0x01;
4137
4138 return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack),
4139 buf->irq_count);
4140}
4141
4142int wm_adsp_compr_pointer(struct snd_compr_stream *stream,
4143 struct snd_compr_tstamp *tstamp)
4144{
4145 struct wm_adsp_compr *compr = stream->runtime->private_data;
Charles Keepax565ace42016-01-06 12:33:18 +00004146 struct wm_adsp *dsp = compr->dsp;
Charles Keepax612047f2016-03-28 14:29:22 +01004147 struct wm_adsp_compr_buf *buf;
Charles Keepax565ace42016-01-06 12:33:18 +00004148 int ret = 0;
4149
Charles Keepax0d3fba32019-02-22 10:04:21 +00004150 compr_dbg(compr, "Pointer request\n");
Charles Keepax565ace42016-01-06 12:33:18 +00004151
4152 mutex_lock(&dsp->pwr_lock);
4153
Charles Keepax612047f2016-03-28 14:29:22 +01004154 buf = compr->buf;
4155
Charles Keepaxaa612f22019-04-04 13:56:01 +01004156 if (dsp->fatal_error || !buf || buf->error) {
Charles Keepax8d280662016-06-13 14:17:11 +01004157 snd_compr_stop_error(stream, SNDRV_PCM_STATE_XRUN);
Charles Keepax565ace42016-01-06 12:33:18 +00004158 ret = -EIO;
4159 goto out;
4160 }
4161
4162 if (buf->avail < wm_adsp_compr_frag_words(compr)) {
4163 ret = wm_adsp_buffer_update_avail(buf);
4164 if (ret < 0) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00004165 compr_err(compr, "Error reading avail: %d\n", ret);
Charles Keepax565ace42016-01-06 12:33:18 +00004166 goto out;
4167 }
4168
4169 /*
4170 * If we really have less than 1 fragment available tell the
4171 * DSP to inform us once a whole fragment is available.
4172 */
4173 if (buf->avail < wm_adsp_compr_frag_words(compr)) {
Charles Keepax58476092016-04-06 11:21:54 +01004174 ret = wm_adsp_buffer_get_error(buf);
Charles Keepax8d280662016-06-13 14:17:11 +01004175 if (ret < 0) {
Charles Keepax789b9302019-04-02 13:49:16 +01004176 if (buf->error)
Charles Keepax8d280662016-06-13 14:17:11 +01004177 snd_compr_stop_error(stream,
4178 SNDRV_PCM_STATE_XRUN);
Charles Keepax58476092016-04-06 11:21:54 +01004179 goto out;
Charles Keepax8d280662016-06-13 14:17:11 +01004180 }
Charles Keepax58476092016-04-06 11:21:54 +01004181
Charles Keepax565ace42016-01-06 12:33:18 +00004182 ret = wm_adsp_buffer_reenable_irq(buf);
4183 if (ret < 0) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00004184 compr_err(compr, "Failed to re-enable buffer IRQ: %d\n",
4185 ret);
Charles Keepax565ace42016-01-06 12:33:18 +00004186 goto out;
4187 }
4188 }
4189 }
4190
4191 tstamp->copied_total = compr->copied_total;
4192 tstamp->copied_total += buf->avail * WM_ADSP_DATA_WORD_SIZE;
Charles Keepaxda2b3352016-02-02 16:41:36 +00004193 tstamp->sampling_rate = compr->sample_rate;
Charles Keepax565ace42016-01-06 12:33:18 +00004194
4195out:
4196 mutex_unlock(&dsp->pwr_lock);
4197
4198 return ret;
4199}
4200EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer);
4201
Charles Keepax83a40ce2016-01-06 12:33:19 +00004202static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target)
4203{
4204 struct wm_adsp_compr_buf *buf = compr->buf;
Charles Keepax83a40ce2016-01-06 12:33:19 +00004205 unsigned int adsp_addr;
4206 int mem_type, nwords, max_read;
Charles Keepaxcc7d6ce2019-02-22 10:04:17 +00004207 int i, ret;
Charles Keepax83a40ce2016-01-06 12:33:19 +00004208
4209 /* Calculate read parameters */
4210 for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i)
4211 if (buf->read_index < buf->regions[i].cumulative_size)
4212 break;
4213
4214 if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions)
4215 return -EINVAL;
4216
4217 mem_type = buf->regions[i].mem_type;
4218 adsp_addr = buf->regions[i].base_addr +
4219 (buf->read_index - buf->regions[i].offset);
4220
4221 max_read = wm_adsp_compr_frag_words(compr);
4222 nwords = buf->regions[i].cumulative_size - buf->read_index;
4223
4224 if (nwords > target)
4225 nwords = target;
4226 if (nwords > buf->avail)
4227 nwords = buf->avail;
4228 if (nwords > max_read)
4229 nwords = max_read;
4230 if (!nwords)
4231 return 0;
4232
4233 /* Read data from DSP */
4234 ret = wm_adsp_read_data_block(buf->dsp, mem_type, adsp_addr,
4235 nwords, compr->raw_buf);
4236 if (ret < 0)
4237 return ret;
4238
Charles Keepaxcc7d6ce2019-02-22 10:04:17 +00004239 wm_adsp_remove_padding(compr->raw_buf, nwords, WM_ADSP_DATA_WORD_SIZE);
Charles Keepax83a40ce2016-01-06 12:33:19 +00004240
4241 /* update read index to account for words read */
4242 buf->read_index += nwords;
4243 if (buf->read_index == wm_adsp_buffer_size(buf))
4244 buf->read_index = 0;
4245
4246 ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index),
4247 buf->read_index);
4248 if (ret < 0)
4249 return ret;
4250
4251 /* update avail to account for words read */
4252 buf->avail -= nwords;
4253
4254 return nwords;
4255}
4256
4257static int wm_adsp_compr_read(struct wm_adsp_compr *compr,
4258 char __user *buf, size_t count)
4259{
Charles Keepaxaa612f22019-04-04 13:56:01 +01004260 struct wm_adsp *dsp = compr->dsp;
Charles Keepax83a40ce2016-01-06 12:33:19 +00004261 int ntotal = 0;
4262 int nwords, nbytes;
4263
Charles Keepax0d3fba32019-02-22 10:04:21 +00004264 compr_dbg(compr, "Requested read of %zu bytes\n", count);
Charles Keepax83a40ce2016-01-06 12:33:19 +00004265
Charles Keepaxaa612f22019-04-04 13:56:01 +01004266 if (dsp->fatal_error || !compr->buf || compr->buf->error) {
Charles Keepax8d280662016-06-13 14:17:11 +01004267 snd_compr_stop_error(compr->stream, SNDRV_PCM_STATE_XRUN);
Charles Keepax83a40ce2016-01-06 12:33:19 +00004268 return -EIO;
Charles Keepax8d280662016-06-13 14:17:11 +01004269 }
Charles Keepax83a40ce2016-01-06 12:33:19 +00004270
4271 count /= WM_ADSP_DATA_WORD_SIZE;
4272
4273 do {
4274 nwords = wm_adsp_buffer_capture_block(compr, count);
4275 if (nwords < 0) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00004276 compr_err(compr, "Failed to capture block: %d\n",
4277 nwords);
Charles Keepax83a40ce2016-01-06 12:33:19 +00004278 return nwords;
4279 }
4280
4281 nbytes = nwords * WM_ADSP_DATA_WORD_SIZE;
4282
Charles Keepax0d3fba32019-02-22 10:04:21 +00004283 compr_dbg(compr, "Read %d bytes\n", nbytes);
Charles Keepax83a40ce2016-01-06 12:33:19 +00004284
4285 if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) {
Charles Keepax0d3fba32019-02-22 10:04:21 +00004286 compr_err(compr, "Failed to copy data to user: %d, %d\n",
4287 ntotal, nbytes);
Charles Keepax83a40ce2016-01-06 12:33:19 +00004288 return -EFAULT;
4289 }
4290
4291 count -= nwords;
4292 ntotal += nbytes;
4293 } while (nwords > 0 && count > 0);
4294
4295 compr->copied_total += ntotal;
4296
4297 return ntotal;
4298}
4299
4300int wm_adsp_compr_copy(struct snd_compr_stream *stream, char __user *buf,
4301 size_t count)
4302{
4303 struct wm_adsp_compr *compr = stream->runtime->private_data;
4304 struct wm_adsp *dsp = compr->dsp;
4305 int ret;
4306
4307 mutex_lock(&dsp->pwr_lock);
4308
4309 if (stream->direction == SND_COMPRESS_CAPTURE)
4310 ret = wm_adsp_compr_read(compr, buf, count);
4311 else
4312 ret = -ENOTSUPP;
4313
4314 mutex_unlock(&dsp->pwr_lock);
4315
4316 return ret;
4317}
4318EXPORT_SYMBOL_GPL(wm_adsp_compr_copy);
4319
Charles Keepaxa2bcbc12019-03-19 11:52:07 +00004320static void wm_adsp_fatal_error(struct wm_adsp *dsp)
4321{
4322 struct wm_adsp_compr *compr;
4323
4324 dsp->fatal_error = true;
4325
4326 list_for_each_entry(compr, &dsp->compr_list, list) {
Charles Keepaxaa612f22019-04-04 13:56:01 +01004327 if (compr->stream)
Charles Keepaxa2bcbc12019-03-19 11:52:07 +00004328 snd_compr_fragment_elapsed(compr->stream);
Charles Keepaxa2bcbc12019-03-19 11:52:07 +00004329 }
4330}
4331
Charles Keepax01ec57a2019-07-25 17:39:29 +01004332irqreturn_t wm_adsp2_bus_error(int irq, void *data)
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01004333{
Charles Keepax01ec57a2019-07-25 17:39:29 +01004334 struct wm_adsp *dsp = (struct wm_adsp *)data;
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01004335 unsigned int val;
4336 struct regmap *regmap = dsp->regmap;
4337 int ret = 0;
4338
Charles Keepaxa2225a62019-03-19 11:52:06 +00004339 mutex_lock(&dsp->pwr_lock);
4340
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01004341 ret = regmap_read(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL, &val);
4342 if (ret) {
4343 adsp_err(dsp,
4344 "Failed to read Region Lock Ctrl register: %d\n", ret);
Charles Keepaxa2225a62019-03-19 11:52:06 +00004345 goto error;
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01004346 }
4347
4348 if (val & ADSP2_WDT_TIMEOUT_STS_MASK) {
4349 adsp_err(dsp, "watchdog timeout error\n");
Charles Keepax81ed8842019-05-01 11:13:32 +01004350 dsp->ops->stop_watchdog(dsp);
Charles Keepaxa2bcbc12019-03-19 11:52:07 +00004351 wm_adsp_fatal_error(dsp);
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01004352 }
4353
4354 if (val & (ADSP2_SLAVE_ERR_MASK | ADSP2_REGION_LOCK_ERR_MASK)) {
4355 if (val & ADSP2_SLAVE_ERR_MASK)
4356 adsp_err(dsp, "bus error: slave error\n");
4357 else
4358 adsp_err(dsp, "bus error: region lock error\n");
4359
4360 ret = regmap_read(regmap, dsp->base + ADSP2_BUS_ERR_ADDR, &val);
4361 if (ret) {
4362 adsp_err(dsp,
4363 "Failed to read Bus Err Addr register: %d\n",
4364 ret);
Charles Keepaxa2225a62019-03-19 11:52:06 +00004365 goto error;
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01004366 }
4367
4368 adsp_err(dsp, "bus error address = 0x%x\n",
4369 val & ADSP2_BUS_ERR_ADDR_MASK);
4370
4371 ret = regmap_read(regmap,
4372 dsp->base + ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR,
4373 &val);
4374 if (ret) {
4375 adsp_err(dsp,
4376 "Failed to read Pmem Xmem Err Addr register: %d\n",
4377 ret);
Charles Keepaxa2225a62019-03-19 11:52:06 +00004378 goto error;
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01004379 }
4380
4381 adsp_err(dsp, "xmem error address = 0x%x\n",
4382 val & ADSP2_XMEM_ERR_ADDR_MASK);
4383 adsp_err(dsp, "pmem error address = 0x%x\n",
4384 (val & ADSP2_PMEM_ERR_ADDR_MASK) >>
4385 ADSP2_PMEM_ERR_ADDR_SHIFT);
4386 }
4387
4388 regmap_update_bits(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL,
4389 ADSP2_CTRL_ERR_EINT, ADSP2_CTRL_ERR_EINT);
4390
Charles Keepaxa2225a62019-03-19 11:52:06 +00004391error:
4392 mutex_unlock(&dsp->pwr_lock);
4393
Mayuresh Kulkarni51a2c942017-04-05 11:08:00 +01004394 return IRQ_HANDLED;
4395}
4396EXPORT_SYMBOL_GPL(wm_adsp2_bus_error);
4397
Charles Keepax01ec57a2019-07-25 17:39:29 +01004398irqreturn_t wm_halo_bus_error(int irq, void *data)
Richard Fitzgerald2ae58132019-03-19 11:52:14 +00004399{
Charles Keepax01ec57a2019-07-25 17:39:29 +01004400 struct wm_adsp *dsp = (struct wm_adsp *)data;
Richard Fitzgerald2ae58132019-03-19 11:52:14 +00004401 struct regmap *regmap = dsp->regmap;
4402 unsigned int fault[6];
4403 struct reg_sequence clear[] = {
4404 { dsp->base + HALO_MPU_XM_VIO_STATUS, 0x0 },
4405 { dsp->base + HALO_MPU_YM_VIO_STATUS, 0x0 },
4406 { dsp->base + HALO_MPU_PM_VIO_STATUS, 0x0 },
4407 };
4408 int ret;
4409
4410 mutex_lock(&dsp->pwr_lock);
4411
4412 ret = regmap_read(regmap, dsp->base_sysinfo + HALO_AHBM_WINDOW_DEBUG_1,
4413 fault);
4414 if (ret) {
4415 adsp_warn(dsp, "Failed to read AHB DEBUG_1: %d\n", ret);
4416 goto exit_unlock;
4417 }
4418
4419 adsp_warn(dsp, "AHB: STATUS: 0x%x ADDR: 0x%x\n",
4420 *fault & HALO_AHBM_FLAGS_ERR_MASK,
4421 (*fault & HALO_AHBM_CORE_ERR_ADDR_MASK) >>
4422 HALO_AHBM_CORE_ERR_ADDR_SHIFT);
4423
4424 ret = regmap_read(regmap, dsp->base_sysinfo + HALO_AHBM_WINDOW_DEBUG_0,
4425 fault);
4426 if (ret) {
4427 adsp_warn(dsp, "Failed to read AHB DEBUG_0: %d\n", ret);
4428 goto exit_unlock;
4429 }
4430
4431 adsp_warn(dsp, "AHB: SYS_ADDR: 0x%x\n", *fault);
4432
4433 ret = regmap_bulk_read(regmap, dsp->base + HALO_MPU_XM_VIO_ADDR,
4434 fault, ARRAY_SIZE(fault));
4435 if (ret) {
4436 adsp_warn(dsp, "Failed to read MPU fault info: %d\n", ret);
4437 goto exit_unlock;
4438 }
4439
4440 adsp_warn(dsp, "XM: STATUS:0x%x ADDR:0x%x\n", fault[1], fault[0]);
4441 adsp_warn(dsp, "YM: STATUS:0x%x ADDR:0x%x\n", fault[3], fault[2]);
4442 adsp_warn(dsp, "PM: STATUS:0x%x ADDR:0x%x\n", fault[5], fault[4]);
4443
4444 ret = regmap_multi_reg_write(dsp->regmap, clear, ARRAY_SIZE(clear));
4445 if (ret)
4446 adsp_warn(dsp, "Failed to clear MPU status: %d\n", ret);
4447
4448exit_unlock:
4449 mutex_unlock(&dsp->pwr_lock);
4450
4451 return IRQ_HANDLED;
4452}
4453EXPORT_SYMBOL_GPL(wm_halo_bus_error);
4454
Stuart Henderson8bc144f2019-03-19 11:52:15 +00004455irqreturn_t wm_halo_wdt_expire(int irq, void *data)
4456{
4457 struct wm_adsp *dsp = data;
4458
4459 mutex_lock(&dsp->pwr_lock);
4460
4461 adsp_warn(dsp, "WDT Expiry Fault\n");
Charles Keepax81ed8842019-05-01 11:13:32 +01004462 dsp->ops->stop_watchdog(dsp);
Stuart Henderson8bc144f2019-03-19 11:52:15 +00004463 wm_adsp_fatal_error(dsp);
4464
4465 mutex_unlock(&dsp->pwr_lock);
4466
4467 return IRQ_HANDLED;
4468}
4469EXPORT_SYMBOL_GPL(wm_halo_wdt_expire);
4470
Charles Keepaxcd537872019-03-19 17:43:09 +00004471static struct wm_adsp_ops wm_adsp1_ops = {
Charles Keepax4e08d502019-03-19 11:52:12 +00004472 .validate_version = wm_adsp_validate_version,
4473 .parse_sizes = wm_adsp1_parse_sizes,
Wen Shi170b1e12019-03-19 11:52:13 +00004474 .region_to_reg = wm_adsp_region_to_reg,
Charles Keepax4e08d502019-03-19 11:52:12 +00004475};
4476
Charles Keepaxcd537872019-03-19 17:43:09 +00004477static struct wm_adsp_ops wm_adsp2_ops[] = {
Charles Keepax4e08d502019-03-19 11:52:12 +00004478 {
Wen Shi170b1e12019-03-19 11:52:13 +00004479 .sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr),
Charles Keepax4e08d502019-03-19 11:52:12 +00004480 .parse_sizes = wm_adsp2_parse_sizes,
4481 .validate_version = wm_adsp_validate_version,
4482 .setup_algs = wm_adsp2_setup_algs,
Wen Shi170b1e12019-03-19 11:52:13 +00004483 .region_to_reg = wm_adsp_region_to_reg,
Charles Keepax4e08d502019-03-19 11:52:12 +00004484
4485 .show_fw_status = wm_adsp2_show_fw_status,
4486
4487 .enable_memory = wm_adsp2_enable_memory,
4488 .disable_memory = wm_adsp2_disable_memory,
4489
4490 .enable_core = wm_adsp2_enable_core,
4491 .disable_core = wm_adsp2_disable_core,
4492
4493 .start_core = wm_adsp2_start_core,
4494 .stop_core = wm_adsp2_stop_core,
4495
4496 },
4497 {
Wen Shi170b1e12019-03-19 11:52:13 +00004498 .sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr),
Charles Keepax4e08d502019-03-19 11:52:12 +00004499 .parse_sizes = wm_adsp2_parse_sizes,
4500 .validate_version = wm_adsp_validate_version,
4501 .setup_algs = wm_adsp2_setup_algs,
Wen Shi170b1e12019-03-19 11:52:13 +00004502 .region_to_reg = wm_adsp_region_to_reg,
Charles Keepax4e08d502019-03-19 11:52:12 +00004503
4504 .show_fw_status = wm_adsp2v2_show_fw_status,
4505
4506 .enable_memory = wm_adsp2_enable_memory,
4507 .disable_memory = wm_adsp2_disable_memory,
4508 .lock_memory = wm_adsp2_lock,
4509
4510 .enable_core = wm_adsp2v2_enable_core,
4511 .disable_core = wm_adsp2v2_disable_core,
4512
4513 .start_core = wm_adsp2_start_core,
4514 .stop_core = wm_adsp2_stop_core,
4515 },
4516 {
Wen Shi170b1e12019-03-19 11:52:13 +00004517 .sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr),
Charles Keepax4e08d502019-03-19 11:52:12 +00004518 .parse_sizes = wm_adsp2_parse_sizes,
4519 .validate_version = wm_adsp_validate_version,
4520 .setup_algs = wm_adsp2_setup_algs,
Wen Shi170b1e12019-03-19 11:52:13 +00004521 .region_to_reg = wm_adsp_region_to_reg,
Charles Keepax4e08d502019-03-19 11:52:12 +00004522
4523 .show_fw_status = wm_adsp2v2_show_fw_status,
4524 .stop_watchdog = wm_adsp_stop_watchdog,
4525
4526 .enable_memory = wm_adsp2_enable_memory,
4527 .disable_memory = wm_adsp2_disable_memory,
4528 .lock_memory = wm_adsp2_lock,
4529
4530 .enable_core = wm_adsp2v2_enable_core,
4531 .disable_core = wm_adsp2v2_disable_core,
4532
4533 .start_core = wm_adsp2_start_core,
4534 .stop_core = wm_adsp2_stop_core,
4535 },
4536};
4537
Charles Keepaxcd537872019-03-19 17:43:09 +00004538static struct wm_adsp_ops wm_halo_ops = {
Wen Shi170b1e12019-03-19 11:52:13 +00004539 .sys_config_size = sizeof(struct wm_halo_system_config_xm_hdr),
4540 .parse_sizes = wm_adsp2_parse_sizes,
4541 .validate_version = wm_halo_validate_version,
4542 .setup_algs = wm_halo_setup_algs,
4543 .region_to_reg = wm_halo_region_to_reg,
4544
4545 .show_fw_status = wm_halo_show_fw_status,
Stuart Henderson8bc144f2019-03-19 11:52:15 +00004546 .stop_watchdog = wm_halo_stop_watchdog,
Wen Shi170b1e12019-03-19 11:52:13 +00004547
4548 .lock_memory = wm_halo_configure_mpu,
4549
4550 .start_core = wm_halo_start_core,
4551 .stop_core = wm_halo_stop_core,
4552};
4553
Praveen Diwakar0a37c6ef2014-07-04 11:17:41 +05304554MODULE_LICENSE("GPL v2");