xref: /openbmc/linux/sound/soc/codecs/wm_adsp.c (revision 6facd2d1)
12159ad93SMark Brown /*
22159ad93SMark Brown  * wm_adsp.c  --  Wolfson ADSP support
32159ad93SMark Brown  *
42159ad93SMark Brown  * Copyright 2012 Wolfson Microelectronics plc
52159ad93SMark Brown  *
62159ad93SMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
72159ad93SMark Brown  *
82159ad93SMark Brown  * This program is free software; you can redistribute it and/or modify
92159ad93SMark Brown  * it under the terms of the GNU General Public License version 2 as
102159ad93SMark Brown  * published by the Free Software Foundation.
112159ad93SMark Brown  */
122159ad93SMark Brown 
132159ad93SMark Brown #include <linux/module.h>
142159ad93SMark Brown #include <linux/moduleparam.h>
152159ad93SMark Brown #include <linux/init.h>
162159ad93SMark Brown #include <linux/delay.h>
172159ad93SMark Brown #include <linux/firmware.h>
18cf17c83cSMark Brown #include <linux/list.h>
192159ad93SMark Brown #include <linux/pm.h>
202159ad93SMark Brown #include <linux/pm_runtime.h>
212159ad93SMark Brown #include <linux/regmap.h>
22973838a0SMark Brown #include <linux/regulator/consumer.h>
232159ad93SMark Brown #include <linux/slab.h>
24cdcd7f72SCharles Keepax #include <linux/vmalloc.h>
256ab2b7b4SDimitris Papastamos #include <linux/workqueue.h>
26f9f55e31SRichard Fitzgerald #include <linux/debugfs.h>
272159ad93SMark Brown #include <sound/core.h>
282159ad93SMark Brown #include <sound/pcm.h>
292159ad93SMark Brown #include <sound/pcm_params.h>
302159ad93SMark Brown #include <sound/soc.h>
312159ad93SMark Brown #include <sound/jack.h>
322159ad93SMark Brown #include <sound/initval.h>
332159ad93SMark Brown #include <sound/tlv.h>
342159ad93SMark Brown 
352159ad93SMark Brown #include "wm_adsp.h"
362159ad93SMark Brown 
372159ad93SMark Brown #define adsp_crit(_dsp, fmt, ...) \
382159ad93SMark Brown 	dev_crit(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
392159ad93SMark Brown #define adsp_err(_dsp, fmt, ...) \
402159ad93SMark Brown 	dev_err(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
412159ad93SMark Brown #define adsp_warn(_dsp, fmt, ...) \
422159ad93SMark Brown 	dev_warn(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
432159ad93SMark Brown #define adsp_info(_dsp, fmt, ...) \
442159ad93SMark Brown 	dev_info(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
452159ad93SMark Brown #define adsp_dbg(_dsp, fmt, ...) \
462159ad93SMark Brown 	dev_dbg(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
472159ad93SMark Brown 
482159ad93SMark Brown #define ADSP1_CONTROL_1                   0x00
492159ad93SMark Brown #define ADSP1_CONTROL_2                   0x02
502159ad93SMark Brown #define ADSP1_CONTROL_3                   0x03
512159ad93SMark Brown #define ADSP1_CONTROL_4                   0x04
522159ad93SMark Brown #define ADSP1_CONTROL_5                   0x06
532159ad93SMark Brown #define ADSP1_CONTROL_6                   0x07
542159ad93SMark Brown #define ADSP1_CONTROL_7                   0x08
552159ad93SMark Brown #define ADSP1_CONTROL_8                   0x09
562159ad93SMark Brown #define ADSP1_CONTROL_9                   0x0A
572159ad93SMark Brown #define ADSP1_CONTROL_10                  0x0B
582159ad93SMark Brown #define ADSP1_CONTROL_11                  0x0C
592159ad93SMark Brown #define ADSP1_CONTROL_12                  0x0D
602159ad93SMark Brown #define ADSP1_CONTROL_13                  0x0F
612159ad93SMark Brown #define ADSP1_CONTROL_14                  0x10
622159ad93SMark Brown #define ADSP1_CONTROL_15                  0x11
632159ad93SMark Brown #define ADSP1_CONTROL_16                  0x12
642159ad93SMark Brown #define ADSP1_CONTROL_17                  0x13
652159ad93SMark Brown #define ADSP1_CONTROL_18                  0x14
662159ad93SMark Brown #define ADSP1_CONTROL_19                  0x16
672159ad93SMark Brown #define ADSP1_CONTROL_20                  0x17
682159ad93SMark Brown #define ADSP1_CONTROL_21                  0x18
692159ad93SMark Brown #define ADSP1_CONTROL_22                  0x1A
702159ad93SMark Brown #define ADSP1_CONTROL_23                  0x1B
712159ad93SMark Brown #define ADSP1_CONTROL_24                  0x1C
722159ad93SMark Brown #define ADSP1_CONTROL_25                  0x1E
732159ad93SMark Brown #define ADSP1_CONTROL_26                  0x20
742159ad93SMark Brown #define ADSP1_CONTROL_27                  0x21
752159ad93SMark Brown #define ADSP1_CONTROL_28                  0x22
762159ad93SMark Brown #define ADSP1_CONTROL_29                  0x23
772159ad93SMark Brown #define ADSP1_CONTROL_30                  0x24
782159ad93SMark Brown #define ADSP1_CONTROL_31                  0x26
792159ad93SMark Brown 
802159ad93SMark Brown /*
812159ad93SMark Brown  * ADSP1 Control 19
822159ad93SMark Brown  */
832159ad93SMark Brown #define ADSP1_WDMA_BUFFER_LENGTH_MASK     0x00FF  /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
842159ad93SMark Brown #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT         0  /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
852159ad93SMark Brown #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH         8  /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
862159ad93SMark Brown 
872159ad93SMark Brown 
882159ad93SMark Brown /*
892159ad93SMark Brown  * ADSP1 Control 30
902159ad93SMark Brown  */
912159ad93SMark Brown #define ADSP1_DBG_CLK_ENA                 0x0008  /* DSP1_DBG_CLK_ENA */
922159ad93SMark Brown #define ADSP1_DBG_CLK_ENA_MASK            0x0008  /* DSP1_DBG_CLK_ENA */
932159ad93SMark Brown #define ADSP1_DBG_CLK_ENA_SHIFT                3  /* DSP1_DBG_CLK_ENA */
942159ad93SMark Brown #define ADSP1_DBG_CLK_ENA_WIDTH                1  /* DSP1_DBG_CLK_ENA */
952159ad93SMark Brown #define ADSP1_SYS_ENA                     0x0004  /* DSP1_SYS_ENA */
962159ad93SMark Brown #define ADSP1_SYS_ENA_MASK                0x0004  /* DSP1_SYS_ENA */
972159ad93SMark Brown #define ADSP1_SYS_ENA_SHIFT                    2  /* DSP1_SYS_ENA */
982159ad93SMark Brown #define ADSP1_SYS_ENA_WIDTH                    1  /* DSP1_SYS_ENA */
992159ad93SMark Brown #define ADSP1_CORE_ENA                    0x0002  /* DSP1_CORE_ENA */
1002159ad93SMark Brown #define ADSP1_CORE_ENA_MASK               0x0002  /* DSP1_CORE_ENA */
1012159ad93SMark Brown #define ADSP1_CORE_ENA_SHIFT                   1  /* DSP1_CORE_ENA */
1022159ad93SMark Brown #define ADSP1_CORE_ENA_WIDTH                   1  /* DSP1_CORE_ENA */
1032159ad93SMark Brown #define ADSP1_START                       0x0001  /* DSP1_START */
1042159ad93SMark Brown #define ADSP1_START_MASK                  0x0001  /* DSP1_START */
1052159ad93SMark Brown #define ADSP1_START_SHIFT                      0  /* DSP1_START */
1062159ad93SMark Brown #define ADSP1_START_WIDTH                      1  /* DSP1_START */
1072159ad93SMark Brown 
10894e205bfSChris Rattray /*
10994e205bfSChris Rattray  * ADSP1 Control 31
11094e205bfSChris Rattray  */
11194e205bfSChris Rattray #define ADSP1_CLK_SEL_MASK                0x0007  /* CLK_SEL_ENA */
11294e205bfSChris Rattray #define ADSP1_CLK_SEL_SHIFT                    0  /* CLK_SEL_ENA */
11394e205bfSChris Rattray #define ADSP1_CLK_SEL_WIDTH                    3  /* CLK_SEL_ENA */
11494e205bfSChris Rattray 
1152d30b575SMark Brown #define ADSP2_CONTROL        0x0
1162d30b575SMark Brown #define ADSP2_CLOCKING       0x1
1172d30b575SMark Brown #define ADSP2_STATUS1        0x4
1182d30b575SMark Brown #define ADSP2_WDMA_CONFIG_1 0x30
1192d30b575SMark Brown #define ADSP2_WDMA_CONFIG_2 0x31
1202d30b575SMark Brown #define ADSP2_RDMA_CONFIG_1 0x34
1212159ad93SMark Brown 
12210337b07SRichard Fitzgerald #define ADSP2_SCRATCH0        0x40
12310337b07SRichard Fitzgerald #define ADSP2_SCRATCH1        0x41
12410337b07SRichard Fitzgerald #define ADSP2_SCRATCH2        0x42
12510337b07SRichard Fitzgerald #define ADSP2_SCRATCH3        0x43
12610337b07SRichard Fitzgerald 
1272159ad93SMark Brown /*
1282159ad93SMark Brown  * ADSP2 Control
1292159ad93SMark Brown  */
1302159ad93SMark Brown 
1312159ad93SMark Brown #define ADSP2_MEM_ENA                     0x0010  /* DSP1_MEM_ENA */
1322159ad93SMark Brown #define ADSP2_MEM_ENA_MASK                0x0010  /* DSP1_MEM_ENA */
1332159ad93SMark Brown #define ADSP2_MEM_ENA_SHIFT                    4  /* DSP1_MEM_ENA */
1342159ad93SMark Brown #define ADSP2_MEM_ENA_WIDTH                    1  /* DSP1_MEM_ENA */
1352159ad93SMark Brown #define ADSP2_SYS_ENA                     0x0004  /* DSP1_SYS_ENA */
1362159ad93SMark Brown #define ADSP2_SYS_ENA_MASK                0x0004  /* DSP1_SYS_ENA */
1372159ad93SMark Brown #define ADSP2_SYS_ENA_SHIFT                    2  /* DSP1_SYS_ENA */
1382159ad93SMark Brown #define ADSP2_SYS_ENA_WIDTH                    1  /* DSP1_SYS_ENA */
1392159ad93SMark Brown #define ADSP2_CORE_ENA                    0x0002  /* DSP1_CORE_ENA */
1402159ad93SMark Brown #define ADSP2_CORE_ENA_MASK               0x0002  /* DSP1_CORE_ENA */
1412159ad93SMark Brown #define ADSP2_CORE_ENA_SHIFT                   1  /* DSP1_CORE_ENA */
1422159ad93SMark Brown #define ADSP2_CORE_ENA_WIDTH                   1  /* DSP1_CORE_ENA */
1432159ad93SMark Brown #define ADSP2_START                       0x0001  /* DSP1_START */
1442159ad93SMark Brown #define ADSP2_START_MASK                  0x0001  /* DSP1_START */
1452159ad93SMark Brown #define ADSP2_START_SHIFT                      0  /* DSP1_START */
1462159ad93SMark Brown #define ADSP2_START_WIDTH                      1  /* DSP1_START */
1472159ad93SMark Brown 
1482159ad93SMark Brown /*
149973838a0SMark Brown  * ADSP2 clocking
150973838a0SMark Brown  */
151973838a0SMark Brown #define ADSP2_CLK_SEL_MASK                0x0007  /* CLK_SEL_ENA */
152973838a0SMark Brown #define ADSP2_CLK_SEL_SHIFT                    0  /* CLK_SEL_ENA */
153973838a0SMark Brown #define ADSP2_CLK_SEL_WIDTH                    3  /* CLK_SEL_ENA */
154973838a0SMark Brown 
155973838a0SMark Brown /*
1562159ad93SMark Brown  * ADSP2 Status 1
1572159ad93SMark Brown  */
1582159ad93SMark Brown #define ADSP2_RAM_RDY                     0x0001
1592159ad93SMark Brown #define ADSP2_RAM_RDY_MASK                0x0001
1602159ad93SMark Brown #define ADSP2_RAM_RDY_SHIFT                    0
1612159ad93SMark Brown #define ADSP2_RAM_RDY_WIDTH                    1
1622159ad93SMark Brown 
1639ee78757SCharles Keepax #define ADSP_MAX_STD_CTRL_SIZE               512
1649ee78757SCharles Keepax 
165cf17c83cSMark Brown struct wm_adsp_buf {
166cf17c83cSMark Brown 	struct list_head list;
167cf17c83cSMark Brown 	void *buf;
168cf17c83cSMark Brown };
169cf17c83cSMark Brown 
170cf17c83cSMark Brown static struct wm_adsp_buf *wm_adsp_buf_alloc(const void *src, size_t len,
171cf17c83cSMark Brown 					     struct list_head *list)
172cf17c83cSMark Brown {
173cf17c83cSMark Brown 	struct wm_adsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL);
174cf17c83cSMark Brown 
175cf17c83cSMark Brown 	if (buf == NULL)
176cf17c83cSMark Brown 		return NULL;
177cf17c83cSMark Brown 
178cdcd7f72SCharles Keepax 	buf->buf = vmalloc(len);
179cf17c83cSMark Brown 	if (!buf->buf) {
180cdcd7f72SCharles Keepax 		vfree(buf);
181cf17c83cSMark Brown 		return NULL;
182cf17c83cSMark Brown 	}
183cdcd7f72SCharles Keepax 	memcpy(buf->buf, src, len);
184cf17c83cSMark Brown 
185cf17c83cSMark Brown 	if (list)
186cf17c83cSMark Brown 		list_add_tail(&buf->list, list);
187cf17c83cSMark Brown 
188cf17c83cSMark Brown 	return buf;
189cf17c83cSMark Brown }
190cf17c83cSMark Brown 
191cf17c83cSMark Brown static void wm_adsp_buf_free(struct list_head *list)
192cf17c83cSMark Brown {
193cf17c83cSMark Brown 	while (!list_empty(list)) {
194cf17c83cSMark Brown 		struct wm_adsp_buf *buf = list_first_entry(list,
195cf17c83cSMark Brown 							   struct wm_adsp_buf,
196cf17c83cSMark Brown 							   list);
197cf17c83cSMark Brown 		list_del(&buf->list);
198cdcd7f72SCharles Keepax 		vfree(buf->buf);
199cf17c83cSMark Brown 		kfree(buf);
200cf17c83cSMark Brown 	}
201cf17c83cSMark Brown }
202cf17c83cSMark Brown 
203dd84f925SMark Brown #define WM_ADSP_FW_MBC_VSS  0
20404d1300fSCharles Keepax #define WM_ADSP_FW_HIFI     1
20504d1300fSCharles Keepax #define WM_ADSP_FW_TX       2
20604d1300fSCharles Keepax #define WM_ADSP_FW_TX_SPK   3
20704d1300fSCharles Keepax #define WM_ADSP_FW_RX       4
20804d1300fSCharles Keepax #define WM_ADSP_FW_RX_ANC   5
20904d1300fSCharles Keepax #define WM_ADSP_FW_CTRL     6
21004d1300fSCharles Keepax #define WM_ADSP_FW_ASR      7
21104d1300fSCharles Keepax #define WM_ADSP_FW_TRACE    8
21204d1300fSCharles Keepax #define WM_ADSP_FW_SPK_PROT 9
21304d1300fSCharles Keepax #define WM_ADSP_FW_MISC     10
21404d1300fSCharles Keepax 
21504d1300fSCharles Keepax #define WM_ADSP_NUM_FW      11
216dd84f925SMark Brown 
2171023dbd9SMark Brown static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = {
218dd84f925SMark Brown 	[WM_ADSP_FW_MBC_VSS] =  "MBC/VSS",
21904d1300fSCharles Keepax 	[WM_ADSP_FW_HIFI] =     "MasterHiFi",
220dd84f925SMark Brown 	[WM_ADSP_FW_TX] =       "Tx",
221dd84f925SMark Brown 	[WM_ADSP_FW_TX_SPK] =   "Tx Speaker",
22204d1300fSCharles Keepax 	[WM_ADSP_FW_RX] =       "Rx",
223dd84f925SMark Brown 	[WM_ADSP_FW_RX_ANC] =   "Rx ANC",
22404d1300fSCharles Keepax 	[WM_ADSP_FW_CTRL] =     "Voice Ctrl",
22504d1300fSCharles Keepax 	[WM_ADSP_FW_ASR] =      "ASR Assist",
22604d1300fSCharles Keepax 	[WM_ADSP_FW_TRACE] =    "Dbg Trace",
22704d1300fSCharles Keepax 	[WM_ADSP_FW_SPK_PROT] = "Protection",
22804d1300fSCharles Keepax 	[WM_ADSP_FW_MISC] =     "Misc",
2291023dbd9SMark Brown };
2301023dbd9SMark Brown 
2312cd19bdbSCharles Keepax struct wm_adsp_system_config_xm_hdr {
2322cd19bdbSCharles Keepax 	__be32 sys_enable;
2332cd19bdbSCharles Keepax 	__be32 fw_id;
2342cd19bdbSCharles Keepax 	__be32 fw_rev;
2352cd19bdbSCharles Keepax 	__be32 boot_status;
2362cd19bdbSCharles Keepax 	__be32 watchdog;
2372cd19bdbSCharles Keepax 	__be32 dma_buffer_size;
2382cd19bdbSCharles Keepax 	__be32 rdma[6];
2392cd19bdbSCharles Keepax 	__be32 wdma[8];
2402cd19bdbSCharles Keepax 	__be32 build_job_name[3];
2412cd19bdbSCharles Keepax 	__be32 build_job_number;
2422cd19bdbSCharles Keepax };
2432cd19bdbSCharles Keepax 
2442cd19bdbSCharles Keepax struct wm_adsp_alg_xm_struct {
2452cd19bdbSCharles Keepax 	__be32 magic;
2462cd19bdbSCharles Keepax 	__be32 smoothing;
2472cd19bdbSCharles Keepax 	__be32 threshold;
2482cd19bdbSCharles Keepax 	__be32 host_buf_ptr;
2492cd19bdbSCharles Keepax 	__be32 start_seq;
2502cd19bdbSCharles Keepax 	__be32 high_water_mark;
2512cd19bdbSCharles Keepax 	__be32 low_water_mark;
2522cd19bdbSCharles Keepax 	__be64 smoothed_power;
2532cd19bdbSCharles Keepax };
2542cd19bdbSCharles Keepax 
2552cd19bdbSCharles Keepax struct wm_adsp_buffer {
2562cd19bdbSCharles Keepax 	__be32 X_buf_base;		/* XM base addr of first X area */
2572cd19bdbSCharles Keepax 	__be32 X_buf_size;		/* Size of 1st X area in words */
2582cd19bdbSCharles Keepax 	__be32 X_buf_base2;		/* XM base addr of 2nd X area */
2592cd19bdbSCharles Keepax 	__be32 X_buf_brk;		/* Total X size in words */
2602cd19bdbSCharles Keepax 	__be32 Y_buf_base;		/* YM base addr of Y area */
2612cd19bdbSCharles Keepax 	__be32 wrap;			/* Total size X and Y in words */
2622cd19bdbSCharles Keepax 	__be32 high_water_mark;		/* Point at which IRQ is asserted */
2632cd19bdbSCharles Keepax 	__be32 irq_count;		/* bits 1-31 count IRQ assertions */
2642cd19bdbSCharles Keepax 	__be32 irq_ack;			/* acked IRQ count, bit 0 enables IRQ */
2652cd19bdbSCharles Keepax 	__be32 next_write_index;	/* word index of next write */
2662cd19bdbSCharles Keepax 	__be32 next_read_index;		/* word index of next read */
2672cd19bdbSCharles Keepax 	__be32 error;			/* error if any */
2682cd19bdbSCharles Keepax 	__be32 oldest_block_index;	/* word index of oldest surviving */
2692cd19bdbSCharles Keepax 	__be32 requested_rewind;	/* how many blocks rewind was done */
2702cd19bdbSCharles Keepax 	__be32 reserved_space;		/* internal */
2712cd19bdbSCharles Keepax 	__be32 min_free;		/* min free space since stream start */
2722cd19bdbSCharles Keepax 	__be32 blocks_written[2];	/* total blocks written (64 bit) */
2732cd19bdbSCharles Keepax 	__be32 words_written[2];	/* total words written (64 bit) */
2742cd19bdbSCharles Keepax };
2752cd19bdbSCharles Keepax 
276721be3beSCharles Keepax struct wm_adsp_compr;
277721be3beSCharles Keepax 
2782cd19bdbSCharles Keepax struct wm_adsp_compr_buf {
2792cd19bdbSCharles Keepax 	struct wm_adsp *dsp;
280721be3beSCharles Keepax 	struct wm_adsp_compr *compr;
2812cd19bdbSCharles Keepax 
2822cd19bdbSCharles Keepax 	struct wm_adsp_buffer_region *regions;
2832cd19bdbSCharles Keepax 	u32 host_buf_ptr;
284565ace46SCharles Keepax 
285565ace46SCharles Keepax 	u32 error;
286565ace46SCharles Keepax 	u32 irq_count;
287565ace46SCharles Keepax 	int read_index;
288565ace46SCharles Keepax 	int avail;
2892cd19bdbSCharles Keepax };
2902cd19bdbSCharles Keepax 
291406abc95SCharles Keepax struct wm_adsp_compr {
292406abc95SCharles Keepax 	struct wm_adsp *dsp;
29395fe9597SCharles Keepax 	struct wm_adsp_compr_buf *buf;
294406abc95SCharles Keepax 
295406abc95SCharles Keepax 	struct snd_compr_stream *stream;
296406abc95SCharles Keepax 	struct snd_compressed_buffer size;
297565ace46SCharles Keepax 
29883a40ce9SCharles Keepax 	u32 *raw_buf;
299565ace46SCharles Keepax 	unsigned int copied_total;
300da2b3358SCharles Keepax 
301da2b3358SCharles Keepax 	unsigned int sample_rate;
302406abc95SCharles Keepax };
303406abc95SCharles Keepax 
304406abc95SCharles Keepax #define WM_ADSP_DATA_WORD_SIZE         3
305406abc95SCharles Keepax 
306406abc95SCharles Keepax #define WM_ADSP_MIN_FRAGMENTS          1
307406abc95SCharles Keepax #define WM_ADSP_MAX_FRAGMENTS          256
308406abc95SCharles Keepax #define WM_ADSP_MIN_FRAGMENT_SIZE      (64 * WM_ADSP_DATA_WORD_SIZE)
309406abc95SCharles Keepax #define WM_ADSP_MAX_FRAGMENT_SIZE      (4096 * WM_ADSP_DATA_WORD_SIZE)
310406abc95SCharles Keepax 
3112cd19bdbSCharles Keepax #define WM_ADSP_ALG_XM_STRUCT_MAGIC    0x49aec7
3122cd19bdbSCharles Keepax 
3132cd19bdbSCharles Keepax #define HOST_BUFFER_FIELD(field) \
3142cd19bdbSCharles Keepax 	(offsetof(struct wm_adsp_buffer, field) / sizeof(__be32))
3152cd19bdbSCharles Keepax 
3162cd19bdbSCharles Keepax #define ALG_XM_FIELD(field) \
3172cd19bdbSCharles Keepax 	(offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32))
3182cd19bdbSCharles Keepax 
3192cd19bdbSCharles Keepax static int wm_adsp_buffer_init(struct wm_adsp *dsp);
3202cd19bdbSCharles Keepax static int wm_adsp_buffer_free(struct wm_adsp *dsp);
3212cd19bdbSCharles Keepax 
3222cd19bdbSCharles Keepax struct wm_adsp_buffer_region {
3232cd19bdbSCharles Keepax 	unsigned int offset;
3242cd19bdbSCharles Keepax 	unsigned int cumulative_size;
3252cd19bdbSCharles Keepax 	unsigned int mem_type;
3262cd19bdbSCharles Keepax 	unsigned int base_addr;
3272cd19bdbSCharles Keepax };
3282cd19bdbSCharles Keepax 
3292cd19bdbSCharles Keepax struct wm_adsp_buffer_region_def {
3302cd19bdbSCharles Keepax 	unsigned int mem_type;
3312cd19bdbSCharles Keepax 	unsigned int base_offset;
3322cd19bdbSCharles Keepax 	unsigned int size_offset;
3332cd19bdbSCharles Keepax };
3342cd19bdbSCharles Keepax 
3353a9686c4SCharles Keepax static const struct wm_adsp_buffer_region_def default_regions[] = {
3362cd19bdbSCharles Keepax 	{
3372cd19bdbSCharles Keepax 		.mem_type = WMFW_ADSP2_XM,
3382cd19bdbSCharles Keepax 		.base_offset = HOST_BUFFER_FIELD(X_buf_base),
3392cd19bdbSCharles Keepax 		.size_offset = HOST_BUFFER_FIELD(X_buf_size),
3402cd19bdbSCharles Keepax 	},
3412cd19bdbSCharles Keepax 	{
3422cd19bdbSCharles Keepax 		.mem_type = WMFW_ADSP2_XM,
3432cd19bdbSCharles Keepax 		.base_offset = HOST_BUFFER_FIELD(X_buf_base2),
3442cd19bdbSCharles Keepax 		.size_offset = HOST_BUFFER_FIELD(X_buf_brk),
3452cd19bdbSCharles Keepax 	},
3462cd19bdbSCharles Keepax 	{
3472cd19bdbSCharles Keepax 		.mem_type = WMFW_ADSP2_YM,
3482cd19bdbSCharles Keepax 		.base_offset = HOST_BUFFER_FIELD(Y_buf_base),
3492cd19bdbSCharles Keepax 		.size_offset = HOST_BUFFER_FIELD(wrap),
3502cd19bdbSCharles Keepax 	},
3512cd19bdbSCharles Keepax };
3522cd19bdbSCharles Keepax 
353406abc95SCharles Keepax struct wm_adsp_fw_caps {
354406abc95SCharles Keepax 	u32 id;
355406abc95SCharles Keepax 	struct snd_codec_desc desc;
3562cd19bdbSCharles Keepax 	int num_regions;
3573a9686c4SCharles Keepax 	const struct wm_adsp_buffer_region_def *region_defs;
358406abc95SCharles Keepax };
359406abc95SCharles Keepax 
360e6d00f34SCharles Keepax static const struct wm_adsp_fw_caps ctrl_caps[] = {
361406abc95SCharles Keepax 	{
362406abc95SCharles Keepax 		.id = SND_AUDIOCODEC_BESPOKE,
363406abc95SCharles Keepax 		.desc = {
364406abc95SCharles Keepax 			.max_ch = 1,
365406abc95SCharles Keepax 			.sample_rates = { 16000 },
366406abc95SCharles Keepax 			.num_sample_rates = 1,
367406abc95SCharles Keepax 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
368406abc95SCharles Keepax 		},
369e6d00f34SCharles Keepax 		.num_regions = ARRAY_SIZE(default_regions),
370e6d00f34SCharles Keepax 		.region_defs = default_regions,
371406abc95SCharles Keepax 	},
372406abc95SCharles Keepax };
373406abc95SCharles Keepax 
3747ce4283cSCharles Keepax static const struct wm_adsp_fw_caps trace_caps[] = {
3757ce4283cSCharles Keepax 	{
3767ce4283cSCharles Keepax 		.id = SND_AUDIOCODEC_BESPOKE,
3777ce4283cSCharles Keepax 		.desc = {
3787ce4283cSCharles Keepax 			.max_ch = 8,
3797ce4283cSCharles Keepax 			.sample_rates = {
3807ce4283cSCharles Keepax 				4000, 8000, 11025, 12000, 16000, 22050,
3817ce4283cSCharles Keepax 				24000, 32000, 44100, 48000, 64000, 88200,
3827ce4283cSCharles Keepax 				96000, 176400, 192000
3837ce4283cSCharles Keepax 			},
3847ce4283cSCharles Keepax 			.num_sample_rates = 15,
3857ce4283cSCharles Keepax 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
3867ce4283cSCharles Keepax 		},
3877ce4283cSCharles Keepax 		.num_regions = ARRAY_SIZE(default_regions),
3887ce4283cSCharles Keepax 		.region_defs = default_regions,
389406abc95SCharles Keepax 	},
390406abc95SCharles Keepax };
391406abc95SCharles Keepax 
392406abc95SCharles Keepax static const struct {
3931023dbd9SMark Brown 	const char *file;
394406abc95SCharles Keepax 	int compr_direction;
395406abc95SCharles Keepax 	int num_caps;
396406abc95SCharles Keepax 	const struct wm_adsp_fw_caps *caps;
3971023dbd9SMark Brown } wm_adsp_fw[WM_ADSP_NUM_FW] = {
398dd84f925SMark Brown 	[WM_ADSP_FW_MBC_VSS] =  { .file = "mbc-vss" },
39904d1300fSCharles Keepax 	[WM_ADSP_FW_HIFI] =     { .file = "hifi" },
400dd84f925SMark Brown 	[WM_ADSP_FW_TX] =       { .file = "tx" },
401dd84f925SMark Brown 	[WM_ADSP_FW_TX_SPK] =   { .file = "tx-spk" },
40204d1300fSCharles Keepax 	[WM_ADSP_FW_RX] =       { .file = "rx" },
403dd84f925SMark Brown 	[WM_ADSP_FW_RX_ANC] =   { .file = "rx-anc" },
404406abc95SCharles Keepax 	[WM_ADSP_FW_CTRL] =     {
405406abc95SCharles Keepax 		.file = "ctrl",
406406abc95SCharles Keepax 		.compr_direction = SND_COMPRESS_CAPTURE,
407e6d00f34SCharles Keepax 		.num_caps = ARRAY_SIZE(ctrl_caps),
408e6d00f34SCharles Keepax 		.caps = ctrl_caps,
409406abc95SCharles Keepax 	},
41004d1300fSCharles Keepax 	[WM_ADSP_FW_ASR] =      { .file = "asr" },
4117ce4283cSCharles Keepax 	[WM_ADSP_FW_TRACE] =    {
4127ce4283cSCharles Keepax 		.file = "trace",
4137ce4283cSCharles Keepax 		.compr_direction = SND_COMPRESS_CAPTURE,
4147ce4283cSCharles Keepax 		.num_caps = ARRAY_SIZE(trace_caps),
4157ce4283cSCharles Keepax 		.caps = trace_caps,
4167ce4283cSCharles Keepax 	},
41704d1300fSCharles Keepax 	[WM_ADSP_FW_SPK_PROT] = { .file = "spk-prot" },
41804d1300fSCharles Keepax 	[WM_ADSP_FW_MISC] =     { .file = "misc" },
4191023dbd9SMark Brown };
4201023dbd9SMark Brown 
4216ab2b7b4SDimitris Papastamos struct wm_coeff_ctl_ops {
4226ab2b7b4SDimitris Papastamos 	int (*xget)(struct snd_kcontrol *kcontrol,
4236ab2b7b4SDimitris Papastamos 		    struct snd_ctl_elem_value *ucontrol);
4246ab2b7b4SDimitris Papastamos 	int (*xput)(struct snd_kcontrol *kcontrol,
4256ab2b7b4SDimitris Papastamos 		    struct snd_ctl_elem_value *ucontrol);
4266ab2b7b4SDimitris Papastamos 	int (*xinfo)(struct snd_kcontrol *kcontrol,
4276ab2b7b4SDimitris Papastamos 		     struct snd_ctl_elem_info *uinfo);
4286ab2b7b4SDimitris Papastamos };
4296ab2b7b4SDimitris Papastamos 
4306ab2b7b4SDimitris Papastamos struct wm_coeff_ctl {
4316ab2b7b4SDimitris Papastamos 	const char *name;
4322323736dSCharles Keepax 	const char *fw_name;
4333809f001SCharles Keepax 	struct wm_adsp_alg_region alg_region;
4346ab2b7b4SDimitris Papastamos 	struct wm_coeff_ctl_ops ops;
4353809f001SCharles Keepax 	struct wm_adsp *dsp;
4366ab2b7b4SDimitris Papastamos 	unsigned int enabled:1;
4376ab2b7b4SDimitris Papastamos 	struct list_head list;
4386ab2b7b4SDimitris Papastamos 	void *cache;
4392323736dSCharles Keepax 	unsigned int offset;
4406ab2b7b4SDimitris Papastamos 	size_t len;
4410c2e3f34SDimitris Papastamos 	unsigned int set:1;
4426ab2b7b4SDimitris Papastamos 	struct snd_kcontrol *kcontrol;
4439ee78757SCharles Keepax 	struct soc_bytes_ext bytes_ext;
44426c22a19SCharles Keepax 	unsigned int flags;
4456ab2b7b4SDimitris Papastamos };
4466ab2b7b4SDimitris Papastamos 
447f9f55e31SRichard Fitzgerald #ifdef CONFIG_DEBUG_FS
448f9f55e31SRichard Fitzgerald static void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, const char *s)
449f9f55e31SRichard Fitzgerald {
450f9f55e31SRichard Fitzgerald 	char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
451f9f55e31SRichard Fitzgerald 
452f9f55e31SRichard Fitzgerald 	kfree(dsp->wmfw_file_name);
453f9f55e31SRichard Fitzgerald 	dsp->wmfw_file_name = tmp;
454f9f55e31SRichard Fitzgerald }
455f9f55e31SRichard Fitzgerald 
456f9f55e31SRichard Fitzgerald static void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, const char *s)
457f9f55e31SRichard Fitzgerald {
458f9f55e31SRichard Fitzgerald 	char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
459f9f55e31SRichard Fitzgerald 
460f9f55e31SRichard Fitzgerald 	kfree(dsp->bin_file_name);
461f9f55e31SRichard Fitzgerald 	dsp->bin_file_name = tmp;
462f9f55e31SRichard Fitzgerald }
463f9f55e31SRichard Fitzgerald 
464f9f55e31SRichard Fitzgerald static void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
465f9f55e31SRichard Fitzgerald {
466f9f55e31SRichard Fitzgerald 	kfree(dsp->wmfw_file_name);
467f9f55e31SRichard Fitzgerald 	kfree(dsp->bin_file_name);
468f9f55e31SRichard Fitzgerald 	dsp->wmfw_file_name = NULL;
469f9f55e31SRichard Fitzgerald 	dsp->bin_file_name = NULL;
470f9f55e31SRichard Fitzgerald }
471f9f55e31SRichard Fitzgerald 
472f9f55e31SRichard Fitzgerald static ssize_t wm_adsp_debugfs_wmfw_read(struct file *file,
473f9f55e31SRichard Fitzgerald 					 char __user *user_buf,
474f9f55e31SRichard Fitzgerald 					 size_t count, loff_t *ppos)
475f9f55e31SRichard Fitzgerald {
476f9f55e31SRichard Fitzgerald 	struct wm_adsp *dsp = file->private_data;
477f9f55e31SRichard Fitzgerald 	ssize_t ret;
478f9f55e31SRichard Fitzgerald 
479078e7183SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
480f9f55e31SRichard Fitzgerald 
481f9f55e31SRichard Fitzgerald 	if (!dsp->wmfw_file_name || !dsp->running)
482f9f55e31SRichard Fitzgerald 		ret = 0;
483f9f55e31SRichard Fitzgerald 	else
484f9f55e31SRichard Fitzgerald 		ret = simple_read_from_buffer(user_buf, count, ppos,
485f9f55e31SRichard Fitzgerald 					      dsp->wmfw_file_name,
486f9f55e31SRichard Fitzgerald 					      strlen(dsp->wmfw_file_name));
487f9f55e31SRichard Fitzgerald 
488078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
489f9f55e31SRichard Fitzgerald 	return ret;
490f9f55e31SRichard Fitzgerald }
491f9f55e31SRichard Fitzgerald 
492f9f55e31SRichard Fitzgerald static ssize_t wm_adsp_debugfs_bin_read(struct file *file,
493f9f55e31SRichard Fitzgerald 					char __user *user_buf,
494f9f55e31SRichard Fitzgerald 					size_t count, loff_t *ppos)
495f9f55e31SRichard Fitzgerald {
496f9f55e31SRichard Fitzgerald 	struct wm_adsp *dsp = file->private_data;
497f9f55e31SRichard Fitzgerald 	ssize_t ret;
498f9f55e31SRichard Fitzgerald 
499078e7183SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
500f9f55e31SRichard Fitzgerald 
501f9f55e31SRichard Fitzgerald 	if (!dsp->bin_file_name || !dsp->running)
502f9f55e31SRichard Fitzgerald 		ret = 0;
503f9f55e31SRichard Fitzgerald 	else
504f9f55e31SRichard Fitzgerald 		ret = simple_read_from_buffer(user_buf, count, ppos,
505f9f55e31SRichard Fitzgerald 					      dsp->bin_file_name,
506f9f55e31SRichard Fitzgerald 					      strlen(dsp->bin_file_name));
507f9f55e31SRichard Fitzgerald 
508078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
509f9f55e31SRichard Fitzgerald 	return ret;
510f9f55e31SRichard Fitzgerald }
511f9f55e31SRichard Fitzgerald 
512f9f55e31SRichard Fitzgerald static const struct {
513f9f55e31SRichard Fitzgerald 	const char *name;
514f9f55e31SRichard Fitzgerald 	const struct file_operations fops;
515f9f55e31SRichard Fitzgerald } wm_adsp_debugfs_fops[] = {
516f9f55e31SRichard Fitzgerald 	{
517f9f55e31SRichard Fitzgerald 		.name = "wmfw_file_name",
518f9f55e31SRichard Fitzgerald 		.fops = {
519f9f55e31SRichard Fitzgerald 			.open = simple_open,
520f9f55e31SRichard Fitzgerald 			.read = wm_adsp_debugfs_wmfw_read,
521f9f55e31SRichard Fitzgerald 		},
522f9f55e31SRichard Fitzgerald 	},
523f9f55e31SRichard Fitzgerald 	{
524f9f55e31SRichard Fitzgerald 		.name = "bin_file_name",
525f9f55e31SRichard Fitzgerald 		.fops = {
526f9f55e31SRichard Fitzgerald 			.open = simple_open,
527f9f55e31SRichard Fitzgerald 			.read = wm_adsp_debugfs_bin_read,
528f9f55e31SRichard Fitzgerald 		},
529f9f55e31SRichard Fitzgerald 	},
530f9f55e31SRichard Fitzgerald };
531f9f55e31SRichard Fitzgerald 
532f9f55e31SRichard Fitzgerald static void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
533f9f55e31SRichard Fitzgerald 				  struct snd_soc_codec *codec)
534f9f55e31SRichard Fitzgerald {
535f9f55e31SRichard Fitzgerald 	struct dentry *root = NULL;
536f9f55e31SRichard Fitzgerald 	char *root_name;
537f9f55e31SRichard Fitzgerald 	int i;
538f9f55e31SRichard Fitzgerald 
539f9f55e31SRichard Fitzgerald 	if (!codec->component.debugfs_root) {
540f9f55e31SRichard Fitzgerald 		adsp_err(dsp, "No codec debugfs root\n");
541f9f55e31SRichard Fitzgerald 		goto err;
542f9f55e31SRichard Fitzgerald 	}
543f9f55e31SRichard Fitzgerald 
544f9f55e31SRichard Fitzgerald 	root_name = kmalloc(PAGE_SIZE, GFP_KERNEL);
545f9f55e31SRichard Fitzgerald 	if (!root_name)
546f9f55e31SRichard Fitzgerald 		goto err;
547f9f55e31SRichard Fitzgerald 
548f9f55e31SRichard Fitzgerald 	snprintf(root_name, PAGE_SIZE, "dsp%d", dsp->num);
549f9f55e31SRichard Fitzgerald 	root = debugfs_create_dir(root_name, codec->component.debugfs_root);
550f9f55e31SRichard Fitzgerald 	kfree(root_name);
551f9f55e31SRichard Fitzgerald 
552f9f55e31SRichard Fitzgerald 	if (!root)
553f9f55e31SRichard Fitzgerald 		goto err;
554f9f55e31SRichard Fitzgerald 
555f9f55e31SRichard Fitzgerald 	if (!debugfs_create_bool("running", S_IRUGO, root, &dsp->running))
556f9f55e31SRichard Fitzgerald 		goto err;
557f9f55e31SRichard Fitzgerald 
558f9f55e31SRichard Fitzgerald 	if (!debugfs_create_x32("fw_id", S_IRUGO, root, &dsp->fw_id))
559f9f55e31SRichard Fitzgerald 		goto err;
560f9f55e31SRichard Fitzgerald 
561f9f55e31SRichard Fitzgerald 	if (!debugfs_create_x32("fw_version", S_IRUGO, root,
562f9f55e31SRichard Fitzgerald 				&dsp->fw_id_version))
563f9f55e31SRichard Fitzgerald 		goto err;
564f9f55e31SRichard Fitzgerald 
565f9f55e31SRichard Fitzgerald 	for (i = 0; i < ARRAY_SIZE(wm_adsp_debugfs_fops); ++i) {
566f9f55e31SRichard Fitzgerald 		if (!debugfs_create_file(wm_adsp_debugfs_fops[i].name,
567f9f55e31SRichard Fitzgerald 					 S_IRUGO, root, dsp,
568f9f55e31SRichard Fitzgerald 					 &wm_adsp_debugfs_fops[i].fops))
569f9f55e31SRichard Fitzgerald 			goto err;
570f9f55e31SRichard Fitzgerald 	}
571f9f55e31SRichard Fitzgerald 
572f9f55e31SRichard Fitzgerald 	dsp->debugfs_root = root;
573f9f55e31SRichard Fitzgerald 	return;
574f9f55e31SRichard Fitzgerald 
575f9f55e31SRichard Fitzgerald err:
576f9f55e31SRichard Fitzgerald 	debugfs_remove_recursive(root);
577f9f55e31SRichard Fitzgerald 	adsp_err(dsp, "Failed to create debugfs\n");
578f9f55e31SRichard Fitzgerald }
579f9f55e31SRichard Fitzgerald 
580f9f55e31SRichard Fitzgerald static void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
581f9f55e31SRichard Fitzgerald {
582f9f55e31SRichard Fitzgerald 	wm_adsp_debugfs_clear(dsp);
583f9f55e31SRichard Fitzgerald 	debugfs_remove_recursive(dsp->debugfs_root);
584f9f55e31SRichard Fitzgerald }
585f9f55e31SRichard Fitzgerald #else
586f9f55e31SRichard Fitzgerald static inline void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
587f9f55e31SRichard Fitzgerald 					 struct snd_soc_codec *codec)
588f9f55e31SRichard Fitzgerald {
589f9f55e31SRichard Fitzgerald }
590f9f55e31SRichard Fitzgerald 
591f9f55e31SRichard Fitzgerald static inline void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
592f9f55e31SRichard Fitzgerald {
593f9f55e31SRichard Fitzgerald }
594f9f55e31SRichard Fitzgerald 
595f9f55e31SRichard Fitzgerald static inline void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp,
596f9f55e31SRichard Fitzgerald 						 const char *s)
597f9f55e31SRichard Fitzgerald {
598f9f55e31SRichard Fitzgerald }
599f9f55e31SRichard Fitzgerald 
600f9f55e31SRichard Fitzgerald static inline void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp,
601f9f55e31SRichard Fitzgerald 						const char *s)
602f9f55e31SRichard Fitzgerald {
603f9f55e31SRichard Fitzgerald }
604f9f55e31SRichard Fitzgerald 
605f9f55e31SRichard Fitzgerald static inline void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
606f9f55e31SRichard Fitzgerald {
607f9f55e31SRichard Fitzgerald }
608f9f55e31SRichard Fitzgerald #endif
609f9f55e31SRichard Fitzgerald 
6101023dbd9SMark Brown static int wm_adsp_fw_get(struct snd_kcontrol *kcontrol,
6111023dbd9SMark Brown 			  struct snd_ctl_elem_value *ucontrol)
6121023dbd9SMark Brown {
613ea53bf77SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
6141023dbd9SMark Brown 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
6153809f001SCharles Keepax 	struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
6161023dbd9SMark Brown 
61715c66570STakashi Iwai 	ucontrol->value.enumerated.item[0] = dsp[e->shift_l].fw;
6181023dbd9SMark Brown 
6191023dbd9SMark Brown 	return 0;
6201023dbd9SMark Brown }
6211023dbd9SMark Brown 
6221023dbd9SMark Brown static int wm_adsp_fw_put(struct snd_kcontrol *kcontrol,
6231023dbd9SMark Brown 			  struct snd_ctl_elem_value *ucontrol)
6241023dbd9SMark Brown {
625ea53bf77SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
6261023dbd9SMark Brown 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
6273809f001SCharles Keepax 	struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
628d27c5e15SCharles Keepax 	int ret = 0;
6291023dbd9SMark Brown 
63015c66570STakashi Iwai 	if (ucontrol->value.enumerated.item[0] == dsp[e->shift_l].fw)
6311023dbd9SMark Brown 		return 0;
6321023dbd9SMark Brown 
63315c66570STakashi Iwai 	if (ucontrol->value.enumerated.item[0] >= WM_ADSP_NUM_FW)
6341023dbd9SMark Brown 		return -EINVAL;
6351023dbd9SMark Brown 
636d27c5e15SCharles Keepax 	mutex_lock(&dsp[e->shift_l].pwr_lock);
6371023dbd9SMark Brown 
638406abc95SCharles Keepax 	if (dsp[e->shift_l].running || dsp[e->shift_l].compr)
639d27c5e15SCharles Keepax 		ret = -EBUSY;
640d27c5e15SCharles Keepax 	else
64115c66570STakashi Iwai 		dsp[e->shift_l].fw = ucontrol->value.enumerated.item[0];
6421023dbd9SMark Brown 
643d27c5e15SCharles Keepax 	mutex_unlock(&dsp[e->shift_l].pwr_lock);
644d27c5e15SCharles Keepax 
645d27c5e15SCharles Keepax 	return ret;
6461023dbd9SMark Brown }
6471023dbd9SMark Brown 
6481023dbd9SMark Brown static const struct soc_enum wm_adsp_fw_enum[] = {
6491023dbd9SMark Brown 	SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
6501023dbd9SMark Brown 	SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
6511023dbd9SMark Brown 	SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
6521023dbd9SMark Brown 	SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
6531023dbd9SMark Brown };
6541023dbd9SMark Brown 
655336d0442SRichard Fitzgerald const struct snd_kcontrol_new wm_adsp_fw_controls[] = {
6561023dbd9SMark Brown 	SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum[0],
6571023dbd9SMark Brown 		     wm_adsp_fw_get, wm_adsp_fw_put),
6581023dbd9SMark Brown 	SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum[1],
6591023dbd9SMark Brown 		     wm_adsp_fw_get, wm_adsp_fw_put),
6601023dbd9SMark Brown 	SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum[2],
6611023dbd9SMark Brown 		     wm_adsp_fw_get, wm_adsp_fw_put),
6621023dbd9SMark Brown 	SOC_ENUM_EXT("DSP4 Firmware", wm_adsp_fw_enum[3],
6631023dbd9SMark Brown 		     wm_adsp_fw_get, wm_adsp_fw_put),
6641023dbd9SMark Brown };
665336d0442SRichard Fitzgerald EXPORT_SYMBOL_GPL(wm_adsp_fw_controls);
6662159ad93SMark Brown 
6672159ad93SMark Brown static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp,
6682159ad93SMark Brown 							int type)
6692159ad93SMark Brown {
6702159ad93SMark Brown 	int i;
6712159ad93SMark Brown 
6722159ad93SMark Brown 	for (i = 0; i < dsp->num_mems; i++)
6732159ad93SMark Brown 		if (dsp->mem[i].type == type)
6742159ad93SMark Brown 			return &dsp->mem[i];
6752159ad93SMark Brown 
6762159ad93SMark Brown 	return NULL;
6772159ad93SMark Brown }
6782159ad93SMark Brown 
6793809f001SCharles Keepax static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *mem,
68045b9ee72SMark Brown 					  unsigned int offset)
68145b9ee72SMark Brown {
6823809f001SCharles Keepax 	if (WARN_ON(!mem))
6836c452bdaSTakashi Iwai 		return offset;
6843809f001SCharles Keepax 	switch (mem->type) {
68545b9ee72SMark Brown 	case WMFW_ADSP1_PM:
6863809f001SCharles Keepax 		return mem->base + (offset * 3);
68745b9ee72SMark Brown 	case WMFW_ADSP1_DM:
6883809f001SCharles Keepax 		return mem->base + (offset * 2);
68945b9ee72SMark Brown 	case WMFW_ADSP2_XM:
6903809f001SCharles Keepax 		return mem->base + (offset * 2);
69145b9ee72SMark Brown 	case WMFW_ADSP2_YM:
6923809f001SCharles Keepax 		return mem->base + (offset * 2);
69345b9ee72SMark Brown 	case WMFW_ADSP1_ZM:
6943809f001SCharles Keepax 		return mem->base + (offset * 2);
69545b9ee72SMark Brown 	default:
6966c452bdaSTakashi Iwai 		WARN(1, "Unknown memory region type");
69745b9ee72SMark Brown 		return offset;
69845b9ee72SMark Brown 	}
69945b9ee72SMark Brown }
70045b9ee72SMark Brown 
70110337b07SRichard Fitzgerald static void wm_adsp2_show_fw_status(struct wm_adsp *dsp)
70210337b07SRichard Fitzgerald {
70310337b07SRichard Fitzgerald 	u16 scratch[4];
70410337b07SRichard Fitzgerald 	int ret;
70510337b07SRichard Fitzgerald 
70610337b07SRichard Fitzgerald 	ret = regmap_raw_read(dsp->regmap, dsp->base + ADSP2_SCRATCH0,
70710337b07SRichard Fitzgerald 				scratch, sizeof(scratch));
70810337b07SRichard Fitzgerald 	if (ret) {
70910337b07SRichard Fitzgerald 		adsp_err(dsp, "Failed to read SCRATCH regs: %d\n", ret);
71010337b07SRichard Fitzgerald 		return;
71110337b07SRichard Fitzgerald 	}
71210337b07SRichard Fitzgerald 
71310337b07SRichard Fitzgerald 	adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
71410337b07SRichard Fitzgerald 		 be16_to_cpu(scratch[0]),
71510337b07SRichard Fitzgerald 		 be16_to_cpu(scratch[1]),
71610337b07SRichard Fitzgerald 		 be16_to_cpu(scratch[2]),
71710337b07SRichard Fitzgerald 		 be16_to_cpu(scratch[3]));
71810337b07SRichard Fitzgerald }
71910337b07SRichard Fitzgerald 
7209ee78757SCharles Keepax static inline struct wm_coeff_ctl *bytes_ext_to_ctl(struct soc_bytes_ext *ext)
7219ee78757SCharles Keepax {
7229ee78757SCharles Keepax 	return container_of(ext, struct wm_coeff_ctl, bytes_ext);
7239ee78757SCharles Keepax }
7249ee78757SCharles Keepax 
7257585a5b0SCharles Keepax static int wm_coeff_info(struct snd_kcontrol *kctl,
7266ab2b7b4SDimitris Papastamos 			 struct snd_ctl_elem_info *uinfo)
7276ab2b7b4SDimitris Papastamos {
7289ee78757SCharles Keepax 	struct soc_bytes_ext *bytes_ext =
7299ee78757SCharles Keepax 		(struct soc_bytes_ext *)kctl->private_value;
7309ee78757SCharles Keepax 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
7316ab2b7b4SDimitris Papastamos 
7326ab2b7b4SDimitris Papastamos 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
7336ab2b7b4SDimitris Papastamos 	uinfo->count = ctl->len;
7346ab2b7b4SDimitris Papastamos 	return 0;
7356ab2b7b4SDimitris Papastamos }
7366ab2b7b4SDimitris Papastamos 
737c9f8dd71SCharles Keepax static int wm_coeff_write_control(struct wm_coeff_ctl *ctl,
7386ab2b7b4SDimitris Papastamos 				  const void *buf, size_t len)
7396ab2b7b4SDimitris Papastamos {
7403809f001SCharles Keepax 	struct wm_adsp_alg_region *alg_region = &ctl->alg_region;
7416ab2b7b4SDimitris Papastamos 	const struct wm_adsp_region *mem;
7423809f001SCharles Keepax 	struct wm_adsp *dsp = ctl->dsp;
7436ab2b7b4SDimitris Papastamos 	void *scratch;
7446ab2b7b4SDimitris Papastamos 	int ret;
7456ab2b7b4SDimitris Papastamos 	unsigned int reg;
7466ab2b7b4SDimitris Papastamos 
7473809f001SCharles Keepax 	mem = wm_adsp_find_region(dsp, alg_region->type);
7486ab2b7b4SDimitris Papastamos 	if (!mem) {
7493809f001SCharles Keepax 		adsp_err(dsp, "No base for region %x\n",
7503809f001SCharles Keepax 			 alg_region->type);
7516ab2b7b4SDimitris Papastamos 		return -EINVAL;
7526ab2b7b4SDimitris Papastamos 	}
7536ab2b7b4SDimitris Papastamos 
7542323736dSCharles Keepax 	reg = ctl->alg_region.base + ctl->offset;
7556ab2b7b4SDimitris Papastamos 	reg = wm_adsp_region_to_reg(mem, reg);
7566ab2b7b4SDimitris Papastamos 
7574f8ea6d7SCharles Keepax 	scratch = kmemdup(buf, len, GFP_KERNEL | GFP_DMA);
7586ab2b7b4SDimitris Papastamos 	if (!scratch)
7596ab2b7b4SDimitris Papastamos 		return -ENOMEM;
7606ab2b7b4SDimitris Papastamos 
7613809f001SCharles Keepax 	ret = regmap_raw_write(dsp->regmap, reg, scratch,
7624f8ea6d7SCharles Keepax 			       len);
7636ab2b7b4SDimitris Papastamos 	if (ret) {
7643809f001SCharles Keepax 		adsp_err(dsp, "Failed to write %zu bytes to %x: %d\n",
7654f8ea6d7SCharles Keepax 			 len, reg, ret);
7666ab2b7b4SDimitris Papastamos 		kfree(scratch);
7676ab2b7b4SDimitris Papastamos 		return ret;
7686ab2b7b4SDimitris Papastamos 	}
7694f8ea6d7SCharles Keepax 	adsp_dbg(dsp, "Wrote %zu bytes to %x\n", len, reg);
7706ab2b7b4SDimitris Papastamos 
7716ab2b7b4SDimitris Papastamos 	kfree(scratch);
7726ab2b7b4SDimitris Papastamos 
7736ab2b7b4SDimitris Papastamos 	return 0;
7746ab2b7b4SDimitris Papastamos }
7756ab2b7b4SDimitris Papastamos 
7767585a5b0SCharles Keepax static int wm_coeff_put(struct snd_kcontrol *kctl,
7776ab2b7b4SDimitris Papastamos 			struct snd_ctl_elem_value *ucontrol)
7786ab2b7b4SDimitris Papastamos {
7799ee78757SCharles Keepax 	struct soc_bytes_ext *bytes_ext =
7809ee78757SCharles Keepax 		(struct soc_bytes_ext *)kctl->private_value;
7819ee78757SCharles Keepax 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
7826ab2b7b4SDimitris Papastamos 	char *p = ucontrol->value.bytes.data;
783168d10e7SCharles Keepax 	int ret = 0;
784168d10e7SCharles Keepax 
785168d10e7SCharles Keepax 	mutex_lock(&ctl->dsp->pwr_lock);
7866ab2b7b4SDimitris Papastamos 
7876ab2b7b4SDimitris Papastamos 	memcpy(ctl->cache, p, ctl->len);
7886ab2b7b4SDimitris Papastamos 
7890c2e3f34SDimitris Papastamos 	ctl->set = 1;
790168d10e7SCharles Keepax 	if (ctl->enabled)
791168d10e7SCharles Keepax 		ret = wm_coeff_write_control(ctl, p, ctl->len);
7926ab2b7b4SDimitris Papastamos 
793168d10e7SCharles Keepax 	mutex_unlock(&ctl->dsp->pwr_lock);
794168d10e7SCharles Keepax 
795168d10e7SCharles Keepax 	return ret;
7966ab2b7b4SDimitris Papastamos }
7976ab2b7b4SDimitris Papastamos 
7989ee78757SCharles Keepax static int wm_coeff_tlv_put(struct snd_kcontrol *kctl,
7999ee78757SCharles Keepax 			    const unsigned int __user *bytes, unsigned int size)
8009ee78757SCharles Keepax {
8019ee78757SCharles Keepax 	struct soc_bytes_ext *bytes_ext =
8029ee78757SCharles Keepax 		(struct soc_bytes_ext *)kctl->private_value;
8039ee78757SCharles Keepax 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
8049ee78757SCharles Keepax 	int ret = 0;
8059ee78757SCharles Keepax 
8069ee78757SCharles Keepax 	mutex_lock(&ctl->dsp->pwr_lock);
8079ee78757SCharles Keepax 
8089ee78757SCharles Keepax 	if (copy_from_user(ctl->cache, bytes, size)) {
8099ee78757SCharles Keepax 		ret = -EFAULT;
8109ee78757SCharles Keepax 	} else {
8119ee78757SCharles Keepax 		ctl->set = 1;
8129ee78757SCharles Keepax 		if (ctl->enabled)
8139ee78757SCharles Keepax 			ret = wm_coeff_write_control(ctl, ctl->cache, size);
8149ee78757SCharles Keepax 	}
8159ee78757SCharles Keepax 
8169ee78757SCharles Keepax 	mutex_unlock(&ctl->dsp->pwr_lock);
8179ee78757SCharles Keepax 
8189ee78757SCharles Keepax 	return ret;
8199ee78757SCharles Keepax }
8209ee78757SCharles Keepax 
821c9f8dd71SCharles Keepax static int wm_coeff_read_control(struct wm_coeff_ctl *ctl,
8226ab2b7b4SDimitris Papastamos 				 void *buf, size_t len)
8236ab2b7b4SDimitris Papastamos {
8243809f001SCharles Keepax 	struct wm_adsp_alg_region *alg_region = &ctl->alg_region;
8256ab2b7b4SDimitris Papastamos 	const struct wm_adsp_region *mem;
8263809f001SCharles Keepax 	struct wm_adsp *dsp = ctl->dsp;
8276ab2b7b4SDimitris Papastamos 	void *scratch;
8286ab2b7b4SDimitris Papastamos 	int ret;
8296ab2b7b4SDimitris Papastamos 	unsigned int reg;
8306ab2b7b4SDimitris Papastamos 
8313809f001SCharles Keepax 	mem = wm_adsp_find_region(dsp, alg_region->type);
8326ab2b7b4SDimitris Papastamos 	if (!mem) {
8333809f001SCharles Keepax 		adsp_err(dsp, "No base for region %x\n",
8343809f001SCharles Keepax 			 alg_region->type);
8356ab2b7b4SDimitris Papastamos 		return -EINVAL;
8366ab2b7b4SDimitris Papastamos 	}
8376ab2b7b4SDimitris Papastamos 
8382323736dSCharles Keepax 	reg = ctl->alg_region.base + ctl->offset;
8396ab2b7b4SDimitris Papastamos 	reg = wm_adsp_region_to_reg(mem, reg);
8406ab2b7b4SDimitris Papastamos 
8414f8ea6d7SCharles Keepax 	scratch = kmalloc(len, GFP_KERNEL | GFP_DMA);
8426ab2b7b4SDimitris Papastamos 	if (!scratch)
8436ab2b7b4SDimitris Papastamos 		return -ENOMEM;
8446ab2b7b4SDimitris Papastamos 
8454f8ea6d7SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, reg, scratch, len);
8466ab2b7b4SDimitris Papastamos 	if (ret) {
8473809f001SCharles Keepax 		adsp_err(dsp, "Failed to read %zu bytes from %x: %d\n",
8485602a643SCharles Keepax 			 len, reg, ret);
8496ab2b7b4SDimitris Papastamos 		kfree(scratch);
8506ab2b7b4SDimitris Papastamos 		return ret;
8516ab2b7b4SDimitris Papastamos 	}
8524f8ea6d7SCharles Keepax 	adsp_dbg(dsp, "Read %zu bytes from %x\n", len, reg);
8536ab2b7b4SDimitris Papastamos 
8544f8ea6d7SCharles Keepax 	memcpy(buf, scratch, len);
8556ab2b7b4SDimitris Papastamos 	kfree(scratch);
8566ab2b7b4SDimitris Papastamos 
8576ab2b7b4SDimitris Papastamos 	return 0;
8586ab2b7b4SDimitris Papastamos }
8596ab2b7b4SDimitris Papastamos 
8607585a5b0SCharles Keepax static int wm_coeff_get(struct snd_kcontrol *kctl,
8616ab2b7b4SDimitris Papastamos 			struct snd_ctl_elem_value *ucontrol)
8626ab2b7b4SDimitris Papastamos {
8639ee78757SCharles Keepax 	struct soc_bytes_ext *bytes_ext =
8649ee78757SCharles Keepax 		(struct soc_bytes_ext *)kctl->private_value;
8659ee78757SCharles Keepax 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
8666ab2b7b4SDimitris Papastamos 	char *p = ucontrol->value.bytes.data;
867168d10e7SCharles Keepax 	int ret = 0;
868168d10e7SCharles Keepax 
869168d10e7SCharles Keepax 	mutex_lock(&ctl->dsp->pwr_lock);
8706ab2b7b4SDimitris Papastamos 
87126c22a19SCharles Keepax 	if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
87226c22a19SCharles Keepax 		if (ctl->enabled)
873168d10e7SCharles Keepax 			ret = wm_coeff_read_control(ctl, p, ctl->len);
87426c22a19SCharles Keepax 		else
875168d10e7SCharles Keepax 			ret = -EPERM;
876168d10e7SCharles Keepax 	} else {
877bc1765d6SCharles Keepax 		if (!ctl->flags && ctl->enabled)
878bc1765d6SCharles Keepax 			ret = wm_coeff_read_control(ctl, ctl->cache, ctl->len);
879bc1765d6SCharles Keepax 
880168d10e7SCharles Keepax 		memcpy(p, ctl->cache, ctl->len);
88126c22a19SCharles Keepax 	}
88226c22a19SCharles Keepax 
883168d10e7SCharles Keepax 	mutex_unlock(&ctl->dsp->pwr_lock);
88426c22a19SCharles Keepax 
885168d10e7SCharles Keepax 	return ret;
8866ab2b7b4SDimitris Papastamos }
8876ab2b7b4SDimitris Papastamos 
8889ee78757SCharles Keepax static int wm_coeff_tlv_get(struct snd_kcontrol *kctl,
8899ee78757SCharles Keepax 			    unsigned int __user *bytes, unsigned int size)
8909ee78757SCharles Keepax {
8919ee78757SCharles Keepax 	struct soc_bytes_ext *bytes_ext =
8929ee78757SCharles Keepax 		(struct soc_bytes_ext *)kctl->private_value;
8939ee78757SCharles Keepax 	struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext);
8949ee78757SCharles Keepax 	int ret = 0;
8959ee78757SCharles Keepax 
8969ee78757SCharles Keepax 	mutex_lock(&ctl->dsp->pwr_lock);
8979ee78757SCharles Keepax 
8989ee78757SCharles Keepax 	if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
8999ee78757SCharles Keepax 		if (ctl->enabled)
9009ee78757SCharles Keepax 			ret = wm_coeff_read_control(ctl, ctl->cache, size);
9019ee78757SCharles Keepax 		else
9029ee78757SCharles Keepax 			ret = -EPERM;
9039ee78757SCharles Keepax 	} else {
9049ee78757SCharles Keepax 		if (!ctl->flags && ctl->enabled)
9059ee78757SCharles Keepax 			ret = wm_coeff_read_control(ctl, ctl->cache, size);
9069ee78757SCharles Keepax 	}
9079ee78757SCharles Keepax 
9089ee78757SCharles Keepax 	if (!ret && copy_to_user(bytes, ctl->cache, size))
9099ee78757SCharles Keepax 		ret = -EFAULT;
9109ee78757SCharles Keepax 
9119ee78757SCharles Keepax 	mutex_unlock(&ctl->dsp->pwr_lock);
9129ee78757SCharles Keepax 
9139ee78757SCharles Keepax 	return ret;
9149ee78757SCharles Keepax }
9159ee78757SCharles Keepax 
9166ab2b7b4SDimitris Papastamos struct wmfw_ctl_work {
9173809f001SCharles Keepax 	struct wm_adsp *dsp;
9186ab2b7b4SDimitris Papastamos 	struct wm_coeff_ctl *ctl;
9196ab2b7b4SDimitris Papastamos 	struct work_struct work;
9206ab2b7b4SDimitris Papastamos };
9216ab2b7b4SDimitris Papastamos 
9229ee78757SCharles Keepax static unsigned int wmfw_convert_flags(unsigned int in, unsigned int len)
9239ee78757SCharles Keepax {
9249ee78757SCharles Keepax 	unsigned int out, rd, wr, vol;
9259ee78757SCharles Keepax 
9269ee78757SCharles Keepax 	if (len > ADSP_MAX_STD_CTRL_SIZE) {
9279ee78757SCharles Keepax 		rd = SNDRV_CTL_ELEM_ACCESS_TLV_READ;
9289ee78757SCharles Keepax 		wr = SNDRV_CTL_ELEM_ACCESS_TLV_WRITE;
9299ee78757SCharles Keepax 		vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
9309ee78757SCharles Keepax 
9319ee78757SCharles Keepax 		out = SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
9329ee78757SCharles Keepax 	} else {
9339ee78757SCharles Keepax 		rd = SNDRV_CTL_ELEM_ACCESS_READ;
9349ee78757SCharles Keepax 		wr = SNDRV_CTL_ELEM_ACCESS_WRITE;
9359ee78757SCharles Keepax 		vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
9369ee78757SCharles Keepax 
9379ee78757SCharles Keepax 		out = 0;
9389ee78757SCharles Keepax 	}
9399ee78757SCharles Keepax 
9409ee78757SCharles Keepax 	if (in) {
9419ee78757SCharles Keepax 		if (in & WMFW_CTL_FLAG_READABLE)
9429ee78757SCharles Keepax 			out |= rd;
9439ee78757SCharles Keepax 		if (in & WMFW_CTL_FLAG_WRITEABLE)
9449ee78757SCharles Keepax 			out |= wr;
9459ee78757SCharles Keepax 		if (in & WMFW_CTL_FLAG_VOLATILE)
9469ee78757SCharles Keepax 			out |= vol;
9479ee78757SCharles Keepax 	} else {
9489ee78757SCharles Keepax 		out |= rd | wr | vol;
9499ee78757SCharles Keepax 	}
9509ee78757SCharles Keepax 
9519ee78757SCharles Keepax 	return out;
9529ee78757SCharles Keepax }
9539ee78757SCharles Keepax 
9543809f001SCharles Keepax static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl)
9556ab2b7b4SDimitris Papastamos {
9566ab2b7b4SDimitris Papastamos 	struct snd_kcontrol_new *kcontrol;
9576ab2b7b4SDimitris Papastamos 	int ret;
9586ab2b7b4SDimitris Papastamos 
95992bb4c32SDimitris Papastamos 	if (!ctl || !ctl->name)
9606ab2b7b4SDimitris Papastamos 		return -EINVAL;
9616ab2b7b4SDimitris Papastamos 
9626ab2b7b4SDimitris Papastamos 	kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL);
9636ab2b7b4SDimitris Papastamos 	if (!kcontrol)
9646ab2b7b4SDimitris Papastamos 		return -ENOMEM;
9656ab2b7b4SDimitris Papastamos 	kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
9666ab2b7b4SDimitris Papastamos 
9676ab2b7b4SDimitris Papastamos 	kcontrol->name = ctl->name;
9686ab2b7b4SDimitris Papastamos 	kcontrol->info = wm_coeff_info;
9696ab2b7b4SDimitris Papastamos 	kcontrol->get = wm_coeff_get;
9706ab2b7b4SDimitris Papastamos 	kcontrol->put = wm_coeff_put;
9719ee78757SCharles Keepax 	kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
9729ee78757SCharles Keepax 	kcontrol->tlv.c = snd_soc_bytes_tlv_callback;
9739ee78757SCharles Keepax 	kcontrol->private_value = (unsigned long)&ctl->bytes_ext;
9746ab2b7b4SDimitris Papastamos 
9759ee78757SCharles Keepax 	ctl->bytes_ext.max = ctl->len;
9769ee78757SCharles Keepax 	ctl->bytes_ext.get = wm_coeff_tlv_get;
9779ee78757SCharles Keepax 	ctl->bytes_ext.put = wm_coeff_tlv_put;
9789ee78757SCharles Keepax 
9799ee78757SCharles Keepax 	kcontrol->access = wmfw_convert_flags(ctl->flags, ctl->len);
98026c22a19SCharles Keepax 
9817d00cd97SCharles Keepax 	ret = snd_soc_add_card_controls(dsp->card, kcontrol, 1);
9826ab2b7b4SDimitris Papastamos 	if (ret < 0)
9836ab2b7b4SDimitris Papastamos 		goto err_kcontrol;
9846ab2b7b4SDimitris Papastamos 
9856ab2b7b4SDimitris Papastamos 	kfree(kcontrol);
9866ab2b7b4SDimitris Papastamos 
9877d00cd97SCharles Keepax 	ctl->kcontrol = snd_soc_card_get_kcontrol(dsp->card, ctl->name);
98881ad93ecSDimitris Papastamos 
9896ab2b7b4SDimitris Papastamos 	return 0;
9906ab2b7b4SDimitris Papastamos 
9916ab2b7b4SDimitris Papastamos err_kcontrol:
9926ab2b7b4SDimitris Papastamos 	kfree(kcontrol);
9936ab2b7b4SDimitris Papastamos 	return ret;
9946ab2b7b4SDimitris Papastamos }
9956ab2b7b4SDimitris Papastamos 
996b21acc1cSCharles Keepax static int wm_coeff_init_control_caches(struct wm_adsp *dsp)
997b21acc1cSCharles Keepax {
998b21acc1cSCharles Keepax 	struct wm_coeff_ctl *ctl;
999b21acc1cSCharles Keepax 	int ret;
1000b21acc1cSCharles Keepax 
1001b21acc1cSCharles Keepax 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
1002b21acc1cSCharles Keepax 		if (!ctl->enabled || ctl->set)
1003b21acc1cSCharles Keepax 			continue;
100426c22a19SCharles Keepax 		if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
100526c22a19SCharles Keepax 			continue;
100626c22a19SCharles Keepax 
10077d00cd97SCharles Keepax 		ret = wm_coeff_read_control(ctl, ctl->cache, ctl->len);
1008b21acc1cSCharles Keepax 		if (ret < 0)
1009b21acc1cSCharles Keepax 			return ret;
1010b21acc1cSCharles Keepax 	}
1011b21acc1cSCharles Keepax 
1012b21acc1cSCharles Keepax 	return 0;
1013b21acc1cSCharles Keepax }
1014b21acc1cSCharles Keepax 
1015b21acc1cSCharles Keepax static int wm_coeff_sync_controls(struct wm_adsp *dsp)
1016b21acc1cSCharles Keepax {
1017b21acc1cSCharles Keepax 	struct wm_coeff_ctl *ctl;
1018b21acc1cSCharles Keepax 	int ret;
1019b21acc1cSCharles Keepax 
1020b21acc1cSCharles Keepax 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
1021b21acc1cSCharles Keepax 		if (!ctl->enabled)
1022b21acc1cSCharles Keepax 			continue;
102326c22a19SCharles Keepax 		if (ctl->set && !(ctl->flags & WMFW_CTL_FLAG_VOLATILE)) {
10247d00cd97SCharles Keepax 			ret = wm_coeff_write_control(ctl, ctl->cache, ctl->len);
1025b21acc1cSCharles Keepax 			if (ret < 0)
1026b21acc1cSCharles Keepax 				return ret;
1027b21acc1cSCharles Keepax 		}
1028b21acc1cSCharles Keepax 	}
1029b21acc1cSCharles Keepax 
1030b21acc1cSCharles Keepax 	return 0;
1031b21acc1cSCharles Keepax }
1032b21acc1cSCharles Keepax 
1033b21acc1cSCharles Keepax static void wm_adsp_ctl_work(struct work_struct *work)
1034b21acc1cSCharles Keepax {
1035b21acc1cSCharles Keepax 	struct wmfw_ctl_work *ctl_work = container_of(work,
1036b21acc1cSCharles Keepax 						      struct wmfw_ctl_work,
1037b21acc1cSCharles Keepax 						      work);
1038b21acc1cSCharles Keepax 
1039b21acc1cSCharles Keepax 	wmfw_add_ctl(ctl_work->dsp, ctl_work->ctl);
1040b21acc1cSCharles Keepax 	kfree(ctl_work);
1041b21acc1cSCharles Keepax }
1042b21acc1cSCharles Keepax 
104366225e98SRichard Fitzgerald static void wm_adsp_free_ctl_blk(struct wm_coeff_ctl *ctl)
104466225e98SRichard Fitzgerald {
104566225e98SRichard Fitzgerald 	kfree(ctl->cache);
104666225e98SRichard Fitzgerald 	kfree(ctl->name);
104766225e98SRichard Fitzgerald 	kfree(ctl);
104866225e98SRichard Fitzgerald }
104966225e98SRichard Fitzgerald 
1050b21acc1cSCharles Keepax static int wm_adsp_create_control(struct wm_adsp *dsp,
1051b21acc1cSCharles Keepax 				  const struct wm_adsp_alg_region *alg_region,
10522323736dSCharles Keepax 				  unsigned int offset, unsigned int len,
105326c22a19SCharles Keepax 				  const char *subname, unsigned int subname_len,
105426c22a19SCharles Keepax 				  unsigned int flags)
1055b21acc1cSCharles Keepax {
1056b21acc1cSCharles Keepax 	struct wm_coeff_ctl *ctl;
1057b21acc1cSCharles Keepax 	struct wmfw_ctl_work *ctl_work;
1058b21acc1cSCharles Keepax 	char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1059b21acc1cSCharles Keepax 	char *region_name;
1060b21acc1cSCharles Keepax 	int ret;
1061b21acc1cSCharles Keepax 
106226c22a19SCharles Keepax 	if (flags & WMFW_CTL_FLAG_SYS)
106326c22a19SCharles Keepax 		return 0;
106426c22a19SCharles Keepax 
1065b21acc1cSCharles Keepax 	switch (alg_region->type) {
1066b21acc1cSCharles Keepax 	case WMFW_ADSP1_PM:
1067b21acc1cSCharles Keepax 		region_name = "PM";
1068b21acc1cSCharles Keepax 		break;
1069b21acc1cSCharles Keepax 	case WMFW_ADSP1_DM:
1070b21acc1cSCharles Keepax 		region_name = "DM";
1071b21acc1cSCharles Keepax 		break;
1072b21acc1cSCharles Keepax 	case WMFW_ADSP2_XM:
1073b21acc1cSCharles Keepax 		region_name = "XM";
1074b21acc1cSCharles Keepax 		break;
1075b21acc1cSCharles Keepax 	case WMFW_ADSP2_YM:
1076b21acc1cSCharles Keepax 		region_name = "YM";
1077b21acc1cSCharles Keepax 		break;
1078b21acc1cSCharles Keepax 	case WMFW_ADSP1_ZM:
1079b21acc1cSCharles Keepax 		region_name = "ZM";
1080b21acc1cSCharles Keepax 		break;
1081b21acc1cSCharles Keepax 	default:
10822323736dSCharles Keepax 		adsp_err(dsp, "Unknown region type: %d\n", alg_region->type);
1083b21acc1cSCharles Keepax 		return -EINVAL;
1084b21acc1cSCharles Keepax 	}
1085b21acc1cSCharles Keepax 
1086cb5b57a9SCharles Keepax 	switch (dsp->fw_ver) {
1087cb5b57a9SCharles Keepax 	case 0:
1088cb5b57a9SCharles Keepax 	case 1:
1089b21acc1cSCharles Keepax 		snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "DSP%d %s %x",
1090b21acc1cSCharles Keepax 			 dsp->num, region_name, alg_region->alg);
1091cb5b57a9SCharles Keepax 		break;
1092cb5b57a9SCharles Keepax 	default:
1093cb5b57a9SCharles Keepax 		ret = snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
1094cb5b57a9SCharles Keepax 				"DSP%d%c %.12s %x", dsp->num, *region_name,
1095cb5b57a9SCharles Keepax 				wm_adsp_fw_text[dsp->fw], alg_region->alg);
1096cb5b57a9SCharles Keepax 
1097cb5b57a9SCharles Keepax 		/* Truncate the subname from the start if it is too long */
1098cb5b57a9SCharles Keepax 		if (subname) {
1099cb5b57a9SCharles Keepax 			int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
1100cb5b57a9SCharles Keepax 			int skip = 0;
1101cb5b57a9SCharles Keepax 
1102cb5b57a9SCharles Keepax 			if (subname_len > avail)
1103cb5b57a9SCharles Keepax 				skip = subname_len - avail;
1104cb5b57a9SCharles Keepax 
1105cb5b57a9SCharles Keepax 			snprintf(name + ret,
1106cb5b57a9SCharles Keepax 				 SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret, " %.*s",
1107cb5b57a9SCharles Keepax 				 subname_len - skip, subname + skip);
1108cb5b57a9SCharles Keepax 		}
1109cb5b57a9SCharles Keepax 		break;
1110cb5b57a9SCharles Keepax 	}
1111b21acc1cSCharles Keepax 
11127585a5b0SCharles Keepax 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
1113b21acc1cSCharles Keepax 		if (!strcmp(ctl->name, name)) {
1114b21acc1cSCharles Keepax 			if (!ctl->enabled)
1115b21acc1cSCharles Keepax 				ctl->enabled = 1;
1116b21acc1cSCharles Keepax 			return 0;
1117b21acc1cSCharles Keepax 		}
1118b21acc1cSCharles Keepax 	}
1119b21acc1cSCharles Keepax 
1120b21acc1cSCharles Keepax 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1121b21acc1cSCharles Keepax 	if (!ctl)
1122b21acc1cSCharles Keepax 		return -ENOMEM;
11232323736dSCharles Keepax 	ctl->fw_name = wm_adsp_fw_text[dsp->fw];
1124b21acc1cSCharles Keepax 	ctl->alg_region = *alg_region;
1125b21acc1cSCharles Keepax 	ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
1126b21acc1cSCharles Keepax 	if (!ctl->name) {
1127b21acc1cSCharles Keepax 		ret = -ENOMEM;
1128b21acc1cSCharles Keepax 		goto err_ctl;
1129b21acc1cSCharles Keepax 	}
1130b21acc1cSCharles Keepax 	ctl->enabled = 1;
1131b21acc1cSCharles Keepax 	ctl->set = 0;
1132b21acc1cSCharles Keepax 	ctl->ops.xget = wm_coeff_get;
1133b21acc1cSCharles Keepax 	ctl->ops.xput = wm_coeff_put;
1134b21acc1cSCharles Keepax 	ctl->dsp = dsp;
1135b21acc1cSCharles Keepax 
113626c22a19SCharles Keepax 	ctl->flags = flags;
11372323736dSCharles Keepax 	ctl->offset = offset;
1138b21acc1cSCharles Keepax 	ctl->len = len;
1139b21acc1cSCharles Keepax 	ctl->cache = kzalloc(ctl->len, GFP_KERNEL);
1140b21acc1cSCharles Keepax 	if (!ctl->cache) {
1141b21acc1cSCharles Keepax 		ret = -ENOMEM;
1142b21acc1cSCharles Keepax 		goto err_ctl_name;
1143b21acc1cSCharles Keepax 	}
1144b21acc1cSCharles Keepax 
11452323736dSCharles Keepax 	list_add(&ctl->list, &dsp->ctl_list);
11462323736dSCharles Keepax 
1147b21acc1cSCharles Keepax 	ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL);
1148b21acc1cSCharles Keepax 	if (!ctl_work) {
1149b21acc1cSCharles Keepax 		ret = -ENOMEM;
1150b21acc1cSCharles Keepax 		goto err_ctl_cache;
1151b21acc1cSCharles Keepax 	}
1152b21acc1cSCharles Keepax 
1153b21acc1cSCharles Keepax 	ctl_work->dsp = dsp;
1154b21acc1cSCharles Keepax 	ctl_work->ctl = ctl;
1155b21acc1cSCharles Keepax 	INIT_WORK(&ctl_work->work, wm_adsp_ctl_work);
1156b21acc1cSCharles Keepax 	schedule_work(&ctl_work->work);
1157b21acc1cSCharles Keepax 
1158b21acc1cSCharles Keepax 	return 0;
1159b21acc1cSCharles Keepax 
1160b21acc1cSCharles Keepax err_ctl_cache:
1161b21acc1cSCharles Keepax 	kfree(ctl->cache);
1162b21acc1cSCharles Keepax err_ctl_name:
1163b21acc1cSCharles Keepax 	kfree(ctl->name);
1164b21acc1cSCharles Keepax err_ctl:
1165b21acc1cSCharles Keepax 	kfree(ctl);
1166b21acc1cSCharles Keepax 
1167b21acc1cSCharles Keepax 	return ret;
1168b21acc1cSCharles Keepax }
1169b21acc1cSCharles Keepax 
11702323736dSCharles Keepax struct wm_coeff_parsed_alg {
11712323736dSCharles Keepax 	int id;
11722323736dSCharles Keepax 	const u8 *name;
11732323736dSCharles Keepax 	int name_len;
11742323736dSCharles Keepax 	int ncoeff;
11752323736dSCharles Keepax };
11762323736dSCharles Keepax 
11772323736dSCharles Keepax struct wm_coeff_parsed_coeff {
11782323736dSCharles Keepax 	int offset;
11792323736dSCharles Keepax 	int mem_type;
11802323736dSCharles Keepax 	const u8 *name;
11812323736dSCharles Keepax 	int name_len;
11822323736dSCharles Keepax 	int ctl_type;
11832323736dSCharles Keepax 	int flags;
11842323736dSCharles Keepax 	int len;
11852323736dSCharles Keepax };
11862323736dSCharles Keepax 
1187cb5b57a9SCharles Keepax static int wm_coeff_parse_string(int bytes, const u8 **pos, const u8 **str)
1188cb5b57a9SCharles Keepax {
1189cb5b57a9SCharles Keepax 	int length;
1190cb5b57a9SCharles Keepax 
1191cb5b57a9SCharles Keepax 	switch (bytes) {
1192cb5b57a9SCharles Keepax 	case 1:
1193cb5b57a9SCharles Keepax 		length = **pos;
1194cb5b57a9SCharles Keepax 		break;
1195cb5b57a9SCharles Keepax 	case 2:
11968299ee81SCharles Keepax 		length = le16_to_cpu(*((__le16 *)*pos));
1197cb5b57a9SCharles Keepax 		break;
1198cb5b57a9SCharles Keepax 	default:
1199cb5b57a9SCharles Keepax 		return 0;
1200cb5b57a9SCharles Keepax 	}
1201cb5b57a9SCharles Keepax 
1202cb5b57a9SCharles Keepax 	if (str)
1203cb5b57a9SCharles Keepax 		*str = *pos + bytes;
1204cb5b57a9SCharles Keepax 
1205cb5b57a9SCharles Keepax 	*pos += ((length + bytes) + 3) & ~0x03;
1206cb5b57a9SCharles Keepax 
1207cb5b57a9SCharles Keepax 	return length;
1208cb5b57a9SCharles Keepax }
1209cb5b57a9SCharles Keepax 
1210cb5b57a9SCharles Keepax static int wm_coeff_parse_int(int bytes, const u8 **pos)
1211cb5b57a9SCharles Keepax {
1212cb5b57a9SCharles Keepax 	int val = 0;
1213cb5b57a9SCharles Keepax 
1214cb5b57a9SCharles Keepax 	switch (bytes) {
1215cb5b57a9SCharles Keepax 	case 2:
12168299ee81SCharles Keepax 		val = le16_to_cpu(*((__le16 *)*pos));
1217cb5b57a9SCharles Keepax 		break;
1218cb5b57a9SCharles Keepax 	case 4:
12198299ee81SCharles Keepax 		val = le32_to_cpu(*((__le32 *)*pos));
1220cb5b57a9SCharles Keepax 		break;
1221cb5b57a9SCharles Keepax 	default:
1222cb5b57a9SCharles Keepax 		break;
1223cb5b57a9SCharles Keepax 	}
1224cb5b57a9SCharles Keepax 
1225cb5b57a9SCharles Keepax 	*pos += bytes;
1226cb5b57a9SCharles Keepax 
1227cb5b57a9SCharles Keepax 	return val;
1228cb5b57a9SCharles Keepax }
1229cb5b57a9SCharles Keepax 
12302323736dSCharles Keepax static inline void wm_coeff_parse_alg(struct wm_adsp *dsp, const u8 **data,
12312323736dSCharles Keepax 				      struct wm_coeff_parsed_alg *blk)
12322323736dSCharles Keepax {
12332323736dSCharles Keepax 	const struct wmfw_adsp_alg_data *raw;
12342323736dSCharles Keepax 
1235cb5b57a9SCharles Keepax 	switch (dsp->fw_ver) {
1236cb5b57a9SCharles Keepax 	case 0:
1237cb5b57a9SCharles Keepax 	case 1:
12382323736dSCharles Keepax 		raw = (const struct wmfw_adsp_alg_data *)*data;
12392323736dSCharles Keepax 		*data = raw->data;
12402323736dSCharles Keepax 
12412323736dSCharles Keepax 		blk->id = le32_to_cpu(raw->id);
12422323736dSCharles Keepax 		blk->name = raw->name;
12432323736dSCharles Keepax 		blk->name_len = strlen(raw->name);
12442323736dSCharles Keepax 		blk->ncoeff = le32_to_cpu(raw->ncoeff);
1245cb5b57a9SCharles Keepax 		break;
1246cb5b57a9SCharles Keepax 	default:
1247cb5b57a9SCharles Keepax 		blk->id = wm_coeff_parse_int(sizeof(raw->id), data);
1248cb5b57a9SCharles Keepax 		blk->name_len = wm_coeff_parse_string(sizeof(u8), data,
1249cb5b57a9SCharles Keepax 						      &blk->name);
1250cb5b57a9SCharles Keepax 		wm_coeff_parse_string(sizeof(u16), data, NULL);
1251cb5b57a9SCharles Keepax 		blk->ncoeff = wm_coeff_parse_int(sizeof(raw->ncoeff), data);
1252cb5b57a9SCharles Keepax 		break;
1253cb5b57a9SCharles Keepax 	}
12542323736dSCharles Keepax 
12552323736dSCharles Keepax 	adsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id);
12562323736dSCharles Keepax 	adsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name);
12572323736dSCharles Keepax 	adsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff);
12582323736dSCharles Keepax }
12592323736dSCharles Keepax 
12602323736dSCharles Keepax static inline void wm_coeff_parse_coeff(struct wm_adsp *dsp, const u8 **data,
12612323736dSCharles Keepax 					struct wm_coeff_parsed_coeff *blk)
12622323736dSCharles Keepax {
12632323736dSCharles Keepax 	const struct wmfw_adsp_coeff_data *raw;
1264cb5b57a9SCharles Keepax 	const u8 *tmp;
1265cb5b57a9SCharles Keepax 	int length;
12662323736dSCharles Keepax 
1267cb5b57a9SCharles Keepax 	switch (dsp->fw_ver) {
1268cb5b57a9SCharles Keepax 	case 0:
1269cb5b57a9SCharles Keepax 	case 1:
12702323736dSCharles Keepax 		raw = (const struct wmfw_adsp_coeff_data *)*data;
12712323736dSCharles Keepax 		*data = *data + sizeof(raw->hdr) + le32_to_cpu(raw->hdr.size);
12722323736dSCharles Keepax 
12732323736dSCharles Keepax 		blk->offset = le16_to_cpu(raw->hdr.offset);
12742323736dSCharles Keepax 		blk->mem_type = le16_to_cpu(raw->hdr.type);
12752323736dSCharles Keepax 		blk->name = raw->name;
12762323736dSCharles Keepax 		blk->name_len = strlen(raw->name);
12772323736dSCharles Keepax 		blk->ctl_type = le16_to_cpu(raw->ctl_type);
12782323736dSCharles Keepax 		blk->flags = le16_to_cpu(raw->flags);
12792323736dSCharles Keepax 		blk->len = le32_to_cpu(raw->len);
1280cb5b57a9SCharles Keepax 		break;
1281cb5b57a9SCharles Keepax 	default:
1282cb5b57a9SCharles Keepax 		tmp = *data;
1283cb5b57a9SCharles Keepax 		blk->offset = wm_coeff_parse_int(sizeof(raw->hdr.offset), &tmp);
1284cb5b57a9SCharles Keepax 		blk->mem_type = wm_coeff_parse_int(sizeof(raw->hdr.type), &tmp);
1285cb5b57a9SCharles Keepax 		length = wm_coeff_parse_int(sizeof(raw->hdr.size), &tmp);
1286cb5b57a9SCharles Keepax 		blk->name_len = wm_coeff_parse_string(sizeof(u8), &tmp,
1287cb5b57a9SCharles Keepax 						      &blk->name);
1288cb5b57a9SCharles Keepax 		wm_coeff_parse_string(sizeof(u8), &tmp, NULL);
1289cb5b57a9SCharles Keepax 		wm_coeff_parse_string(sizeof(u16), &tmp, NULL);
1290cb5b57a9SCharles Keepax 		blk->ctl_type = wm_coeff_parse_int(sizeof(raw->ctl_type), &tmp);
1291cb5b57a9SCharles Keepax 		blk->flags = wm_coeff_parse_int(sizeof(raw->flags), &tmp);
1292cb5b57a9SCharles Keepax 		blk->len = wm_coeff_parse_int(sizeof(raw->len), &tmp);
1293cb5b57a9SCharles Keepax 
1294cb5b57a9SCharles Keepax 		*data = *data + sizeof(raw->hdr) + length;
1295cb5b57a9SCharles Keepax 		break;
1296cb5b57a9SCharles Keepax 	}
12972323736dSCharles Keepax 
12982323736dSCharles Keepax 	adsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type);
12992323736dSCharles Keepax 	adsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset);
13002323736dSCharles Keepax 	adsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name);
13012323736dSCharles Keepax 	adsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags);
13022323736dSCharles Keepax 	adsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type);
13032323736dSCharles Keepax 	adsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len);
13042323736dSCharles Keepax }
13052323736dSCharles Keepax 
13062323736dSCharles Keepax static int wm_adsp_parse_coeff(struct wm_adsp *dsp,
13072323736dSCharles Keepax 			       const struct wmfw_region *region)
13082323736dSCharles Keepax {
13092323736dSCharles Keepax 	struct wm_adsp_alg_region alg_region = {};
13102323736dSCharles Keepax 	struct wm_coeff_parsed_alg alg_blk;
13112323736dSCharles Keepax 	struct wm_coeff_parsed_coeff coeff_blk;
13122323736dSCharles Keepax 	const u8 *data = region->data;
13132323736dSCharles Keepax 	int i, ret;
13142323736dSCharles Keepax 
13152323736dSCharles Keepax 	wm_coeff_parse_alg(dsp, &data, &alg_blk);
13162323736dSCharles Keepax 	for (i = 0; i < alg_blk.ncoeff; i++) {
13172323736dSCharles Keepax 		wm_coeff_parse_coeff(dsp, &data, &coeff_blk);
13182323736dSCharles Keepax 
13192323736dSCharles Keepax 		switch (coeff_blk.ctl_type) {
13202323736dSCharles Keepax 		case SNDRV_CTL_ELEM_TYPE_BYTES:
13212323736dSCharles Keepax 			break;
13222323736dSCharles Keepax 		default:
13232323736dSCharles Keepax 			adsp_err(dsp, "Unknown control type: %d\n",
13242323736dSCharles Keepax 				 coeff_blk.ctl_type);
13252323736dSCharles Keepax 			return -EINVAL;
13262323736dSCharles Keepax 		}
13272323736dSCharles Keepax 
13282323736dSCharles Keepax 		alg_region.type = coeff_blk.mem_type;
13292323736dSCharles Keepax 		alg_region.alg = alg_blk.id;
13302323736dSCharles Keepax 
13312323736dSCharles Keepax 		ret = wm_adsp_create_control(dsp, &alg_region,
13322323736dSCharles Keepax 					     coeff_blk.offset,
13332323736dSCharles Keepax 					     coeff_blk.len,
13342323736dSCharles Keepax 					     coeff_blk.name,
133526c22a19SCharles Keepax 					     coeff_blk.name_len,
133626c22a19SCharles Keepax 					     coeff_blk.flags);
13372323736dSCharles Keepax 		if (ret < 0)
13382323736dSCharles Keepax 			adsp_err(dsp, "Failed to create control: %.*s, %d\n",
13392323736dSCharles Keepax 				 coeff_blk.name_len, coeff_blk.name, ret);
13402323736dSCharles Keepax 	}
13412323736dSCharles Keepax 
13422323736dSCharles Keepax 	return 0;
13432323736dSCharles Keepax }
13442323736dSCharles Keepax 
13452159ad93SMark Brown static int wm_adsp_load(struct wm_adsp *dsp)
13462159ad93SMark Brown {
1347cf17c83cSMark Brown 	LIST_HEAD(buf_list);
13482159ad93SMark Brown 	const struct firmware *firmware;
13492159ad93SMark Brown 	struct regmap *regmap = dsp->regmap;
13502159ad93SMark Brown 	unsigned int pos = 0;
13512159ad93SMark Brown 	const struct wmfw_header *header;
13522159ad93SMark Brown 	const struct wmfw_adsp1_sizes *adsp1_sizes;
13532159ad93SMark Brown 	const struct wmfw_adsp2_sizes *adsp2_sizes;
13542159ad93SMark Brown 	const struct wmfw_footer *footer;
13552159ad93SMark Brown 	const struct wmfw_region *region;
13562159ad93SMark Brown 	const struct wm_adsp_region *mem;
13572159ad93SMark Brown 	const char *region_name;
13582159ad93SMark Brown 	char *file, *text;
1359cf17c83cSMark Brown 	struct wm_adsp_buf *buf;
13602159ad93SMark Brown 	unsigned int reg;
13612159ad93SMark Brown 	int regions = 0;
13622159ad93SMark Brown 	int ret, offset, type, sizes;
13632159ad93SMark Brown 
13642159ad93SMark Brown 	file = kzalloc(PAGE_SIZE, GFP_KERNEL);
13652159ad93SMark Brown 	if (file == NULL)
13662159ad93SMark Brown 		return -ENOMEM;
13672159ad93SMark Brown 
13681023dbd9SMark Brown 	snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.wmfw", dsp->part, dsp->num,
13691023dbd9SMark Brown 		 wm_adsp_fw[dsp->fw].file);
13702159ad93SMark Brown 	file[PAGE_SIZE - 1] = '\0';
13712159ad93SMark Brown 
13722159ad93SMark Brown 	ret = request_firmware(&firmware, file, dsp->dev);
13732159ad93SMark Brown 	if (ret != 0) {
13742159ad93SMark Brown 		adsp_err(dsp, "Failed to request '%s'\n", file);
13752159ad93SMark Brown 		goto out;
13762159ad93SMark Brown 	}
13772159ad93SMark Brown 	ret = -EINVAL;
13782159ad93SMark Brown 
13792159ad93SMark Brown 	pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
13802159ad93SMark Brown 	if (pos >= firmware->size) {
13812159ad93SMark Brown 		adsp_err(dsp, "%s: file too short, %zu bytes\n",
13822159ad93SMark Brown 			 file, firmware->size);
13832159ad93SMark Brown 		goto out_fw;
13842159ad93SMark Brown 	}
13852159ad93SMark Brown 
13862159ad93SMark Brown 	header = (void *)&firmware->data[0];
13872159ad93SMark Brown 
13882159ad93SMark Brown 	if (memcmp(&header->magic[0], "WMFW", 4) != 0) {
13892159ad93SMark Brown 		adsp_err(dsp, "%s: invalid magic\n", file);
13902159ad93SMark Brown 		goto out_fw;
13912159ad93SMark Brown 	}
13922159ad93SMark Brown 
13932323736dSCharles Keepax 	switch (header->ver) {
13942323736dSCharles Keepax 	case 0:
1395c61e59feSCharles Keepax 		adsp_warn(dsp, "%s: Depreciated file format %d\n",
1396c61e59feSCharles Keepax 			  file, header->ver);
1397c61e59feSCharles Keepax 		break;
13982323736dSCharles Keepax 	case 1:
1399cb5b57a9SCharles Keepax 	case 2:
14002323736dSCharles Keepax 		break;
14012323736dSCharles Keepax 	default:
14022159ad93SMark Brown 		adsp_err(dsp, "%s: unknown file format %d\n",
14032159ad93SMark Brown 			 file, header->ver);
14042159ad93SMark Brown 		goto out_fw;
14052159ad93SMark Brown 	}
14062323736dSCharles Keepax 
14073626992aSDimitris Papastamos 	adsp_info(dsp, "Firmware version: %d\n", header->ver);
14082323736dSCharles Keepax 	dsp->fw_ver = header->ver;
14092159ad93SMark Brown 
14102159ad93SMark Brown 	if (header->core != dsp->type) {
14112159ad93SMark Brown 		adsp_err(dsp, "%s: invalid core %d != %d\n",
14122159ad93SMark Brown 			 file, header->core, dsp->type);
14132159ad93SMark Brown 		goto out_fw;
14142159ad93SMark Brown 	}
14152159ad93SMark Brown 
14162159ad93SMark Brown 	switch (dsp->type) {
14172159ad93SMark Brown 	case WMFW_ADSP1:
14182159ad93SMark Brown 		pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
14192159ad93SMark Brown 		adsp1_sizes = (void *)&(header[1]);
14202159ad93SMark Brown 		footer = (void *)&(adsp1_sizes[1]);
14212159ad93SMark Brown 		sizes = sizeof(*adsp1_sizes);
14222159ad93SMark Brown 
14232159ad93SMark Brown 		adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n",
14242159ad93SMark Brown 			 file, le32_to_cpu(adsp1_sizes->dm),
14252159ad93SMark Brown 			 le32_to_cpu(adsp1_sizes->pm),
14262159ad93SMark Brown 			 le32_to_cpu(adsp1_sizes->zm));
14272159ad93SMark Brown 		break;
14282159ad93SMark Brown 
14292159ad93SMark Brown 	case WMFW_ADSP2:
14302159ad93SMark Brown 		pos = sizeof(*header) + sizeof(*adsp2_sizes) + sizeof(*footer);
14312159ad93SMark Brown 		adsp2_sizes = (void *)&(header[1]);
14322159ad93SMark Brown 		footer = (void *)&(adsp2_sizes[1]);
14332159ad93SMark Brown 		sizes = sizeof(*adsp2_sizes);
14342159ad93SMark Brown 
14352159ad93SMark Brown 		adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n",
14362159ad93SMark Brown 			 file, le32_to_cpu(adsp2_sizes->xm),
14372159ad93SMark Brown 			 le32_to_cpu(adsp2_sizes->ym),
14382159ad93SMark Brown 			 le32_to_cpu(adsp2_sizes->pm),
14392159ad93SMark Brown 			 le32_to_cpu(adsp2_sizes->zm));
14402159ad93SMark Brown 		break;
14412159ad93SMark Brown 
14422159ad93SMark Brown 	default:
14436c452bdaSTakashi Iwai 		WARN(1, "Unknown DSP type");
14442159ad93SMark Brown 		goto out_fw;
14452159ad93SMark Brown 	}
14462159ad93SMark Brown 
14472159ad93SMark Brown 	if (le32_to_cpu(header->len) != sizeof(*header) +
14482159ad93SMark Brown 	    sizes + sizeof(*footer)) {
14492159ad93SMark Brown 		adsp_err(dsp, "%s: unexpected header length %d\n",
14502159ad93SMark Brown 			 file, le32_to_cpu(header->len));
14512159ad93SMark Brown 		goto out_fw;
14522159ad93SMark Brown 	}
14532159ad93SMark Brown 
14542159ad93SMark Brown 	adsp_dbg(dsp, "%s: timestamp %llu\n", file,
14552159ad93SMark Brown 		 le64_to_cpu(footer->timestamp));
14562159ad93SMark Brown 
14572159ad93SMark Brown 	while (pos < firmware->size &&
14582159ad93SMark Brown 	       pos - firmware->size > sizeof(*region)) {
14592159ad93SMark Brown 		region = (void *)&(firmware->data[pos]);
14602159ad93SMark Brown 		region_name = "Unknown";
14612159ad93SMark Brown 		reg = 0;
14622159ad93SMark Brown 		text = NULL;
14632159ad93SMark Brown 		offset = le32_to_cpu(region->offset) & 0xffffff;
14642159ad93SMark Brown 		type = be32_to_cpu(region->type) & 0xff;
14652159ad93SMark Brown 		mem = wm_adsp_find_region(dsp, type);
14662159ad93SMark Brown 
14672159ad93SMark Brown 		switch (type) {
14682159ad93SMark Brown 		case WMFW_NAME_TEXT:
14692159ad93SMark Brown 			region_name = "Firmware name";
14702159ad93SMark Brown 			text = kzalloc(le32_to_cpu(region->len) + 1,
14712159ad93SMark Brown 				       GFP_KERNEL);
14722159ad93SMark Brown 			break;
14732323736dSCharles Keepax 		case WMFW_ALGORITHM_DATA:
14742323736dSCharles Keepax 			region_name = "Algorithm";
14752323736dSCharles Keepax 			ret = wm_adsp_parse_coeff(dsp, region);
14762323736dSCharles Keepax 			if (ret != 0)
14772323736dSCharles Keepax 				goto out_fw;
14782323736dSCharles Keepax 			break;
14792159ad93SMark Brown 		case WMFW_INFO_TEXT:
14802159ad93SMark Brown 			region_name = "Information";
14812159ad93SMark Brown 			text = kzalloc(le32_to_cpu(region->len) + 1,
14822159ad93SMark Brown 				       GFP_KERNEL);
14832159ad93SMark Brown 			break;
14842159ad93SMark Brown 		case WMFW_ABSOLUTE:
14852159ad93SMark Brown 			region_name = "Absolute";
14862159ad93SMark Brown 			reg = offset;
14872159ad93SMark Brown 			break;
14882159ad93SMark Brown 		case WMFW_ADSP1_PM:
14892159ad93SMark Brown 			region_name = "PM";
149045b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
14912159ad93SMark Brown 			break;
14922159ad93SMark Brown 		case WMFW_ADSP1_DM:
14932159ad93SMark Brown 			region_name = "DM";
149445b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
14952159ad93SMark Brown 			break;
14962159ad93SMark Brown 		case WMFW_ADSP2_XM:
14972159ad93SMark Brown 			region_name = "XM";
149845b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
14992159ad93SMark Brown 			break;
15002159ad93SMark Brown 		case WMFW_ADSP2_YM:
15012159ad93SMark Brown 			region_name = "YM";
150245b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
15032159ad93SMark Brown 			break;
15042159ad93SMark Brown 		case WMFW_ADSP1_ZM:
15052159ad93SMark Brown 			region_name = "ZM";
150645b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
15072159ad93SMark Brown 			break;
15082159ad93SMark Brown 		default:
15092159ad93SMark Brown 			adsp_warn(dsp,
15102159ad93SMark Brown 				  "%s.%d: Unknown region type %x at %d(%x)\n",
15112159ad93SMark Brown 				  file, regions, type, pos, pos);
15122159ad93SMark Brown 			break;
15132159ad93SMark Brown 		}
15142159ad93SMark Brown 
15152159ad93SMark Brown 		adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file,
15162159ad93SMark Brown 			 regions, le32_to_cpu(region->len), offset,
15172159ad93SMark Brown 			 region_name);
15182159ad93SMark Brown 
15192159ad93SMark Brown 		if (text) {
15202159ad93SMark Brown 			memcpy(text, region->data, le32_to_cpu(region->len));
15212159ad93SMark Brown 			adsp_info(dsp, "%s: %s\n", file, text);
15222159ad93SMark Brown 			kfree(text);
15232159ad93SMark Brown 		}
15242159ad93SMark Brown 
15252159ad93SMark Brown 		if (reg) {
1526cdcd7f72SCharles Keepax 			buf = wm_adsp_buf_alloc(region->data,
1527cdcd7f72SCharles Keepax 						le32_to_cpu(region->len),
1528cf17c83cSMark Brown 						&buf_list);
1529a76fefabSMark Brown 			if (!buf) {
1530a76fefabSMark Brown 				adsp_err(dsp, "Out of memory\n");
15317328823dSDimitris Papastamos 				ret = -ENOMEM;
15327328823dSDimitris Papastamos 				goto out_fw;
1533a76fefabSMark Brown 			}
1534a76fefabSMark Brown 
1535cdcd7f72SCharles Keepax 			ret = regmap_raw_write_async(regmap, reg, buf->buf,
1536cdcd7f72SCharles Keepax 						     le32_to_cpu(region->len));
15372159ad93SMark Brown 			if (ret != 0) {
15382159ad93SMark Brown 				adsp_err(dsp,
1539cdcd7f72SCharles Keepax 					"%s.%d: Failed to write %d bytes at %d in %s: %d\n",
15402159ad93SMark Brown 					file, regions,
1541cdcd7f72SCharles Keepax 					le32_to_cpu(region->len), offset,
15422159ad93SMark Brown 					region_name, ret);
15432159ad93SMark Brown 				goto out_fw;
15442159ad93SMark Brown 			}
15452159ad93SMark Brown 		}
15462159ad93SMark Brown 
15472159ad93SMark Brown 		pos += le32_to_cpu(region->len) + sizeof(*region);
15482159ad93SMark Brown 		regions++;
15492159ad93SMark Brown 	}
15502159ad93SMark Brown 
1551cf17c83cSMark Brown 	ret = regmap_async_complete(regmap);
1552cf17c83cSMark Brown 	if (ret != 0) {
1553cf17c83cSMark Brown 		adsp_err(dsp, "Failed to complete async write: %d\n", ret);
1554cf17c83cSMark Brown 		goto out_fw;
1555cf17c83cSMark Brown 	}
1556cf17c83cSMark Brown 
15572159ad93SMark Brown 	if (pos > firmware->size)
15582159ad93SMark Brown 		adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
15592159ad93SMark Brown 			  file, regions, pos - firmware->size);
15602159ad93SMark Brown 
1561f9f55e31SRichard Fitzgerald 	wm_adsp_debugfs_save_wmfwname(dsp, file);
1562f9f55e31SRichard Fitzgerald 
15632159ad93SMark Brown out_fw:
1564cf17c83cSMark Brown 	regmap_async_complete(regmap);
1565cf17c83cSMark Brown 	wm_adsp_buf_free(&buf_list);
15662159ad93SMark Brown 	release_firmware(firmware);
15672159ad93SMark Brown out:
15682159ad93SMark Brown 	kfree(file);
15692159ad93SMark Brown 
15702159ad93SMark Brown 	return ret;
15712159ad93SMark Brown }
15722159ad93SMark Brown 
15732323736dSCharles Keepax static void wm_adsp_ctl_fixup_base(struct wm_adsp *dsp,
15742323736dSCharles Keepax 				  const struct wm_adsp_alg_region *alg_region)
15752323736dSCharles Keepax {
15762323736dSCharles Keepax 	struct wm_coeff_ctl *ctl;
15772323736dSCharles Keepax 
15782323736dSCharles Keepax 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
15792323736dSCharles Keepax 		if (ctl->fw_name == wm_adsp_fw_text[dsp->fw] &&
15802323736dSCharles Keepax 		    alg_region->alg == ctl->alg_region.alg &&
15812323736dSCharles Keepax 		    alg_region->type == ctl->alg_region.type) {
15822323736dSCharles Keepax 			ctl->alg_region.base = alg_region->base;
15832323736dSCharles Keepax 		}
15842323736dSCharles Keepax 	}
15852323736dSCharles Keepax }
15862323736dSCharles Keepax 
15873809f001SCharles Keepax static void *wm_adsp_read_algs(struct wm_adsp *dsp, size_t n_algs,
1588b618a185SCharles Keepax 			       unsigned int pos, unsigned int len)
1589db40517cSMark Brown {
1590b618a185SCharles Keepax 	void *alg;
1591b618a185SCharles Keepax 	int ret;
1592db40517cSMark Brown 	__be32 val;
1593db40517cSMark Brown 
15943809f001SCharles Keepax 	if (n_algs == 0) {
1595b618a185SCharles Keepax 		adsp_err(dsp, "No algorithms\n");
1596b618a185SCharles Keepax 		return ERR_PTR(-EINVAL);
1597db40517cSMark Brown 	}
1598db40517cSMark Brown 
15993809f001SCharles Keepax 	if (n_algs > 1024) {
16003809f001SCharles Keepax 		adsp_err(dsp, "Algorithm count %zx excessive\n", n_algs);
1601b618a185SCharles Keepax 		return ERR_PTR(-EINVAL);
1602b618a185SCharles Keepax 	}
1603b618a185SCharles Keepax 
1604b618a185SCharles Keepax 	/* Read the terminator first to validate the length */
1605b618a185SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, pos + len, &val, sizeof(val));
1606b618a185SCharles Keepax 	if (ret != 0) {
1607b618a185SCharles Keepax 		adsp_err(dsp, "Failed to read algorithm list end: %d\n",
1608b618a185SCharles Keepax 			ret);
1609b618a185SCharles Keepax 		return ERR_PTR(ret);
1610b618a185SCharles Keepax 	}
1611b618a185SCharles Keepax 
1612b618a185SCharles Keepax 	if (be32_to_cpu(val) != 0xbedead)
1613b618a185SCharles Keepax 		adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbeadead\n",
1614b618a185SCharles Keepax 			  pos + len, be32_to_cpu(val));
1615b618a185SCharles Keepax 
1616b618a185SCharles Keepax 	alg = kzalloc(len * 2, GFP_KERNEL | GFP_DMA);
1617b618a185SCharles Keepax 	if (!alg)
1618b618a185SCharles Keepax 		return ERR_PTR(-ENOMEM);
1619b618a185SCharles Keepax 
1620b618a185SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, pos, alg, len * 2);
1621b618a185SCharles Keepax 	if (ret != 0) {
16227d00cd97SCharles Keepax 		adsp_err(dsp, "Failed to read algorithm list: %d\n", ret);
1623b618a185SCharles Keepax 		kfree(alg);
1624b618a185SCharles Keepax 		return ERR_PTR(ret);
1625b618a185SCharles Keepax 	}
1626b618a185SCharles Keepax 
1627b618a185SCharles Keepax 	return alg;
1628b618a185SCharles Keepax }
1629b618a185SCharles Keepax 
163014197095SCharles Keepax static struct wm_adsp_alg_region *
163114197095SCharles Keepax 	wm_adsp_find_alg_region(struct wm_adsp *dsp, int type, unsigned int id)
163214197095SCharles Keepax {
163314197095SCharles Keepax 	struct wm_adsp_alg_region *alg_region;
163414197095SCharles Keepax 
163514197095SCharles Keepax 	list_for_each_entry(alg_region, &dsp->alg_regions, list) {
163614197095SCharles Keepax 		if (id == alg_region->alg && type == alg_region->type)
163714197095SCharles Keepax 			return alg_region;
163814197095SCharles Keepax 	}
163914197095SCharles Keepax 
164014197095SCharles Keepax 	return NULL;
164114197095SCharles Keepax }
164214197095SCharles Keepax 
1643d9d20e17SCharles Keepax static struct wm_adsp_alg_region *wm_adsp_create_region(struct wm_adsp *dsp,
1644d9d20e17SCharles Keepax 							int type, __be32 id,
1645d9d20e17SCharles Keepax 							__be32 base)
1646d9d20e17SCharles Keepax {
1647d9d20e17SCharles Keepax 	struct wm_adsp_alg_region *alg_region;
1648d9d20e17SCharles Keepax 
1649d9d20e17SCharles Keepax 	alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL);
1650d9d20e17SCharles Keepax 	if (!alg_region)
1651d9d20e17SCharles Keepax 		return ERR_PTR(-ENOMEM);
1652d9d20e17SCharles Keepax 
1653d9d20e17SCharles Keepax 	alg_region->type = type;
1654d9d20e17SCharles Keepax 	alg_region->alg = be32_to_cpu(id);
1655d9d20e17SCharles Keepax 	alg_region->base = be32_to_cpu(base);
1656d9d20e17SCharles Keepax 
1657d9d20e17SCharles Keepax 	list_add_tail(&alg_region->list, &dsp->alg_regions);
1658d9d20e17SCharles Keepax 
16592323736dSCharles Keepax 	if (dsp->fw_ver > 0)
16602323736dSCharles Keepax 		wm_adsp_ctl_fixup_base(dsp, alg_region);
16612323736dSCharles Keepax 
1662d9d20e17SCharles Keepax 	return alg_region;
1663d9d20e17SCharles Keepax }
1664d9d20e17SCharles Keepax 
166556574d54SRichard Fitzgerald static void wm_adsp_free_alg_regions(struct wm_adsp *dsp)
166656574d54SRichard Fitzgerald {
166756574d54SRichard Fitzgerald 	struct wm_adsp_alg_region *alg_region;
166856574d54SRichard Fitzgerald 
166956574d54SRichard Fitzgerald 	while (!list_empty(&dsp->alg_regions)) {
167056574d54SRichard Fitzgerald 		alg_region = list_first_entry(&dsp->alg_regions,
167156574d54SRichard Fitzgerald 					      struct wm_adsp_alg_region,
167256574d54SRichard Fitzgerald 					      list);
167356574d54SRichard Fitzgerald 		list_del(&alg_region->list);
167456574d54SRichard Fitzgerald 		kfree(alg_region);
167556574d54SRichard Fitzgerald 	}
167656574d54SRichard Fitzgerald }
167756574d54SRichard Fitzgerald 
1678b618a185SCharles Keepax static int wm_adsp1_setup_algs(struct wm_adsp *dsp)
1679b618a185SCharles Keepax {
1680b618a185SCharles Keepax 	struct wmfw_adsp1_id_hdr adsp1_id;
1681b618a185SCharles Keepax 	struct wmfw_adsp1_alg_hdr *adsp1_alg;
16823809f001SCharles Keepax 	struct wm_adsp_alg_region *alg_region;
1683b618a185SCharles Keepax 	const struct wm_adsp_region *mem;
1684b618a185SCharles Keepax 	unsigned int pos, len;
16853809f001SCharles Keepax 	size_t n_algs;
1686b618a185SCharles Keepax 	int i, ret;
1687b618a185SCharles Keepax 
1688b618a185SCharles Keepax 	mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM);
16896c452bdaSTakashi Iwai 	if (WARN_ON(!mem))
1690db40517cSMark Brown 		return -EINVAL;
1691db40517cSMark Brown 
1692b618a185SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id,
1693db40517cSMark Brown 			      sizeof(adsp1_id));
1694db40517cSMark Brown 	if (ret != 0) {
1695db40517cSMark Brown 		adsp_err(dsp, "Failed to read algorithm info: %d\n",
1696db40517cSMark Brown 			 ret);
1697db40517cSMark Brown 		return ret;
1698db40517cSMark Brown 	}
1699db40517cSMark Brown 
17003809f001SCharles Keepax 	n_algs = be32_to_cpu(adsp1_id.n_algs);
1701f395a218SMark Brown 	dsp->fw_id = be32_to_cpu(adsp1_id.fw.id);
1702db40517cSMark Brown 	adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
1703f395a218SMark Brown 		  dsp->fw_id,
1704db40517cSMark Brown 		  (be32_to_cpu(adsp1_id.fw.ver) & 0xff0000) >> 16,
1705db40517cSMark Brown 		  (be32_to_cpu(adsp1_id.fw.ver) & 0xff00) >> 8,
1706db40517cSMark Brown 		  be32_to_cpu(adsp1_id.fw.ver) & 0xff,
17073809f001SCharles Keepax 		  n_algs);
1708db40517cSMark Brown 
1709d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
1710d9d20e17SCharles Keepax 					   adsp1_id.fw.id, adsp1_id.zm);
1711d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1712d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1713ac50009fSMark Brown 
1714d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
1715d9d20e17SCharles Keepax 					   adsp1_id.fw.id, adsp1_id.dm);
1716d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1717d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1718ac50009fSMark Brown 
1719db40517cSMark Brown 	pos = sizeof(adsp1_id) / 2;
17203809f001SCharles Keepax 	len = (sizeof(*adsp1_alg) * n_algs) / 2;
1721db40517cSMark Brown 
17223809f001SCharles Keepax 	adsp1_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len);
1723b618a185SCharles Keepax 	if (IS_ERR(adsp1_alg))
1724b618a185SCharles Keepax 		return PTR_ERR(adsp1_alg);
1725db40517cSMark Brown 
17263809f001SCharles Keepax 	for (i = 0; i < n_algs; i++) {
1727471f4885SMark Brown 		adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
1728db40517cSMark Brown 			  i, be32_to_cpu(adsp1_alg[i].alg.id),
1729db40517cSMark Brown 			  (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16,
1730db40517cSMark Brown 			  (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8,
1731471f4885SMark Brown 			  be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff,
1732471f4885SMark Brown 			  be32_to_cpu(adsp1_alg[i].dm),
1733471f4885SMark Brown 			  be32_to_cpu(adsp1_alg[i].zm));
1734471f4885SMark Brown 
1735d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
1736d9d20e17SCharles Keepax 						   adsp1_alg[i].alg.id,
1737d9d20e17SCharles Keepax 						   adsp1_alg[i].dm);
1738d9d20e17SCharles Keepax 		if (IS_ERR(alg_region)) {
1739d9d20e17SCharles Keepax 			ret = PTR_ERR(alg_region);
1740d6d52179SJS Park 			goto out;
1741d6d52179SJS Park 		}
17422323736dSCharles Keepax 		if (dsp->fw_ver == 0) {
17433809f001SCharles Keepax 			if (i + 1 < n_algs) {
17446958eb2aSCharles Keepax 				len = be32_to_cpu(adsp1_alg[i + 1].dm);
17456958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp1_alg[i].dm);
17466958eb2aSCharles Keepax 				len *= 4;
17472323736dSCharles Keepax 				wm_adsp_create_control(dsp, alg_region, 0,
174826c22a19SCharles Keepax 						       len, NULL, 0, 0);
17496ab2b7b4SDimitris Papastamos 			} else {
17506ab2b7b4SDimitris Papastamos 				adsp_warn(dsp, "Missing length info for region DM with ID %x\n",
17516ab2b7b4SDimitris Papastamos 					  be32_to_cpu(adsp1_alg[i].alg.id));
17526ab2b7b4SDimitris Papastamos 			}
17532323736dSCharles Keepax 		}
1754471f4885SMark Brown 
1755d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
1756d9d20e17SCharles Keepax 						   adsp1_alg[i].alg.id,
1757d9d20e17SCharles Keepax 						   adsp1_alg[i].zm);
1758d9d20e17SCharles Keepax 		if (IS_ERR(alg_region)) {
1759d9d20e17SCharles Keepax 			ret = PTR_ERR(alg_region);
1760d6d52179SJS Park 			goto out;
1761d6d52179SJS Park 		}
17622323736dSCharles Keepax 		if (dsp->fw_ver == 0) {
17633809f001SCharles Keepax 			if (i + 1 < n_algs) {
17646958eb2aSCharles Keepax 				len = be32_to_cpu(adsp1_alg[i + 1].zm);
17656958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp1_alg[i].zm);
17666958eb2aSCharles Keepax 				len *= 4;
17672323736dSCharles Keepax 				wm_adsp_create_control(dsp, alg_region, 0,
176826c22a19SCharles Keepax 						       len, NULL, 0, 0);
17696ab2b7b4SDimitris Papastamos 			} else {
17706ab2b7b4SDimitris Papastamos 				adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
17716ab2b7b4SDimitris Papastamos 					  be32_to_cpu(adsp1_alg[i].alg.id));
17726ab2b7b4SDimitris Papastamos 			}
1773b618a185SCharles Keepax 		}
17742323736dSCharles Keepax 	}
1775db40517cSMark Brown 
1776b618a185SCharles Keepax out:
1777b618a185SCharles Keepax 	kfree(adsp1_alg);
1778b618a185SCharles Keepax 	return ret;
1779b618a185SCharles Keepax }
1780b618a185SCharles Keepax 
1781b618a185SCharles Keepax static int wm_adsp2_setup_algs(struct wm_adsp *dsp)
1782b618a185SCharles Keepax {
1783b618a185SCharles Keepax 	struct wmfw_adsp2_id_hdr adsp2_id;
1784b618a185SCharles Keepax 	struct wmfw_adsp2_alg_hdr *adsp2_alg;
17853809f001SCharles Keepax 	struct wm_adsp_alg_region *alg_region;
1786b618a185SCharles Keepax 	const struct wm_adsp_region *mem;
1787b618a185SCharles Keepax 	unsigned int pos, len;
17883809f001SCharles Keepax 	size_t n_algs;
1789b618a185SCharles Keepax 	int i, ret;
1790b618a185SCharles Keepax 
1791b618a185SCharles Keepax 	mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM);
1792b618a185SCharles Keepax 	if (WARN_ON(!mem))
1793b618a185SCharles Keepax 		return -EINVAL;
1794b618a185SCharles Keepax 
1795b618a185SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id,
1796b618a185SCharles Keepax 			      sizeof(adsp2_id));
1797b618a185SCharles Keepax 	if (ret != 0) {
1798b618a185SCharles Keepax 		adsp_err(dsp, "Failed to read algorithm info: %d\n",
1799b618a185SCharles Keepax 			 ret);
1800b618a185SCharles Keepax 		return ret;
1801b618a185SCharles Keepax 	}
1802b618a185SCharles Keepax 
18033809f001SCharles Keepax 	n_algs = be32_to_cpu(adsp2_id.n_algs);
1804b618a185SCharles Keepax 	dsp->fw_id = be32_to_cpu(adsp2_id.fw.id);
1805f9f55e31SRichard Fitzgerald 	dsp->fw_id_version = be32_to_cpu(adsp2_id.fw.ver);
1806b618a185SCharles Keepax 	adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
1807b618a185SCharles Keepax 		  dsp->fw_id,
1808f9f55e31SRichard Fitzgerald 		  (dsp->fw_id_version & 0xff0000) >> 16,
1809f9f55e31SRichard Fitzgerald 		  (dsp->fw_id_version & 0xff00) >> 8,
1810f9f55e31SRichard Fitzgerald 		  dsp->fw_id_version & 0xff,
18113809f001SCharles Keepax 		  n_algs);
1812b618a185SCharles Keepax 
1813d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
1814d9d20e17SCharles Keepax 					   adsp2_id.fw.id, adsp2_id.xm);
1815d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1816d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1817b618a185SCharles Keepax 
1818d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
1819d9d20e17SCharles Keepax 					   adsp2_id.fw.id, adsp2_id.ym);
1820d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1821d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1822b618a185SCharles Keepax 
1823d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
1824d9d20e17SCharles Keepax 					   adsp2_id.fw.id, adsp2_id.zm);
1825d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1826d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1827b618a185SCharles Keepax 
1828b618a185SCharles Keepax 	pos = sizeof(adsp2_id) / 2;
18293809f001SCharles Keepax 	len = (sizeof(*adsp2_alg) * n_algs) / 2;
1830b618a185SCharles Keepax 
18313809f001SCharles Keepax 	adsp2_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len);
1832b618a185SCharles Keepax 	if (IS_ERR(adsp2_alg))
1833b618a185SCharles Keepax 		return PTR_ERR(adsp2_alg);
1834b618a185SCharles Keepax 
18353809f001SCharles Keepax 	for (i = 0; i < n_algs; i++) {
1836471f4885SMark Brown 		adsp_info(dsp,
1837471f4885SMark Brown 			  "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
1838db40517cSMark Brown 			  i, be32_to_cpu(adsp2_alg[i].alg.id),
1839db40517cSMark Brown 			  (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16,
1840db40517cSMark Brown 			  (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8,
1841471f4885SMark Brown 			  be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff,
1842471f4885SMark Brown 			  be32_to_cpu(adsp2_alg[i].xm),
1843471f4885SMark Brown 			  be32_to_cpu(adsp2_alg[i].ym),
1844471f4885SMark Brown 			  be32_to_cpu(adsp2_alg[i].zm));
1845471f4885SMark Brown 
1846d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
1847d9d20e17SCharles Keepax 						   adsp2_alg[i].alg.id,
1848d9d20e17SCharles Keepax 						   adsp2_alg[i].xm);
1849d9d20e17SCharles Keepax 		if (IS_ERR(alg_region)) {
1850d9d20e17SCharles Keepax 			ret = PTR_ERR(alg_region);
1851d6d52179SJS Park 			goto out;
1852d6d52179SJS Park 		}
18532323736dSCharles Keepax 		if (dsp->fw_ver == 0) {
18543809f001SCharles Keepax 			if (i + 1 < n_algs) {
18556958eb2aSCharles Keepax 				len = be32_to_cpu(adsp2_alg[i + 1].xm);
18566958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp2_alg[i].xm);
18576958eb2aSCharles Keepax 				len *= 4;
18582323736dSCharles Keepax 				wm_adsp_create_control(dsp, alg_region, 0,
185926c22a19SCharles Keepax 						       len, NULL, 0, 0);
18606ab2b7b4SDimitris Papastamos 			} else {
18616ab2b7b4SDimitris Papastamos 				adsp_warn(dsp, "Missing length info for region XM with ID %x\n",
18626ab2b7b4SDimitris Papastamos 					  be32_to_cpu(adsp2_alg[i].alg.id));
18636ab2b7b4SDimitris Papastamos 			}
18642323736dSCharles Keepax 		}
1865471f4885SMark Brown 
1866d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
1867d9d20e17SCharles Keepax 						   adsp2_alg[i].alg.id,
1868d9d20e17SCharles Keepax 						   adsp2_alg[i].ym);
1869d9d20e17SCharles Keepax 		if (IS_ERR(alg_region)) {
1870d9d20e17SCharles Keepax 			ret = PTR_ERR(alg_region);
1871d6d52179SJS Park 			goto out;
1872d6d52179SJS Park 		}
18732323736dSCharles Keepax 		if (dsp->fw_ver == 0) {
18743809f001SCharles Keepax 			if (i + 1 < n_algs) {
18756958eb2aSCharles Keepax 				len = be32_to_cpu(adsp2_alg[i + 1].ym);
18766958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp2_alg[i].ym);
18776958eb2aSCharles Keepax 				len *= 4;
18782323736dSCharles Keepax 				wm_adsp_create_control(dsp, alg_region, 0,
187926c22a19SCharles Keepax 						       len, NULL, 0, 0);
18806ab2b7b4SDimitris Papastamos 			} else {
18816ab2b7b4SDimitris Papastamos 				adsp_warn(dsp, "Missing length info for region YM with ID %x\n",
18826ab2b7b4SDimitris Papastamos 					  be32_to_cpu(adsp2_alg[i].alg.id));
18836ab2b7b4SDimitris Papastamos 			}
18842323736dSCharles Keepax 		}
1885471f4885SMark Brown 
1886d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
1887d9d20e17SCharles Keepax 						   adsp2_alg[i].alg.id,
1888d9d20e17SCharles Keepax 						   adsp2_alg[i].zm);
1889d9d20e17SCharles Keepax 		if (IS_ERR(alg_region)) {
1890d9d20e17SCharles Keepax 			ret = PTR_ERR(alg_region);
1891d6d52179SJS Park 			goto out;
1892d6d52179SJS Park 		}
18932323736dSCharles Keepax 		if (dsp->fw_ver == 0) {
18943809f001SCharles Keepax 			if (i + 1 < n_algs) {
18956958eb2aSCharles Keepax 				len = be32_to_cpu(adsp2_alg[i + 1].zm);
18966958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp2_alg[i].zm);
18976958eb2aSCharles Keepax 				len *= 4;
18982323736dSCharles Keepax 				wm_adsp_create_control(dsp, alg_region, 0,
189926c22a19SCharles Keepax 						       len, NULL, 0, 0);
19006ab2b7b4SDimitris Papastamos 			} else {
19016ab2b7b4SDimitris Papastamos 				adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
19026ab2b7b4SDimitris Papastamos 					  be32_to_cpu(adsp2_alg[i].alg.id));
19036ab2b7b4SDimitris Papastamos 			}
1904db40517cSMark Brown 		}
19052323736dSCharles Keepax 	}
1906db40517cSMark Brown 
1907db40517cSMark Brown out:
1908b618a185SCharles Keepax 	kfree(adsp2_alg);
1909db40517cSMark Brown 	return ret;
1910db40517cSMark Brown }
1911db40517cSMark Brown 
19122159ad93SMark Brown static int wm_adsp_load_coeff(struct wm_adsp *dsp)
19132159ad93SMark Brown {
1914cf17c83cSMark Brown 	LIST_HEAD(buf_list);
19152159ad93SMark Brown 	struct regmap *regmap = dsp->regmap;
19162159ad93SMark Brown 	struct wmfw_coeff_hdr *hdr;
19172159ad93SMark Brown 	struct wmfw_coeff_item *blk;
19182159ad93SMark Brown 	const struct firmware *firmware;
1919471f4885SMark Brown 	const struct wm_adsp_region *mem;
1920471f4885SMark Brown 	struct wm_adsp_alg_region *alg_region;
19212159ad93SMark Brown 	const char *region_name;
19222159ad93SMark Brown 	int ret, pos, blocks, type, offset, reg;
19232159ad93SMark Brown 	char *file;
1924cf17c83cSMark Brown 	struct wm_adsp_buf *buf;
19252159ad93SMark Brown 
19262159ad93SMark Brown 	file = kzalloc(PAGE_SIZE, GFP_KERNEL);
19272159ad93SMark Brown 	if (file == NULL)
19282159ad93SMark Brown 		return -ENOMEM;
19292159ad93SMark Brown 
19301023dbd9SMark Brown 	snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.bin", dsp->part, dsp->num,
19311023dbd9SMark Brown 		 wm_adsp_fw[dsp->fw].file);
19322159ad93SMark Brown 	file[PAGE_SIZE - 1] = '\0';
19332159ad93SMark Brown 
19342159ad93SMark Brown 	ret = request_firmware(&firmware, file, dsp->dev);
19352159ad93SMark Brown 	if (ret != 0) {
19362159ad93SMark Brown 		adsp_warn(dsp, "Failed to request '%s'\n", file);
19372159ad93SMark Brown 		ret = 0;
19382159ad93SMark Brown 		goto out;
19392159ad93SMark Brown 	}
19402159ad93SMark Brown 	ret = -EINVAL;
19412159ad93SMark Brown 
19422159ad93SMark Brown 	if (sizeof(*hdr) >= firmware->size) {
19432159ad93SMark Brown 		adsp_err(dsp, "%s: file too short, %zu bytes\n",
19442159ad93SMark Brown 			file, firmware->size);
19452159ad93SMark Brown 		goto out_fw;
19462159ad93SMark Brown 	}
19472159ad93SMark Brown 
19482159ad93SMark Brown 	hdr = (void *)&firmware->data[0];
19492159ad93SMark Brown 	if (memcmp(hdr->magic, "WMDR", 4) != 0) {
19502159ad93SMark Brown 		adsp_err(dsp, "%s: invalid magic\n", file);
1951a4cdbec7SCharles Keepax 		goto out_fw;
19522159ad93SMark Brown 	}
19532159ad93SMark Brown 
1954c712326dSMark Brown 	switch (be32_to_cpu(hdr->rev) & 0xff) {
1955c712326dSMark Brown 	case 1:
1956c712326dSMark Brown 		break;
1957c712326dSMark Brown 	default:
1958c712326dSMark Brown 		adsp_err(dsp, "%s: Unsupported coefficient file format %d\n",
1959c712326dSMark Brown 			 file, be32_to_cpu(hdr->rev) & 0xff);
1960c712326dSMark Brown 		ret = -EINVAL;
1961c712326dSMark Brown 		goto out_fw;
1962c712326dSMark Brown 	}
1963c712326dSMark Brown 
19642159ad93SMark Brown 	adsp_dbg(dsp, "%s: v%d.%d.%d\n", file,
19652159ad93SMark Brown 		(le32_to_cpu(hdr->ver) >> 16) & 0xff,
19662159ad93SMark Brown 		(le32_to_cpu(hdr->ver) >>  8) & 0xff,
19672159ad93SMark Brown 		le32_to_cpu(hdr->ver) & 0xff);
19682159ad93SMark Brown 
19692159ad93SMark Brown 	pos = le32_to_cpu(hdr->len);
19702159ad93SMark Brown 
19712159ad93SMark Brown 	blocks = 0;
19722159ad93SMark Brown 	while (pos < firmware->size &&
19732159ad93SMark Brown 	       pos - firmware->size > sizeof(*blk)) {
19742159ad93SMark Brown 		blk = (void *)(&firmware->data[pos]);
19752159ad93SMark Brown 
1976c712326dSMark Brown 		type = le16_to_cpu(blk->type);
1977c712326dSMark Brown 		offset = le16_to_cpu(blk->offset);
19782159ad93SMark Brown 
19792159ad93SMark Brown 		adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n",
19802159ad93SMark Brown 			 file, blocks, le32_to_cpu(blk->id),
19812159ad93SMark Brown 			 (le32_to_cpu(blk->ver) >> 16) & 0xff,
19822159ad93SMark Brown 			 (le32_to_cpu(blk->ver) >>  8) & 0xff,
19832159ad93SMark Brown 			 le32_to_cpu(blk->ver) & 0xff);
19842159ad93SMark Brown 		adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n",
19852159ad93SMark Brown 			 file, blocks, le32_to_cpu(blk->len), offset, type);
19862159ad93SMark Brown 
19872159ad93SMark Brown 		reg = 0;
19882159ad93SMark Brown 		region_name = "Unknown";
19892159ad93SMark Brown 		switch (type) {
1990c712326dSMark Brown 		case (WMFW_NAME_TEXT << 8):
1991c712326dSMark Brown 		case (WMFW_INFO_TEXT << 8):
19922159ad93SMark Brown 			break;
1993c712326dSMark Brown 		case (WMFW_ABSOLUTE << 8):
1994f395a218SMark Brown 			/*
1995f395a218SMark Brown 			 * Old files may use this for global
1996f395a218SMark Brown 			 * coefficients.
1997f395a218SMark Brown 			 */
1998f395a218SMark Brown 			if (le32_to_cpu(blk->id) == dsp->fw_id &&
1999f395a218SMark Brown 			    offset == 0) {
2000f395a218SMark Brown 				region_name = "global coefficients";
2001f395a218SMark Brown 				mem = wm_adsp_find_region(dsp, type);
2002f395a218SMark Brown 				if (!mem) {
2003f395a218SMark Brown 					adsp_err(dsp, "No ZM\n");
2004f395a218SMark Brown 					break;
2005f395a218SMark Brown 				}
2006f395a218SMark Brown 				reg = wm_adsp_region_to_reg(mem, 0);
2007f395a218SMark Brown 
2008f395a218SMark Brown 			} else {
20092159ad93SMark Brown 				region_name = "register";
20102159ad93SMark Brown 				reg = offset;
2011f395a218SMark Brown 			}
20122159ad93SMark Brown 			break;
2013471f4885SMark Brown 
2014471f4885SMark Brown 		case WMFW_ADSP1_DM:
2015471f4885SMark Brown 		case WMFW_ADSP1_ZM:
2016471f4885SMark Brown 		case WMFW_ADSP2_XM:
2017471f4885SMark Brown 		case WMFW_ADSP2_YM:
2018471f4885SMark Brown 			adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n",
2019471f4885SMark Brown 				 file, blocks, le32_to_cpu(blk->len),
2020471f4885SMark Brown 				 type, le32_to_cpu(blk->id));
2021471f4885SMark Brown 
2022471f4885SMark Brown 			mem = wm_adsp_find_region(dsp, type);
2023471f4885SMark Brown 			if (!mem) {
2024471f4885SMark Brown 				adsp_err(dsp, "No base for region %x\n", type);
2025471f4885SMark Brown 				break;
2026471f4885SMark Brown 			}
2027471f4885SMark Brown 
202814197095SCharles Keepax 			alg_region = wm_adsp_find_alg_region(dsp, type,
202914197095SCharles Keepax 						le32_to_cpu(blk->id));
203014197095SCharles Keepax 			if (alg_region) {
2031338c5188SMark Brown 				reg = alg_region->base;
203214197095SCharles Keepax 				reg = wm_adsp_region_to_reg(mem, reg);
2033338c5188SMark Brown 				reg += offset;
203414197095SCharles Keepax 			} else {
2035471f4885SMark Brown 				adsp_err(dsp, "No %x for algorithm %x\n",
2036471f4885SMark Brown 					 type, le32_to_cpu(blk->id));
203714197095SCharles Keepax 			}
2038471f4885SMark Brown 			break;
2039471f4885SMark Brown 
20402159ad93SMark Brown 		default:
204125c62f7eSMark Brown 			adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n",
204225c62f7eSMark Brown 				 file, blocks, type, pos);
20432159ad93SMark Brown 			break;
20442159ad93SMark Brown 		}
20452159ad93SMark Brown 
20462159ad93SMark Brown 		if (reg) {
2047cf17c83cSMark Brown 			buf = wm_adsp_buf_alloc(blk->data,
2048cf17c83cSMark Brown 						le32_to_cpu(blk->len),
2049cf17c83cSMark Brown 						&buf_list);
2050a76fefabSMark Brown 			if (!buf) {
2051a76fefabSMark Brown 				adsp_err(dsp, "Out of memory\n");
2052f4b82812SWei Yongjun 				ret = -ENOMEM;
2053f4b82812SWei Yongjun 				goto out_fw;
2054a76fefabSMark Brown 			}
2055a76fefabSMark Brown 
205620da6d5aSMark Brown 			adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n",
205720da6d5aSMark Brown 				 file, blocks, le32_to_cpu(blk->len),
205820da6d5aSMark Brown 				 reg);
2059cf17c83cSMark Brown 			ret = regmap_raw_write_async(regmap, reg, buf->buf,
20602159ad93SMark Brown 						     le32_to_cpu(blk->len));
20612159ad93SMark Brown 			if (ret != 0) {
20622159ad93SMark Brown 				adsp_err(dsp,
206343bc3bf6SDimitris Papastamos 					"%s.%d: Failed to write to %x in %s: %d\n",
206443bc3bf6SDimitris Papastamos 					file, blocks, reg, region_name, ret);
20652159ad93SMark Brown 			}
20662159ad93SMark Brown 		}
20672159ad93SMark Brown 
2068be951017SCharles Keepax 		pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03;
20692159ad93SMark Brown 		blocks++;
20702159ad93SMark Brown 	}
20712159ad93SMark Brown 
2072cf17c83cSMark Brown 	ret = regmap_async_complete(regmap);
2073cf17c83cSMark Brown 	if (ret != 0)
2074cf17c83cSMark Brown 		adsp_err(dsp, "Failed to complete async write: %d\n", ret);
2075cf17c83cSMark Brown 
20762159ad93SMark Brown 	if (pos > firmware->size)
20772159ad93SMark Brown 		adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
20782159ad93SMark Brown 			  file, blocks, pos - firmware->size);
20792159ad93SMark Brown 
2080f9f55e31SRichard Fitzgerald 	wm_adsp_debugfs_save_binname(dsp, file);
2081f9f55e31SRichard Fitzgerald 
20822159ad93SMark Brown out_fw:
20839da7a5a9SCharles Keepax 	regmap_async_complete(regmap);
20842159ad93SMark Brown 	release_firmware(firmware);
2085cf17c83cSMark Brown 	wm_adsp_buf_free(&buf_list);
20862159ad93SMark Brown out:
20872159ad93SMark Brown 	kfree(file);
2088f4b82812SWei Yongjun 	return ret;
20892159ad93SMark Brown }
20902159ad93SMark Brown 
20913809f001SCharles Keepax int wm_adsp1_init(struct wm_adsp *dsp)
20925e7a7a22SMark Brown {
20933809f001SCharles Keepax 	INIT_LIST_HEAD(&dsp->alg_regions);
20945e7a7a22SMark Brown 
2095078e7183SCharles Keepax 	mutex_init(&dsp->pwr_lock);
2096078e7183SCharles Keepax 
20975e7a7a22SMark Brown 	return 0;
20985e7a7a22SMark Brown }
20995e7a7a22SMark Brown EXPORT_SYMBOL_GPL(wm_adsp1_init);
21005e7a7a22SMark Brown 
21012159ad93SMark Brown int wm_adsp1_event(struct snd_soc_dapm_widget *w,
21022159ad93SMark Brown 		   struct snd_kcontrol *kcontrol,
21032159ad93SMark Brown 		   int event)
21042159ad93SMark Brown {
210572718517SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
21062159ad93SMark Brown 	struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
21072159ad93SMark Brown 	struct wm_adsp *dsp = &dsps[w->shift];
21086ab2b7b4SDimitris Papastamos 	struct wm_coeff_ctl *ctl;
21092159ad93SMark Brown 	int ret;
21107585a5b0SCharles Keepax 	unsigned int val;
21112159ad93SMark Brown 
211200200107SLars-Peter Clausen 	dsp->card = codec->component.card;
211392bb4c32SDimitris Papastamos 
2114078e7183SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2115078e7183SCharles Keepax 
21162159ad93SMark Brown 	switch (event) {
21172159ad93SMark Brown 	case SND_SOC_DAPM_POST_PMU:
21182159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
21192159ad93SMark Brown 				   ADSP1_SYS_ENA, ADSP1_SYS_ENA);
21202159ad93SMark Brown 
212194e205bfSChris Rattray 		/*
212294e205bfSChris Rattray 		 * For simplicity set the DSP clock rate to be the
212394e205bfSChris Rattray 		 * SYSCLK rate rather than making it configurable.
212494e205bfSChris Rattray 		 */
212594e205bfSChris Rattray 		if (dsp->sysclk_reg) {
212694e205bfSChris Rattray 			ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val);
212794e205bfSChris Rattray 			if (ret != 0) {
212894e205bfSChris Rattray 				adsp_err(dsp, "Failed to read SYSCLK state: %d\n",
212994e205bfSChris Rattray 				ret);
2130078e7183SCharles Keepax 				goto err_mutex;
213194e205bfSChris Rattray 			}
213294e205bfSChris Rattray 
21337d00cd97SCharles Keepax 			val = (val & dsp->sysclk_mask) >> dsp->sysclk_shift;
213494e205bfSChris Rattray 
213594e205bfSChris Rattray 			ret = regmap_update_bits(dsp->regmap,
213694e205bfSChris Rattray 						 dsp->base + ADSP1_CONTROL_31,
213794e205bfSChris Rattray 						 ADSP1_CLK_SEL_MASK, val);
213894e205bfSChris Rattray 			if (ret != 0) {
213994e205bfSChris Rattray 				adsp_err(dsp, "Failed to set clock rate: %d\n",
214094e205bfSChris Rattray 					 ret);
2141078e7183SCharles Keepax 				goto err_mutex;
214294e205bfSChris Rattray 			}
214394e205bfSChris Rattray 		}
214494e205bfSChris Rattray 
21452159ad93SMark Brown 		ret = wm_adsp_load(dsp);
21462159ad93SMark Brown 		if (ret != 0)
2147078e7183SCharles Keepax 			goto err_ena;
21482159ad93SMark Brown 
2149b618a185SCharles Keepax 		ret = wm_adsp1_setup_algs(dsp);
2150db40517cSMark Brown 		if (ret != 0)
2151078e7183SCharles Keepax 			goto err_ena;
2152db40517cSMark Brown 
21532159ad93SMark Brown 		ret = wm_adsp_load_coeff(dsp);
21542159ad93SMark Brown 		if (ret != 0)
2155078e7183SCharles Keepax 			goto err_ena;
21562159ad93SMark Brown 
21570c2e3f34SDimitris Papastamos 		/* Initialize caches for enabled and unset controls */
215881ad93ecSDimitris Papastamos 		ret = wm_coeff_init_control_caches(dsp);
21596ab2b7b4SDimitris Papastamos 		if (ret != 0)
2160078e7183SCharles Keepax 			goto err_ena;
21616ab2b7b4SDimitris Papastamos 
21620c2e3f34SDimitris Papastamos 		/* Sync set controls */
216381ad93ecSDimitris Papastamos 		ret = wm_coeff_sync_controls(dsp);
21646ab2b7b4SDimitris Papastamos 		if (ret != 0)
2165078e7183SCharles Keepax 			goto err_ena;
21666ab2b7b4SDimitris Papastamos 
21672159ad93SMark Brown 		/* Start the core running */
21682159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
21692159ad93SMark Brown 				   ADSP1_CORE_ENA | ADSP1_START,
21702159ad93SMark Brown 				   ADSP1_CORE_ENA | ADSP1_START);
21712159ad93SMark Brown 		break;
21722159ad93SMark Brown 
21732159ad93SMark Brown 	case SND_SOC_DAPM_PRE_PMD:
21742159ad93SMark Brown 		/* Halt the core */
21752159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
21762159ad93SMark Brown 				   ADSP1_CORE_ENA | ADSP1_START, 0);
21772159ad93SMark Brown 
21782159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19,
21792159ad93SMark Brown 				   ADSP1_WDMA_BUFFER_LENGTH_MASK, 0);
21802159ad93SMark Brown 
21812159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
21822159ad93SMark Brown 				   ADSP1_SYS_ENA, 0);
21836ab2b7b4SDimitris Papastamos 
218481ad93ecSDimitris Papastamos 		list_for_each_entry(ctl, &dsp->ctl_list, list)
21856ab2b7b4SDimitris Papastamos 			ctl->enabled = 0;
2186b0101b4fSDimitris Papastamos 
218756574d54SRichard Fitzgerald 
218856574d54SRichard Fitzgerald 		wm_adsp_free_alg_regions(dsp);
21892159ad93SMark Brown 		break;
21902159ad93SMark Brown 
21912159ad93SMark Brown 	default:
21922159ad93SMark Brown 		break;
21932159ad93SMark Brown 	}
21942159ad93SMark Brown 
2195078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2196078e7183SCharles Keepax 
21972159ad93SMark Brown 	return 0;
21982159ad93SMark Brown 
2199078e7183SCharles Keepax err_ena:
22002159ad93SMark Brown 	regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
22012159ad93SMark Brown 			   ADSP1_SYS_ENA, 0);
2202078e7183SCharles Keepax err_mutex:
2203078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2204078e7183SCharles Keepax 
22052159ad93SMark Brown 	return ret;
22062159ad93SMark Brown }
22072159ad93SMark Brown EXPORT_SYMBOL_GPL(wm_adsp1_event);
22082159ad93SMark Brown 
22092159ad93SMark Brown static int wm_adsp2_ena(struct wm_adsp *dsp)
22102159ad93SMark Brown {
22112159ad93SMark Brown 	unsigned int val;
22122159ad93SMark Brown 	int ret, count;
22132159ad93SMark Brown 
22141552c325SMark Brown 	ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL,
22152159ad93SMark Brown 				       ADSP2_SYS_ENA, ADSP2_SYS_ENA);
22162159ad93SMark Brown 	if (ret != 0)
22172159ad93SMark Brown 		return ret;
22182159ad93SMark Brown 
22192159ad93SMark Brown 	/* Wait for the RAM to start, should be near instantaneous */
2220939fd1e8SCharles Keepax 	for (count = 0; count < 10; ++count) {
22217d00cd97SCharles Keepax 		ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1, &val);
22222159ad93SMark Brown 		if (ret != 0)
22232159ad93SMark Brown 			return ret;
2224939fd1e8SCharles Keepax 
2225939fd1e8SCharles Keepax 		if (val & ADSP2_RAM_RDY)
2226939fd1e8SCharles Keepax 			break;
2227939fd1e8SCharles Keepax 
2228939fd1e8SCharles Keepax 		msleep(1);
2229939fd1e8SCharles Keepax 	}
22302159ad93SMark Brown 
22312159ad93SMark Brown 	if (!(val & ADSP2_RAM_RDY)) {
22322159ad93SMark Brown 		adsp_err(dsp, "Failed to start DSP RAM\n");
22332159ad93SMark Brown 		return -EBUSY;
22342159ad93SMark Brown 	}
22352159ad93SMark Brown 
22362159ad93SMark Brown 	adsp_dbg(dsp, "RAM ready after %d polls\n", count);
22372159ad93SMark Brown 
22382159ad93SMark Brown 	return 0;
22392159ad93SMark Brown }
22402159ad93SMark Brown 
224118b1a902SCharles Keepax static void wm_adsp2_boot_work(struct work_struct *work)
22422159ad93SMark Brown {
2243d8a64d6aSCharles Keepax 	struct wm_adsp *dsp = container_of(work,
2244d8a64d6aSCharles Keepax 					   struct wm_adsp,
2245d8a64d6aSCharles Keepax 					   boot_work);
22462159ad93SMark Brown 	int ret;
22472159ad93SMark Brown 
2248078e7183SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2249078e7183SCharles Keepax 
22502159ad93SMark Brown 	ret = wm_adsp2_ena(dsp);
22512159ad93SMark Brown 	if (ret != 0)
2252078e7183SCharles Keepax 		goto err_mutex;
22532159ad93SMark Brown 
22542159ad93SMark Brown 	ret = wm_adsp_load(dsp);
22552159ad93SMark Brown 	if (ret != 0)
2256078e7183SCharles Keepax 		goto err_ena;
22572159ad93SMark Brown 
2258b618a185SCharles Keepax 	ret = wm_adsp2_setup_algs(dsp);
2259db40517cSMark Brown 	if (ret != 0)
2260078e7183SCharles Keepax 		goto err_ena;
2261db40517cSMark Brown 
22622159ad93SMark Brown 	ret = wm_adsp_load_coeff(dsp);
22632159ad93SMark Brown 	if (ret != 0)
2264078e7183SCharles Keepax 		goto err_ena;
22652159ad93SMark Brown 
22660c2e3f34SDimitris Papastamos 	/* Initialize caches for enabled and unset controls */
226781ad93ecSDimitris Papastamos 	ret = wm_coeff_init_control_caches(dsp);
22686ab2b7b4SDimitris Papastamos 	if (ret != 0)
2269078e7183SCharles Keepax 		goto err_ena;
22706ab2b7b4SDimitris Papastamos 
22710c2e3f34SDimitris Papastamos 	/* Sync set controls */
227281ad93ecSDimitris Papastamos 	ret = wm_coeff_sync_controls(dsp);
22736ab2b7b4SDimitris Papastamos 	if (ret != 0)
2274078e7183SCharles Keepax 		goto err_ena;
22756ab2b7b4SDimitris Papastamos 
22761023dbd9SMark Brown 	dsp->running = true;
2277d8a64d6aSCharles Keepax 
2278078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2279078e7183SCharles Keepax 
2280d8a64d6aSCharles Keepax 	return;
2281d8a64d6aSCharles Keepax 
2282078e7183SCharles Keepax err_ena:
2283d8a64d6aSCharles Keepax 	regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2284d8a64d6aSCharles Keepax 			   ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
2285078e7183SCharles Keepax err_mutex:
2286078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2287d8a64d6aSCharles Keepax }
2288d8a64d6aSCharles Keepax 
2289d82d767fSCharles Keepax static void wm_adsp2_set_dspclk(struct wm_adsp *dsp, unsigned int freq)
2290d82d767fSCharles Keepax {
2291d82d767fSCharles Keepax 	int ret;
2292d82d767fSCharles Keepax 
2293d82d767fSCharles Keepax 	ret = regmap_update_bits_async(dsp->regmap,
2294d82d767fSCharles Keepax 				       dsp->base + ADSP2_CLOCKING,
2295d82d767fSCharles Keepax 				       ADSP2_CLK_SEL_MASK,
2296d82d767fSCharles Keepax 				       freq << ADSP2_CLK_SEL_SHIFT);
2297d82d767fSCharles Keepax 	if (ret != 0)
2298d82d767fSCharles Keepax 		adsp_err(dsp, "Failed to set clock rate: %d\n", ret);
2299d82d767fSCharles Keepax }
2300d82d767fSCharles Keepax 
230112db5eddSCharles Keepax int wm_adsp2_early_event(struct snd_soc_dapm_widget *w,
2302d82d767fSCharles Keepax 			 struct snd_kcontrol *kcontrol, int event,
2303d82d767fSCharles Keepax 			 unsigned int freq)
230412db5eddSCharles Keepax {
230572718517SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
230612db5eddSCharles Keepax 	struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
230712db5eddSCharles Keepax 	struct wm_adsp *dsp = &dsps[w->shift];
230812db5eddSCharles Keepax 
230900200107SLars-Peter Clausen 	dsp->card = codec->component.card;
231012db5eddSCharles Keepax 
231112db5eddSCharles Keepax 	switch (event) {
231212db5eddSCharles Keepax 	case SND_SOC_DAPM_PRE_PMU:
2313d82d767fSCharles Keepax 		wm_adsp2_set_dspclk(dsp, freq);
231412db5eddSCharles Keepax 		queue_work(system_unbound_wq, &dsp->boot_work);
231512db5eddSCharles Keepax 		break;
231612db5eddSCharles Keepax 	default:
231712db5eddSCharles Keepax 		break;
2318cab27258SCharles Keepax 	}
231912db5eddSCharles Keepax 
232012db5eddSCharles Keepax 	return 0;
232112db5eddSCharles Keepax }
232212db5eddSCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp2_early_event);
232312db5eddSCharles Keepax 
2324d8a64d6aSCharles Keepax int wm_adsp2_event(struct snd_soc_dapm_widget *w,
2325d8a64d6aSCharles Keepax 		   struct snd_kcontrol *kcontrol, int event)
2326d8a64d6aSCharles Keepax {
232772718517SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2328d8a64d6aSCharles Keepax 	struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
2329d8a64d6aSCharles Keepax 	struct wm_adsp *dsp = &dsps[w->shift];
2330d8a64d6aSCharles Keepax 	struct wm_coeff_ctl *ctl;
2331d8a64d6aSCharles Keepax 	int ret;
2332d8a64d6aSCharles Keepax 
2333d8a64d6aSCharles Keepax 	switch (event) {
2334d8a64d6aSCharles Keepax 	case SND_SOC_DAPM_POST_PMU:
2335d8a64d6aSCharles Keepax 		flush_work(&dsp->boot_work);
2336d8a64d6aSCharles Keepax 
2337d8a64d6aSCharles Keepax 		if (!dsp->running)
2338d8a64d6aSCharles Keepax 			return -EIO;
2339d8a64d6aSCharles Keepax 
2340d8a64d6aSCharles Keepax 		ret = regmap_update_bits(dsp->regmap,
2341d8a64d6aSCharles Keepax 					 dsp->base + ADSP2_CONTROL,
234200e4c3b6SCharles Keepax 					 ADSP2_CORE_ENA | ADSP2_START,
234300e4c3b6SCharles Keepax 					 ADSP2_CORE_ENA | ADSP2_START);
2344d8a64d6aSCharles Keepax 		if (ret != 0)
2345d8a64d6aSCharles Keepax 			goto err;
23462cd19bdbSCharles Keepax 
2347612047f0SCharles Keepax 		mutex_lock(&dsp->pwr_lock);
2348612047f0SCharles Keepax 
23492cd19bdbSCharles Keepax 		if (wm_adsp_fw[dsp->fw].num_caps != 0)
23502cd19bdbSCharles Keepax 			ret = wm_adsp_buffer_init(dsp);
23512cd19bdbSCharles Keepax 
2352612047f0SCharles Keepax 		mutex_unlock(&dsp->pwr_lock);
2353612047f0SCharles Keepax 
23542159ad93SMark Brown 		break;
23552159ad93SMark Brown 
23562159ad93SMark Brown 	case SND_SOC_DAPM_PRE_PMD:
235710337b07SRichard Fitzgerald 		/* Log firmware state, it can be useful for analysis */
235810337b07SRichard Fitzgerald 		wm_adsp2_show_fw_status(dsp);
235910337b07SRichard Fitzgerald 
2360078e7183SCharles Keepax 		mutex_lock(&dsp->pwr_lock);
2361078e7183SCharles Keepax 
2362f9f55e31SRichard Fitzgerald 		wm_adsp_debugfs_clear(dsp);
2363f9f55e31SRichard Fitzgerald 
2364f9f55e31SRichard Fitzgerald 		dsp->fw_id = 0;
2365f9f55e31SRichard Fitzgerald 		dsp->fw_id_version = 0;
23661023dbd9SMark Brown 		dsp->running = false;
23671023dbd9SMark Brown 
23682159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
23696facd2d1SSimon Trimmer 				   ADSP2_CORE_ENA | ADSP2_START, 0);
2370973838a0SMark Brown 
23712d30b575SMark Brown 		/* Make sure DMAs are quiesced */
23726facd2d1SSimon Trimmer 		regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
23732d30b575SMark Brown 		regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
23742d30b575SMark Brown 		regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0);
23756facd2d1SSimon Trimmer 
23766facd2d1SSimon Trimmer 		regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
23776facd2d1SSimon Trimmer 				   ADSP2_SYS_ENA, 0);
23782d30b575SMark Brown 
237981ad93ecSDimitris Papastamos 		list_for_each_entry(ctl, &dsp->ctl_list, list)
23806ab2b7b4SDimitris Papastamos 			ctl->enabled = 0;
23816ab2b7b4SDimitris Papastamos 
238256574d54SRichard Fitzgerald 		wm_adsp_free_alg_regions(dsp);
2383ddbc5efeSCharles Keepax 
23842cd19bdbSCharles Keepax 		if (wm_adsp_fw[dsp->fw].num_caps != 0)
23852cd19bdbSCharles Keepax 			wm_adsp_buffer_free(dsp);
23862cd19bdbSCharles Keepax 
2387078e7183SCharles Keepax 		mutex_unlock(&dsp->pwr_lock);
2388078e7183SCharles Keepax 
2389ddbc5efeSCharles Keepax 		adsp_dbg(dsp, "Shutdown complete\n");
23902159ad93SMark Brown 		break;
23912159ad93SMark Brown 
23922159ad93SMark Brown 	default:
23932159ad93SMark Brown 		break;
23942159ad93SMark Brown 	}
23952159ad93SMark Brown 
23962159ad93SMark Brown 	return 0;
23972159ad93SMark Brown err:
23982159ad93SMark Brown 	regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2399a7f9be7eSMark Brown 			   ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
24002159ad93SMark Brown 	return ret;
24012159ad93SMark Brown }
24022159ad93SMark Brown EXPORT_SYMBOL_GPL(wm_adsp2_event);
2403973838a0SMark Brown 
2404f5e2ce92SRichard Fitzgerald int wm_adsp2_codec_probe(struct wm_adsp *dsp, struct snd_soc_codec *codec)
2405f5e2ce92SRichard Fitzgerald {
2406f9f55e31SRichard Fitzgerald 	wm_adsp2_init_debugfs(dsp, codec);
2407f9f55e31SRichard Fitzgerald 
2408218e5087SRichard Fitzgerald 	return snd_soc_add_codec_controls(codec,
2409336d0442SRichard Fitzgerald 					  &wm_adsp_fw_controls[dsp->num - 1],
2410336d0442SRichard Fitzgerald 					  1);
2411f5e2ce92SRichard Fitzgerald }
2412f5e2ce92SRichard Fitzgerald EXPORT_SYMBOL_GPL(wm_adsp2_codec_probe);
2413f5e2ce92SRichard Fitzgerald 
2414f5e2ce92SRichard Fitzgerald int wm_adsp2_codec_remove(struct wm_adsp *dsp, struct snd_soc_codec *codec)
2415f5e2ce92SRichard Fitzgerald {
2416f9f55e31SRichard Fitzgerald 	wm_adsp2_cleanup_debugfs(dsp);
2417f9f55e31SRichard Fitzgerald 
2418f5e2ce92SRichard Fitzgerald 	return 0;
2419f5e2ce92SRichard Fitzgerald }
2420f5e2ce92SRichard Fitzgerald EXPORT_SYMBOL_GPL(wm_adsp2_codec_remove);
2421f5e2ce92SRichard Fitzgerald 
242281ac58b1SRichard Fitzgerald int wm_adsp2_init(struct wm_adsp *dsp)
2423973838a0SMark Brown {
2424973838a0SMark Brown 	int ret;
2425973838a0SMark Brown 
242610a2b662SMark Brown 	/*
242710a2b662SMark Brown 	 * Disable the DSP memory by default when in reset for a small
242810a2b662SMark Brown 	 * power saving.
242910a2b662SMark Brown 	 */
24303809f001SCharles Keepax 	ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
243110a2b662SMark Brown 				 ADSP2_MEM_ENA, 0);
243210a2b662SMark Brown 	if (ret != 0) {
24333809f001SCharles Keepax 		adsp_err(dsp, "Failed to clear memory retention: %d\n", ret);
243410a2b662SMark Brown 		return ret;
243510a2b662SMark Brown 	}
243610a2b662SMark Brown 
24373809f001SCharles Keepax 	INIT_LIST_HEAD(&dsp->alg_regions);
24383809f001SCharles Keepax 	INIT_LIST_HEAD(&dsp->ctl_list);
24393809f001SCharles Keepax 	INIT_WORK(&dsp->boot_work, wm_adsp2_boot_work);
24406ab2b7b4SDimitris Papastamos 
2441078e7183SCharles Keepax 	mutex_init(&dsp->pwr_lock);
2442078e7183SCharles Keepax 
2443973838a0SMark Brown 	return 0;
2444973838a0SMark Brown }
2445973838a0SMark Brown EXPORT_SYMBOL_GPL(wm_adsp2_init);
24460a37c6efSPraveen Diwakar 
244766225e98SRichard Fitzgerald void wm_adsp2_remove(struct wm_adsp *dsp)
244866225e98SRichard Fitzgerald {
244966225e98SRichard Fitzgerald 	struct wm_coeff_ctl *ctl;
245066225e98SRichard Fitzgerald 
245166225e98SRichard Fitzgerald 	while (!list_empty(&dsp->ctl_list)) {
245266225e98SRichard Fitzgerald 		ctl = list_first_entry(&dsp->ctl_list, struct wm_coeff_ctl,
245366225e98SRichard Fitzgerald 					list);
245466225e98SRichard Fitzgerald 		list_del(&ctl->list);
245566225e98SRichard Fitzgerald 		wm_adsp_free_ctl_blk(ctl);
245666225e98SRichard Fitzgerald 	}
245766225e98SRichard Fitzgerald }
245866225e98SRichard Fitzgerald EXPORT_SYMBOL_GPL(wm_adsp2_remove);
245966225e98SRichard Fitzgerald 
2460edd71350SCharles Keepax static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr)
2461edd71350SCharles Keepax {
2462edd71350SCharles Keepax 	return compr->buf != NULL;
2463edd71350SCharles Keepax }
2464edd71350SCharles Keepax 
2465edd71350SCharles Keepax static int wm_adsp_compr_attach(struct wm_adsp_compr *compr)
2466edd71350SCharles Keepax {
2467edd71350SCharles Keepax 	/*
2468edd71350SCharles Keepax 	 * Note this will be more complex once each DSP can support multiple
2469edd71350SCharles Keepax 	 * streams
2470edd71350SCharles Keepax 	 */
2471edd71350SCharles Keepax 	if (!compr->dsp->buffer)
2472edd71350SCharles Keepax 		return -EINVAL;
2473edd71350SCharles Keepax 
2474edd71350SCharles Keepax 	compr->buf = compr->dsp->buffer;
2475721be3beSCharles Keepax 	compr->buf->compr = compr;
2476edd71350SCharles Keepax 
2477edd71350SCharles Keepax 	return 0;
2478edd71350SCharles Keepax }
2479edd71350SCharles Keepax 
2480721be3beSCharles Keepax static void wm_adsp_compr_detach(struct wm_adsp_compr *compr)
2481721be3beSCharles Keepax {
2482721be3beSCharles Keepax 	if (!compr)
2483721be3beSCharles Keepax 		return;
2484721be3beSCharles Keepax 
2485721be3beSCharles Keepax 	/* Wake the poll so it can see buffer is no longer attached */
2486721be3beSCharles Keepax 	if (compr->stream)
2487721be3beSCharles Keepax 		snd_compr_fragment_elapsed(compr->stream);
2488721be3beSCharles Keepax 
2489721be3beSCharles Keepax 	if (wm_adsp_compr_attached(compr)) {
2490721be3beSCharles Keepax 		compr->buf->compr = NULL;
2491721be3beSCharles Keepax 		compr->buf = NULL;
2492721be3beSCharles Keepax 	}
2493721be3beSCharles Keepax }
2494721be3beSCharles Keepax 
2495406abc95SCharles Keepax int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream)
2496406abc95SCharles Keepax {
2497406abc95SCharles Keepax 	struct wm_adsp_compr *compr;
2498406abc95SCharles Keepax 	int ret = 0;
2499406abc95SCharles Keepax 
2500406abc95SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2501406abc95SCharles Keepax 
2502406abc95SCharles Keepax 	if (wm_adsp_fw[dsp->fw].num_caps == 0) {
2503406abc95SCharles Keepax 		adsp_err(dsp, "Firmware does not support compressed API\n");
2504406abc95SCharles Keepax 		ret = -ENXIO;
2505406abc95SCharles Keepax 		goto out;
2506406abc95SCharles Keepax 	}
2507406abc95SCharles Keepax 
2508406abc95SCharles Keepax 	if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) {
2509406abc95SCharles Keepax 		adsp_err(dsp, "Firmware does not support stream direction\n");
2510406abc95SCharles Keepax 		ret = -EINVAL;
2511406abc95SCharles Keepax 		goto out;
2512406abc95SCharles Keepax 	}
2513406abc95SCharles Keepax 
251495fe9597SCharles Keepax 	if (dsp->compr) {
251595fe9597SCharles Keepax 		/* It is expect this limitation will be removed in future */
251695fe9597SCharles Keepax 		adsp_err(dsp, "Only a single stream supported per DSP\n");
251795fe9597SCharles Keepax 		ret = -EBUSY;
251895fe9597SCharles Keepax 		goto out;
251995fe9597SCharles Keepax 	}
252095fe9597SCharles Keepax 
2521406abc95SCharles Keepax 	compr = kzalloc(sizeof(*compr), GFP_KERNEL);
2522406abc95SCharles Keepax 	if (!compr) {
2523406abc95SCharles Keepax 		ret = -ENOMEM;
2524406abc95SCharles Keepax 		goto out;
2525406abc95SCharles Keepax 	}
2526406abc95SCharles Keepax 
2527406abc95SCharles Keepax 	compr->dsp = dsp;
2528406abc95SCharles Keepax 	compr->stream = stream;
2529406abc95SCharles Keepax 
2530406abc95SCharles Keepax 	dsp->compr = compr;
2531406abc95SCharles Keepax 
2532406abc95SCharles Keepax 	stream->runtime->private_data = compr;
2533406abc95SCharles Keepax 
2534406abc95SCharles Keepax out:
2535406abc95SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2536406abc95SCharles Keepax 
2537406abc95SCharles Keepax 	return ret;
2538406abc95SCharles Keepax }
2539406abc95SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_open);
2540406abc95SCharles Keepax 
2541406abc95SCharles Keepax int wm_adsp_compr_free(struct snd_compr_stream *stream)
2542406abc95SCharles Keepax {
2543406abc95SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
2544406abc95SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
2545406abc95SCharles Keepax 
2546406abc95SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2547406abc95SCharles Keepax 
2548721be3beSCharles Keepax 	wm_adsp_compr_detach(compr);
2549406abc95SCharles Keepax 	dsp->compr = NULL;
2550406abc95SCharles Keepax 
255183a40ce9SCharles Keepax 	kfree(compr->raw_buf);
2552406abc95SCharles Keepax 	kfree(compr);
2553406abc95SCharles Keepax 
2554406abc95SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2555406abc95SCharles Keepax 
2556406abc95SCharles Keepax 	return 0;
2557406abc95SCharles Keepax }
2558406abc95SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_free);
2559406abc95SCharles Keepax 
2560406abc95SCharles Keepax static int wm_adsp_compr_check_params(struct snd_compr_stream *stream,
2561406abc95SCharles Keepax 				      struct snd_compr_params *params)
2562406abc95SCharles Keepax {
2563406abc95SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
2564406abc95SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
2565406abc95SCharles Keepax 	const struct wm_adsp_fw_caps *caps;
2566406abc95SCharles Keepax 	const struct snd_codec_desc *desc;
2567406abc95SCharles Keepax 	int i, j;
2568406abc95SCharles Keepax 
2569406abc95SCharles Keepax 	if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE ||
2570406abc95SCharles Keepax 	    params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE ||
2571406abc95SCharles Keepax 	    params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS ||
2572406abc95SCharles Keepax 	    params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS ||
2573406abc95SCharles Keepax 	    params->buffer.fragment_size % WM_ADSP_DATA_WORD_SIZE) {
2574406abc95SCharles Keepax 		adsp_err(dsp, "Invalid buffer fragsize=%d fragments=%d\n",
2575406abc95SCharles Keepax 			 params->buffer.fragment_size,
2576406abc95SCharles Keepax 			 params->buffer.fragments);
2577406abc95SCharles Keepax 
2578406abc95SCharles Keepax 		return -EINVAL;
2579406abc95SCharles Keepax 	}
2580406abc95SCharles Keepax 
2581406abc95SCharles Keepax 	for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) {
2582406abc95SCharles Keepax 		caps = &wm_adsp_fw[dsp->fw].caps[i];
2583406abc95SCharles Keepax 		desc = &caps->desc;
2584406abc95SCharles Keepax 
2585406abc95SCharles Keepax 		if (caps->id != params->codec.id)
2586406abc95SCharles Keepax 			continue;
2587406abc95SCharles Keepax 
2588406abc95SCharles Keepax 		if (stream->direction == SND_COMPRESS_PLAYBACK) {
2589406abc95SCharles Keepax 			if (desc->max_ch < params->codec.ch_out)
2590406abc95SCharles Keepax 				continue;
2591406abc95SCharles Keepax 		} else {
2592406abc95SCharles Keepax 			if (desc->max_ch < params->codec.ch_in)
2593406abc95SCharles Keepax 				continue;
2594406abc95SCharles Keepax 		}
2595406abc95SCharles Keepax 
2596406abc95SCharles Keepax 		if (!(desc->formats & (1 << params->codec.format)))
2597406abc95SCharles Keepax 			continue;
2598406abc95SCharles Keepax 
2599406abc95SCharles Keepax 		for (j = 0; j < desc->num_sample_rates; ++j)
2600406abc95SCharles Keepax 			if (desc->sample_rates[j] == params->codec.sample_rate)
2601406abc95SCharles Keepax 				return 0;
2602406abc95SCharles Keepax 	}
2603406abc95SCharles Keepax 
2604406abc95SCharles Keepax 	adsp_err(dsp, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n",
2605406abc95SCharles Keepax 		 params->codec.id, params->codec.ch_in, params->codec.ch_out,
2606406abc95SCharles Keepax 		 params->codec.sample_rate, params->codec.format);
2607406abc95SCharles Keepax 	return -EINVAL;
2608406abc95SCharles Keepax }
2609406abc95SCharles Keepax 
2610565ace46SCharles Keepax static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr)
2611565ace46SCharles Keepax {
2612565ace46SCharles Keepax 	return compr->size.fragment_size / WM_ADSP_DATA_WORD_SIZE;
2613565ace46SCharles Keepax }
2614565ace46SCharles Keepax 
2615406abc95SCharles Keepax int wm_adsp_compr_set_params(struct snd_compr_stream *stream,
2616406abc95SCharles Keepax 			     struct snd_compr_params *params)
2617406abc95SCharles Keepax {
2618406abc95SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
261983a40ce9SCharles Keepax 	unsigned int size;
2620406abc95SCharles Keepax 	int ret;
2621406abc95SCharles Keepax 
2622406abc95SCharles Keepax 	ret = wm_adsp_compr_check_params(stream, params);
2623406abc95SCharles Keepax 	if (ret)
2624406abc95SCharles Keepax 		return ret;
2625406abc95SCharles Keepax 
2626406abc95SCharles Keepax 	compr->size = params->buffer;
2627406abc95SCharles Keepax 
2628406abc95SCharles Keepax 	adsp_dbg(compr->dsp, "fragment_size=%d fragments=%d\n",
2629406abc95SCharles Keepax 		 compr->size.fragment_size, compr->size.fragments);
2630406abc95SCharles Keepax 
263183a40ce9SCharles Keepax 	size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf);
263283a40ce9SCharles Keepax 	compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL);
263383a40ce9SCharles Keepax 	if (!compr->raw_buf)
263483a40ce9SCharles Keepax 		return -ENOMEM;
263583a40ce9SCharles Keepax 
2636da2b3358SCharles Keepax 	compr->sample_rate = params->codec.sample_rate;
2637da2b3358SCharles Keepax 
2638406abc95SCharles Keepax 	return 0;
2639406abc95SCharles Keepax }
2640406abc95SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params);
2641406abc95SCharles Keepax 
2642406abc95SCharles Keepax int wm_adsp_compr_get_caps(struct snd_compr_stream *stream,
2643406abc95SCharles Keepax 			   struct snd_compr_caps *caps)
2644406abc95SCharles Keepax {
2645406abc95SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
2646406abc95SCharles Keepax 	int fw = compr->dsp->fw;
2647406abc95SCharles Keepax 	int i;
2648406abc95SCharles Keepax 
2649406abc95SCharles Keepax 	if (wm_adsp_fw[fw].caps) {
2650406abc95SCharles Keepax 		for (i = 0; i < wm_adsp_fw[fw].num_caps; i++)
2651406abc95SCharles Keepax 			caps->codecs[i] = wm_adsp_fw[fw].caps[i].id;
2652406abc95SCharles Keepax 
2653406abc95SCharles Keepax 		caps->num_codecs = i;
2654406abc95SCharles Keepax 		caps->direction = wm_adsp_fw[fw].compr_direction;
2655406abc95SCharles Keepax 
2656406abc95SCharles Keepax 		caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE;
2657406abc95SCharles Keepax 		caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE;
2658406abc95SCharles Keepax 		caps->min_fragments = WM_ADSP_MIN_FRAGMENTS;
2659406abc95SCharles Keepax 		caps->max_fragments = WM_ADSP_MAX_FRAGMENTS;
2660406abc95SCharles Keepax 	}
2661406abc95SCharles Keepax 
2662406abc95SCharles Keepax 	return 0;
2663406abc95SCharles Keepax }
2664406abc95SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps);
2665406abc95SCharles Keepax 
26662cd19bdbSCharles Keepax static int wm_adsp_read_data_block(struct wm_adsp *dsp, int mem_type,
26672cd19bdbSCharles Keepax 				   unsigned int mem_addr,
26682cd19bdbSCharles Keepax 				   unsigned int num_words, u32 *data)
26692cd19bdbSCharles Keepax {
26702cd19bdbSCharles Keepax 	struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
26712cd19bdbSCharles Keepax 	unsigned int i, reg;
26722cd19bdbSCharles Keepax 	int ret;
26732cd19bdbSCharles Keepax 
26742cd19bdbSCharles Keepax 	if (!mem)
26752cd19bdbSCharles Keepax 		return -EINVAL;
26762cd19bdbSCharles Keepax 
26772cd19bdbSCharles Keepax 	reg = wm_adsp_region_to_reg(mem, mem_addr);
26782cd19bdbSCharles Keepax 
26792cd19bdbSCharles Keepax 	ret = regmap_raw_read(dsp->regmap, reg, data,
26802cd19bdbSCharles Keepax 			      sizeof(*data) * num_words);
26812cd19bdbSCharles Keepax 	if (ret < 0)
26822cd19bdbSCharles Keepax 		return ret;
26832cd19bdbSCharles Keepax 
26842cd19bdbSCharles Keepax 	for (i = 0; i < num_words; ++i)
26852cd19bdbSCharles Keepax 		data[i] = be32_to_cpu(data[i]) & 0x00ffffffu;
26862cd19bdbSCharles Keepax 
26872cd19bdbSCharles Keepax 	return 0;
26882cd19bdbSCharles Keepax }
26892cd19bdbSCharles Keepax 
26902cd19bdbSCharles Keepax static inline int wm_adsp_read_data_word(struct wm_adsp *dsp, int mem_type,
26912cd19bdbSCharles Keepax 					 unsigned int mem_addr, u32 *data)
26922cd19bdbSCharles Keepax {
26932cd19bdbSCharles Keepax 	return wm_adsp_read_data_block(dsp, mem_type, mem_addr, 1, data);
26942cd19bdbSCharles Keepax }
26952cd19bdbSCharles Keepax 
26962cd19bdbSCharles Keepax static int wm_adsp_write_data_word(struct wm_adsp *dsp, int mem_type,
26972cd19bdbSCharles Keepax 				   unsigned int mem_addr, u32 data)
26982cd19bdbSCharles Keepax {
26992cd19bdbSCharles Keepax 	struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
27002cd19bdbSCharles Keepax 	unsigned int reg;
27012cd19bdbSCharles Keepax 
27022cd19bdbSCharles Keepax 	if (!mem)
27032cd19bdbSCharles Keepax 		return -EINVAL;
27042cd19bdbSCharles Keepax 
27052cd19bdbSCharles Keepax 	reg = wm_adsp_region_to_reg(mem, mem_addr);
27062cd19bdbSCharles Keepax 
27072cd19bdbSCharles Keepax 	data = cpu_to_be32(data & 0x00ffffffu);
27082cd19bdbSCharles Keepax 
27092cd19bdbSCharles Keepax 	return regmap_raw_write(dsp->regmap, reg, &data, sizeof(data));
27102cd19bdbSCharles Keepax }
27112cd19bdbSCharles Keepax 
27122cd19bdbSCharles Keepax static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf,
27132cd19bdbSCharles Keepax 				      unsigned int field_offset, u32 *data)
27142cd19bdbSCharles Keepax {
27152cd19bdbSCharles Keepax 	return wm_adsp_read_data_word(buf->dsp, WMFW_ADSP2_XM,
27162cd19bdbSCharles Keepax 				      buf->host_buf_ptr + field_offset, data);
27172cd19bdbSCharles Keepax }
27182cd19bdbSCharles Keepax 
27192cd19bdbSCharles Keepax static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf,
27202cd19bdbSCharles Keepax 				       unsigned int field_offset, u32 data)
27212cd19bdbSCharles Keepax {
27222cd19bdbSCharles Keepax 	return wm_adsp_write_data_word(buf->dsp, WMFW_ADSP2_XM,
27232cd19bdbSCharles Keepax 				       buf->host_buf_ptr + field_offset, data);
27242cd19bdbSCharles Keepax }
27252cd19bdbSCharles Keepax 
27262cd19bdbSCharles Keepax static int wm_adsp_buffer_locate(struct wm_adsp_compr_buf *buf)
27272cd19bdbSCharles Keepax {
27282cd19bdbSCharles Keepax 	struct wm_adsp_alg_region *alg_region;
27292cd19bdbSCharles Keepax 	struct wm_adsp *dsp = buf->dsp;
27302cd19bdbSCharles Keepax 	u32 xmalg, addr, magic;
27312cd19bdbSCharles Keepax 	int i, ret;
27322cd19bdbSCharles Keepax 
27332cd19bdbSCharles Keepax 	alg_region = wm_adsp_find_alg_region(dsp, WMFW_ADSP2_XM, dsp->fw_id);
27342cd19bdbSCharles Keepax 	xmalg = sizeof(struct wm_adsp_system_config_xm_hdr) / sizeof(__be32);
27352cd19bdbSCharles Keepax 
27362cd19bdbSCharles Keepax 	addr = alg_region->base + xmalg + ALG_XM_FIELD(magic);
27372cd19bdbSCharles Keepax 	ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr, &magic);
27382cd19bdbSCharles Keepax 	if (ret < 0)
27392cd19bdbSCharles Keepax 		return ret;
27402cd19bdbSCharles Keepax 
27412cd19bdbSCharles Keepax 	if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC)
27422cd19bdbSCharles Keepax 		return -EINVAL;
27432cd19bdbSCharles Keepax 
27442cd19bdbSCharles Keepax 	addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr);
27452cd19bdbSCharles Keepax 	for (i = 0; i < 5; ++i) {
27462cd19bdbSCharles Keepax 		ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr,
27472cd19bdbSCharles Keepax 					     &buf->host_buf_ptr);
27482cd19bdbSCharles Keepax 		if (ret < 0)
27492cd19bdbSCharles Keepax 			return ret;
27502cd19bdbSCharles Keepax 
27512cd19bdbSCharles Keepax 		if (buf->host_buf_ptr)
27522cd19bdbSCharles Keepax 			break;
27532cd19bdbSCharles Keepax 
27542cd19bdbSCharles Keepax 		usleep_range(1000, 2000);
27552cd19bdbSCharles Keepax 	}
27562cd19bdbSCharles Keepax 
27572cd19bdbSCharles Keepax 	if (!buf->host_buf_ptr)
27582cd19bdbSCharles Keepax 		return -EIO;
27592cd19bdbSCharles Keepax 
27602cd19bdbSCharles Keepax 	adsp_dbg(dsp, "host_buf_ptr=%x\n", buf->host_buf_ptr);
27612cd19bdbSCharles Keepax 
27622cd19bdbSCharles Keepax 	return 0;
27632cd19bdbSCharles Keepax }
27642cd19bdbSCharles Keepax 
27652cd19bdbSCharles Keepax static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf)
27662cd19bdbSCharles Keepax {
27672cd19bdbSCharles Keepax 	const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps;
27682cd19bdbSCharles Keepax 	struct wm_adsp_buffer_region *region;
27692cd19bdbSCharles Keepax 	u32 offset = 0;
27702cd19bdbSCharles Keepax 	int i, ret;
27712cd19bdbSCharles Keepax 
27722cd19bdbSCharles Keepax 	for (i = 0; i < caps->num_regions; ++i) {
27732cd19bdbSCharles Keepax 		region = &buf->regions[i];
27742cd19bdbSCharles Keepax 
27752cd19bdbSCharles Keepax 		region->offset = offset;
27762cd19bdbSCharles Keepax 		region->mem_type = caps->region_defs[i].mem_type;
27772cd19bdbSCharles Keepax 
27782cd19bdbSCharles Keepax 		ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset,
27792cd19bdbSCharles Keepax 					  &region->base_addr);
27802cd19bdbSCharles Keepax 		if (ret < 0)
27812cd19bdbSCharles Keepax 			return ret;
27822cd19bdbSCharles Keepax 
27832cd19bdbSCharles Keepax 		ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset,
27842cd19bdbSCharles Keepax 					  &offset);
27852cd19bdbSCharles Keepax 		if (ret < 0)
27862cd19bdbSCharles Keepax 			return ret;
27872cd19bdbSCharles Keepax 
27882cd19bdbSCharles Keepax 		region->cumulative_size = offset;
27892cd19bdbSCharles Keepax 
27902cd19bdbSCharles Keepax 		adsp_dbg(buf->dsp,
27912cd19bdbSCharles Keepax 			 "region=%d type=%d base=%04x off=%04x size=%04x\n",
27922cd19bdbSCharles Keepax 			 i, region->mem_type, region->base_addr,
27932cd19bdbSCharles Keepax 			 region->offset, region->cumulative_size);
27942cd19bdbSCharles Keepax 	}
27952cd19bdbSCharles Keepax 
27962cd19bdbSCharles Keepax 	return 0;
27972cd19bdbSCharles Keepax }
27982cd19bdbSCharles Keepax 
27992cd19bdbSCharles Keepax static int wm_adsp_buffer_init(struct wm_adsp *dsp)
28002cd19bdbSCharles Keepax {
28012cd19bdbSCharles Keepax 	struct wm_adsp_compr_buf *buf;
28022cd19bdbSCharles Keepax 	int ret;
28032cd19bdbSCharles Keepax 
28042cd19bdbSCharles Keepax 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
28052cd19bdbSCharles Keepax 	if (!buf)
28062cd19bdbSCharles Keepax 		return -ENOMEM;
28072cd19bdbSCharles Keepax 
28082cd19bdbSCharles Keepax 	buf->dsp = dsp;
2809565ace46SCharles Keepax 	buf->read_index = -1;
2810565ace46SCharles Keepax 	buf->irq_count = 0xFFFFFFFF;
28112cd19bdbSCharles Keepax 
28122cd19bdbSCharles Keepax 	ret = wm_adsp_buffer_locate(buf);
28132cd19bdbSCharles Keepax 	if (ret < 0) {
28142cd19bdbSCharles Keepax 		adsp_err(dsp, "Failed to acquire host buffer: %d\n", ret);
28152cd19bdbSCharles Keepax 		goto err_buffer;
28162cd19bdbSCharles Keepax 	}
28172cd19bdbSCharles Keepax 
28182cd19bdbSCharles Keepax 	buf->regions = kcalloc(wm_adsp_fw[dsp->fw].caps->num_regions,
28192cd19bdbSCharles Keepax 			       sizeof(*buf->regions), GFP_KERNEL);
28202cd19bdbSCharles Keepax 	if (!buf->regions) {
28212cd19bdbSCharles Keepax 		ret = -ENOMEM;
28222cd19bdbSCharles Keepax 		goto err_buffer;
28232cd19bdbSCharles Keepax 	}
28242cd19bdbSCharles Keepax 
28252cd19bdbSCharles Keepax 	ret = wm_adsp_buffer_populate(buf);
28262cd19bdbSCharles Keepax 	if (ret < 0) {
28272cd19bdbSCharles Keepax 		adsp_err(dsp, "Failed to populate host buffer: %d\n", ret);
28282cd19bdbSCharles Keepax 		goto err_regions;
28292cd19bdbSCharles Keepax 	}
28302cd19bdbSCharles Keepax 
28312cd19bdbSCharles Keepax 	dsp->buffer = buf;
28322cd19bdbSCharles Keepax 
28332cd19bdbSCharles Keepax 	return 0;
28342cd19bdbSCharles Keepax 
28352cd19bdbSCharles Keepax err_regions:
28362cd19bdbSCharles Keepax 	kfree(buf->regions);
28372cd19bdbSCharles Keepax err_buffer:
28382cd19bdbSCharles Keepax 	kfree(buf);
28392cd19bdbSCharles Keepax 	return ret;
28402cd19bdbSCharles Keepax }
28412cd19bdbSCharles Keepax 
28422cd19bdbSCharles Keepax static int wm_adsp_buffer_free(struct wm_adsp *dsp)
28432cd19bdbSCharles Keepax {
28442cd19bdbSCharles Keepax 	if (dsp->buffer) {
2845721be3beSCharles Keepax 		wm_adsp_compr_detach(dsp->buffer->compr);
2846721be3beSCharles Keepax 
28472cd19bdbSCharles Keepax 		kfree(dsp->buffer->regions);
28482cd19bdbSCharles Keepax 		kfree(dsp->buffer);
28492cd19bdbSCharles Keepax 
28502cd19bdbSCharles Keepax 		dsp->buffer = NULL;
28512cd19bdbSCharles Keepax 	}
28522cd19bdbSCharles Keepax 
28532cd19bdbSCharles Keepax 	return 0;
28542cd19bdbSCharles Keepax }
28552cd19bdbSCharles Keepax 
285695fe9597SCharles Keepax int wm_adsp_compr_trigger(struct snd_compr_stream *stream, int cmd)
285795fe9597SCharles Keepax {
285895fe9597SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
285995fe9597SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
286095fe9597SCharles Keepax 	int ret = 0;
286195fe9597SCharles Keepax 
286295fe9597SCharles Keepax 	adsp_dbg(dsp, "Trigger: %d\n", cmd);
286395fe9597SCharles Keepax 
286495fe9597SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
286595fe9597SCharles Keepax 
286695fe9597SCharles Keepax 	switch (cmd) {
286795fe9597SCharles Keepax 	case SNDRV_PCM_TRIGGER_START:
286895fe9597SCharles Keepax 		if (wm_adsp_compr_attached(compr))
286995fe9597SCharles Keepax 			break;
287095fe9597SCharles Keepax 
287195fe9597SCharles Keepax 		ret = wm_adsp_compr_attach(compr);
287295fe9597SCharles Keepax 		if (ret < 0) {
287395fe9597SCharles Keepax 			adsp_err(dsp, "Failed to link buffer and stream: %d\n",
287495fe9597SCharles Keepax 				 ret);
287595fe9597SCharles Keepax 			break;
287695fe9597SCharles Keepax 		}
2877565ace46SCharles Keepax 
2878565ace46SCharles Keepax 		/* Trigger the IRQ at one fragment of data */
2879565ace46SCharles Keepax 		ret = wm_adsp_buffer_write(compr->buf,
2880565ace46SCharles Keepax 					   HOST_BUFFER_FIELD(high_water_mark),
2881565ace46SCharles Keepax 					   wm_adsp_compr_frag_words(compr));
2882565ace46SCharles Keepax 		if (ret < 0) {
2883565ace46SCharles Keepax 			adsp_err(dsp, "Failed to set high water mark: %d\n",
2884565ace46SCharles Keepax 				 ret);
2885565ace46SCharles Keepax 			break;
2886565ace46SCharles Keepax 		}
288795fe9597SCharles Keepax 		break;
288895fe9597SCharles Keepax 	case SNDRV_PCM_TRIGGER_STOP:
288995fe9597SCharles Keepax 		break;
289095fe9597SCharles Keepax 	default:
289195fe9597SCharles Keepax 		ret = -EINVAL;
289295fe9597SCharles Keepax 		break;
289395fe9597SCharles Keepax 	}
289495fe9597SCharles Keepax 
289595fe9597SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
289695fe9597SCharles Keepax 
289795fe9597SCharles Keepax 	return ret;
289895fe9597SCharles Keepax }
289995fe9597SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger);
290095fe9597SCharles Keepax 
2901565ace46SCharles Keepax static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf)
2902565ace46SCharles Keepax {
2903565ace46SCharles Keepax 	int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1;
2904565ace46SCharles Keepax 
2905565ace46SCharles Keepax 	return buf->regions[last_region].cumulative_size;
2906565ace46SCharles Keepax }
2907565ace46SCharles Keepax 
2908565ace46SCharles Keepax static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf)
2909565ace46SCharles Keepax {
2910565ace46SCharles Keepax 	u32 next_read_index, next_write_index;
2911565ace46SCharles Keepax 	int write_index, read_index, avail;
2912565ace46SCharles Keepax 	int ret;
2913565ace46SCharles Keepax 
2914565ace46SCharles Keepax 	/* Only sync read index if we haven't already read a valid index */
2915565ace46SCharles Keepax 	if (buf->read_index < 0) {
2916565ace46SCharles Keepax 		ret = wm_adsp_buffer_read(buf,
2917565ace46SCharles Keepax 				HOST_BUFFER_FIELD(next_read_index),
2918565ace46SCharles Keepax 				&next_read_index);
2919565ace46SCharles Keepax 		if (ret < 0)
2920565ace46SCharles Keepax 			return ret;
2921565ace46SCharles Keepax 
2922565ace46SCharles Keepax 		read_index = sign_extend32(next_read_index, 23);
2923565ace46SCharles Keepax 
2924565ace46SCharles Keepax 		if (read_index < 0) {
2925565ace46SCharles Keepax 			adsp_dbg(buf->dsp, "Avail check on unstarted stream\n");
2926565ace46SCharles Keepax 			return 0;
2927565ace46SCharles Keepax 		}
2928565ace46SCharles Keepax 
2929565ace46SCharles Keepax 		buf->read_index = read_index;
2930565ace46SCharles Keepax 	}
2931565ace46SCharles Keepax 
2932565ace46SCharles Keepax 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index),
2933565ace46SCharles Keepax 			&next_write_index);
2934565ace46SCharles Keepax 	if (ret < 0)
2935565ace46SCharles Keepax 		return ret;
2936565ace46SCharles Keepax 
2937565ace46SCharles Keepax 	write_index = sign_extend32(next_write_index, 23);
2938565ace46SCharles Keepax 
2939565ace46SCharles Keepax 	avail = write_index - buf->read_index;
2940565ace46SCharles Keepax 	if (avail < 0)
2941565ace46SCharles Keepax 		avail += wm_adsp_buffer_size(buf);
2942565ace46SCharles Keepax 
2943565ace46SCharles Keepax 	adsp_dbg(buf->dsp, "readindex=0x%x, writeindex=0x%x, avail=%d\n",
294433d740e0SCharles Keepax 		 buf->read_index, write_index, avail * WM_ADSP_DATA_WORD_SIZE);
2945565ace46SCharles Keepax 
2946565ace46SCharles Keepax 	buf->avail = avail;
2947565ace46SCharles Keepax 
2948565ace46SCharles Keepax 	return 0;
2949565ace46SCharles Keepax }
2950565ace46SCharles Keepax 
29519771b18aSCharles Keepax static int wm_adsp_buffer_get_error(struct wm_adsp_compr_buf *buf)
29529771b18aSCharles Keepax {
29539771b18aSCharles Keepax 	int ret;
29549771b18aSCharles Keepax 
29559771b18aSCharles Keepax 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error);
29569771b18aSCharles Keepax 	if (ret < 0) {
29579771b18aSCharles Keepax 		adsp_err(buf->dsp, "Failed to check buffer error: %d\n", ret);
29589771b18aSCharles Keepax 		return ret;
29599771b18aSCharles Keepax 	}
29609771b18aSCharles Keepax 	if (buf->error != 0) {
29619771b18aSCharles Keepax 		adsp_err(buf->dsp, "Buffer error occurred: %d\n", buf->error);
29629771b18aSCharles Keepax 		return -EIO;
29639771b18aSCharles Keepax 	}
29649771b18aSCharles Keepax 
29659771b18aSCharles Keepax 	return 0;
29669771b18aSCharles Keepax }
29679771b18aSCharles Keepax 
2968565ace46SCharles Keepax int wm_adsp_compr_handle_irq(struct wm_adsp *dsp)
2969565ace46SCharles Keepax {
2970612047f0SCharles Keepax 	struct wm_adsp_compr_buf *buf;
2971612047f0SCharles Keepax 	struct wm_adsp_compr *compr;
2972565ace46SCharles Keepax 	int ret = 0;
2973565ace46SCharles Keepax 
2974565ace46SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2975565ace46SCharles Keepax 
2976612047f0SCharles Keepax 	buf = dsp->buffer;
2977612047f0SCharles Keepax 	compr = dsp->compr;
2978612047f0SCharles Keepax 
2979565ace46SCharles Keepax 	if (!buf) {
2980565ace46SCharles Keepax 		ret = -ENODEV;
2981565ace46SCharles Keepax 		goto out;
2982565ace46SCharles Keepax 	}
2983565ace46SCharles Keepax 
2984565ace46SCharles Keepax 	adsp_dbg(dsp, "Handling buffer IRQ\n");
2985565ace46SCharles Keepax 
29869771b18aSCharles Keepax 	ret = wm_adsp_buffer_get_error(buf);
29879771b18aSCharles Keepax 	if (ret < 0)
29885847609eSCharles Keepax 		goto out_notify; /* Wake poll to report error */
2989565ace46SCharles Keepax 
2990565ace46SCharles Keepax 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count),
2991565ace46SCharles Keepax 				  &buf->irq_count);
2992565ace46SCharles Keepax 	if (ret < 0) {
2993565ace46SCharles Keepax 		adsp_err(dsp, "Failed to get irq_count: %d\n", ret);
2994565ace46SCharles Keepax 		goto out;
2995565ace46SCharles Keepax 	}
2996565ace46SCharles Keepax 
2997565ace46SCharles Keepax 	ret = wm_adsp_buffer_update_avail(buf);
2998565ace46SCharles Keepax 	if (ret < 0) {
2999565ace46SCharles Keepax 		adsp_err(dsp, "Error reading avail: %d\n", ret);
3000565ace46SCharles Keepax 		goto out;
3001565ace46SCharles Keepax 	}
3002565ace46SCharles Keepax 
30035847609eSCharles Keepax out_notify:
3004c7dae7c4SCharles Keepax 	if (compr && compr->stream)
300583a40ce9SCharles Keepax 		snd_compr_fragment_elapsed(compr->stream);
300683a40ce9SCharles Keepax 
3007565ace46SCharles Keepax out:
3008565ace46SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
3009565ace46SCharles Keepax 
3010565ace46SCharles Keepax 	return ret;
3011565ace46SCharles Keepax }
3012565ace46SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq);
3013565ace46SCharles Keepax 
3014565ace46SCharles Keepax static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf)
3015565ace46SCharles Keepax {
3016565ace46SCharles Keepax 	if (buf->irq_count & 0x01)
3017565ace46SCharles Keepax 		return 0;
3018565ace46SCharles Keepax 
3019565ace46SCharles Keepax 	adsp_dbg(buf->dsp, "Enable IRQ(0x%x) for next fragment\n",
3020565ace46SCharles Keepax 		 buf->irq_count);
3021565ace46SCharles Keepax 
3022565ace46SCharles Keepax 	buf->irq_count |= 0x01;
3023565ace46SCharles Keepax 
3024565ace46SCharles Keepax 	return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack),
3025565ace46SCharles Keepax 				    buf->irq_count);
3026565ace46SCharles Keepax }
3027565ace46SCharles Keepax 
3028565ace46SCharles Keepax int wm_adsp_compr_pointer(struct snd_compr_stream *stream,
3029565ace46SCharles Keepax 			  struct snd_compr_tstamp *tstamp)
3030565ace46SCharles Keepax {
3031565ace46SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
3032565ace46SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
3033612047f0SCharles Keepax 	struct wm_adsp_compr_buf *buf;
3034565ace46SCharles Keepax 	int ret = 0;
3035565ace46SCharles Keepax 
3036565ace46SCharles Keepax 	adsp_dbg(dsp, "Pointer request\n");
3037565ace46SCharles Keepax 
3038565ace46SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
3039565ace46SCharles Keepax 
3040612047f0SCharles Keepax 	buf = compr->buf;
3041612047f0SCharles Keepax 
304228ee3d73SCharles Keepax 	if (!compr->buf || compr->buf->error) {
30438d280664SCharles Keepax 		snd_compr_stop_error(stream, SNDRV_PCM_STATE_XRUN);
3044565ace46SCharles Keepax 		ret = -EIO;
3045565ace46SCharles Keepax 		goto out;
3046565ace46SCharles Keepax 	}
3047565ace46SCharles Keepax 
3048565ace46SCharles Keepax 	if (buf->avail < wm_adsp_compr_frag_words(compr)) {
3049565ace46SCharles Keepax 		ret = wm_adsp_buffer_update_avail(buf);
3050565ace46SCharles Keepax 		if (ret < 0) {
3051565ace46SCharles Keepax 			adsp_err(dsp, "Error reading avail: %d\n", ret);
3052565ace46SCharles Keepax 			goto out;
3053565ace46SCharles Keepax 		}
3054565ace46SCharles Keepax 
3055565ace46SCharles Keepax 		/*
3056565ace46SCharles Keepax 		 * If we really have less than 1 fragment available tell the
3057565ace46SCharles Keepax 		 * DSP to inform us once a whole fragment is available.
3058565ace46SCharles Keepax 		 */
3059565ace46SCharles Keepax 		if (buf->avail < wm_adsp_compr_frag_words(compr)) {
30605847609eSCharles Keepax 			ret = wm_adsp_buffer_get_error(buf);
30618d280664SCharles Keepax 			if (ret < 0) {
30628d280664SCharles Keepax 				if (compr->buf->error)
30638d280664SCharles Keepax 					snd_compr_stop_error(stream,
30648d280664SCharles Keepax 							SNDRV_PCM_STATE_XRUN);
30655847609eSCharles Keepax 				goto out;
30668d280664SCharles Keepax 			}
30675847609eSCharles Keepax 
3068565ace46SCharles Keepax 			ret = wm_adsp_buffer_reenable_irq(buf);
3069565ace46SCharles Keepax 			if (ret < 0) {
3070565ace46SCharles Keepax 				adsp_err(dsp,
3071565ace46SCharles Keepax 					 "Failed to re-enable buffer IRQ: %d\n",
3072565ace46SCharles Keepax 					 ret);
3073565ace46SCharles Keepax 				goto out;
3074565ace46SCharles Keepax 			}
3075565ace46SCharles Keepax 		}
3076565ace46SCharles Keepax 	}
3077565ace46SCharles Keepax 
3078565ace46SCharles Keepax 	tstamp->copied_total = compr->copied_total;
3079565ace46SCharles Keepax 	tstamp->copied_total += buf->avail * WM_ADSP_DATA_WORD_SIZE;
3080da2b3358SCharles Keepax 	tstamp->sampling_rate = compr->sample_rate;
3081565ace46SCharles Keepax 
3082565ace46SCharles Keepax out:
3083565ace46SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
3084565ace46SCharles Keepax 
3085565ace46SCharles Keepax 	return ret;
3086565ace46SCharles Keepax }
3087565ace46SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer);
3088565ace46SCharles Keepax 
308983a40ce9SCharles Keepax static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target)
309083a40ce9SCharles Keepax {
309183a40ce9SCharles Keepax 	struct wm_adsp_compr_buf *buf = compr->buf;
309283a40ce9SCharles Keepax 	u8 *pack_in = (u8 *)compr->raw_buf;
309383a40ce9SCharles Keepax 	u8 *pack_out = (u8 *)compr->raw_buf;
309483a40ce9SCharles Keepax 	unsigned int adsp_addr;
309583a40ce9SCharles Keepax 	int mem_type, nwords, max_read;
309683a40ce9SCharles Keepax 	int i, j, ret;
309783a40ce9SCharles Keepax 
309883a40ce9SCharles Keepax 	/* Calculate read parameters */
309983a40ce9SCharles Keepax 	for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i)
310083a40ce9SCharles Keepax 		if (buf->read_index < buf->regions[i].cumulative_size)
310183a40ce9SCharles Keepax 			break;
310283a40ce9SCharles Keepax 
310383a40ce9SCharles Keepax 	if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions)
310483a40ce9SCharles Keepax 		return -EINVAL;
310583a40ce9SCharles Keepax 
310683a40ce9SCharles Keepax 	mem_type = buf->regions[i].mem_type;
310783a40ce9SCharles Keepax 	adsp_addr = buf->regions[i].base_addr +
310883a40ce9SCharles Keepax 		    (buf->read_index - buf->regions[i].offset);
310983a40ce9SCharles Keepax 
311083a40ce9SCharles Keepax 	max_read = wm_adsp_compr_frag_words(compr);
311183a40ce9SCharles Keepax 	nwords = buf->regions[i].cumulative_size - buf->read_index;
311283a40ce9SCharles Keepax 
311383a40ce9SCharles Keepax 	if (nwords > target)
311483a40ce9SCharles Keepax 		nwords = target;
311583a40ce9SCharles Keepax 	if (nwords > buf->avail)
311683a40ce9SCharles Keepax 		nwords = buf->avail;
311783a40ce9SCharles Keepax 	if (nwords > max_read)
311883a40ce9SCharles Keepax 		nwords = max_read;
311983a40ce9SCharles Keepax 	if (!nwords)
312083a40ce9SCharles Keepax 		return 0;
312183a40ce9SCharles Keepax 
312283a40ce9SCharles Keepax 	/* Read data from DSP */
312383a40ce9SCharles Keepax 	ret = wm_adsp_read_data_block(buf->dsp, mem_type, adsp_addr,
312483a40ce9SCharles Keepax 				      nwords, compr->raw_buf);
312583a40ce9SCharles Keepax 	if (ret < 0)
312683a40ce9SCharles Keepax 		return ret;
312783a40ce9SCharles Keepax 
312883a40ce9SCharles Keepax 	/* Remove the padding bytes from the data read from the DSP */
312983a40ce9SCharles Keepax 	for (i = 0; i < nwords; i++) {
313083a40ce9SCharles Keepax 		for (j = 0; j < WM_ADSP_DATA_WORD_SIZE; j++)
313183a40ce9SCharles Keepax 			*pack_out++ = *pack_in++;
313283a40ce9SCharles Keepax 
313383a40ce9SCharles Keepax 		pack_in += sizeof(*(compr->raw_buf)) - WM_ADSP_DATA_WORD_SIZE;
313483a40ce9SCharles Keepax 	}
313583a40ce9SCharles Keepax 
313683a40ce9SCharles Keepax 	/* update read index to account for words read */
313783a40ce9SCharles Keepax 	buf->read_index += nwords;
313883a40ce9SCharles Keepax 	if (buf->read_index == wm_adsp_buffer_size(buf))
313983a40ce9SCharles Keepax 		buf->read_index = 0;
314083a40ce9SCharles Keepax 
314183a40ce9SCharles Keepax 	ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index),
314283a40ce9SCharles Keepax 				   buf->read_index);
314383a40ce9SCharles Keepax 	if (ret < 0)
314483a40ce9SCharles Keepax 		return ret;
314583a40ce9SCharles Keepax 
314683a40ce9SCharles Keepax 	/* update avail to account for words read */
314783a40ce9SCharles Keepax 	buf->avail -= nwords;
314883a40ce9SCharles Keepax 
314983a40ce9SCharles Keepax 	return nwords;
315083a40ce9SCharles Keepax }
315183a40ce9SCharles Keepax 
315283a40ce9SCharles Keepax static int wm_adsp_compr_read(struct wm_adsp_compr *compr,
315383a40ce9SCharles Keepax 			      char __user *buf, size_t count)
315483a40ce9SCharles Keepax {
315583a40ce9SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
315683a40ce9SCharles Keepax 	int ntotal = 0;
315783a40ce9SCharles Keepax 	int nwords, nbytes;
315883a40ce9SCharles Keepax 
315983a40ce9SCharles Keepax 	adsp_dbg(dsp, "Requested read of %zu bytes\n", count);
316083a40ce9SCharles Keepax 
316128ee3d73SCharles Keepax 	if (!compr->buf || compr->buf->error) {
31628d280664SCharles Keepax 		snd_compr_stop_error(compr->stream, SNDRV_PCM_STATE_XRUN);
316383a40ce9SCharles Keepax 		return -EIO;
31648d280664SCharles Keepax 	}
316583a40ce9SCharles Keepax 
316683a40ce9SCharles Keepax 	count /= WM_ADSP_DATA_WORD_SIZE;
316783a40ce9SCharles Keepax 
316883a40ce9SCharles Keepax 	do {
316983a40ce9SCharles Keepax 		nwords = wm_adsp_buffer_capture_block(compr, count);
317083a40ce9SCharles Keepax 		if (nwords < 0) {
317183a40ce9SCharles Keepax 			adsp_err(dsp, "Failed to capture block: %d\n", nwords);
317283a40ce9SCharles Keepax 			return nwords;
317383a40ce9SCharles Keepax 		}
317483a40ce9SCharles Keepax 
317583a40ce9SCharles Keepax 		nbytes = nwords * WM_ADSP_DATA_WORD_SIZE;
317683a40ce9SCharles Keepax 
317783a40ce9SCharles Keepax 		adsp_dbg(dsp, "Read %d bytes\n", nbytes);
317883a40ce9SCharles Keepax 
317983a40ce9SCharles Keepax 		if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) {
318083a40ce9SCharles Keepax 			adsp_err(dsp, "Failed to copy data to user: %d, %d\n",
318183a40ce9SCharles Keepax 				 ntotal, nbytes);
318283a40ce9SCharles Keepax 			return -EFAULT;
318383a40ce9SCharles Keepax 		}
318483a40ce9SCharles Keepax 
318583a40ce9SCharles Keepax 		count -= nwords;
318683a40ce9SCharles Keepax 		ntotal += nbytes;
318783a40ce9SCharles Keepax 	} while (nwords > 0 && count > 0);
318883a40ce9SCharles Keepax 
318983a40ce9SCharles Keepax 	compr->copied_total += ntotal;
319083a40ce9SCharles Keepax 
319183a40ce9SCharles Keepax 	return ntotal;
319283a40ce9SCharles Keepax }
319383a40ce9SCharles Keepax 
319483a40ce9SCharles Keepax int wm_adsp_compr_copy(struct snd_compr_stream *stream, char __user *buf,
319583a40ce9SCharles Keepax 		       size_t count)
319683a40ce9SCharles Keepax {
319783a40ce9SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
319883a40ce9SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
319983a40ce9SCharles Keepax 	int ret;
320083a40ce9SCharles Keepax 
320183a40ce9SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
320283a40ce9SCharles Keepax 
320383a40ce9SCharles Keepax 	if (stream->direction == SND_COMPRESS_CAPTURE)
320483a40ce9SCharles Keepax 		ret = wm_adsp_compr_read(compr, buf, count);
320583a40ce9SCharles Keepax 	else
320683a40ce9SCharles Keepax 		ret = -ENOTSUPP;
320783a40ce9SCharles Keepax 
320883a40ce9SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
320983a40ce9SCharles Keepax 
321083a40ce9SCharles Keepax 	return ret;
321183a40ce9SCharles Keepax }
321283a40ce9SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_copy);
321383a40ce9SCharles Keepax 
32140a37c6efSPraveen Diwakar MODULE_LICENSE("GPL v2");
3215