xref: /openbmc/linux/sound/soc/codecs/wm_adsp.c (revision 7d00cd97)
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 <linux/mfd/arizona/registers.h>
362159ad93SMark Brown 
37dc91428aSMark Brown #include "arizona.h"
382159ad93SMark Brown #include "wm_adsp.h"
392159ad93SMark Brown 
402159ad93SMark Brown #define adsp_crit(_dsp, fmt, ...) \
412159ad93SMark Brown 	dev_crit(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
422159ad93SMark Brown #define adsp_err(_dsp, fmt, ...) \
432159ad93SMark Brown 	dev_err(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
442159ad93SMark Brown #define adsp_warn(_dsp, fmt, ...) \
452159ad93SMark Brown 	dev_warn(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
462159ad93SMark Brown #define adsp_info(_dsp, fmt, ...) \
472159ad93SMark Brown 	dev_info(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
482159ad93SMark Brown #define adsp_dbg(_dsp, fmt, ...) \
492159ad93SMark Brown 	dev_dbg(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
502159ad93SMark Brown 
512159ad93SMark Brown #define ADSP1_CONTROL_1                   0x00
522159ad93SMark Brown #define ADSP1_CONTROL_2                   0x02
532159ad93SMark Brown #define ADSP1_CONTROL_3                   0x03
542159ad93SMark Brown #define ADSP1_CONTROL_4                   0x04
552159ad93SMark Brown #define ADSP1_CONTROL_5                   0x06
562159ad93SMark Brown #define ADSP1_CONTROL_6                   0x07
572159ad93SMark Brown #define ADSP1_CONTROL_7                   0x08
582159ad93SMark Brown #define ADSP1_CONTROL_8                   0x09
592159ad93SMark Brown #define ADSP1_CONTROL_9                   0x0A
602159ad93SMark Brown #define ADSP1_CONTROL_10                  0x0B
612159ad93SMark Brown #define ADSP1_CONTROL_11                  0x0C
622159ad93SMark Brown #define ADSP1_CONTROL_12                  0x0D
632159ad93SMark Brown #define ADSP1_CONTROL_13                  0x0F
642159ad93SMark Brown #define ADSP1_CONTROL_14                  0x10
652159ad93SMark Brown #define ADSP1_CONTROL_15                  0x11
662159ad93SMark Brown #define ADSP1_CONTROL_16                  0x12
672159ad93SMark Brown #define ADSP1_CONTROL_17                  0x13
682159ad93SMark Brown #define ADSP1_CONTROL_18                  0x14
692159ad93SMark Brown #define ADSP1_CONTROL_19                  0x16
702159ad93SMark Brown #define ADSP1_CONTROL_20                  0x17
712159ad93SMark Brown #define ADSP1_CONTROL_21                  0x18
722159ad93SMark Brown #define ADSP1_CONTROL_22                  0x1A
732159ad93SMark Brown #define ADSP1_CONTROL_23                  0x1B
742159ad93SMark Brown #define ADSP1_CONTROL_24                  0x1C
752159ad93SMark Brown #define ADSP1_CONTROL_25                  0x1E
762159ad93SMark Brown #define ADSP1_CONTROL_26                  0x20
772159ad93SMark Brown #define ADSP1_CONTROL_27                  0x21
782159ad93SMark Brown #define ADSP1_CONTROL_28                  0x22
792159ad93SMark Brown #define ADSP1_CONTROL_29                  0x23
802159ad93SMark Brown #define ADSP1_CONTROL_30                  0x24
812159ad93SMark Brown #define ADSP1_CONTROL_31                  0x26
822159ad93SMark Brown 
832159ad93SMark Brown /*
842159ad93SMark Brown  * ADSP1 Control 19
852159ad93SMark Brown  */
862159ad93SMark Brown #define ADSP1_WDMA_BUFFER_LENGTH_MASK     0x00FF  /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
872159ad93SMark Brown #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT         0  /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
882159ad93SMark Brown #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH         8  /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
892159ad93SMark Brown 
902159ad93SMark Brown 
912159ad93SMark Brown /*
922159ad93SMark Brown  * ADSP1 Control 30
932159ad93SMark Brown  */
942159ad93SMark Brown #define ADSP1_DBG_CLK_ENA                 0x0008  /* DSP1_DBG_CLK_ENA */
952159ad93SMark Brown #define ADSP1_DBG_CLK_ENA_MASK            0x0008  /* DSP1_DBG_CLK_ENA */
962159ad93SMark Brown #define ADSP1_DBG_CLK_ENA_SHIFT                3  /* DSP1_DBG_CLK_ENA */
972159ad93SMark Brown #define ADSP1_DBG_CLK_ENA_WIDTH                1  /* DSP1_DBG_CLK_ENA */
982159ad93SMark Brown #define ADSP1_SYS_ENA                     0x0004  /* DSP1_SYS_ENA */
992159ad93SMark Brown #define ADSP1_SYS_ENA_MASK                0x0004  /* DSP1_SYS_ENA */
1002159ad93SMark Brown #define ADSP1_SYS_ENA_SHIFT                    2  /* DSP1_SYS_ENA */
1012159ad93SMark Brown #define ADSP1_SYS_ENA_WIDTH                    1  /* DSP1_SYS_ENA */
1022159ad93SMark Brown #define ADSP1_CORE_ENA                    0x0002  /* DSP1_CORE_ENA */
1032159ad93SMark Brown #define ADSP1_CORE_ENA_MASK               0x0002  /* DSP1_CORE_ENA */
1042159ad93SMark Brown #define ADSP1_CORE_ENA_SHIFT                   1  /* DSP1_CORE_ENA */
1052159ad93SMark Brown #define ADSP1_CORE_ENA_WIDTH                   1  /* DSP1_CORE_ENA */
1062159ad93SMark Brown #define ADSP1_START                       0x0001  /* DSP1_START */
1072159ad93SMark Brown #define ADSP1_START_MASK                  0x0001  /* DSP1_START */
1082159ad93SMark Brown #define ADSP1_START_SHIFT                      0  /* DSP1_START */
1092159ad93SMark Brown #define ADSP1_START_WIDTH                      1  /* DSP1_START */
1102159ad93SMark Brown 
11194e205bfSChris Rattray /*
11294e205bfSChris Rattray  * ADSP1 Control 31
11394e205bfSChris Rattray  */
11494e205bfSChris Rattray #define ADSP1_CLK_SEL_MASK                0x0007  /* CLK_SEL_ENA */
11594e205bfSChris Rattray #define ADSP1_CLK_SEL_SHIFT                    0  /* CLK_SEL_ENA */
11694e205bfSChris Rattray #define ADSP1_CLK_SEL_WIDTH                    3  /* CLK_SEL_ENA */
11794e205bfSChris Rattray 
1182d30b575SMark Brown #define ADSP2_CONTROL        0x0
1192d30b575SMark Brown #define ADSP2_CLOCKING       0x1
1202d30b575SMark Brown #define ADSP2_STATUS1        0x4
1212d30b575SMark Brown #define ADSP2_WDMA_CONFIG_1 0x30
1222d30b575SMark Brown #define ADSP2_WDMA_CONFIG_2 0x31
1232d30b575SMark Brown #define ADSP2_RDMA_CONFIG_1 0x34
1242159ad93SMark Brown 
12510337b07SRichard Fitzgerald #define ADSP2_SCRATCH0        0x40
12610337b07SRichard Fitzgerald #define ADSP2_SCRATCH1        0x41
12710337b07SRichard Fitzgerald #define ADSP2_SCRATCH2        0x42
12810337b07SRichard Fitzgerald #define ADSP2_SCRATCH3        0x43
12910337b07SRichard Fitzgerald 
1302159ad93SMark Brown /*
1312159ad93SMark Brown  * ADSP2 Control
1322159ad93SMark Brown  */
1332159ad93SMark Brown 
1342159ad93SMark Brown #define ADSP2_MEM_ENA                     0x0010  /* DSP1_MEM_ENA */
1352159ad93SMark Brown #define ADSP2_MEM_ENA_MASK                0x0010  /* DSP1_MEM_ENA */
1362159ad93SMark Brown #define ADSP2_MEM_ENA_SHIFT                    4  /* DSP1_MEM_ENA */
1372159ad93SMark Brown #define ADSP2_MEM_ENA_WIDTH                    1  /* DSP1_MEM_ENA */
1382159ad93SMark Brown #define ADSP2_SYS_ENA                     0x0004  /* DSP1_SYS_ENA */
1392159ad93SMark Brown #define ADSP2_SYS_ENA_MASK                0x0004  /* DSP1_SYS_ENA */
1402159ad93SMark Brown #define ADSP2_SYS_ENA_SHIFT                    2  /* DSP1_SYS_ENA */
1412159ad93SMark Brown #define ADSP2_SYS_ENA_WIDTH                    1  /* DSP1_SYS_ENA */
1422159ad93SMark Brown #define ADSP2_CORE_ENA                    0x0002  /* DSP1_CORE_ENA */
1432159ad93SMark Brown #define ADSP2_CORE_ENA_MASK               0x0002  /* DSP1_CORE_ENA */
1442159ad93SMark Brown #define ADSP2_CORE_ENA_SHIFT                   1  /* DSP1_CORE_ENA */
1452159ad93SMark Brown #define ADSP2_CORE_ENA_WIDTH                   1  /* DSP1_CORE_ENA */
1462159ad93SMark Brown #define ADSP2_START                       0x0001  /* DSP1_START */
1472159ad93SMark Brown #define ADSP2_START_MASK                  0x0001  /* DSP1_START */
1482159ad93SMark Brown #define ADSP2_START_SHIFT                      0  /* DSP1_START */
1492159ad93SMark Brown #define ADSP2_START_WIDTH                      1  /* DSP1_START */
1502159ad93SMark Brown 
1512159ad93SMark Brown /*
152973838a0SMark Brown  * ADSP2 clocking
153973838a0SMark Brown  */
154973838a0SMark Brown #define ADSP2_CLK_SEL_MASK                0x0007  /* CLK_SEL_ENA */
155973838a0SMark Brown #define ADSP2_CLK_SEL_SHIFT                    0  /* CLK_SEL_ENA */
156973838a0SMark Brown #define ADSP2_CLK_SEL_WIDTH                    3  /* CLK_SEL_ENA */
157973838a0SMark Brown 
158973838a0SMark Brown /*
1592159ad93SMark Brown  * ADSP2 Status 1
1602159ad93SMark Brown  */
1612159ad93SMark Brown #define ADSP2_RAM_RDY                     0x0001
1622159ad93SMark Brown #define ADSP2_RAM_RDY_MASK                0x0001
1632159ad93SMark Brown #define ADSP2_RAM_RDY_SHIFT                    0
1642159ad93SMark Brown #define ADSP2_RAM_RDY_WIDTH                    1
1652159ad93SMark Brown 
166cf17c83cSMark Brown struct wm_adsp_buf {
167cf17c83cSMark Brown 	struct list_head list;
168cf17c83cSMark Brown 	void *buf;
169cf17c83cSMark Brown };
170cf17c83cSMark Brown 
171cf17c83cSMark Brown static struct wm_adsp_buf *wm_adsp_buf_alloc(const void *src, size_t len,
172cf17c83cSMark Brown 					     struct list_head *list)
173cf17c83cSMark Brown {
174cf17c83cSMark Brown 	struct wm_adsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL);
175cf17c83cSMark Brown 
176cf17c83cSMark Brown 	if (buf == NULL)
177cf17c83cSMark Brown 		return NULL;
178cf17c83cSMark Brown 
179cdcd7f72SCharles Keepax 	buf->buf = vmalloc(len);
180cf17c83cSMark Brown 	if (!buf->buf) {
181cdcd7f72SCharles Keepax 		vfree(buf);
182cf17c83cSMark Brown 		return NULL;
183cf17c83cSMark Brown 	}
184cdcd7f72SCharles Keepax 	memcpy(buf->buf, src, len);
185cf17c83cSMark Brown 
186cf17c83cSMark Brown 	if (list)
187cf17c83cSMark Brown 		list_add_tail(&buf->list, list);
188cf17c83cSMark Brown 
189cf17c83cSMark Brown 	return buf;
190cf17c83cSMark Brown }
191cf17c83cSMark Brown 
192cf17c83cSMark Brown static void wm_adsp_buf_free(struct list_head *list)
193cf17c83cSMark Brown {
194cf17c83cSMark Brown 	while (!list_empty(list)) {
195cf17c83cSMark Brown 		struct wm_adsp_buf *buf = list_first_entry(list,
196cf17c83cSMark Brown 							   struct wm_adsp_buf,
197cf17c83cSMark Brown 							   list);
198cf17c83cSMark Brown 		list_del(&buf->list);
199cdcd7f72SCharles Keepax 		vfree(buf->buf);
200cf17c83cSMark Brown 		kfree(buf);
201cf17c83cSMark Brown 	}
202cf17c83cSMark Brown }
203cf17c83cSMark Brown 
204dd84f925SMark Brown #define WM_ADSP_FW_MBC_VSS  0
20504d1300fSCharles Keepax #define WM_ADSP_FW_HIFI     1
20604d1300fSCharles Keepax #define WM_ADSP_FW_TX       2
20704d1300fSCharles Keepax #define WM_ADSP_FW_TX_SPK   3
20804d1300fSCharles Keepax #define WM_ADSP_FW_RX       4
20904d1300fSCharles Keepax #define WM_ADSP_FW_RX_ANC   5
21004d1300fSCharles Keepax #define WM_ADSP_FW_CTRL     6
21104d1300fSCharles Keepax #define WM_ADSP_FW_ASR      7
21204d1300fSCharles Keepax #define WM_ADSP_FW_TRACE    8
21304d1300fSCharles Keepax #define WM_ADSP_FW_SPK_PROT 9
21404d1300fSCharles Keepax #define WM_ADSP_FW_MISC     10
21504d1300fSCharles Keepax 
21604d1300fSCharles Keepax #define WM_ADSP_NUM_FW      11
217dd84f925SMark Brown 
2181023dbd9SMark Brown static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = {
219dd84f925SMark Brown 	[WM_ADSP_FW_MBC_VSS] =  "MBC/VSS",
22004d1300fSCharles Keepax 	[WM_ADSP_FW_HIFI] =     "MasterHiFi",
221dd84f925SMark Brown 	[WM_ADSP_FW_TX] =       "Tx",
222dd84f925SMark Brown 	[WM_ADSP_FW_TX_SPK] =   "Tx Speaker",
22304d1300fSCharles Keepax 	[WM_ADSP_FW_RX] =       "Rx",
224dd84f925SMark Brown 	[WM_ADSP_FW_RX_ANC] =   "Rx ANC",
22504d1300fSCharles Keepax 	[WM_ADSP_FW_CTRL] =     "Voice Ctrl",
22604d1300fSCharles Keepax 	[WM_ADSP_FW_ASR] =      "ASR Assist",
22704d1300fSCharles Keepax 	[WM_ADSP_FW_TRACE] =    "Dbg Trace",
22804d1300fSCharles Keepax 	[WM_ADSP_FW_SPK_PROT] = "Protection",
22904d1300fSCharles Keepax 	[WM_ADSP_FW_MISC] =     "Misc",
2301023dbd9SMark Brown };
2311023dbd9SMark Brown 
2322cd19bdbSCharles Keepax struct wm_adsp_system_config_xm_hdr {
2332cd19bdbSCharles Keepax 	__be32 sys_enable;
2342cd19bdbSCharles Keepax 	__be32 fw_id;
2352cd19bdbSCharles Keepax 	__be32 fw_rev;
2362cd19bdbSCharles Keepax 	__be32 boot_status;
2372cd19bdbSCharles Keepax 	__be32 watchdog;
2382cd19bdbSCharles Keepax 	__be32 dma_buffer_size;
2392cd19bdbSCharles Keepax 	__be32 rdma[6];
2402cd19bdbSCharles Keepax 	__be32 wdma[8];
2412cd19bdbSCharles Keepax 	__be32 build_job_name[3];
2422cd19bdbSCharles Keepax 	__be32 build_job_number;
2432cd19bdbSCharles Keepax };
2442cd19bdbSCharles Keepax 
2452cd19bdbSCharles Keepax struct wm_adsp_alg_xm_struct {
2462cd19bdbSCharles Keepax 	__be32 magic;
2472cd19bdbSCharles Keepax 	__be32 smoothing;
2482cd19bdbSCharles Keepax 	__be32 threshold;
2492cd19bdbSCharles Keepax 	__be32 host_buf_ptr;
2502cd19bdbSCharles Keepax 	__be32 start_seq;
2512cd19bdbSCharles Keepax 	__be32 high_water_mark;
2522cd19bdbSCharles Keepax 	__be32 low_water_mark;
2532cd19bdbSCharles Keepax 	__be64 smoothed_power;
2542cd19bdbSCharles Keepax };
2552cd19bdbSCharles Keepax 
2562cd19bdbSCharles Keepax struct wm_adsp_buffer {
2572cd19bdbSCharles Keepax 	__be32 X_buf_base;		/* XM base addr of first X area */
2582cd19bdbSCharles Keepax 	__be32 X_buf_size;		/* Size of 1st X area in words */
2592cd19bdbSCharles Keepax 	__be32 X_buf_base2;		/* XM base addr of 2nd X area */
2602cd19bdbSCharles Keepax 	__be32 X_buf_brk;		/* Total X size in words */
2612cd19bdbSCharles Keepax 	__be32 Y_buf_base;		/* YM base addr of Y area */
2622cd19bdbSCharles Keepax 	__be32 wrap;			/* Total size X and Y in words */
2632cd19bdbSCharles Keepax 	__be32 high_water_mark;		/* Point at which IRQ is asserted */
2642cd19bdbSCharles Keepax 	__be32 irq_count;		/* bits 1-31 count IRQ assertions */
2652cd19bdbSCharles Keepax 	__be32 irq_ack;			/* acked IRQ count, bit 0 enables IRQ */
2662cd19bdbSCharles Keepax 	__be32 next_write_index;	/* word index of next write */
2672cd19bdbSCharles Keepax 	__be32 next_read_index;		/* word index of next read */
2682cd19bdbSCharles Keepax 	__be32 error;			/* error if any */
2692cd19bdbSCharles Keepax 	__be32 oldest_block_index;	/* word index of oldest surviving */
2702cd19bdbSCharles Keepax 	__be32 requested_rewind;	/* how many blocks rewind was done */
2712cd19bdbSCharles Keepax 	__be32 reserved_space;		/* internal */
2722cd19bdbSCharles Keepax 	__be32 min_free;		/* min free space since stream start */
2732cd19bdbSCharles Keepax 	__be32 blocks_written[2];	/* total blocks written (64 bit) */
2742cd19bdbSCharles Keepax 	__be32 words_written[2];	/* total words written (64 bit) */
2752cd19bdbSCharles Keepax };
2762cd19bdbSCharles Keepax 
2772cd19bdbSCharles Keepax struct wm_adsp_compr_buf {
2782cd19bdbSCharles Keepax 	struct wm_adsp *dsp;
2792cd19bdbSCharles Keepax 
2802cd19bdbSCharles Keepax 	struct wm_adsp_buffer_region *regions;
2812cd19bdbSCharles Keepax 	u32 host_buf_ptr;
282565ace46SCharles Keepax 
283565ace46SCharles Keepax 	u32 error;
284565ace46SCharles Keepax 	u32 irq_count;
285565ace46SCharles Keepax 	int read_index;
286565ace46SCharles Keepax 	int avail;
2872cd19bdbSCharles Keepax };
2882cd19bdbSCharles Keepax 
289406abc95SCharles Keepax struct wm_adsp_compr {
290406abc95SCharles Keepax 	struct wm_adsp *dsp;
29195fe9597SCharles Keepax 	struct wm_adsp_compr_buf *buf;
292406abc95SCharles Keepax 
293406abc95SCharles Keepax 	struct snd_compr_stream *stream;
294406abc95SCharles Keepax 	struct snd_compressed_buffer size;
295565ace46SCharles Keepax 
29683a40ce9SCharles Keepax 	u32 *raw_buf;
297565ace46SCharles Keepax 	unsigned int copied_total;
298da2b3358SCharles Keepax 
299da2b3358SCharles Keepax 	unsigned int sample_rate;
300406abc95SCharles Keepax };
301406abc95SCharles Keepax 
302406abc95SCharles Keepax #define WM_ADSP_DATA_WORD_SIZE         3
303406abc95SCharles Keepax 
304406abc95SCharles Keepax #define WM_ADSP_MIN_FRAGMENTS          1
305406abc95SCharles Keepax #define WM_ADSP_MAX_FRAGMENTS          256
306406abc95SCharles Keepax #define WM_ADSP_MIN_FRAGMENT_SIZE      (64 * WM_ADSP_DATA_WORD_SIZE)
307406abc95SCharles Keepax #define WM_ADSP_MAX_FRAGMENT_SIZE      (4096 * WM_ADSP_DATA_WORD_SIZE)
308406abc95SCharles Keepax 
3092cd19bdbSCharles Keepax #define WM_ADSP_ALG_XM_STRUCT_MAGIC    0x49aec7
3102cd19bdbSCharles Keepax 
3112cd19bdbSCharles Keepax #define HOST_BUFFER_FIELD(field) \
3122cd19bdbSCharles Keepax 	(offsetof(struct wm_adsp_buffer, field) / sizeof(__be32))
3132cd19bdbSCharles Keepax 
3142cd19bdbSCharles Keepax #define ALG_XM_FIELD(field) \
3152cd19bdbSCharles Keepax 	(offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32))
3162cd19bdbSCharles Keepax 
3172cd19bdbSCharles Keepax static int wm_adsp_buffer_init(struct wm_adsp *dsp);
3182cd19bdbSCharles Keepax static int wm_adsp_buffer_free(struct wm_adsp *dsp);
3192cd19bdbSCharles Keepax 
3202cd19bdbSCharles Keepax struct wm_adsp_buffer_region {
3212cd19bdbSCharles Keepax 	unsigned int offset;
3222cd19bdbSCharles Keepax 	unsigned int cumulative_size;
3232cd19bdbSCharles Keepax 	unsigned int mem_type;
3242cd19bdbSCharles Keepax 	unsigned int base_addr;
3252cd19bdbSCharles Keepax };
3262cd19bdbSCharles Keepax 
3272cd19bdbSCharles Keepax struct wm_adsp_buffer_region_def {
3282cd19bdbSCharles Keepax 	unsigned int mem_type;
3292cd19bdbSCharles Keepax 	unsigned int base_offset;
3302cd19bdbSCharles Keepax 	unsigned int size_offset;
3312cd19bdbSCharles Keepax };
3322cd19bdbSCharles Keepax 
3332cd19bdbSCharles Keepax static struct wm_adsp_buffer_region_def ez2control_regions[] = {
3342cd19bdbSCharles Keepax 	{
3352cd19bdbSCharles Keepax 		.mem_type = WMFW_ADSP2_XM,
3362cd19bdbSCharles Keepax 		.base_offset = HOST_BUFFER_FIELD(X_buf_base),
3372cd19bdbSCharles Keepax 		.size_offset = HOST_BUFFER_FIELD(X_buf_size),
3382cd19bdbSCharles Keepax 	},
3392cd19bdbSCharles Keepax 	{
3402cd19bdbSCharles Keepax 		.mem_type = WMFW_ADSP2_XM,
3412cd19bdbSCharles Keepax 		.base_offset = HOST_BUFFER_FIELD(X_buf_base2),
3422cd19bdbSCharles Keepax 		.size_offset = HOST_BUFFER_FIELD(X_buf_brk),
3432cd19bdbSCharles Keepax 	},
3442cd19bdbSCharles Keepax 	{
3452cd19bdbSCharles Keepax 		.mem_type = WMFW_ADSP2_YM,
3462cd19bdbSCharles Keepax 		.base_offset = HOST_BUFFER_FIELD(Y_buf_base),
3472cd19bdbSCharles Keepax 		.size_offset = HOST_BUFFER_FIELD(wrap),
3482cd19bdbSCharles Keepax 	},
3492cd19bdbSCharles Keepax };
3502cd19bdbSCharles Keepax 
351406abc95SCharles Keepax struct wm_adsp_fw_caps {
352406abc95SCharles Keepax 	u32 id;
353406abc95SCharles Keepax 	struct snd_codec_desc desc;
3542cd19bdbSCharles Keepax 	int num_regions;
3552cd19bdbSCharles Keepax 	struct wm_adsp_buffer_region_def *region_defs;
356406abc95SCharles Keepax };
357406abc95SCharles Keepax 
358406abc95SCharles Keepax static const struct wm_adsp_fw_caps ez2control_caps[] = {
359406abc95SCharles Keepax 	{
360406abc95SCharles Keepax 		.id = SND_AUDIOCODEC_BESPOKE,
361406abc95SCharles Keepax 		.desc = {
362406abc95SCharles Keepax 			.max_ch = 1,
363406abc95SCharles Keepax 			.sample_rates = { 16000 },
364406abc95SCharles Keepax 			.num_sample_rates = 1,
365406abc95SCharles Keepax 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
366406abc95SCharles Keepax 		},
3672cd19bdbSCharles Keepax 		.num_regions = ARRAY_SIZE(ez2control_regions),
3682cd19bdbSCharles Keepax 		.region_defs = ez2control_regions,
369406abc95SCharles Keepax 	},
370406abc95SCharles Keepax };
371406abc95SCharles Keepax 
372406abc95SCharles Keepax static const struct {
3731023dbd9SMark Brown 	const char *file;
374406abc95SCharles Keepax 	int compr_direction;
375406abc95SCharles Keepax 	int num_caps;
376406abc95SCharles Keepax 	const struct wm_adsp_fw_caps *caps;
3771023dbd9SMark Brown } wm_adsp_fw[WM_ADSP_NUM_FW] = {
378dd84f925SMark Brown 	[WM_ADSP_FW_MBC_VSS] =  { .file = "mbc-vss" },
37904d1300fSCharles Keepax 	[WM_ADSP_FW_HIFI] =     { .file = "hifi" },
380dd84f925SMark Brown 	[WM_ADSP_FW_TX] =       { .file = "tx" },
381dd84f925SMark Brown 	[WM_ADSP_FW_TX_SPK] =   { .file = "tx-spk" },
38204d1300fSCharles Keepax 	[WM_ADSP_FW_RX] =       { .file = "rx" },
383dd84f925SMark Brown 	[WM_ADSP_FW_RX_ANC] =   { .file = "rx-anc" },
384406abc95SCharles Keepax 	[WM_ADSP_FW_CTRL] =     {
385406abc95SCharles Keepax 		.file = "ctrl",
386406abc95SCharles Keepax 		.compr_direction = SND_COMPRESS_CAPTURE,
387406abc95SCharles Keepax 		.num_caps = ARRAY_SIZE(ez2control_caps),
388406abc95SCharles Keepax 		.caps = ez2control_caps,
389406abc95SCharles Keepax 	},
39004d1300fSCharles Keepax 	[WM_ADSP_FW_ASR] =      { .file = "asr" },
39104d1300fSCharles Keepax 	[WM_ADSP_FW_TRACE] =    { .file = "trace" },
39204d1300fSCharles Keepax 	[WM_ADSP_FW_SPK_PROT] = { .file = "spk-prot" },
39304d1300fSCharles Keepax 	[WM_ADSP_FW_MISC] =     { .file = "misc" },
3941023dbd9SMark Brown };
3951023dbd9SMark Brown 
3966ab2b7b4SDimitris Papastamos struct wm_coeff_ctl_ops {
3976ab2b7b4SDimitris Papastamos 	int (*xget)(struct snd_kcontrol *kcontrol,
3986ab2b7b4SDimitris Papastamos 		    struct snd_ctl_elem_value *ucontrol);
3996ab2b7b4SDimitris Papastamos 	int (*xput)(struct snd_kcontrol *kcontrol,
4006ab2b7b4SDimitris Papastamos 		    struct snd_ctl_elem_value *ucontrol);
4016ab2b7b4SDimitris Papastamos 	int (*xinfo)(struct snd_kcontrol *kcontrol,
4026ab2b7b4SDimitris Papastamos 		     struct snd_ctl_elem_info *uinfo);
4036ab2b7b4SDimitris Papastamos };
4046ab2b7b4SDimitris Papastamos 
4056ab2b7b4SDimitris Papastamos struct wm_coeff_ctl {
4066ab2b7b4SDimitris Papastamos 	const char *name;
4072323736dSCharles Keepax 	const char *fw_name;
4083809f001SCharles Keepax 	struct wm_adsp_alg_region alg_region;
4096ab2b7b4SDimitris Papastamos 	struct wm_coeff_ctl_ops ops;
4103809f001SCharles Keepax 	struct wm_adsp *dsp;
4116ab2b7b4SDimitris Papastamos 	unsigned int enabled:1;
4126ab2b7b4SDimitris Papastamos 	struct list_head list;
4136ab2b7b4SDimitris Papastamos 	void *cache;
4142323736dSCharles Keepax 	unsigned int offset;
4156ab2b7b4SDimitris Papastamos 	size_t len;
4160c2e3f34SDimitris Papastamos 	unsigned int set:1;
4176ab2b7b4SDimitris Papastamos 	struct snd_kcontrol *kcontrol;
41826c22a19SCharles Keepax 	unsigned int flags;
4196ab2b7b4SDimitris Papastamos };
4206ab2b7b4SDimitris Papastamos 
421f9f55e31SRichard Fitzgerald #ifdef CONFIG_DEBUG_FS
422f9f55e31SRichard Fitzgerald static void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, const char *s)
423f9f55e31SRichard Fitzgerald {
424f9f55e31SRichard Fitzgerald 	char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
425f9f55e31SRichard Fitzgerald 
426f9f55e31SRichard Fitzgerald 	kfree(dsp->wmfw_file_name);
427f9f55e31SRichard Fitzgerald 	dsp->wmfw_file_name = tmp;
428f9f55e31SRichard Fitzgerald }
429f9f55e31SRichard Fitzgerald 
430f9f55e31SRichard Fitzgerald static void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, const char *s)
431f9f55e31SRichard Fitzgerald {
432f9f55e31SRichard Fitzgerald 	char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
433f9f55e31SRichard Fitzgerald 
434f9f55e31SRichard Fitzgerald 	kfree(dsp->bin_file_name);
435f9f55e31SRichard Fitzgerald 	dsp->bin_file_name = tmp;
436f9f55e31SRichard Fitzgerald }
437f9f55e31SRichard Fitzgerald 
438f9f55e31SRichard Fitzgerald static void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
439f9f55e31SRichard Fitzgerald {
440f9f55e31SRichard Fitzgerald 	kfree(dsp->wmfw_file_name);
441f9f55e31SRichard Fitzgerald 	kfree(dsp->bin_file_name);
442f9f55e31SRichard Fitzgerald 	dsp->wmfw_file_name = NULL;
443f9f55e31SRichard Fitzgerald 	dsp->bin_file_name = NULL;
444f9f55e31SRichard Fitzgerald }
445f9f55e31SRichard Fitzgerald 
446f9f55e31SRichard Fitzgerald static ssize_t wm_adsp_debugfs_wmfw_read(struct file *file,
447f9f55e31SRichard Fitzgerald 					 char __user *user_buf,
448f9f55e31SRichard Fitzgerald 					 size_t count, loff_t *ppos)
449f9f55e31SRichard Fitzgerald {
450f9f55e31SRichard Fitzgerald 	struct wm_adsp *dsp = file->private_data;
451f9f55e31SRichard Fitzgerald 	ssize_t ret;
452f9f55e31SRichard Fitzgerald 
453078e7183SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
454f9f55e31SRichard Fitzgerald 
455f9f55e31SRichard Fitzgerald 	if (!dsp->wmfw_file_name || !dsp->running)
456f9f55e31SRichard Fitzgerald 		ret = 0;
457f9f55e31SRichard Fitzgerald 	else
458f9f55e31SRichard Fitzgerald 		ret = simple_read_from_buffer(user_buf, count, ppos,
459f9f55e31SRichard Fitzgerald 					      dsp->wmfw_file_name,
460f9f55e31SRichard Fitzgerald 					      strlen(dsp->wmfw_file_name));
461f9f55e31SRichard Fitzgerald 
462078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
463f9f55e31SRichard Fitzgerald 	return ret;
464f9f55e31SRichard Fitzgerald }
465f9f55e31SRichard Fitzgerald 
466f9f55e31SRichard Fitzgerald static ssize_t wm_adsp_debugfs_bin_read(struct file *file,
467f9f55e31SRichard Fitzgerald 					char __user *user_buf,
468f9f55e31SRichard Fitzgerald 					size_t count, loff_t *ppos)
469f9f55e31SRichard Fitzgerald {
470f9f55e31SRichard Fitzgerald 	struct wm_adsp *dsp = file->private_data;
471f9f55e31SRichard Fitzgerald 	ssize_t ret;
472f9f55e31SRichard Fitzgerald 
473078e7183SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
474f9f55e31SRichard Fitzgerald 
475f9f55e31SRichard Fitzgerald 	if (!dsp->bin_file_name || !dsp->running)
476f9f55e31SRichard Fitzgerald 		ret = 0;
477f9f55e31SRichard Fitzgerald 	else
478f9f55e31SRichard Fitzgerald 		ret = simple_read_from_buffer(user_buf, count, ppos,
479f9f55e31SRichard Fitzgerald 					      dsp->bin_file_name,
480f9f55e31SRichard Fitzgerald 					      strlen(dsp->bin_file_name));
481f9f55e31SRichard Fitzgerald 
482078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
483f9f55e31SRichard Fitzgerald 	return ret;
484f9f55e31SRichard Fitzgerald }
485f9f55e31SRichard Fitzgerald 
486f9f55e31SRichard Fitzgerald static const struct {
487f9f55e31SRichard Fitzgerald 	const char *name;
488f9f55e31SRichard Fitzgerald 	const struct file_operations fops;
489f9f55e31SRichard Fitzgerald } wm_adsp_debugfs_fops[] = {
490f9f55e31SRichard Fitzgerald 	{
491f9f55e31SRichard Fitzgerald 		.name = "wmfw_file_name",
492f9f55e31SRichard Fitzgerald 		.fops = {
493f9f55e31SRichard Fitzgerald 			.open = simple_open,
494f9f55e31SRichard Fitzgerald 			.read = wm_adsp_debugfs_wmfw_read,
495f9f55e31SRichard Fitzgerald 		},
496f9f55e31SRichard Fitzgerald 	},
497f9f55e31SRichard Fitzgerald 	{
498f9f55e31SRichard Fitzgerald 		.name = "bin_file_name",
499f9f55e31SRichard Fitzgerald 		.fops = {
500f9f55e31SRichard Fitzgerald 			.open = simple_open,
501f9f55e31SRichard Fitzgerald 			.read = wm_adsp_debugfs_bin_read,
502f9f55e31SRichard Fitzgerald 		},
503f9f55e31SRichard Fitzgerald 	},
504f9f55e31SRichard Fitzgerald };
505f9f55e31SRichard Fitzgerald 
506f9f55e31SRichard Fitzgerald static void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
507f9f55e31SRichard Fitzgerald 				  struct snd_soc_codec *codec)
508f9f55e31SRichard Fitzgerald {
509f9f55e31SRichard Fitzgerald 	struct dentry *root = NULL;
510f9f55e31SRichard Fitzgerald 	char *root_name;
511f9f55e31SRichard Fitzgerald 	int i;
512f9f55e31SRichard Fitzgerald 
513f9f55e31SRichard Fitzgerald 	if (!codec->component.debugfs_root) {
514f9f55e31SRichard Fitzgerald 		adsp_err(dsp, "No codec debugfs root\n");
515f9f55e31SRichard Fitzgerald 		goto err;
516f9f55e31SRichard Fitzgerald 	}
517f9f55e31SRichard Fitzgerald 
518f9f55e31SRichard Fitzgerald 	root_name = kmalloc(PAGE_SIZE, GFP_KERNEL);
519f9f55e31SRichard Fitzgerald 	if (!root_name)
520f9f55e31SRichard Fitzgerald 		goto err;
521f9f55e31SRichard Fitzgerald 
522f9f55e31SRichard Fitzgerald 	snprintf(root_name, PAGE_SIZE, "dsp%d", dsp->num);
523f9f55e31SRichard Fitzgerald 	root = debugfs_create_dir(root_name, codec->component.debugfs_root);
524f9f55e31SRichard Fitzgerald 	kfree(root_name);
525f9f55e31SRichard Fitzgerald 
526f9f55e31SRichard Fitzgerald 	if (!root)
527f9f55e31SRichard Fitzgerald 		goto err;
528f9f55e31SRichard Fitzgerald 
529f9f55e31SRichard Fitzgerald 	if (!debugfs_create_bool("running", S_IRUGO, root, &dsp->running))
530f9f55e31SRichard Fitzgerald 		goto err;
531f9f55e31SRichard Fitzgerald 
532f9f55e31SRichard Fitzgerald 	if (!debugfs_create_x32("fw_id", S_IRUGO, root, &dsp->fw_id))
533f9f55e31SRichard Fitzgerald 		goto err;
534f9f55e31SRichard Fitzgerald 
535f9f55e31SRichard Fitzgerald 	if (!debugfs_create_x32("fw_version", S_IRUGO, root,
536f9f55e31SRichard Fitzgerald 				&dsp->fw_id_version))
537f9f55e31SRichard Fitzgerald 		goto err;
538f9f55e31SRichard Fitzgerald 
539f9f55e31SRichard Fitzgerald 	for (i = 0; i < ARRAY_SIZE(wm_adsp_debugfs_fops); ++i) {
540f9f55e31SRichard Fitzgerald 		if (!debugfs_create_file(wm_adsp_debugfs_fops[i].name,
541f9f55e31SRichard Fitzgerald 					 S_IRUGO, root, dsp,
542f9f55e31SRichard Fitzgerald 					 &wm_adsp_debugfs_fops[i].fops))
543f9f55e31SRichard Fitzgerald 			goto err;
544f9f55e31SRichard Fitzgerald 	}
545f9f55e31SRichard Fitzgerald 
546f9f55e31SRichard Fitzgerald 	dsp->debugfs_root = root;
547f9f55e31SRichard Fitzgerald 	return;
548f9f55e31SRichard Fitzgerald 
549f9f55e31SRichard Fitzgerald err:
550f9f55e31SRichard Fitzgerald 	debugfs_remove_recursive(root);
551f9f55e31SRichard Fitzgerald 	adsp_err(dsp, "Failed to create debugfs\n");
552f9f55e31SRichard Fitzgerald }
553f9f55e31SRichard Fitzgerald 
554f9f55e31SRichard Fitzgerald static void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
555f9f55e31SRichard Fitzgerald {
556f9f55e31SRichard Fitzgerald 	wm_adsp_debugfs_clear(dsp);
557f9f55e31SRichard Fitzgerald 	debugfs_remove_recursive(dsp->debugfs_root);
558f9f55e31SRichard Fitzgerald }
559f9f55e31SRichard Fitzgerald #else
560f9f55e31SRichard Fitzgerald static inline void wm_adsp2_init_debugfs(struct wm_adsp *dsp,
561f9f55e31SRichard Fitzgerald 					 struct snd_soc_codec *codec)
562f9f55e31SRichard Fitzgerald {
563f9f55e31SRichard Fitzgerald }
564f9f55e31SRichard Fitzgerald 
565f9f55e31SRichard Fitzgerald static inline void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp)
566f9f55e31SRichard Fitzgerald {
567f9f55e31SRichard Fitzgerald }
568f9f55e31SRichard Fitzgerald 
569f9f55e31SRichard Fitzgerald static inline void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp,
570f9f55e31SRichard Fitzgerald 						 const char *s)
571f9f55e31SRichard Fitzgerald {
572f9f55e31SRichard Fitzgerald }
573f9f55e31SRichard Fitzgerald 
574f9f55e31SRichard Fitzgerald static inline void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp,
575f9f55e31SRichard Fitzgerald 						const char *s)
576f9f55e31SRichard Fitzgerald {
577f9f55e31SRichard Fitzgerald }
578f9f55e31SRichard Fitzgerald 
579f9f55e31SRichard Fitzgerald static inline void wm_adsp_debugfs_clear(struct wm_adsp *dsp)
580f9f55e31SRichard Fitzgerald {
581f9f55e31SRichard Fitzgerald }
582f9f55e31SRichard Fitzgerald #endif
583f9f55e31SRichard Fitzgerald 
5841023dbd9SMark Brown static int wm_adsp_fw_get(struct snd_kcontrol *kcontrol,
5851023dbd9SMark Brown 			  struct snd_ctl_elem_value *ucontrol)
5861023dbd9SMark Brown {
587ea53bf77SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
5881023dbd9SMark Brown 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
5893809f001SCharles Keepax 	struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
5901023dbd9SMark Brown 
5913809f001SCharles Keepax 	ucontrol->value.integer.value[0] = dsp[e->shift_l].fw;
5921023dbd9SMark Brown 
5931023dbd9SMark Brown 	return 0;
5941023dbd9SMark Brown }
5951023dbd9SMark Brown 
5961023dbd9SMark Brown static int wm_adsp_fw_put(struct snd_kcontrol *kcontrol,
5971023dbd9SMark Brown 			  struct snd_ctl_elem_value *ucontrol)
5981023dbd9SMark Brown {
599ea53bf77SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
6001023dbd9SMark Brown 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
6013809f001SCharles Keepax 	struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
602d27c5e15SCharles Keepax 	int ret = 0;
6031023dbd9SMark Brown 
6043809f001SCharles Keepax 	if (ucontrol->value.integer.value[0] == dsp[e->shift_l].fw)
6051023dbd9SMark Brown 		return 0;
6061023dbd9SMark Brown 
6071023dbd9SMark Brown 	if (ucontrol->value.integer.value[0] >= WM_ADSP_NUM_FW)
6081023dbd9SMark Brown 		return -EINVAL;
6091023dbd9SMark Brown 
610d27c5e15SCharles Keepax 	mutex_lock(&dsp[e->shift_l].pwr_lock);
6111023dbd9SMark Brown 
612406abc95SCharles Keepax 	if (dsp[e->shift_l].running || dsp[e->shift_l].compr)
613d27c5e15SCharles Keepax 		ret = -EBUSY;
614d27c5e15SCharles Keepax 	else
6153809f001SCharles Keepax 		dsp[e->shift_l].fw = ucontrol->value.integer.value[0];
6161023dbd9SMark Brown 
617d27c5e15SCharles Keepax 	mutex_unlock(&dsp[e->shift_l].pwr_lock);
618d27c5e15SCharles Keepax 
619d27c5e15SCharles Keepax 	return ret;
6201023dbd9SMark Brown }
6211023dbd9SMark Brown 
6221023dbd9SMark Brown static const struct soc_enum wm_adsp_fw_enum[] = {
6231023dbd9SMark Brown 	SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
6241023dbd9SMark Brown 	SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
6251023dbd9SMark Brown 	SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
6261023dbd9SMark Brown 	SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
6271023dbd9SMark Brown };
6281023dbd9SMark Brown 
629336d0442SRichard Fitzgerald const struct snd_kcontrol_new wm_adsp_fw_controls[] = {
6301023dbd9SMark Brown 	SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum[0],
6311023dbd9SMark Brown 		     wm_adsp_fw_get, wm_adsp_fw_put),
6321023dbd9SMark Brown 	SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum[1],
6331023dbd9SMark Brown 		     wm_adsp_fw_get, wm_adsp_fw_put),
6341023dbd9SMark Brown 	SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum[2],
6351023dbd9SMark Brown 		     wm_adsp_fw_get, wm_adsp_fw_put),
6361023dbd9SMark Brown 	SOC_ENUM_EXT("DSP4 Firmware", wm_adsp_fw_enum[3],
6371023dbd9SMark Brown 		     wm_adsp_fw_get, wm_adsp_fw_put),
6381023dbd9SMark Brown };
639336d0442SRichard Fitzgerald EXPORT_SYMBOL_GPL(wm_adsp_fw_controls);
6402159ad93SMark Brown 
6412159ad93SMark Brown static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp,
6422159ad93SMark Brown 							int type)
6432159ad93SMark Brown {
6442159ad93SMark Brown 	int i;
6452159ad93SMark Brown 
6462159ad93SMark Brown 	for (i = 0; i < dsp->num_mems; i++)
6472159ad93SMark Brown 		if (dsp->mem[i].type == type)
6482159ad93SMark Brown 			return &dsp->mem[i];
6492159ad93SMark Brown 
6502159ad93SMark Brown 	return NULL;
6512159ad93SMark Brown }
6522159ad93SMark Brown 
6533809f001SCharles Keepax static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *mem,
65445b9ee72SMark Brown 					  unsigned int offset)
65545b9ee72SMark Brown {
6563809f001SCharles Keepax 	if (WARN_ON(!mem))
6576c452bdaSTakashi Iwai 		return offset;
6583809f001SCharles Keepax 	switch (mem->type) {
65945b9ee72SMark Brown 	case WMFW_ADSP1_PM:
6603809f001SCharles Keepax 		return mem->base + (offset * 3);
66145b9ee72SMark Brown 	case WMFW_ADSP1_DM:
6623809f001SCharles Keepax 		return mem->base + (offset * 2);
66345b9ee72SMark Brown 	case WMFW_ADSP2_XM:
6643809f001SCharles Keepax 		return mem->base + (offset * 2);
66545b9ee72SMark Brown 	case WMFW_ADSP2_YM:
6663809f001SCharles Keepax 		return mem->base + (offset * 2);
66745b9ee72SMark Brown 	case WMFW_ADSP1_ZM:
6683809f001SCharles Keepax 		return mem->base + (offset * 2);
66945b9ee72SMark Brown 	default:
6706c452bdaSTakashi Iwai 		WARN(1, "Unknown memory region type");
67145b9ee72SMark Brown 		return offset;
67245b9ee72SMark Brown 	}
67345b9ee72SMark Brown }
67445b9ee72SMark Brown 
67510337b07SRichard Fitzgerald static void wm_adsp2_show_fw_status(struct wm_adsp *dsp)
67610337b07SRichard Fitzgerald {
67710337b07SRichard Fitzgerald 	u16 scratch[4];
67810337b07SRichard Fitzgerald 	int ret;
67910337b07SRichard Fitzgerald 
68010337b07SRichard Fitzgerald 	ret = regmap_raw_read(dsp->regmap, dsp->base + ADSP2_SCRATCH0,
68110337b07SRichard Fitzgerald 				scratch, sizeof(scratch));
68210337b07SRichard Fitzgerald 	if (ret) {
68310337b07SRichard Fitzgerald 		adsp_err(dsp, "Failed to read SCRATCH regs: %d\n", ret);
68410337b07SRichard Fitzgerald 		return;
68510337b07SRichard Fitzgerald 	}
68610337b07SRichard Fitzgerald 
68710337b07SRichard Fitzgerald 	adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
68810337b07SRichard Fitzgerald 		 be16_to_cpu(scratch[0]),
68910337b07SRichard Fitzgerald 		 be16_to_cpu(scratch[1]),
69010337b07SRichard Fitzgerald 		 be16_to_cpu(scratch[2]),
69110337b07SRichard Fitzgerald 		 be16_to_cpu(scratch[3]));
69210337b07SRichard Fitzgerald }
69310337b07SRichard Fitzgerald 
6947585a5b0SCharles Keepax static int wm_coeff_info(struct snd_kcontrol *kctl,
6956ab2b7b4SDimitris Papastamos 			 struct snd_ctl_elem_info *uinfo)
6966ab2b7b4SDimitris Papastamos {
6977585a5b0SCharles Keepax 	struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kctl->private_value;
6986ab2b7b4SDimitris Papastamos 
6996ab2b7b4SDimitris Papastamos 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
7006ab2b7b4SDimitris Papastamos 	uinfo->count = ctl->len;
7016ab2b7b4SDimitris Papastamos 	return 0;
7026ab2b7b4SDimitris Papastamos }
7036ab2b7b4SDimitris Papastamos 
704c9f8dd71SCharles Keepax static int wm_coeff_write_control(struct wm_coeff_ctl *ctl,
7056ab2b7b4SDimitris Papastamos 				  const void *buf, size_t len)
7066ab2b7b4SDimitris Papastamos {
7073809f001SCharles Keepax 	struct wm_adsp_alg_region *alg_region = &ctl->alg_region;
7086ab2b7b4SDimitris Papastamos 	const struct wm_adsp_region *mem;
7093809f001SCharles Keepax 	struct wm_adsp *dsp = ctl->dsp;
7106ab2b7b4SDimitris Papastamos 	void *scratch;
7116ab2b7b4SDimitris Papastamos 	int ret;
7126ab2b7b4SDimitris Papastamos 	unsigned int reg;
7136ab2b7b4SDimitris Papastamos 
7143809f001SCharles Keepax 	mem = wm_adsp_find_region(dsp, alg_region->type);
7156ab2b7b4SDimitris Papastamos 	if (!mem) {
7163809f001SCharles Keepax 		adsp_err(dsp, "No base for region %x\n",
7173809f001SCharles Keepax 			 alg_region->type);
7186ab2b7b4SDimitris Papastamos 		return -EINVAL;
7196ab2b7b4SDimitris Papastamos 	}
7206ab2b7b4SDimitris Papastamos 
7212323736dSCharles Keepax 	reg = ctl->alg_region.base + ctl->offset;
7226ab2b7b4SDimitris Papastamos 	reg = wm_adsp_region_to_reg(mem, reg);
7236ab2b7b4SDimitris Papastamos 
7246ab2b7b4SDimitris Papastamos 	scratch = kmemdup(buf, ctl->len, GFP_KERNEL | GFP_DMA);
7256ab2b7b4SDimitris Papastamos 	if (!scratch)
7266ab2b7b4SDimitris Papastamos 		return -ENOMEM;
7276ab2b7b4SDimitris Papastamos 
7283809f001SCharles Keepax 	ret = regmap_raw_write(dsp->regmap, reg, scratch,
7296ab2b7b4SDimitris Papastamos 			       ctl->len);
7306ab2b7b4SDimitris Papastamos 	if (ret) {
7313809f001SCharles Keepax 		adsp_err(dsp, "Failed to write %zu bytes to %x: %d\n",
73243bc3bf6SDimitris Papastamos 			 ctl->len, reg, ret);
7336ab2b7b4SDimitris Papastamos 		kfree(scratch);
7346ab2b7b4SDimitris Papastamos 		return ret;
7356ab2b7b4SDimitris Papastamos 	}
7363809f001SCharles Keepax 	adsp_dbg(dsp, "Wrote %zu bytes to %x\n", ctl->len, reg);
7376ab2b7b4SDimitris Papastamos 
7386ab2b7b4SDimitris Papastamos 	kfree(scratch);
7396ab2b7b4SDimitris Papastamos 
7406ab2b7b4SDimitris Papastamos 	return 0;
7416ab2b7b4SDimitris Papastamos }
7426ab2b7b4SDimitris Papastamos 
7437585a5b0SCharles Keepax static int wm_coeff_put(struct snd_kcontrol *kctl,
7446ab2b7b4SDimitris Papastamos 			struct snd_ctl_elem_value *ucontrol)
7456ab2b7b4SDimitris Papastamos {
7467585a5b0SCharles Keepax 	struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kctl->private_value;
7476ab2b7b4SDimitris Papastamos 	char *p = ucontrol->value.bytes.data;
748168d10e7SCharles Keepax 	int ret = 0;
749168d10e7SCharles Keepax 
750168d10e7SCharles Keepax 	mutex_lock(&ctl->dsp->pwr_lock);
7516ab2b7b4SDimitris Papastamos 
7526ab2b7b4SDimitris Papastamos 	memcpy(ctl->cache, p, ctl->len);
7536ab2b7b4SDimitris Papastamos 
7540c2e3f34SDimitris Papastamos 	ctl->set = 1;
755168d10e7SCharles Keepax 	if (ctl->enabled)
756168d10e7SCharles Keepax 		ret = wm_coeff_write_control(ctl, p, ctl->len);
7576ab2b7b4SDimitris Papastamos 
758168d10e7SCharles Keepax 	mutex_unlock(&ctl->dsp->pwr_lock);
759168d10e7SCharles Keepax 
760168d10e7SCharles Keepax 	return ret;
7616ab2b7b4SDimitris Papastamos }
7626ab2b7b4SDimitris Papastamos 
763c9f8dd71SCharles Keepax static int wm_coeff_read_control(struct wm_coeff_ctl *ctl,
7646ab2b7b4SDimitris Papastamos 				 void *buf, size_t len)
7656ab2b7b4SDimitris Papastamos {
7663809f001SCharles Keepax 	struct wm_adsp_alg_region *alg_region = &ctl->alg_region;
7676ab2b7b4SDimitris Papastamos 	const struct wm_adsp_region *mem;
7683809f001SCharles Keepax 	struct wm_adsp *dsp = ctl->dsp;
7696ab2b7b4SDimitris Papastamos 	void *scratch;
7706ab2b7b4SDimitris Papastamos 	int ret;
7716ab2b7b4SDimitris Papastamos 	unsigned int reg;
7726ab2b7b4SDimitris Papastamos 
7733809f001SCharles Keepax 	mem = wm_adsp_find_region(dsp, alg_region->type);
7746ab2b7b4SDimitris Papastamos 	if (!mem) {
7753809f001SCharles Keepax 		adsp_err(dsp, "No base for region %x\n",
7763809f001SCharles Keepax 			 alg_region->type);
7776ab2b7b4SDimitris Papastamos 		return -EINVAL;
7786ab2b7b4SDimitris Papastamos 	}
7796ab2b7b4SDimitris Papastamos 
7802323736dSCharles Keepax 	reg = ctl->alg_region.base + ctl->offset;
7816ab2b7b4SDimitris Papastamos 	reg = wm_adsp_region_to_reg(mem, reg);
7826ab2b7b4SDimitris Papastamos 
7836ab2b7b4SDimitris Papastamos 	scratch = kmalloc(ctl->len, GFP_KERNEL | GFP_DMA);
7846ab2b7b4SDimitris Papastamos 	if (!scratch)
7856ab2b7b4SDimitris Papastamos 		return -ENOMEM;
7866ab2b7b4SDimitris Papastamos 
7873809f001SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, reg, scratch, ctl->len);
7886ab2b7b4SDimitris Papastamos 	if (ret) {
7893809f001SCharles Keepax 		adsp_err(dsp, "Failed to read %zu bytes from %x: %d\n",
79043bc3bf6SDimitris Papastamos 			 ctl->len, reg, ret);
7916ab2b7b4SDimitris Papastamos 		kfree(scratch);
7926ab2b7b4SDimitris Papastamos 		return ret;
7936ab2b7b4SDimitris Papastamos 	}
7943809f001SCharles Keepax 	adsp_dbg(dsp, "Read %zu bytes from %x\n", ctl->len, reg);
7956ab2b7b4SDimitris Papastamos 
7966ab2b7b4SDimitris Papastamos 	memcpy(buf, scratch, ctl->len);
7976ab2b7b4SDimitris Papastamos 	kfree(scratch);
7986ab2b7b4SDimitris Papastamos 
7996ab2b7b4SDimitris Papastamos 	return 0;
8006ab2b7b4SDimitris Papastamos }
8016ab2b7b4SDimitris Papastamos 
8027585a5b0SCharles Keepax static int wm_coeff_get(struct snd_kcontrol *kctl,
8036ab2b7b4SDimitris Papastamos 			struct snd_ctl_elem_value *ucontrol)
8046ab2b7b4SDimitris Papastamos {
8057585a5b0SCharles Keepax 	struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kctl->private_value;
8066ab2b7b4SDimitris Papastamos 	char *p = ucontrol->value.bytes.data;
807168d10e7SCharles Keepax 	int ret = 0;
808168d10e7SCharles Keepax 
809168d10e7SCharles Keepax 	mutex_lock(&ctl->dsp->pwr_lock);
8106ab2b7b4SDimitris Papastamos 
81126c22a19SCharles Keepax 	if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
81226c22a19SCharles Keepax 		if (ctl->enabled)
813168d10e7SCharles Keepax 			ret = wm_coeff_read_control(ctl, p, ctl->len);
81426c22a19SCharles Keepax 		else
815168d10e7SCharles Keepax 			ret = -EPERM;
816168d10e7SCharles Keepax 	} else {
817bc1765d6SCharles Keepax 		if (!ctl->flags && ctl->enabled)
818bc1765d6SCharles Keepax 			ret = wm_coeff_read_control(ctl, ctl->cache, ctl->len);
819bc1765d6SCharles Keepax 
820168d10e7SCharles Keepax 		memcpy(p, ctl->cache, ctl->len);
82126c22a19SCharles Keepax 	}
82226c22a19SCharles Keepax 
823168d10e7SCharles Keepax 	mutex_unlock(&ctl->dsp->pwr_lock);
82426c22a19SCharles Keepax 
825168d10e7SCharles Keepax 	return ret;
8266ab2b7b4SDimitris Papastamos }
8276ab2b7b4SDimitris Papastamos 
8286ab2b7b4SDimitris Papastamos struct wmfw_ctl_work {
8293809f001SCharles Keepax 	struct wm_adsp *dsp;
8306ab2b7b4SDimitris Papastamos 	struct wm_coeff_ctl *ctl;
8316ab2b7b4SDimitris Papastamos 	struct work_struct work;
8326ab2b7b4SDimitris Papastamos };
8336ab2b7b4SDimitris Papastamos 
8343809f001SCharles Keepax static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl)
8356ab2b7b4SDimitris Papastamos {
8366ab2b7b4SDimitris Papastamos 	struct snd_kcontrol_new *kcontrol;
8376ab2b7b4SDimitris Papastamos 	int ret;
8386ab2b7b4SDimitris Papastamos 
83992bb4c32SDimitris Papastamos 	if (!ctl || !ctl->name)
8406ab2b7b4SDimitris Papastamos 		return -EINVAL;
8416ab2b7b4SDimitris Papastamos 
8426ab2b7b4SDimitris Papastamos 	kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL);
8436ab2b7b4SDimitris Papastamos 	if (!kcontrol)
8446ab2b7b4SDimitris Papastamos 		return -ENOMEM;
8456ab2b7b4SDimitris Papastamos 	kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
8466ab2b7b4SDimitris Papastamos 
8476ab2b7b4SDimitris Papastamos 	kcontrol->name = ctl->name;
8486ab2b7b4SDimitris Papastamos 	kcontrol->info = wm_coeff_info;
8496ab2b7b4SDimitris Papastamos 	kcontrol->get = wm_coeff_get;
8506ab2b7b4SDimitris Papastamos 	kcontrol->put = wm_coeff_put;
8516ab2b7b4SDimitris Papastamos 	kcontrol->private_value = (unsigned long)ctl;
8526ab2b7b4SDimitris Papastamos 
85326c22a19SCharles Keepax 	if (ctl->flags) {
85426c22a19SCharles Keepax 		if (ctl->flags & WMFW_CTL_FLAG_WRITEABLE)
85526c22a19SCharles Keepax 			kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
85626c22a19SCharles Keepax 		if (ctl->flags & WMFW_CTL_FLAG_READABLE)
85726c22a19SCharles Keepax 			kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_READ;
85826c22a19SCharles Keepax 		if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
85926c22a19SCharles Keepax 			kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_VOLATILE;
860cc815c4bSCharles Keepax 	} else {
861cc815c4bSCharles Keepax 		kcontrol->access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
862cc815c4bSCharles Keepax 		kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_VOLATILE;
86326c22a19SCharles Keepax 	}
86426c22a19SCharles Keepax 
8657d00cd97SCharles Keepax 	ret = snd_soc_add_card_controls(dsp->card, kcontrol, 1);
8666ab2b7b4SDimitris Papastamos 	if (ret < 0)
8676ab2b7b4SDimitris Papastamos 		goto err_kcontrol;
8686ab2b7b4SDimitris Papastamos 
8696ab2b7b4SDimitris Papastamos 	kfree(kcontrol);
8706ab2b7b4SDimitris Papastamos 
8717d00cd97SCharles Keepax 	ctl->kcontrol = snd_soc_card_get_kcontrol(dsp->card, ctl->name);
87281ad93ecSDimitris Papastamos 
8736ab2b7b4SDimitris Papastamos 	return 0;
8746ab2b7b4SDimitris Papastamos 
8756ab2b7b4SDimitris Papastamos err_kcontrol:
8766ab2b7b4SDimitris Papastamos 	kfree(kcontrol);
8776ab2b7b4SDimitris Papastamos 	return ret;
8786ab2b7b4SDimitris Papastamos }
8796ab2b7b4SDimitris Papastamos 
880b21acc1cSCharles Keepax static int wm_coeff_init_control_caches(struct wm_adsp *dsp)
881b21acc1cSCharles Keepax {
882b21acc1cSCharles Keepax 	struct wm_coeff_ctl *ctl;
883b21acc1cSCharles Keepax 	int ret;
884b21acc1cSCharles Keepax 
885b21acc1cSCharles Keepax 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
886b21acc1cSCharles Keepax 		if (!ctl->enabled || ctl->set)
887b21acc1cSCharles Keepax 			continue;
88826c22a19SCharles Keepax 		if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
88926c22a19SCharles Keepax 			continue;
89026c22a19SCharles Keepax 
8917d00cd97SCharles Keepax 		ret = wm_coeff_read_control(ctl, ctl->cache, ctl->len);
892b21acc1cSCharles Keepax 		if (ret < 0)
893b21acc1cSCharles Keepax 			return ret;
894b21acc1cSCharles Keepax 	}
895b21acc1cSCharles Keepax 
896b21acc1cSCharles Keepax 	return 0;
897b21acc1cSCharles Keepax }
898b21acc1cSCharles Keepax 
899b21acc1cSCharles Keepax static int wm_coeff_sync_controls(struct wm_adsp *dsp)
900b21acc1cSCharles Keepax {
901b21acc1cSCharles Keepax 	struct wm_coeff_ctl *ctl;
902b21acc1cSCharles Keepax 	int ret;
903b21acc1cSCharles Keepax 
904b21acc1cSCharles Keepax 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
905b21acc1cSCharles Keepax 		if (!ctl->enabled)
906b21acc1cSCharles Keepax 			continue;
90726c22a19SCharles Keepax 		if (ctl->set && !(ctl->flags & WMFW_CTL_FLAG_VOLATILE)) {
9087d00cd97SCharles Keepax 			ret = wm_coeff_write_control(ctl, ctl->cache, ctl->len);
909b21acc1cSCharles Keepax 			if (ret < 0)
910b21acc1cSCharles Keepax 				return ret;
911b21acc1cSCharles Keepax 		}
912b21acc1cSCharles Keepax 	}
913b21acc1cSCharles Keepax 
914b21acc1cSCharles Keepax 	return 0;
915b21acc1cSCharles Keepax }
916b21acc1cSCharles Keepax 
917b21acc1cSCharles Keepax static void wm_adsp_ctl_work(struct work_struct *work)
918b21acc1cSCharles Keepax {
919b21acc1cSCharles Keepax 	struct wmfw_ctl_work *ctl_work = container_of(work,
920b21acc1cSCharles Keepax 						      struct wmfw_ctl_work,
921b21acc1cSCharles Keepax 						      work);
922b21acc1cSCharles Keepax 
923b21acc1cSCharles Keepax 	wmfw_add_ctl(ctl_work->dsp, ctl_work->ctl);
924b21acc1cSCharles Keepax 	kfree(ctl_work);
925b21acc1cSCharles Keepax }
926b21acc1cSCharles Keepax 
927b21acc1cSCharles Keepax static int wm_adsp_create_control(struct wm_adsp *dsp,
928b21acc1cSCharles Keepax 				  const struct wm_adsp_alg_region *alg_region,
9292323736dSCharles Keepax 				  unsigned int offset, unsigned int len,
93026c22a19SCharles Keepax 				  const char *subname, unsigned int subname_len,
93126c22a19SCharles Keepax 				  unsigned int flags)
932b21acc1cSCharles Keepax {
933b21acc1cSCharles Keepax 	struct wm_coeff_ctl *ctl;
934b21acc1cSCharles Keepax 	struct wmfw_ctl_work *ctl_work;
935b21acc1cSCharles Keepax 	char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
936b21acc1cSCharles Keepax 	char *region_name;
937b21acc1cSCharles Keepax 	int ret;
938b21acc1cSCharles Keepax 
93926c22a19SCharles Keepax 	if (flags & WMFW_CTL_FLAG_SYS)
94026c22a19SCharles Keepax 		return 0;
94126c22a19SCharles Keepax 
942b21acc1cSCharles Keepax 	switch (alg_region->type) {
943b21acc1cSCharles Keepax 	case WMFW_ADSP1_PM:
944b21acc1cSCharles Keepax 		region_name = "PM";
945b21acc1cSCharles Keepax 		break;
946b21acc1cSCharles Keepax 	case WMFW_ADSP1_DM:
947b21acc1cSCharles Keepax 		region_name = "DM";
948b21acc1cSCharles Keepax 		break;
949b21acc1cSCharles Keepax 	case WMFW_ADSP2_XM:
950b21acc1cSCharles Keepax 		region_name = "XM";
951b21acc1cSCharles Keepax 		break;
952b21acc1cSCharles Keepax 	case WMFW_ADSP2_YM:
953b21acc1cSCharles Keepax 		region_name = "YM";
954b21acc1cSCharles Keepax 		break;
955b21acc1cSCharles Keepax 	case WMFW_ADSP1_ZM:
956b21acc1cSCharles Keepax 		region_name = "ZM";
957b21acc1cSCharles Keepax 		break;
958b21acc1cSCharles Keepax 	default:
9592323736dSCharles Keepax 		adsp_err(dsp, "Unknown region type: %d\n", alg_region->type);
960b21acc1cSCharles Keepax 		return -EINVAL;
961b21acc1cSCharles Keepax 	}
962b21acc1cSCharles Keepax 
963cb5b57a9SCharles Keepax 	switch (dsp->fw_ver) {
964cb5b57a9SCharles Keepax 	case 0:
965cb5b57a9SCharles Keepax 	case 1:
966b21acc1cSCharles Keepax 		snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "DSP%d %s %x",
967b21acc1cSCharles Keepax 			 dsp->num, region_name, alg_region->alg);
968cb5b57a9SCharles Keepax 		break;
969cb5b57a9SCharles Keepax 	default:
970cb5b57a9SCharles Keepax 		ret = snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
971cb5b57a9SCharles Keepax 				"DSP%d%c %.12s %x", dsp->num, *region_name,
972cb5b57a9SCharles Keepax 				wm_adsp_fw_text[dsp->fw], alg_region->alg);
973cb5b57a9SCharles Keepax 
974cb5b57a9SCharles Keepax 		/* Truncate the subname from the start if it is too long */
975cb5b57a9SCharles Keepax 		if (subname) {
976cb5b57a9SCharles Keepax 			int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
977cb5b57a9SCharles Keepax 			int skip = 0;
978cb5b57a9SCharles Keepax 
979cb5b57a9SCharles Keepax 			if (subname_len > avail)
980cb5b57a9SCharles Keepax 				skip = subname_len - avail;
981cb5b57a9SCharles Keepax 
982cb5b57a9SCharles Keepax 			snprintf(name + ret,
983cb5b57a9SCharles Keepax 				 SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret, " %.*s",
984cb5b57a9SCharles Keepax 				 subname_len - skip, subname + skip);
985cb5b57a9SCharles Keepax 		}
986cb5b57a9SCharles Keepax 		break;
987cb5b57a9SCharles Keepax 	}
988b21acc1cSCharles Keepax 
9897585a5b0SCharles Keepax 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
990b21acc1cSCharles Keepax 		if (!strcmp(ctl->name, name)) {
991b21acc1cSCharles Keepax 			if (!ctl->enabled)
992b21acc1cSCharles Keepax 				ctl->enabled = 1;
993b21acc1cSCharles Keepax 			return 0;
994b21acc1cSCharles Keepax 		}
995b21acc1cSCharles Keepax 	}
996b21acc1cSCharles Keepax 
997b21acc1cSCharles Keepax 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
998b21acc1cSCharles Keepax 	if (!ctl)
999b21acc1cSCharles Keepax 		return -ENOMEM;
10002323736dSCharles Keepax 	ctl->fw_name = wm_adsp_fw_text[dsp->fw];
1001b21acc1cSCharles Keepax 	ctl->alg_region = *alg_region;
1002b21acc1cSCharles Keepax 	ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
1003b21acc1cSCharles Keepax 	if (!ctl->name) {
1004b21acc1cSCharles Keepax 		ret = -ENOMEM;
1005b21acc1cSCharles Keepax 		goto err_ctl;
1006b21acc1cSCharles Keepax 	}
1007b21acc1cSCharles Keepax 	ctl->enabled = 1;
1008b21acc1cSCharles Keepax 	ctl->set = 0;
1009b21acc1cSCharles Keepax 	ctl->ops.xget = wm_coeff_get;
1010b21acc1cSCharles Keepax 	ctl->ops.xput = wm_coeff_put;
1011b21acc1cSCharles Keepax 	ctl->dsp = dsp;
1012b21acc1cSCharles Keepax 
101326c22a19SCharles Keepax 	ctl->flags = flags;
10142323736dSCharles Keepax 	ctl->offset = offset;
1015b21acc1cSCharles Keepax 	if (len > 512) {
1016b21acc1cSCharles Keepax 		adsp_warn(dsp, "Truncating control %s from %d\n",
1017b21acc1cSCharles Keepax 			  ctl->name, len);
1018b21acc1cSCharles Keepax 		len = 512;
1019b21acc1cSCharles Keepax 	}
1020b21acc1cSCharles Keepax 	ctl->len = len;
1021b21acc1cSCharles Keepax 	ctl->cache = kzalloc(ctl->len, GFP_KERNEL);
1022b21acc1cSCharles Keepax 	if (!ctl->cache) {
1023b21acc1cSCharles Keepax 		ret = -ENOMEM;
1024b21acc1cSCharles Keepax 		goto err_ctl_name;
1025b21acc1cSCharles Keepax 	}
1026b21acc1cSCharles Keepax 
10272323736dSCharles Keepax 	list_add(&ctl->list, &dsp->ctl_list);
10282323736dSCharles Keepax 
1029b21acc1cSCharles Keepax 	ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL);
1030b21acc1cSCharles Keepax 	if (!ctl_work) {
1031b21acc1cSCharles Keepax 		ret = -ENOMEM;
1032b21acc1cSCharles Keepax 		goto err_ctl_cache;
1033b21acc1cSCharles Keepax 	}
1034b21acc1cSCharles Keepax 
1035b21acc1cSCharles Keepax 	ctl_work->dsp = dsp;
1036b21acc1cSCharles Keepax 	ctl_work->ctl = ctl;
1037b21acc1cSCharles Keepax 	INIT_WORK(&ctl_work->work, wm_adsp_ctl_work);
1038b21acc1cSCharles Keepax 	schedule_work(&ctl_work->work);
1039b21acc1cSCharles Keepax 
1040b21acc1cSCharles Keepax 	return 0;
1041b21acc1cSCharles Keepax 
1042b21acc1cSCharles Keepax err_ctl_cache:
1043b21acc1cSCharles Keepax 	kfree(ctl->cache);
1044b21acc1cSCharles Keepax err_ctl_name:
1045b21acc1cSCharles Keepax 	kfree(ctl->name);
1046b21acc1cSCharles Keepax err_ctl:
1047b21acc1cSCharles Keepax 	kfree(ctl);
1048b21acc1cSCharles Keepax 
1049b21acc1cSCharles Keepax 	return ret;
1050b21acc1cSCharles Keepax }
1051b21acc1cSCharles Keepax 
10522323736dSCharles Keepax struct wm_coeff_parsed_alg {
10532323736dSCharles Keepax 	int id;
10542323736dSCharles Keepax 	const u8 *name;
10552323736dSCharles Keepax 	int name_len;
10562323736dSCharles Keepax 	int ncoeff;
10572323736dSCharles Keepax };
10582323736dSCharles Keepax 
10592323736dSCharles Keepax struct wm_coeff_parsed_coeff {
10602323736dSCharles Keepax 	int offset;
10612323736dSCharles Keepax 	int mem_type;
10622323736dSCharles Keepax 	const u8 *name;
10632323736dSCharles Keepax 	int name_len;
10642323736dSCharles Keepax 	int ctl_type;
10652323736dSCharles Keepax 	int flags;
10662323736dSCharles Keepax 	int len;
10672323736dSCharles Keepax };
10682323736dSCharles Keepax 
1069cb5b57a9SCharles Keepax static int wm_coeff_parse_string(int bytes, const u8 **pos, const u8 **str)
1070cb5b57a9SCharles Keepax {
1071cb5b57a9SCharles Keepax 	int length;
1072cb5b57a9SCharles Keepax 
1073cb5b57a9SCharles Keepax 	switch (bytes) {
1074cb5b57a9SCharles Keepax 	case 1:
1075cb5b57a9SCharles Keepax 		length = **pos;
1076cb5b57a9SCharles Keepax 		break;
1077cb5b57a9SCharles Keepax 	case 2:
10788299ee81SCharles Keepax 		length = le16_to_cpu(*((__le16 *)*pos));
1079cb5b57a9SCharles Keepax 		break;
1080cb5b57a9SCharles Keepax 	default:
1081cb5b57a9SCharles Keepax 		return 0;
1082cb5b57a9SCharles Keepax 	}
1083cb5b57a9SCharles Keepax 
1084cb5b57a9SCharles Keepax 	if (str)
1085cb5b57a9SCharles Keepax 		*str = *pos + bytes;
1086cb5b57a9SCharles Keepax 
1087cb5b57a9SCharles Keepax 	*pos += ((length + bytes) + 3) & ~0x03;
1088cb5b57a9SCharles Keepax 
1089cb5b57a9SCharles Keepax 	return length;
1090cb5b57a9SCharles Keepax }
1091cb5b57a9SCharles Keepax 
1092cb5b57a9SCharles Keepax static int wm_coeff_parse_int(int bytes, const u8 **pos)
1093cb5b57a9SCharles Keepax {
1094cb5b57a9SCharles Keepax 	int val = 0;
1095cb5b57a9SCharles Keepax 
1096cb5b57a9SCharles Keepax 	switch (bytes) {
1097cb5b57a9SCharles Keepax 	case 2:
10988299ee81SCharles Keepax 		val = le16_to_cpu(*((__le16 *)*pos));
1099cb5b57a9SCharles Keepax 		break;
1100cb5b57a9SCharles Keepax 	case 4:
11018299ee81SCharles Keepax 		val = le32_to_cpu(*((__le32 *)*pos));
1102cb5b57a9SCharles Keepax 		break;
1103cb5b57a9SCharles Keepax 	default:
1104cb5b57a9SCharles Keepax 		break;
1105cb5b57a9SCharles Keepax 	}
1106cb5b57a9SCharles Keepax 
1107cb5b57a9SCharles Keepax 	*pos += bytes;
1108cb5b57a9SCharles Keepax 
1109cb5b57a9SCharles Keepax 	return val;
1110cb5b57a9SCharles Keepax }
1111cb5b57a9SCharles Keepax 
11122323736dSCharles Keepax static inline void wm_coeff_parse_alg(struct wm_adsp *dsp, const u8 **data,
11132323736dSCharles Keepax 				      struct wm_coeff_parsed_alg *blk)
11142323736dSCharles Keepax {
11152323736dSCharles Keepax 	const struct wmfw_adsp_alg_data *raw;
11162323736dSCharles Keepax 
1117cb5b57a9SCharles Keepax 	switch (dsp->fw_ver) {
1118cb5b57a9SCharles Keepax 	case 0:
1119cb5b57a9SCharles Keepax 	case 1:
11202323736dSCharles Keepax 		raw = (const struct wmfw_adsp_alg_data *)*data;
11212323736dSCharles Keepax 		*data = raw->data;
11222323736dSCharles Keepax 
11232323736dSCharles Keepax 		blk->id = le32_to_cpu(raw->id);
11242323736dSCharles Keepax 		blk->name = raw->name;
11252323736dSCharles Keepax 		blk->name_len = strlen(raw->name);
11262323736dSCharles Keepax 		blk->ncoeff = le32_to_cpu(raw->ncoeff);
1127cb5b57a9SCharles Keepax 		break;
1128cb5b57a9SCharles Keepax 	default:
1129cb5b57a9SCharles Keepax 		blk->id = wm_coeff_parse_int(sizeof(raw->id), data);
1130cb5b57a9SCharles Keepax 		blk->name_len = wm_coeff_parse_string(sizeof(u8), data,
1131cb5b57a9SCharles Keepax 						      &blk->name);
1132cb5b57a9SCharles Keepax 		wm_coeff_parse_string(sizeof(u16), data, NULL);
1133cb5b57a9SCharles Keepax 		blk->ncoeff = wm_coeff_parse_int(sizeof(raw->ncoeff), data);
1134cb5b57a9SCharles Keepax 		break;
1135cb5b57a9SCharles Keepax 	}
11362323736dSCharles Keepax 
11372323736dSCharles Keepax 	adsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id);
11382323736dSCharles Keepax 	adsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name);
11392323736dSCharles Keepax 	adsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff);
11402323736dSCharles Keepax }
11412323736dSCharles Keepax 
11422323736dSCharles Keepax static inline void wm_coeff_parse_coeff(struct wm_adsp *dsp, const u8 **data,
11432323736dSCharles Keepax 					struct wm_coeff_parsed_coeff *blk)
11442323736dSCharles Keepax {
11452323736dSCharles Keepax 	const struct wmfw_adsp_coeff_data *raw;
1146cb5b57a9SCharles Keepax 	const u8 *tmp;
1147cb5b57a9SCharles Keepax 	int length;
11482323736dSCharles Keepax 
1149cb5b57a9SCharles Keepax 	switch (dsp->fw_ver) {
1150cb5b57a9SCharles Keepax 	case 0:
1151cb5b57a9SCharles Keepax 	case 1:
11522323736dSCharles Keepax 		raw = (const struct wmfw_adsp_coeff_data *)*data;
11532323736dSCharles Keepax 		*data = *data + sizeof(raw->hdr) + le32_to_cpu(raw->hdr.size);
11542323736dSCharles Keepax 
11552323736dSCharles Keepax 		blk->offset = le16_to_cpu(raw->hdr.offset);
11562323736dSCharles Keepax 		blk->mem_type = le16_to_cpu(raw->hdr.type);
11572323736dSCharles Keepax 		blk->name = raw->name;
11582323736dSCharles Keepax 		blk->name_len = strlen(raw->name);
11592323736dSCharles Keepax 		blk->ctl_type = le16_to_cpu(raw->ctl_type);
11602323736dSCharles Keepax 		blk->flags = le16_to_cpu(raw->flags);
11612323736dSCharles Keepax 		blk->len = le32_to_cpu(raw->len);
1162cb5b57a9SCharles Keepax 		break;
1163cb5b57a9SCharles Keepax 	default:
1164cb5b57a9SCharles Keepax 		tmp = *data;
1165cb5b57a9SCharles Keepax 		blk->offset = wm_coeff_parse_int(sizeof(raw->hdr.offset), &tmp);
1166cb5b57a9SCharles Keepax 		blk->mem_type = wm_coeff_parse_int(sizeof(raw->hdr.type), &tmp);
1167cb5b57a9SCharles Keepax 		length = wm_coeff_parse_int(sizeof(raw->hdr.size), &tmp);
1168cb5b57a9SCharles Keepax 		blk->name_len = wm_coeff_parse_string(sizeof(u8), &tmp,
1169cb5b57a9SCharles Keepax 						      &blk->name);
1170cb5b57a9SCharles Keepax 		wm_coeff_parse_string(sizeof(u8), &tmp, NULL);
1171cb5b57a9SCharles Keepax 		wm_coeff_parse_string(sizeof(u16), &tmp, NULL);
1172cb5b57a9SCharles Keepax 		blk->ctl_type = wm_coeff_parse_int(sizeof(raw->ctl_type), &tmp);
1173cb5b57a9SCharles Keepax 		blk->flags = wm_coeff_parse_int(sizeof(raw->flags), &tmp);
1174cb5b57a9SCharles Keepax 		blk->len = wm_coeff_parse_int(sizeof(raw->len), &tmp);
1175cb5b57a9SCharles Keepax 
1176cb5b57a9SCharles Keepax 		*data = *data + sizeof(raw->hdr) + length;
1177cb5b57a9SCharles Keepax 		break;
1178cb5b57a9SCharles Keepax 	}
11792323736dSCharles Keepax 
11802323736dSCharles Keepax 	adsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type);
11812323736dSCharles Keepax 	adsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset);
11822323736dSCharles Keepax 	adsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name);
11832323736dSCharles Keepax 	adsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags);
11842323736dSCharles Keepax 	adsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type);
11852323736dSCharles Keepax 	adsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len);
11862323736dSCharles Keepax }
11872323736dSCharles Keepax 
11882323736dSCharles Keepax static int wm_adsp_parse_coeff(struct wm_adsp *dsp,
11892323736dSCharles Keepax 			       const struct wmfw_region *region)
11902323736dSCharles Keepax {
11912323736dSCharles Keepax 	struct wm_adsp_alg_region alg_region = {};
11922323736dSCharles Keepax 	struct wm_coeff_parsed_alg alg_blk;
11932323736dSCharles Keepax 	struct wm_coeff_parsed_coeff coeff_blk;
11942323736dSCharles Keepax 	const u8 *data = region->data;
11952323736dSCharles Keepax 	int i, ret;
11962323736dSCharles Keepax 
11972323736dSCharles Keepax 	wm_coeff_parse_alg(dsp, &data, &alg_blk);
11982323736dSCharles Keepax 	for (i = 0; i < alg_blk.ncoeff; i++) {
11992323736dSCharles Keepax 		wm_coeff_parse_coeff(dsp, &data, &coeff_blk);
12002323736dSCharles Keepax 
12012323736dSCharles Keepax 		switch (coeff_blk.ctl_type) {
12022323736dSCharles Keepax 		case SNDRV_CTL_ELEM_TYPE_BYTES:
12032323736dSCharles Keepax 			break;
12042323736dSCharles Keepax 		default:
12052323736dSCharles Keepax 			adsp_err(dsp, "Unknown control type: %d\n",
12062323736dSCharles Keepax 				 coeff_blk.ctl_type);
12072323736dSCharles Keepax 			return -EINVAL;
12082323736dSCharles Keepax 		}
12092323736dSCharles Keepax 
12102323736dSCharles Keepax 		alg_region.type = coeff_blk.mem_type;
12112323736dSCharles Keepax 		alg_region.alg = alg_blk.id;
12122323736dSCharles Keepax 
12132323736dSCharles Keepax 		ret = wm_adsp_create_control(dsp, &alg_region,
12142323736dSCharles Keepax 					     coeff_blk.offset,
12152323736dSCharles Keepax 					     coeff_blk.len,
12162323736dSCharles Keepax 					     coeff_blk.name,
121726c22a19SCharles Keepax 					     coeff_blk.name_len,
121826c22a19SCharles Keepax 					     coeff_blk.flags);
12192323736dSCharles Keepax 		if (ret < 0)
12202323736dSCharles Keepax 			adsp_err(dsp, "Failed to create control: %.*s, %d\n",
12212323736dSCharles Keepax 				 coeff_blk.name_len, coeff_blk.name, ret);
12222323736dSCharles Keepax 	}
12232323736dSCharles Keepax 
12242323736dSCharles Keepax 	return 0;
12252323736dSCharles Keepax }
12262323736dSCharles Keepax 
12272159ad93SMark Brown static int wm_adsp_load(struct wm_adsp *dsp)
12282159ad93SMark Brown {
1229cf17c83cSMark Brown 	LIST_HEAD(buf_list);
12302159ad93SMark Brown 	const struct firmware *firmware;
12312159ad93SMark Brown 	struct regmap *regmap = dsp->regmap;
12322159ad93SMark Brown 	unsigned int pos = 0;
12332159ad93SMark Brown 	const struct wmfw_header *header;
12342159ad93SMark Brown 	const struct wmfw_adsp1_sizes *adsp1_sizes;
12352159ad93SMark Brown 	const struct wmfw_adsp2_sizes *adsp2_sizes;
12362159ad93SMark Brown 	const struct wmfw_footer *footer;
12372159ad93SMark Brown 	const struct wmfw_region *region;
12382159ad93SMark Brown 	const struct wm_adsp_region *mem;
12392159ad93SMark Brown 	const char *region_name;
12402159ad93SMark Brown 	char *file, *text;
1241cf17c83cSMark Brown 	struct wm_adsp_buf *buf;
12422159ad93SMark Brown 	unsigned int reg;
12432159ad93SMark Brown 	int regions = 0;
12442159ad93SMark Brown 	int ret, offset, type, sizes;
12452159ad93SMark Brown 
12462159ad93SMark Brown 	file = kzalloc(PAGE_SIZE, GFP_KERNEL);
12472159ad93SMark Brown 	if (file == NULL)
12482159ad93SMark Brown 		return -ENOMEM;
12492159ad93SMark Brown 
12501023dbd9SMark Brown 	snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.wmfw", dsp->part, dsp->num,
12511023dbd9SMark Brown 		 wm_adsp_fw[dsp->fw].file);
12522159ad93SMark Brown 	file[PAGE_SIZE - 1] = '\0';
12532159ad93SMark Brown 
12542159ad93SMark Brown 	ret = request_firmware(&firmware, file, dsp->dev);
12552159ad93SMark Brown 	if (ret != 0) {
12562159ad93SMark Brown 		adsp_err(dsp, "Failed to request '%s'\n", file);
12572159ad93SMark Brown 		goto out;
12582159ad93SMark Brown 	}
12592159ad93SMark Brown 	ret = -EINVAL;
12602159ad93SMark Brown 
12612159ad93SMark Brown 	pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
12622159ad93SMark Brown 	if (pos >= firmware->size) {
12632159ad93SMark Brown 		adsp_err(dsp, "%s: file too short, %zu bytes\n",
12642159ad93SMark Brown 			 file, firmware->size);
12652159ad93SMark Brown 		goto out_fw;
12662159ad93SMark Brown 	}
12672159ad93SMark Brown 
12682159ad93SMark Brown 	header = (void *)&firmware->data[0];
12692159ad93SMark Brown 
12702159ad93SMark Brown 	if (memcmp(&header->magic[0], "WMFW", 4) != 0) {
12712159ad93SMark Brown 		adsp_err(dsp, "%s: invalid magic\n", file);
12722159ad93SMark Brown 		goto out_fw;
12732159ad93SMark Brown 	}
12742159ad93SMark Brown 
12752323736dSCharles Keepax 	switch (header->ver) {
12762323736dSCharles Keepax 	case 0:
1277c61e59feSCharles Keepax 		adsp_warn(dsp, "%s: Depreciated file format %d\n",
1278c61e59feSCharles Keepax 			  file, header->ver);
1279c61e59feSCharles Keepax 		break;
12802323736dSCharles Keepax 	case 1:
1281cb5b57a9SCharles Keepax 	case 2:
12822323736dSCharles Keepax 		break;
12832323736dSCharles Keepax 	default:
12842159ad93SMark Brown 		adsp_err(dsp, "%s: unknown file format %d\n",
12852159ad93SMark Brown 			 file, header->ver);
12862159ad93SMark Brown 		goto out_fw;
12872159ad93SMark Brown 	}
12882323736dSCharles Keepax 
12893626992aSDimitris Papastamos 	adsp_info(dsp, "Firmware version: %d\n", header->ver);
12902323736dSCharles Keepax 	dsp->fw_ver = header->ver;
12912159ad93SMark Brown 
12922159ad93SMark Brown 	if (header->core != dsp->type) {
12932159ad93SMark Brown 		adsp_err(dsp, "%s: invalid core %d != %d\n",
12942159ad93SMark Brown 			 file, header->core, dsp->type);
12952159ad93SMark Brown 		goto out_fw;
12962159ad93SMark Brown 	}
12972159ad93SMark Brown 
12982159ad93SMark Brown 	switch (dsp->type) {
12992159ad93SMark Brown 	case WMFW_ADSP1:
13002159ad93SMark Brown 		pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
13012159ad93SMark Brown 		adsp1_sizes = (void *)&(header[1]);
13022159ad93SMark Brown 		footer = (void *)&(adsp1_sizes[1]);
13032159ad93SMark Brown 		sizes = sizeof(*adsp1_sizes);
13042159ad93SMark Brown 
13052159ad93SMark Brown 		adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n",
13062159ad93SMark Brown 			 file, le32_to_cpu(adsp1_sizes->dm),
13072159ad93SMark Brown 			 le32_to_cpu(adsp1_sizes->pm),
13082159ad93SMark Brown 			 le32_to_cpu(adsp1_sizes->zm));
13092159ad93SMark Brown 		break;
13102159ad93SMark Brown 
13112159ad93SMark Brown 	case WMFW_ADSP2:
13122159ad93SMark Brown 		pos = sizeof(*header) + sizeof(*adsp2_sizes) + sizeof(*footer);
13132159ad93SMark Brown 		adsp2_sizes = (void *)&(header[1]);
13142159ad93SMark Brown 		footer = (void *)&(adsp2_sizes[1]);
13152159ad93SMark Brown 		sizes = sizeof(*adsp2_sizes);
13162159ad93SMark Brown 
13172159ad93SMark Brown 		adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n",
13182159ad93SMark Brown 			 file, le32_to_cpu(adsp2_sizes->xm),
13192159ad93SMark Brown 			 le32_to_cpu(adsp2_sizes->ym),
13202159ad93SMark Brown 			 le32_to_cpu(adsp2_sizes->pm),
13212159ad93SMark Brown 			 le32_to_cpu(adsp2_sizes->zm));
13222159ad93SMark Brown 		break;
13232159ad93SMark Brown 
13242159ad93SMark Brown 	default:
13256c452bdaSTakashi Iwai 		WARN(1, "Unknown DSP type");
13262159ad93SMark Brown 		goto out_fw;
13272159ad93SMark Brown 	}
13282159ad93SMark Brown 
13292159ad93SMark Brown 	if (le32_to_cpu(header->len) != sizeof(*header) +
13302159ad93SMark Brown 	    sizes + sizeof(*footer)) {
13312159ad93SMark Brown 		adsp_err(dsp, "%s: unexpected header length %d\n",
13322159ad93SMark Brown 			 file, le32_to_cpu(header->len));
13332159ad93SMark Brown 		goto out_fw;
13342159ad93SMark Brown 	}
13352159ad93SMark Brown 
13362159ad93SMark Brown 	adsp_dbg(dsp, "%s: timestamp %llu\n", file,
13372159ad93SMark Brown 		 le64_to_cpu(footer->timestamp));
13382159ad93SMark Brown 
13392159ad93SMark Brown 	while (pos < firmware->size &&
13402159ad93SMark Brown 	       pos - firmware->size > sizeof(*region)) {
13412159ad93SMark Brown 		region = (void *)&(firmware->data[pos]);
13422159ad93SMark Brown 		region_name = "Unknown";
13432159ad93SMark Brown 		reg = 0;
13442159ad93SMark Brown 		text = NULL;
13452159ad93SMark Brown 		offset = le32_to_cpu(region->offset) & 0xffffff;
13462159ad93SMark Brown 		type = be32_to_cpu(region->type) & 0xff;
13472159ad93SMark Brown 		mem = wm_adsp_find_region(dsp, type);
13482159ad93SMark Brown 
13492159ad93SMark Brown 		switch (type) {
13502159ad93SMark Brown 		case WMFW_NAME_TEXT:
13512159ad93SMark Brown 			region_name = "Firmware name";
13522159ad93SMark Brown 			text = kzalloc(le32_to_cpu(region->len) + 1,
13532159ad93SMark Brown 				       GFP_KERNEL);
13542159ad93SMark Brown 			break;
13552323736dSCharles Keepax 		case WMFW_ALGORITHM_DATA:
13562323736dSCharles Keepax 			region_name = "Algorithm";
13572323736dSCharles Keepax 			ret = wm_adsp_parse_coeff(dsp, region);
13582323736dSCharles Keepax 			if (ret != 0)
13592323736dSCharles Keepax 				goto out_fw;
13602323736dSCharles Keepax 			break;
13612159ad93SMark Brown 		case WMFW_INFO_TEXT:
13622159ad93SMark Brown 			region_name = "Information";
13632159ad93SMark Brown 			text = kzalloc(le32_to_cpu(region->len) + 1,
13642159ad93SMark Brown 				       GFP_KERNEL);
13652159ad93SMark Brown 			break;
13662159ad93SMark Brown 		case WMFW_ABSOLUTE:
13672159ad93SMark Brown 			region_name = "Absolute";
13682159ad93SMark Brown 			reg = offset;
13692159ad93SMark Brown 			break;
13702159ad93SMark Brown 		case WMFW_ADSP1_PM:
13712159ad93SMark Brown 			region_name = "PM";
137245b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
13732159ad93SMark Brown 			break;
13742159ad93SMark Brown 		case WMFW_ADSP1_DM:
13752159ad93SMark Brown 			region_name = "DM";
137645b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
13772159ad93SMark Brown 			break;
13782159ad93SMark Brown 		case WMFW_ADSP2_XM:
13792159ad93SMark Brown 			region_name = "XM";
138045b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
13812159ad93SMark Brown 			break;
13822159ad93SMark Brown 		case WMFW_ADSP2_YM:
13832159ad93SMark Brown 			region_name = "YM";
138445b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
13852159ad93SMark Brown 			break;
13862159ad93SMark Brown 		case WMFW_ADSP1_ZM:
13872159ad93SMark Brown 			region_name = "ZM";
138845b9ee72SMark Brown 			reg = wm_adsp_region_to_reg(mem, offset);
13892159ad93SMark Brown 			break;
13902159ad93SMark Brown 		default:
13912159ad93SMark Brown 			adsp_warn(dsp,
13922159ad93SMark Brown 				  "%s.%d: Unknown region type %x at %d(%x)\n",
13932159ad93SMark Brown 				  file, regions, type, pos, pos);
13942159ad93SMark Brown 			break;
13952159ad93SMark Brown 		}
13962159ad93SMark Brown 
13972159ad93SMark Brown 		adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file,
13982159ad93SMark Brown 			 regions, le32_to_cpu(region->len), offset,
13992159ad93SMark Brown 			 region_name);
14002159ad93SMark Brown 
14012159ad93SMark Brown 		if (text) {
14022159ad93SMark Brown 			memcpy(text, region->data, le32_to_cpu(region->len));
14032159ad93SMark Brown 			adsp_info(dsp, "%s: %s\n", file, text);
14042159ad93SMark Brown 			kfree(text);
14052159ad93SMark Brown 		}
14062159ad93SMark Brown 
14072159ad93SMark Brown 		if (reg) {
1408cdcd7f72SCharles Keepax 			buf = wm_adsp_buf_alloc(region->data,
1409cdcd7f72SCharles Keepax 						le32_to_cpu(region->len),
1410cf17c83cSMark Brown 						&buf_list);
1411a76fefabSMark Brown 			if (!buf) {
1412a76fefabSMark Brown 				adsp_err(dsp, "Out of memory\n");
14137328823dSDimitris Papastamos 				ret = -ENOMEM;
14147328823dSDimitris Papastamos 				goto out_fw;
1415a76fefabSMark Brown 			}
1416a76fefabSMark Brown 
1417cdcd7f72SCharles Keepax 			ret = regmap_raw_write_async(regmap, reg, buf->buf,
1418cdcd7f72SCharles Keepax 						     le32_to_cpu(region->len));
14192159ad93SMark Brown 			if (ret != 0) {
14202159ad93SMark Brown 				adsp_err(dsp,
1421cdcd7f72SCharles Keepax 					"%s.%d: Failed to write %d bytes at %d in %s: %d\n",
14222159ad93SMark Brown 					file, regions,
1423cdcd7f72SCharles Keepax 					le32_to_cpu(region->len), offset,
14242159ad93SMark Brown 					region_name, ret);
14252159ad93SMark Brown 				goto out_fw;
14262159ad93SMark Brown 			}
14272159ad93SMark Brown 		}
14282159ad93SMark Brown 
14292159ad93SMark Brown 		pos += le32_to_cpu(region->len) + sizeof(*region);
14302159ad93SMark Brown 		regions++;
14312159ad93SMark Brown 	}
14322159ad93SMark Brown 
1433cf17c83cSMark Brown 	ret = regmap_async_complete(regmap);
1434cf17c83cSMark Brown 	if (ret != 0) {
1435cf17c83cSMark Brown 		adsp_err(dsp, "Failed to complete async write: %d\n", ret);
1436cf17c83cSMark Brown 		goto out_fw;
1437cf17c83cSMark Brown 	}
1438cf17c83cSMark Brown 
14392159ad93SMark Brown 	if (pos > firmware->size)
14402159ad93SMark Brown 		adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
14412159ad93SMark Brown 			  file, regions, pos - firmware->size);
14422159ad93SMark Brown 
1443f9f55e31SRichard Fitzgerald 	wm_adsp_debugfs_save_wmfwname(dsp, file);
1444f9f55e31SRichard Fitzgerald 
14452159ad93SMark Brown out_fw:
1446cf17c83cSMark Brown 	regmap_async_complete(regmap);
1447cf17c83cSMark Brown 	wm_adsp_buf_free(&buf_list);
14482159ad93SMark Brown 	release_firmware(firmware);
14492159ad93SMark Brown out:
14502159ad93SMark Brown 	kfree(file);
14512159ad93SMark Brown 
14522159ad93SMark Brown 	return ret;
14532159ad93SMark Brown }
14542159ad93SMark Brown 
14552323736dSCharles Keepax static void wm_adsp_ctl_fixup_base(struct wm_adsp *dsp,
14562323736dSCharles Keepax 				  const struct wm_adsp_alg_region *alg_region)
14572323736dSCharles Keepax {
14582323736dSCharles Keepax 	struct wm_coeff_ctl *ctl;
14592323736dSCharles Keepax 
14602323736dSCharles Keepax 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
14612323736dSCharles Keepax 		if (ctl->fw_name == wm_adsp_fw_text[dsp->fw] &&
14622323736dSCharles Keepax 		    alg_region->alg == ctl->alg_region.alg &&
14632323736dSCharles Keepax 		    alg_region->type == ctl->alg_region.type) {
14642323736dSCharles Keepax 			ctl->alg_region.base = alg_region->base;
14652323736dSCharles Keepax 		}
14662323736dSCharles Keepax 	}
14672323736dSCharles Keepax }
14682323736dSCharles Keepax 
14693809f001SCharles Keepax static void *wm_adsp_read_algs(struct wm_adsp *dsp, size_t n_algs,
1470b618a185SCharles Keepax 			       unsigned int pos, unsigned int len)
1471db40517cSMark Brown {
1472b618a185SCharles Keepax 	void *alg;
1473b618a185SCharles Keepax 	int ret;
1474db40517cSMark Brown 	__be32 val;
1475db40517cSMark Brown 
14763809f001SCharles Keepax 	if (n_algs == 0) {
1477b618a185SCharles Keepax 		adsp_err(dsp, "No algorithms\n");
1478b618a185SCharles Keepax 		return ERR_PTR(-EINVAL);
1479db40517cSMark Brown 	}
1480db40517cSMark Brown 
14813809f001SCharles Keepax 	if (n_algs > 1024) {
14823809f001SCharles Keepax 		adsp_err(dsp, "Algorithm count %zx excessive\n", n_algs);
1483b618a185SCharles Keepax 		return ERR_PTR(-EINVAL);
1484b618a185SCharles Keepax 	}
1485b618a185SCharles Keepax 
1486b618a185SCharles Keepax 	/* Read the terminator first to validate the length */
1487b618a185SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, pos + len, &val, sizeof(val));
1488b618a185SCharles Keepax 	if (ret != 0) {
1489b618a185SCharles Keepax 		adsp_err(dsp, "Failed to read algorithm list end: %d\n",
1490b618a185SCharles Keepax 			ret);
1491b618a185SCharles Keepax 		return ERR_PTR(ret);
1492b618a185SCharles Keepax 	}
1493b618a185SCharles Keepax 
1494b618a185SCharles Keepax 	if (be32_to_cpu(val) != 0xbedead)
1495b618a185SCharles Keepax 		adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbeadead\n",
1496b618a185SCharles Keepax 			  pos + len, be32_to_cpu(val));
1497b618a185SCharles Keepax 
1498b618a185SCharles Keepax 	alg = kzalloc(len * 2, GFP_KERNEL | GFP_DMA);
1499b618a185SCharles Keepax 	if (!alg)
1500b618a185SCharles Keepax 		return ERR_PTR(-ENOMEM);
1501b618a185SCharles Keepax 
1502b618a185SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, pos, alg, len * 2);
1503b618a185SCharles Keepax 	if (ret != 0) {
15047d00cd97SCharles Keepax 		adsp_err(dsp, "Failed to read algorithm list: %d\n", ret);
1505b618a185SCharles Keepax 		kfree(alg);
1506b618a185SCharles Keepax 		return ERR_PTR(ret);
1507b618a185SCharles Keepax 	}
1508b618a185SCharles Keepax 
1509b618a185SCharles Keepax 	return alg;
1510b618a185SCharles Keepax }
1511b618a185SCharles Keepax 
151214197095SCharles Keepax static struct wm_adsp_alg_region *
151314197095SCharles Keepax 	wm_adsp_find_alg_region(struct wm_adsp *dsp, int type, unsigned int id)
151414197095SCharles Keepax {
151514197095SCharles Keepax 	struct wm_adsp_alg_region *alg_region;
151614197095SCharles Keepax 
151714197095SCharles Keepax 	list_for_each_entry(alg_region, &dsp->alg_regions, list) {
151814197095SCharles Keepax 		if (id == alg_region->alg && type == alg_region->type)
151914197095SCharles Keepax 			return alg_region;
152014197095SCharles Keepax 	}
152114197095SCharles Keepax 
152214197095SCharles Keepax 	return NULL;
152314197095SCharles Keepax }
152414197095SCharles Keepax 
1525d9d20e17SCharles Keepax static struct wm_adsp_alg_region *wm_adsp_create_region(struct wm_adsp *dsp,
1526d9d20e17SCharles Keepax 							int type, __be32 id,
1527d9d20e17SCharles Keepax 							__be32 base)
1528d9d20e17SCharles Keepax {
1529d9d20e17SCharles Keepax 	struct wm_adsp_alg_region *alg_region;
1530d9d20e17SCharles Keepax 
1531d9d20e17SCharles Keepax 	alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL);
1532d9d20e17SCharles Keepax 	if (!alg_region)
1533d9d20e17SCharles Keepax 		return ERR_PTR(-ENOMEM);
1534d9d20e17SCharles Keepax 
1535d9d20e17SCharles Keepax 	alg_region->type = type;
1536d9d20e17SCharles Keepax 	alg_region->alg = be32_to_cpu(id);
1537d9d20e17SCharles Keepax 	alg_region->base = be32_to_cpu(base);
1538d9d20e17SCharles Keepax 
1539d9d20e17SCharles Keepax 	list_add_tail(&alg_region->list, &dsp->alg_regions);
1540d9d20e17SCharles Keepax 
15412323736dSCharles Keepax 	if (dsp->fw_ver > 0)
15422323736dSCharles Keepax 		wm_adsp_ctl_fixup_base(dsp, alg_region);
15432323736dSCharles Keepax 
1544d9d20e17SCharles Keepax 	return alg_region;
1545d9d20e17SCharles Keepax }
1546d9d20e17SCharles Keepax 
1547b618a185SCharles Keepax static int wm_adsp1_setup_algs(struct wm_adsp *dsp)
1548b618a185SCharles Keepax {
1549b618a185SCharles Keepax 	struct wmfw_adsp1_id_hdr adsp1_id;
1550b618a185SCharles Keepax 	struct wmfw_adsp1_alg_hdr *adsp1_alg;
15513809f001SCharles Keepax 	struct wm_adsp_alg_region *alg_region;
1552b618a185SCharles Keepax 	const struct wm_adsp_region *mem;
1553b618a185SCharles Keepax 	unsigned int pos, len;
15543809f001SCharles Keepax 	size_t n_algs;
1555b618a185SCharles Keepax 	int i, ret;
1556b618a185SCharles Keepax 
1557b618a185SCharles Keepax 	mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM);
15586c452bdaSTakashi Iwai 	if (WARN_ON(!mem))
1559db40517cSMark Brown 		return -EINVAL;
1560db40517cSMark Brown 
1561b618a185SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id,
1562db40517cSMark Brown 			      sizeof(adsp1_id));
1563db40517cSMark Brown 	if (ret != 0) {
1564db40517cSMark Brown 		adsp_err(dsp, "Failed to read algorithm info: %d\n",
1565db40517cSMark Brown 			 ret);
1566db40517cSMark Brown 		return ret;
1567db40517cSMark Brown 	}
1568db40517cSMark Brown 
15693809f001SCharles Keepax 	n_algs = be32_to_cpu(adsp1_id.n_algs);
1570f395a218SMark Brown 	dsp->fw_id = be32_to_cpu(adsp1_id.fw.id);
1571db40517cSMark Brown 	adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
1572f395a218SMark Brown 		  dsp->fw_id,
1573db40517cSMark Brown 		  (be32_to_cpu(adsp1_id.fw.ver) & 0xff0000) >> 16,
1574db40517cSMark Brown 		  (be32_to_cpu(adsp1_id.fw.ver) & 0xff00) >> 8,
1575db40517cSMark Brown 		  be32_to_cpu(adsp1_id.fw.ver) & 0xff,
15763809f001SCharles Keepax 		  n_algs);
1577db40517cSMark Brown 
1578d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
1579d9d20e17SCharles Keepax 					   adsp1_id.fw.id, adsp1_id.zm);
1580d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1581d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1582ac50009fSMark Brown 
1583d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
1584d9d20e17SCharles Keepax 					   adsp1_id.fw.id, adsp1_id.dm);
1585d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1586d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1587ac50009fSMark Brown 
1588db40517cSMark Brown 	pos = sizeof(adsp1_id) / 2;
15893809f001SCharles Keepax 	len = (sizeof(*adsp1_alg) * n_algs) / 2;
1590db40517cSMark Brown 
15913809f001SCharles Keepax 	adsp1_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len);
1592b618a185SCharles Keepax 	if (IS_ERR(adsp1_alg))
1593b618a185SCharles Keepax 		return PTR_ERR(adsp1_alg);
1594db40517cSMark Brown 
15953809f001SCharles Keepax 	for (i = 0; i < n_algs; i++) {
1596471f4885SMark Brown 		adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
1597db40517cSMark Brown 			  i, be32_to_cpu(adsp1_alg[i].alg.id),
1598db40517cSMark Brown 			  (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16,
1599db40517cSMark Brown 			  (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8,
1600471f4885SMark Brown 			  be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff,
1601471f4885SMark Brown 			  be32_to_cpu(adsp1_alg[i].dm),
1602471f4885SMark Brown 			  be32_to_cpu(adsp1_alg[i].zm));
1603471f4885SMark Brown 
1604d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
1605d9d20e17SCharles Keepax 						   adsp1_alg[i].alg.id,
1606d9d20e17SCharles Keepax 						   adsp1_alg[i].dm);
1607d9d20e17SCharles Keepax 		if (IS_ERR(alg_region)) {
1608d9d20e17SCharles Keepax 			ret = PTR_ERR(alg_region);
1609d6d52179SJS Park 			goto out;
1610d6d52179SJS Park 		}
16112323736dSCharles Keepax 		if (dsp->fw_ver == 0) {
16123809f001SCharles Keepax 			if (i + 1 < n_algs) {
16136958eb2aSCharles Keepax 				len = be32_to_cpu(adsp1_alg[i + 1].dm);
16146958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp1_alg[i].dm);
16156958eb2aSCharles Keepax 				len *= 4;
16162323736dSCharles Keepax 				wm_adsp_create_control(dsp, alg_region, 0,
161726c22a19SCharles Keepax 						       len, NULL, 0, 0);
16186ab2b7b4SDimitris Papastamos 			} else {
16196ab2b7b4SDimitris Papastamos 				adsp_warn(dsp, "Missing length info for region DM with ID %x\n",
16206ab2b7b4SDimitris Papastamos 					  be32_to_cpu(adsp1_alg[i].alg.id));
16216ab2b7b4SDimitris Papastamos 			}
16222323736dSCharles Keepax 		}
1623471f4885SMark Brown 
1624d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
1625d9d20e17SCharles Keepax 						   adsp1_alg[i].alg.id,
1626d9d20e17SCharles Keepax 						   adsp1_alg[i].zm);
1627d9d20e17SCharles Keepax 		if (IS_ERR(alg_region)) {
1628d9d20e17SCharles Keepax 			ret = PTR_ERR(alg_region);
1629d6d52179SJS Park 			goto out;
1630d6d52179SJS Park 		}
16312323736dSCharles Keepax 		if (dsp->fw_ver == 0) {
16323809f001SCharles Keepax 			if (i + 1 < n_algs) {
16336958eb2aSCharles Keepax 				len = be32_to_cpu(adsp1_alg[i + 1].zm);
16346958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp1_alg[i].zm);
16356958eb2aSCharles Keepax 				len *= 4;
16362323736dSCharles Keepax 				wm_adsp_create_control(dsp, alg_region, 0,
163726c22a19SCharles Keepax 						       len, NULL, 0, 0);
16386ab2b7b4SDimitris Papastamos 			} else {
16396ab2b7b4SDimitris Papastamos 				adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
16406ab2b7b4SDimitris Papastamos 					  be32_to_cpu(adsp1_alg[i].alg.id));
16416ab2b7b4SDimitris Papastamos 			}
1642b618a185SCharles Keepax 		}
16432323736dSCharles Keepax 	}
1644db40517cSMark Brown 
1645b618a185SCharles Keepax out:
1646b618a185SCharles Keepax 	kfree(adsp1_alg);
1647b618a185SCharles Keepax 	return ret;
1648b618a185SCharles Keepax }
1649b618a185SCharles Keepax 
1650b618a185SCharles Keepax static int wm_adsp2_setup_algs(struct wm_adsp *dsp)
1651b618a185SCharles Keepax {
1652b618a185SCharles Keepax 	struct wmfw_adsp2_id_hdr adsp2_id;
1653b618a185SCharles Keepax 	struct wmfw_adsp2_alg_hdr *adsp2_alg;
16543809f001SCharles Keepax 	struct wm_adsp_alg_region *alg_region;
1655b618a185SCharles Keepax 	const struct wm_adsp_region *mem;
1656b618a185SCharles Keepax 	unsigned int pos, len;
16573809f001SCharles Keepax 	size_t n_algs;
1658b618a185SCharles Keepax 	int i, ret;
1659b618a185SCharles Keepax 
1660b618a185SCharles Keepax 	mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM);
1661b618a185SCharles Keepax 	if (WARN_ON(!mem))
1662b618a185SCharles Keepax 		return -EINVAL;
1663b618a185SCharles Keepax 
1664b618a185SCharles Keepax 	ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id,
1665b618a185SCharles Keepax 			      sizeof(adsp2_id));
1666b618a185SCharles Keepax 	if (ret != 0) {
1667b618a185SCharles Keepax 		adsp_err(dsp, "Failed to read algorithm info: %d\n",
1668b618a185SCharles Keepax 			 ret);
1669b618a185SCharles Keepax 		return ret;
1670b618a185SCharles Keepax 	}
1671b618a185SCharles Keepax 
16723809f001SCharles Keepax 	n_algs = be32_to_cpu(adsp2_id.n_algs);
1673b618a185SCharles Keepax 	dsp->fw_id = be32_to_cpu(adsp2_id.fw.id);
1674f9f55e31SRichard Fitzgerald 	dsp->fw_id_version = be32_to_cpu(adsp2_id.fw.ver);
1675b618a185SCharles Keepax 	adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
1676b618a185SCharles Keepax 		  dsp->fw_id,
1677f9f55e31SRichard Fitzgerald 		  (dsp->fw_id_version & 0xff0000) >> 16,
1678f9f55e31SRichard Fitzgerald 		  (dsp->fw_id_version & 0xff00) >> 8,
1679f9f55e31SRichard Fitzgerald 		  dsp->fw_id_version & 0xff,
16803809f001SCharles Keepax 		  n_algs);
1681b618a185SCharles Keepax 
1682d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
1683d9d20e17SCharles Keepax 					   adsp2_id.fw.id, adsp2_id.xm);
1684d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1685d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1686b618a185SCharles Keepax 
1687d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
1688d9d20e17SCharles Keepax 					   adsp2_id.fw.id, adsp2_id.ym);
1689d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1690d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1691b618a185SCharles Keepax 
1692d9d20e17SCharles Keepax 	alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
1693d9d20e17SCharles Keepax 					   adsp2_id.fw.id, adsp2_id.zm);
1694d9d20e17SCharles Keepax 	if (IS_ERR(alg_region))
1695d9d20e17SCharles Keepax 		return PTR_ERR(alg_region);
1696b618a185SCharles Keepax 
1697b618a185SCharles Keepax 	pos = sizeof(adsp2_id) / 2;
16983809f001SCharles Keepax 	len = (sizeof(*adsp2_alg) * n_algs) / 2;
1699b618a185SCharles Keepax 
17003809f001SCharles Keepax 	adsp2_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len);
1701b618a185SCharles Keepax 	if (IS_ERR(adsp2_alg))
1702b618a185SCharles Keepax 		return PTR_ERR(adsp2_alg);
1703b618a185SCharles Keepax 
17043809f001SCharles Keepax 	for (i = 0; i < n_algs; i++) {
1705471f4885SMark Brown 		adsp_info(dsp,
1706471f4885SMark Brown 			  "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
1707db40517cSMark Brown 			  i, be32_to_cpu(adsp2_alg[i].alg.id),
1708db40517cSMark Brown 			  (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16,
1709db40517cSMark Brown 			  (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8,
1710471f4885SMark Brown 			  be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff,
1711471f4885SMark Brown 			  be32_to_cpu(adsp2_alg[i].xm),
1712471f4885SMark Brown 			  be32_to_cpu(adsp2_alg[i].ym),
1713471f4885SMark Brown 			  be32_to_cpu(adsp2_alg[i].zm));
1714471f4885SMark Brown 
1715d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
1716d9d20e17SCharles Keepax 						   adsp2_alg[i].alg.id,
1717d9d20e17SCharles Keepax 						   adsp2_alg[i].xm);
1718d9d20e17SCharles Keepax 		if (IS_ERR(alg_region)) {
1719d9d20e17SCharles Keepax 			ret = PTR_ERR(alg_region);
1720d6d52179SJS Park 			goto out;
1721d6d52179SJS Park 		}
17222323736dSCharles Keepax 		if (dsp->fw_ver == 0) {
17233809f001SCharles Keepax 			if (i + 1 < n_algs) {
17246958eb2aSCharles Keepax 				len = be32_to_cpu(adsp2_alg[i + 1].xm);
17256958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp2_alg[i].xm);
17266958eb2aSCharles Keepax 				len *= 4;
17272323736dSCharles Keepax 				wm_adsp_create_control(dsp, alg_region, 0,
172826c22a19SCharles Keepax 						       len, NULL, 0, 0);
17296ab2b7b4SDimitris Papastamos 			} else {
17306ab2b7b4SDimitris Papastamos 				adsp_warn(dsp, "Missing length info for region XM with ID %x\n",
17316ab2b7b4SDimitris Papastamos 					  be32_to_cpu(adsp2_alg[i].alg.id));
17326ab2b7b4SDimitris Papastamos 			}
17332323736dSCharles Keepax 		}
1734471f4885SMark Brown 
1735d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
1736d9d20e17SCharles Keepax 						   adsp2_alg[i].alg.id,
1737d9d20e17SCharles Keepax 						   adsp2_alg[i].ym);
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(adsp2_alg[i + 1].ym);
17456958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp2_alg[i].ym);
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 YM with ID %x\n",
17516ab2b7b4SDimitris Papastamos 					  be32_to_cpu(adsp2_alg[i].alg.id));
17526ab2b7b4SDimitris Papastamos 			}
17532323736dSCharles Keepax 		}
1754471f4885SMark Brown 
1755d9d20e17SCharles Keepax 		alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
1756d9d20e17SCharles Keepax 						   adsp2_alg[i].alg.id,
1757d9d20e17SCharles Keepax 						   adsp2_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(adsp2_alg[i + 1].zm);
17656958eb2aSCharles Keepax 				len -= be32_to_cpu(adsp2_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(adsp2_alg[i].alg.id));
17726ab2b7b4SDimitris Papastamos 			}
1773db40517cSMark Brown 		}
17742323736dSCharles Keepax 	}
1775db40517cSMark Brown 
1776db40517cSMark Brown out:
1777b618a185SCharles Keepax 	kfree(adsp2_alg);
1778db40517cSMark Brown 	return ret;
1779db40517cSMark Brown }
1780db40517cSMark Brown 
17812159ad93SMark Brown static int wm_adsp_load_coeff(struct wm_adsp *dsp)
17822159ad93SMark Brown {
1783cf17c83cSMark Brown 	LIST_HEAD(buf_list);
17842159ad93SMark Brown 	struct regmap *regmap = dsp->regmap;
17852159ad93SMark Brown 	struct wmfw_coeff_hdr *hdr;
17862159ad93SMark Brown 	struct wmfw_coeff_item *blk;
17872159ad93SMark Brown 	const struct firmware *firmware;
1788471f4885SMark Brown 	const struct wm_adsp_region *mem;
1789471f4885SMark Brown 	struct wm_adsp_alg_region *alg_region;
17902159ad93SMark Brown 	const char *region_name;
17912159ad93SMark Brown 	int ret, pos, blocks, type, offset, reg;
17922159ad93SMark Brown 	char *file;
1793cf17c83cSMark Brown 	struct wm_adsp_buf *buf;
17942159ad93SMark Brown 
17952159ad93SMark Brown 	file = kzalloc(PAGE_SIZE, GFP_KERNEL);
17962159ad93SMark Brown 	if (file == NULL)
17972159ad93SMark Brown 		return -ENOMEM;
17982159ad93SMark Brown 
17991023dbd9SMark Brown 	snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.bin", dsp->part, dsp->num,
18001023dbd9SMark Brown 		 wm_adsp_fw[dsp->fw].file);
18012159ad93SMark Brown 	file[PAGE_SIZE - 1] = '\0';
18022159ad93SMark Brown 
18032159ad93SMark Brown 	ret = request_firmware(&firmware, file, dsp->dev);
18042159ad93SMark Brown 	if (ret != 0) {
18052159ad93SMark Brown 		adsp_warn(dsp, "Failed to request '%s'\n", file);
18062159ad93SMark Brown 		ret = 0;
18072159ad93SMark Brown 		goto out;
18082159ad93SMark Brown 	}
18092159ad93SMark Brown 	ret = -EINVAL;
18102159ad93SMark Brown 
18112159ad93SMark Brown 	if (sizeof(*hdr) >= firmware->size) {
18122159ad93SMark Brown 		adsp_err(dsp, "%s: file too short, %zu bytes\n",
18132159ad93SMark Brown 			file, firmware->size);
18142159ad93SMark Brown 		goto out_fw;
18152159ad93SMark Brown 	}
18162159ad93SMark Brown 
18172159ad93SMark Brown 	hdr = (void *)&firmware->data[0];
18182159ad93SMark Brown 	if (memcmp(hdr->magic, "WMDR", 4) != 0) {
18192159ad93SMark Brown 		adsp_err(dsp, "%s: invalid magic\n", file);
1820a4cdbec7SCharles Keepax 		goto out_fw;
18212159ad93SMark Brown 	}
18222159ad93SMark Brown 
1823c712326dSMark Brown 	switch (be32_to_cpu(hdr->rev) & 0xff) {
1824c712326dSMark Brown 	case 1:
1825c712326dSMark Brown 		break;
1826c712326dSMark Brown 	default:
1827c712326dSMark Brown 		adsp_err(dsp, "%s: Unsupported coefficient file format %d\n",
1828c712326dSMark Brown 			 file, be32_to_cpu(hdr->rev) & 0xff);
1829c712326dSMark Brown 		ret = -EINVAL;
1830c712326dSMark Brown 		goto out_fw;
1831c712326dSMark Brown 	}
1832c712326dSMark Brown 
18332159ad93SMark Brown 	adsp_dbg(dsp, "%s: v%d.%d.%d\n", file,
18342159ad93SMark Brown 		(le32_to_cpu(hdr->ver) >> 16) & 0xff,
18352159ad93SMark Brown 		(le32_to_cpu(hdr->ver) >>  8) & 0xff,
18362159ad93SMark Brown 		le32_to_cpu(hdr->ver) & 0xff);
18372159ad93SMark Brown 
18382159ad93SMark Brown 	pos = le32_to_cpu(hdr->len);
18392159ad93SMark Brown 
18402159ad93SMark Brown 	blocks = 0;
18412159ad93SMark Brown 	while (pos < firmware->size &&
18422159ad93SMark Brown 	       pos - firmware->size > sizeof(*blk)) {
18432159ad93SMark Brown 		blk = (void *)(&firmware->data[pos]);
18442159ad93SMark Brown 
1845c712326dSMark Brown 		type = le16_to_cpu(blk->type);
1846c712326dSMark Brown 		offset = le16_to_cpu(blk->offset);
18472159ad93SMark Brown 
18482159ad93SMark Brown 		adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n",
18492159ad93SMark Brown 			 file, blocks, le32_to_cpu(blk->id),
18502159ad93SMark Brown 			 (le32_to_cpu(blk->ver) >> 16) & 0xff,
18512159ad93SMark Brown 			 (le32_to_cpu(blk->ver) >>  8) & 0xff,
18522159ad93SMark Brown 			 le32_to_cpu(blk->ver) & 0xff);
18532159ad93SMark Brown 		adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n",
18542159ad93SMark Brown 			 file, blocks, le32_to_cpu(blk->len), offset, type);
18552159ad93SMark Brown 
18562159ad93SMark Brown 		reg = 0;
18572159ad93SMark Brown 		region_name = "Unknown";
18582159ad93SMark Brown 		switch (type) {
1859c712326dSMark Brown 		case (WMFW_NAME_TEXT << 8):
1860c712326dSMark Brown 		case (WMFW_INFO_TEXT << 8):
18612159ad93SMark Brown 			break;
1862c712326dSMark Brown 		case (WMFW_ABSOLUTE << 8):
1863f395a218SMark Brown 			/*
1864f395a218SMark Brown 			 * Old files may use this for global
1865f395a218SMark Brown 			 * coefficients.
1866f395a218SMark Brown 			 */
1867f395a218SMark Brown 			if (le32_to_cpu(blk->id) == dsp->fw_id &&
1868f395a218SMark Brown 			    offset == 0) {
1869f395a218SMark Brown 				region_name = "global coefficients";
1870f395a218SMark Brown 				mem = wm_adsp_find_region(dsp, type);
1871f395a218SMark Brown 				if (!mem) {
1872f395a218SMark Brown 					adsp_err(dsp, "No ZM\n");
1873f395a218SMark Brown 					break;
1874f395a218SMark Brown 				}
1875f395a218SMark Brown 				reg = wm_adsp_region_to_reg(mem, 0);
1876f395a218SMark Brown 
1877f395a218SMark Brown 			} else {
18782159ad93SMark Brown 				region_name = "register";
18792159ad93SMark Brown 				reg = offset;
1880f395a218SMark Brown 			}
18812159ad93SMark Brown 			break;
1882471f4885SMark Brown 
1883471f4885SMark Brown 		case WMFW_ADSP1_DM:
1884471f4885SMark Brown 		case WMFW_ADSP1_ZM:
1885471f4885SMark Brown 		case WMFW_ADSP2_XM:
1886471f4885SMark Brown 		case WMFW_ADSP2_YM:
1887471f4885SMark Brown 			adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n",
1888471f4885SMark Brown 				 file, blocks, le32_to_cpu(blk->len),
1889471f4885SMark Brown 				 type, le32_to_cpu(blk->id));
1890471f4885SMark Brown 
1891471f4885SMark Brown 			mem = wm_adsp_find_region(dsp, type);
1892471f4885SMark Brown 			if (!mem) {
1893471f4885SMark Brown 				adsp_err(dsp, "No base for region %x\n", type);
1894471f4885SMark Brown 				break;
1895471f4885SMark Brown 			}
1896471f4885SMark Brown 
189714197095SCharles Keepax 			alg_region = wm_adsp_find_alg_region(dsp, type,
189814197095SCharles Keepax 						le32_to_cpu(blk->id));
189914197095SCharles Keepax 			if (alg_region) {
1900338c5188SMark Brown 				reg = alg_region->base;
190114197095SCharles Keepax 				reg = wm_adsp_region_to_reg(mem, reg);
1902338c5188SMark Brown 				reg += offset;
190314197095SCharles Keepax 			} else {
1904471f4885SMark Brown 				adsp_err(dsp, "No %x for algorithm %x\n",
1905471f4885SMark Brown 					 type, le32_to_cpu(blk->id));
190614197095SCharles Keepax 			}
1907471f4885SMark Brown 			break;
1908471f4885SMark Brown 
19092159ad93SMark Brown 		default:
191025c62f7eSMark Brown 			adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n",
191125c62f7eSMark Brown 				 file, blocks, type, pos);
19122159ad93SMark Brown 			break;
19132159ad93SMark Brown 		}
19142159ad93SMark Brown 
19152159ad93SMark Brown 		if (reg) {
1916cf17c83cSMark Brown 			buf = wm_adsp_buf_alloc(blk->data,
1917cf17c83cSMark Brown 						le32_to_cpu(blk->len),
1918cf17c83cSMark Brown 						&buf_list);
1919a76fefabSMark Brown 			if (!buf) {
1920a76fefabSMark Brown 				adsp_err(dsp, "Out of memory\n");
1921f4b82812SWei Yongjun 				ret = -ENOMEM;
1922f4b82812SWei Yongjun 				goto out_fw;
1923a76fefabSMark Brown 			}
1924a76fefabSMark Brown 
192520da6d5aSMark Brown 			adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n",
192620da6d5aSMark Brown 				 file, blocks, le32_to_cpu(blk->len),
192720da6d5aSMark Brown 				 reg);
1928cf17c83cSMark Brown 			ret = regmap_raw_write_async(regmap, reg, buf->buf,
19292159ad93SMark Brown 						     le32_to_cpu(blk->len));
19302159ad93SMark Brown 			if (ret != 0) {
19312159ad93SMark Brown 				adsp_err(dsp,
193243bc3bf6SDimitris Papastamos 					"%s.%d: Failed to write to %x in %s: %d\n",
193343bc3bf6SDimitris Papastamos 					file, blocks, reg, region_name, ret);
19342159ad93SMark Brown 			}
19352159ad93SMark Brown 		}
19362159ad93SMark Brown 
1937be951017SCharles Keepax 		pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03;
19382159ad93SMark Brown 		blocks++;
19392159ad93SMark Brown 	}
19402159ad93SMark Brown 
1941cf17c83cSMark Brown 	ret = regmap_async_complete(regmap);
1942cf17c83cSMark Brown 	if (ret != 0)
1943cf17c83cSMark Brown 		adsp_err(dsp, "Failed to complete async write: %d\n", ret);
1944cf17c83cSMark Brown 
19452159ad93SMark Brown 	if (pos > firmware->size)
19462159ad93SMark Brown 		adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
19472159ad93SMark Brown 			  file, blocks, pos - firmware->size);
19482159ad93SMark Brown 
1949f9f55e31SRichard Fitzgerald 	wm_adsp_debugfs_save_binname(dsp, file);
1950f9f55e31SRichard Fitzgerald 
19512159ad93SMark Brown out_fw:
19529da7a5a9SCharles Keepax 	regmap_async_complete(regmap);
19532159ad93SMark Brown 	release_firmware(firmware);
1954cf17c83cSMark Brown 	wm_adsp_buf_free(&buf_list);
19552159ad93SMark Brown out:
19562159ad93SMark Brown 	kfree(file);
1957f4b82812SWei Yongjun 	return ret;
19582159ad93SMark Brown }
19592159ad93SMark Brown 
19603809f001SCharles Keepax int wm_adsp1_init(struct wm_adsp *dsp)
19615e7a7a22SMark Brown {
19623809f001SCharles Keepax 	INIT_LIST_HEAD(&dsp->alg_regions);
19635e7a7a22SMark Brown 
1964078e7183SCharles Keepax 	mutex_init(&dsp->pwr_lock);
1965078e7183SCharles Keepax 
19665e7a7a22SMark Brown 	return 0;
19675e7a7a22SMark Brown }
19685e7a7a22SMark Brown EXPORT_SYMBOL_GPL(wm_adsp1_init);
19695e7a7a22SMark Brown 
19702159ad93SMark Brown int wm_adsp1_event(struct snd_soc_dapm_widget *w,
19712159ad93SMark Brown 		   struct snd_kcontrol *kcontrol,
19722159ad93SMark Brown 		   int event)
19732159ad93SMark Brown {
197472718517SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
19752159ad93SMark Brown 	struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
19762159ad93SMark Brown 	struct wm_adsp *dsp = &dsps[w->shift];
1977b0101b4fSDimitris Papastamos 	struct wm_adsp_alg_region *alg_region;
19786ab2b7b4SDimitris Papastamos 	struct wm_coeff_ctl *ctl;
19792159ad93SMark Brown 	int ret;
19807585a5b0SCharles Keepax 	unsigned int val;
19812159ad93SMark Brown 
198200200107SLars-Peter Clausen 	dsp->card = codec->component.card;
198392bb4c32SDimitris Papastamos 
1984078e7183SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
1985078e7183SCharles Keepax 
19862159ad93SMark Brown 	switch (event) {
19872159ad93SMark Brown 	case SND_SOC_DAPM_POST_PMU:
19882159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
19892159ad93SMark Brown 				   ADSP1_SYS_ENA, ADSP1_SYS_ENA);
19902159ad93SMark Brown 
199194e205bfSChris Rattray 		/*
199294e205bfSChris Rattray 		 * For simplicity set the DSP clock rate to be the
199394e205bfSChris Rattray 		 * SYSCLK rate rather than making it configurable.
199494e205bfSChris Rattray 		 */
199594e205bfSChris Rattray 		if (dsp->sysclk_reg) {
199694e205bfSChris Rattray 			ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val);
199794e205bfSChris Rattray 			if (ret != 0) {
199894e205bfSChris Rattray 				adsp_err(dsp, "Failed to read SYSCLK state: %d\n",
199994e205bfSChris Rattray 				ret);
2000078e7183SCharles Keepax 				goto err_mutex;
200194e205bfSChris Rattray 			}
200294e205bfSChris Rattray 
20037d00cd97SCharles Keepax 			val = (val & dsp->sysclk_mask) >> dsp->sysclk_shift;
200494e205bfSChris Rattray 
200594e205bfSChris Rattray 			ret = regmap_update_bits(dsp->regmap,
200694e205bfSChris Rattray 						 dsp->base + ADSP1_CONTROL_31,
200794e205bfSChris Rattray 						 ADSP1_CLK_SEL_MASK, val);
200894e205bfSChris Rattray 			if (ret != 0) {
200994e205bfSChris Rattray 				adsp_err(dsp, "Failed to set clock rate: %d\n",
201094e205bfSChris Rattray 					 ret);
2011078e7183SCharles Keepax 				goto err_mutex;
201294e205bfSChris Rattray 			}
201394e205bfSChris Rattray 		}
201494e205bfSChris Rattray 
20152159ad93SMark Brown 		ret = wm_adsp_load(dsp);
20162159ad93SMark Brown 		if (ret != 0)
2017078e7183SCharles Keepax 			goto err_ena;
20182159ad93SMark Brown 
2019b618a185SCharles Keepax 		ret = wm_adsp1_setup_algs(dsp);
2020db40517cSMark Brown 		if (ret != 0)
2021078e7183SCharles Keepax 			goto err_ena;
2022db40517cSMark Brown 
20232159ad93SMark Brown 		ret = wm_adsp_load_coeff(dsp);
20242159ad93SMark Brown 		if (ret != 0)
2025078e7183SCharles Keepax 			goto err_ena;
20262159ad93SMark Brown 
20270c2e3f34SDimitris Papastamos 		/* Initialize caches for enabled and unset controls */
202881ad93ecSDimitris Papastamos 		ret = wm_coeff_init_control_caches(dsp);
20296ab2b7b4SDimitris Papastamos 		if (ret != 0)
2030078e7183SCharles Keepax 			goto err_ena;
20316ab2b7b4SDimitris Papastamos 
20320c2e3f34SDimitris Papastamos 		/* Sync set controls */
203381ad93ecSDimitris Papastamos 		ret = wm_coeff_sync_controls(dsp);
20346ab2b7b4SDimitris Papastamos 		if (ret != 0)
2035078e7183SCharles Keepax 			goto err_ena;
20366ab2b7b4SDimitris Papastamos 
20372159ad93SMark Brown 		/* Start the core running */
20382159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
20392159ad93SMark Brown 				   ADSP1_CORE_ENA | ADSP1_START,
20402159ad93SMark Brown 				   ADSP1_CORE_ENA | ADSP1_START);
20412159ad93SMark Brown 		break;
20422159ad93SMark Brown 
20432159ad93SMark Brown 	case SND_SOC_DAPM_PRE_PMD:
20442159ad93SMark Brown 		/* Halt the core */
20452159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
20462159ad93SMark Brown 				   ADSP1_CORE_ENA | ADSP1_START, 0);
20472159ad93SMark Brown 
20482159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19,
20492159ad93SMark Brown 				   ADSP1_WDMA_BUFFER_LENGTH_MASK, 0);
20502159ad93SMark Brown 
20512159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
20522159ad93SMark Brown 				   ADSP1_SYS_ENA, 0);
20536ab2b7b4SDimitris Papastamos 
205481ad93ecSDimitris Papastamos 		list_for_each_entry(ctl, &dsp->ctl_list, list)
20556ab2b7b4SDimitris Papastamos 			ctl->enabled = 0;
2056b0101b4fSDimitris Papastamos 
2057b0101b4fSDimitris Papastamos 		while (!list_empty(&dsp->alg_regions)) {
2058b0101b4fSDimitris Papastamos 			alg_region = list_first_entry(&dsp->alg_regions,
2059b0101b4fSDimitris Papastamos 						      struct wm_adsp_alg_region,
2060b0101b4fSDimitris Papastamos 						      list);
2061b0101b4fSDimitris Papastamos 			list_del(&alg_region->list);
2062b0101b4fSDimitris Papastamos 			kfree(alg_region);
2063b0101b4fSDimitris Papastamos 		}
20642159ad93SMark Brown 		break;
20652159ad93SMark Brown 
20662159ad93SMark Brown 	default:
20672159ad93SMark Brown 		break;
20682159ad93SMark Brown 	}
20692159ad93SMark Brown 
2070078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2071078e7183SCharles Keepax 
20722159ad93SMark Brown 	return 0;
20732159ad93SMark Brown 
2074078e7183SCharles Keepax err_ena:
20752159ad93SMark Brown 	regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
20762159ad93SMark Brown 			   ADSP1_SYS_ENA, 0);
2077078e7183SCharles Keepax err_mutex:
2078078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2079078e7183SCharles Keepax 
20802159ad93SMark Brown 	return ret;
20812159ad93SMark Brown }
20822159ad93SMark Brown EXPORT_SYMBOL_GPL(wm_adsp1_event);
20832159ad93SMark Brown 
20842159ad93SMark Brown static int wm_adsp2_ena(struct wm_adsp *dsp)
20852159ad93SMark Brown {
20862159ad93SMark Brown 	unsigned int val;
20872159ad93SMark Brown 	int ret, count;
20882159ad93SMark Brown 
20891552c325SMark Brown 	ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL,
20902159ad93SMark Brown 				       ADSP2_SYS_ENA, ADSP2_SYS_ENA);
20912159ad93SMark Brown 	if (ret != 0)
20922159ad93SMark Brown 		return ret;
20932159ad93SMark Brown 
20942159ad93SMark Brown 	/* Wait for the RAM to start, should be near instantaneous */
2095939fd1e8SCharles Keepax 	for (count = 0; count < 10; ++count) {
20967d00cd97SCharles Keepax 		ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1, &val);
20972159ad93SMark Brown 		if (ret != 0)
20982159ad93SMark Brown 			return ret;
2099939fd1e8SCharles Keepax 
2100939fd1e8SCharles Keepax 		if (val & ADSP2_RAM_RDY)
2101939fd1e8SCharles Keepax 			break;
2102939fd1e8SCharles Keepax 
2103939fd1e8SCharles Keepax 		msleep(1);
2104939fd1e8SCharles Keepax 	}
21052159ad93SMark Brown 
21062159ad93SMark Brown 	if (!(val & ADSP2_RAM_RDY)) {
21072159ad93SMark Brown 		adsp_err(dsp, "Failed to start DSP RAM\n");
21082159ad93SMark Brown 		return -EBUSY;
21092159ad93SMark Brown 	}
21102159ad93SMark Brown 
21112159ad93SMark Brown 	adsp_dbg(dsp, "RAM ready after %d polls\n", count);
21122159ad93SMark Brown 
21132159ad93SMark Brown 	return 0;
21142159ad93SMark Brown }
21152159ad93SMark Brown 
211618b1a902SCharles Keepax static void wm_adsp2_boot_work(struct work_struct *work)
21172159ad93SMark Brown {
2118d8a64d6aSCharles Keepax 	struct wm_adsp *dsp = container_of(work,
2119d8a64d6aSCharles Keepax 					   struct wm_adsp,
2120d8a64d6aSCharles Keepax 					   boot_work);
21212159ad93SMark Brown 	int ret;
2122d8a64d6aSCharles Keepax 	unsigned int val;
21232159ad93SMark Brown 
2124078e7183SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2125078e7183SCharles Keepax 
2126dd49e2c8SMark Brown 	/*
2127dd49e2c8SMark Brown 	 * For simplicity set the DSP clock rate to be the
2128dd49e2c8SMark Brown 	 * SYSCLK rate rather than making it configurable.
2129dd49e2c8SMark Brown 	 */
2130dd49e2c8SMark Brown 	ret = regmap_read(dsp->regmap, ARIZONA_SYSTEM_CLOCK_1, &val);
2131dd49e2c8SMark Brown 	if (ret != 0) {
2132d8a64d6aSCharles Keepax 		adsp_err(dsp, "Failed to read SYSCLK state: %d\n", ret);
2133078e7183SCharles Keepax 		goto err_mutex;
2134dd49e2c8SMark Brown 	}
2135dd49e2c8SMark Brown 	val = (val & ARIZONA_SYSCLK_FREQ_MASK)
2136dd49e2c8SMark Brown 		>> ARIZONA_SYSCLK_FREQ_SHIFT;
2137dd49e2c8SMark Brown 
21381552c325SMark Brown 	ret = regmap_update_bits_async(dsp->regmap,
2139dd49e2c8SMark Brown 				       dsp->base + ADSP2_CLOCKING,
2140dd49e2c8SMark Brown 				       ADSP2_CLK_SEL_MASK, val);
2141dd49e2c8SMark Brown 	if (ret != 0) {
2142d8a64d6aSCharles Keepax 		adsp_err(dsp, "Failed to set clock rate: %d\n", ret);
2143078e7183SCharles Keepax 		goto err_mutex;
2144dd49e2c8SMark Brown 	}
2145dd49e2c8SMark Brown 
21462159ad93SMark Brown 	ret = wm_adsp2_ena(dsp);
21472159ad93SMark Brown 	if (ret != 0)
2148078e7183SCharles Keepax 		goto err_mutex;
21492159ad93SMark Brown 
21502159ad93SMark Brown 	ret = wm_adsp_load(dsp);
21512159ad93SMark Brown 	if (ret != 0)
2152078e7183SCharles Keepax 		goto err_ena;
21532159ad93SMark Brown 
2154b618a185SCharles Keepax 	ret = wm_adsp2_setup_algs(dsp);
2155db40517cSMark Brown 	if (ret != 0)
2156078e7183SCharles Keepax 		goto err_ena;
2157db40517cSMark Brown 
21582159ad93SMark Brown 	ret = wm_adsp_load_coeff(dsp);
21592159ad93SMark Brown 	if (ret != 0)
2160078e7183SCharles Keepax 		goto err_ena;
21612159ad93SMark Brown 
21620c2e3f34SDimitris Papastamos 	/* Initialize caches for enabled and unset controls */
216381ad93ecSDimitris Papastamos 	ret = wm_coeff_init_control_caches(dsp);
21646ab2b7b4SDimitris Papastamos 	if (ret != 0)
2165078e7183SCharles Keepax 		goto err_ena;
21666ab2b7b4SDimitris Papastamos 
21670c2e3f34SDimitris Papastamos 	/* Sync set controls */
216881ad93ecSDimitris Papastamos 	ret = wm_coeff_sync_controls(dsp);
21696ab2b7b4SDimitris Papastamos 	if (ret != 0)
2170078e7183SCharles Keepax 		goto err_ena;
21716ab2b7b4SDimitris Papastamos 
21721023dbd9SMark Brown 	dsp->running = true;
2173d8a64d6aSCharles Keepax 
2174078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2175078e7183SCharles Keepax 
2176d8a64d6aSCharles Keepax 	return;
2177d8a64d6aSCharles Keepax 
2178078e7183SCharles Keepax err_ena:
2179d8a64d6aSCharles Keepax 	regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2180d8a64d6aSCharles Keepax 			   ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
2181078e7183SCharles Keepax err_mutex:
2182078e7183SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2183d8a64d6aSCharles Keepax }
2184d8a64d6aSCharles Keepax 
218512db5eddSCharles Keepax int wm_adsp2_early_event(struct snd_soc_dapm_widget *w,
218612db5eddSCharles Keepax 		   struct snd_kcontrol *kcontrol, int event)
218712db5eddSCharles Keepax {
218872718517SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
218912db5eddSCharles Keepax 	struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
219012db5eddSCharles Keepax 	struct wm_adsp *dsp = &dsps[w->shift];
219112db5eddSCharles Keepax 
219200200107SLars-Peter Clausen 	dsp->card = codec->component.card;
219312db5eddSCharles Keepax 
219412db5eddSCharles Keepax 	switch (event) {
219512db5eddSCharles Keepax 	case SND_SOC_DAPM_PRE_PMU:
219612db5eddSCharles Keepax 		queue_work(system_unbound_wq, &dsp->boot_work);
219712db5eddSCharles Keepax 		break;
219812db5eddSCharles Keepax 	default:
219912db5eddSCharles Keepax 		break;
2200cab27258SCharles Keepax 	}
220112db5eddSCharles Keepax 
220212db5eddSCharles Keepax 	return 0;
220312db5eddSCharles Keepax }
220412db5eddSCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp2_early_event);
220512db5eddSCharles Keepax 
2206d8a64d6aSCharles Keepax int wm_adsp2_event(struct snd_soc_dapm_widget *w,
2207d8a64d6aSCharles Keepax 		   struct snd_kcontrol *kcontrol, int event)
2208d8a64d6aSCharles Keepax {
220972718517SLars-Peter Clausen 	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2210d8a64d6aSCharles Keepax 	struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
2211d8a64d6aSCharles Keepax 	struct wm_adsp *dsp = &dsps[w->shift];
2212d8a64d6aSCharles Keepax 	struct wm_adsp_alg_region *alg_region;
2213d8a64d6aSCharles Keepax 	struct wm_coeff_ctl *ctl;
2214d8a64d6aSCharles Keepax 	int ret;
2215d8a64d6aSCharles Keepax 
2216d8a64d6aSCharles Keepax 	switch (event) {
2217d8a64d6aSCharles Keepax 	case SND_SOC_DAPM_POST_PMU:
2218d8a64d6aSCharles Keepax 		flush_work(&dsp->boot_work);
2219d8a64d6aSCharles Keepax 
2220d8a64d6aSCharles Keepax 		if (!dsp->running)
2221d8a64d6aSCharles Keepax 			return -EIO;
2222d8a64d6aSCharles Keepax 
2223d8a64d6aSCharles Keepax 		ret = regmap_update_bits(dsp->regmap,
2224d8a64d6aSCharles Keepax 					 dsp->base + ADSP2_CONTROL,
222500e4c3b6SCharles Keepax 					 ADSP2_CORE_ENA | ADSP2_START,
222600e4c3b6SCharles Keepax 					 ADSP2_CORE_ENA | ADSP2_START);
2227d8a64d6aSCharles Keepax 		if (ret != 0)
2228d8a64d6aSCharles Keepax 			goto err;
22292cd19bdbSCharles Keepax 
22302cd19bdbSCharles Keepax 		if (wm_adsp_fw[dsp->fw].num_caps != 0)
22312cd19bdbSCharles Keepax 			ret = wm_adsp_buffer_init(dsp);
22322cd19bdbSCharles Keepax 
22332159ad93SMark Brown 		break;
22342159ad93SMark Brown 
22352159ad93SMark Brown 	case SND_SOC_DAPM_PRE_PMD:
223610337b07SRichard Fitzgerald 		/* Log firmware state, it can be useful for analysis */
223710337b07SRichard Fitzgerald 		wm_adsp2_show_fw_status(dsp);
223810337b07SRichard Fitzgerald 
2239078e7183SCharles Keepax 		mutex_lock(&dsp->pwr_lock);
2240078e7183SCharles Keepax 
2241f9f55e31SRichard Fitzgerald 		wm_adsp_debugfs_clear(dsp);
2242f9f55e31SRichard Fitzgerald 
2243f9f55e31SRichard Fitzgerald 		dsp->fw_id = 0;
2244f9f55e31SRichard Fitzgerald 		dsp->fw_id_version = 0;
22451023dbd9SMark Brown 		dsp->running = false;
22461023dbd9SMark Brown 
22472159ad93SMark Brown 		regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2248a7f9be7eSMark Brown 				   ADSP2_SYS_ENA | ADSP2_CORE_ENA |
2249a7f9be7eSMark Brown 				   ADSP2_START, 0);
2250973838a0SMark Brown 
22512d30b575SMark Brown 		/* Make sure DMAs are quiesced */
22522d30b575SMark Brown 		regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
22532d30b575SMark Brown 		regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0);
22542d30b575SMark Brown 		regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
22552d30b575SMark Brown 
225681ad93ecSDimitris Papastamos 		list_for_each_entry(ctl, &dsp->ctl_list, list)
22576ab2b7b4SDimitris Papastamos 			ctl->enabled = 0;
22586ab2b7b4SDimitris Papastamos 
2259471f4885SMark Brown 		while (!list_empty(&dsp->alg_regions)) {
2260471f4885SMark Brown 			alg_region = list_first_entry(&dsp->alg_regions,
2261471f4885SMark Brown 						      struct wm_adsp_alg_region,
2262471f4885SMark Brown 						      list);
2263471f4885SMark Brown 			list_del(&alg_region->list);
2264471f4885SMark Brown 			kfree(alg_region);
2265471f4885SMark Brown 		}
2266ddbc5efeSCharles Keepax 
22672cd19bdbSCharles Keepax 		if (wm_adsp_fw[dsp->fw].num_caps != 0)
22682cd19bdbSCharles Keepax 			wm_adsp_buffer_free(dsp);
22692cd19bdbSCharles Keepax 
2270078e7183SCharles Keepax 		mutex_unlock(&dsp->pwr_lock);
2271078e7183SCharles Keepax 
2272ddbc5efeSCharles Keepax 		adsp_dbg(dsp, "Shutdown complete\n");
22732159ad93SMark Brown 		break;
22742159ad93SMark Brown 
22752159ad93SMark Brown 	default:
22762159ad93SMark Brown 		break;
22772159ad93SMark Brown 	}
22782159ad93SMark Brown 
22792159ad93SMark Brown 	return 0;
22802159ad93SMark Brown err:
22812159ad93SMark Brown 	regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2282a7f9be7eSMark Brown 			   ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
22832159ad93SMark Brown 	return ret;
22842159ad93SMark Brown }
22852159ad93SMark Brown EXPORT_SYMBOL_GPL(wm_adsp2_event);
2286973838a0SMark Brown 
2287f5e2ce92SRichard Fitzgerald int wm_adsp2_codec_probe(struct wm_adsp *dsp, struct snd_soc_codec *codec)
2288f5e2ce92SRichard Fitzgerald {
2289f9f55e31SRichard Fitzgerald 	wm_adsp2_init_debugfs(dsp, codec);
2290f9f55e31SRichard Fitzgerald 
2291218e5087SRichard Fitzgerald 	return snd_soc_add_codec_controls(codec,
2292336d0442SRichard Fitzgerald 					  &wm_adsp_fw_controls[dsp->num - 1],
2293336d0442SRichard Fitzgerald 					  1);
2294f5e2ce92SRichard Fitzgerald }
2295f5e2ce92SRichard Fitzgerald EXPORT_SYMBOL_GPL(wm_adsp2_codec_probe);
2296f5e2ce92SRichard Fitzgerald 
2297f5e2ce92SRichard Fitzgerald int wm_adsp2_codec_remove(struct wm_adsp *dsp, struct snd_soc_codec *codec)
2298f5e2ce92SRichard Fitzgerald {
2299f9f55e31SRichard Fitzgerald 	wm_adsp2_cleanup_debugfs(dsp);
2300f9f55e31SRichard Fitzgerald 
2301f5e2ce92SRichard Fitzgerald 	return 0;
2302f5e2ce92SRichard Fitzgerald }
2303f5e2ce92SRichard Fitzgerald EXPORT_SYMBOL_GPL(wm_adsp2_codec_remove);
2304f5e2ce92SRichard Fitzgerald 
230581ac58b1SRichard Fitzgerald int wm_adsp2_init(struct wm_adsp *dsp)
2306973838a0SMark Brown {
2307973838a0SMark Brown 	int ret;
2308973838a0SMark Brown 
230910a2b662SMark Brown 	/*
231010a2b662SMark Brown 	 * Disable the DSP memory by default when in reset for a small
231110a2b662SMark Brown 	 * power saving.
231210a2b662SMark Brown 	 */
23133809f001SCharles Keepax 	ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
231410a2b662SMark Brown 				 ADSP2_MEM_ENA, 0);
231510a2b662SMark Brown 	if (ret != 0) {
23163809f001SCharles Keepax 		adsp_err(dsp, "Failed to clear memory retention: %d\n", ret);
231710a2b662SMark Brown 		return ret;
231810a2b662SMark Brown 	}
231910a2b662SMark Brown 
23203809f001SCharles Keepax 	INIT_LIST_HEAD(&dsp->alg_regions);
23213809f001SCharles Keepax 	INIT_LIST_HEAD(&dsp->ctl_list);
23223809f001SCharles Keepax 	INIT_WORK(&dsp->boot_work, wm_adsp2_boot_work);
23236ab2b7b4SDimitris Papastamos 
2324078e7183SCharles Keepax 	mutex_init(&dsp->pwr_lock);
2325078e7183SCharles Keepax 
2326973838a0SMark Brown 	return 0;
2327973838a0SMark Brown }
2328973838a0SMark Brown EXPORT_SYMBOL_GPL(wm_adsp2_init);
23290a37c6efSPraveen Diwakar 
2330406abc95SCharles Keepax int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream)
2331406abc95SCharles Keepax {
2332406abc95SCharles Keepax 	struct wm_adsp_compr *compr;
2333406abc95SCharles Keepax 	int ret = 0;
2334406abc95SCharles Keepax 
2335406abc95SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2336406abc95SCharles Keepax 
2337406abc95SCharles Keepax 	if (wm_adsp_fw[dsp->fw].num_caps == 0) {
2338406abc95SCharles Keepax 		adsp_err(dsp, "Firmware does not support compressed API\n");
2339406abc95SCharles Keepax 		ret = -ENXIO;
2340406abc95SCharles Keepax 		goto out;
2341406abc95SCharles Keepax 	}
2342406abc95SCharles Keepax 
2343406abc95SCharles Keepax 	if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) {
2344406abc95SCharles Keepax 		adsp_err(dsp, "Firmware does not support stream direction\n");
2345406abc95SCharles Keepax 		ret = -EINVAL;
2346406abc95SCharles Keepax 		goto out;
2347406abc95SCharles Keepax 	}
2348406abc95SCharles Keepax 
234995fe9597SCharles Keepax 	if (dsp->compr) {
235095fe9597SCharles Keepax 		/* It is expect this limitation will be removed in future */
235195fe9597SCharles Keepax 		adsp_err(dsp, "Only a single stream supported per DSP\n");
235295fe9597SCharles Keepax 		ret = -EBUSY;
235395fe9597SCharles Keepax 		goto out;
235495fe9597SCharles Keepax 	}
235595fe9597SCharles Keepax 
2356406abc95SCharles Keepax 	compr = kzalloc(sizeof(*compr), GFP_KERNEL);
2357406abc95SCharles Keepax 	if (!compr) {
2358406abc95SCharles Keepax 		ret = -ENOMEM;
2359406abc95SCharles Keepax 		goto out;
2360406abc95SCharles Keepax 	}
2361406abc95SCharles Keepax 
2362406abc95SCharles Keepax 	compr->dsp = dsp;
2363406abc95SCharles Keepax 	compr->stream = stream;
2364406abc95SCharles Keepax 
2365406abc95SCharles Keepax 	dsp->compr = compr;
2366406abc95SCharles Keepax 
2367406abc95SCharles Keepax 	stream->runtime->private_data = compr;
2368406abc95SCharles Keepax 
2369406abc95SCharles Keepax out:
2370406abc95SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2371406abc95SCharles Keepax 
2372406abc95SCharles Keepax 	return ret;
2373406abc95SCharles Keepax }
2374406abc95SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_open);
2375406abc95SCharles Keepax 
2376406abc95SCharles Keepax int wm_adsp_compr_free(struct snd_compr_stream *stream)
2377406abc95SCharles Keepax {
2378406abc95SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
2379406abc95SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
2380406abc95SCharles Keepax 
2381406abc95SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2382406abc95SCharles Keepax 
2383406abc95SCharles Keepax 	dsp->compr = NULL;
2384406abc95SCharles Keepax 
238583a40ce9SCharles Keepax 	kfree(compr->raw_buf);
2386406abc95SCharles Keepax 	kfree(compr);
2387406abc95SCharles Keepax 
2388406abc95SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2389406abc95SCharles Keepax 
2390406abc95SCharles Keepax 	return 0;
2391406abc95SCharles Keepax }
2392406abc95SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_free);
2393406abc95SCharles Keepax 
2394406abc95SCharles Keepax static int wm_adsp_compr_check_params(struct snd_compr_stream *stream,
2395406abc95SCharles Keepax 				      struct snd_compr_params *params)
2396406abc95SCharles Keepax {
2397406abc95SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
2398406abc95SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
2399406abc95SCharles Keepax 	const struct wm_adsp_fw_caps *caps;
2400406abc95SCharles Keepax 	const struct snd_codec_desc *desc;
2401406abc95SCharles Keepax 	int i, j;
2402406abc95SCharles Keepax 
2403406abc95SCharles Keepax 	if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE ||
2404406abc95SCharles Keepax 	    params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE ||
2405406abc95SCharles Keepax 	    params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS ||
2406406abc95SCharles Keepax 	    params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS ||
2407406abc95SCharles Keepax 	    params->buffer.fragment_size % WM_ADSP_DATA_WORD_SIZE) {
2408406abc95SCharles Keepax 		adsp_err(dsp, "Invalid buffer fragsize=%d fragments=%d\n",
2409406abc95SCharles Keepax 			 params->buffer.fragment_size,
2410406abc95SCharles Keepax 			 params->buffer.fragments);
2411406abc95SCharles Keepax 
2412406abc95SCharles Keepax 		return -EINVAL;
2413406abc95SCharles Keepax 	}
2414406abc95SCharles Keepax 
2415406abc95SCharles Keepax 	for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) {
2416406abc95SCharles Keepax 		caps = &wm_adsp_fw[dsp->fw].caps[i];
2417406abc95SCharles Keepax 		desc = &caps->desc;
2418406abc95SCharles Keepax 
2419406abc95SCharles Keepax 		if (caps->id != params->codec.id)
2420406abc95SCharles Keepax 			continue;
2421406abc95SCharles Keepax 
2422406abc95SCharles Keepax 		if (stream->direction == SND_COMPRESS_PLAYBACK) {
2423406abc95SCharles Keepax 			if (desc->max_ch < params->codec.ch_out)
2424406abc95SCharles Keepax 				continue;
2425406abc95SCharles Keepax 		} else {
2426406abc95SCharles Keepax 			if (desc->max_ch < params->codec.ch_in)
2427406abc95SCharles Keepax 				continue;
2428406abc95SCharles Keepax 		}
2429406abc95SCharles Keepax 
2430406abc95SCharles Keepax 		if (!(desc->formats & (1 << params->codec.format)))
2431406abc95SCharles Keepax 			continue;
2432406abc95SCharles Keepax 
2433406abc95SCharles Keepax 		for (j = 0; j < desc->num_sample_rates; ++j)
2434406abc95SCharles Keepax 			if (desc->sample_rates[j] == params->codec.sample_rate)
2435406abc95SCharles Keepax 				return 0;
2436406abc95SCharles Keepax 	}
2437406abc95SCharles Keepax 
2438406abc95SCharles Keepax 	adsp_err(dsp, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n",
2439406abc95SCharles Keepax 		 params->codec.id, params->codec.ch_in, params->codec.ch_out,
2440406abc95SCharles Keepax 		 params->codec.sample_rate, params->codec.format);
2441406abc95SCharles Keepax 	return -EINVAL;
2442406abc95SCharles Keepax }
2443406abc95SCharles Keepax 
2444565ace46SCharles Keepax static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr)
2445565ace46SCharles Keepax {
2446565ace46SCharles Keepax 	return compr->size.fragment_size / WM_ADSP_DATA_WORD_SIZE;
2447565ace46SCharles Keepax }
2448565ace46SCharles Keepax 
2449406abc95SCharles Keepax int wm_adsp_compr_set_params(struct snd_compr_stream *stream,
2450406abc95SCharles Keepax 			     struct snd_compr_params *params)
2451406abc95SCharles Keepax {
2452406abc95SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
245383a40ce9SCharles Keepax 	unsigned int size;
2454406abc95SCharles Keepax 	int ret;
2455406abc95SCharles Keepax 
2456406abc95SCharles Keepax 	ret = wm_adsp_compr_check_params(stream, params);
2457406abc95SCharles Keepax 	if (ret)
2458406abc95SCharles Keepax 		return ret;
2459406abc95SCharles Keepax 
2460406abc95SCharles Keepax 	compr->size = params->buffer;
2461406abc95SCharles Keepax 
2462406abc95SCharles Keepax 	adsp_dbg(compr->dsp, "fragment_size=%d fragments=%d\n",
2463406abc95SCharles Keepax 		 compr->size.fragment_size, compr->size.fragments);
2464406abc95SCharles Keepax 
246583a40ce9SCharles Keepax 	size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf);
246683a40ce9SCharles Keepax 	compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL);
246783a40ce9SCharles Keepax 	if (!compr->raw_buf)
246883a40ce9SCharles Keepax 		return -ENOMEM;
246983a40ce9SCharles Keepax 
2470da2b3358SCharles Keepax 	compr->sample_rate = params->codec.sample_rate;
2471da2b3358SCharles Keepax 
2472406abc95SCharles Keepax 	return 0;
2473406abc95SCharles Keepax }
2474406abc95SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params);
2475406abc95SCharles Keepax 
2476406abc95SCharles Keepax int wm_adsp_compr_get_caps(struct snd_compr_stream *stream,
2477406abc95SCharles Keepax 			   struct snd_compr_caps *caps)
2478406abc95SCharles Keepax {
2479406abc95SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
2480406abc95SCharles Keepax 	int fw = compr->dsp->fw;
2481406abc95SCharles Keepax 	int i;
2482406abc95SCharles Keepax 
2483406abc95SCharles Keepax 	if (wm_adsp_fw[fw].caps) {
2484406abc95SCharles Keepax 		for (i = 0; i < wm_adsp_fw[fw].num_caps; i++)
2485406abc95SCharles Keepax 			caps->codecs[i] = wm_adsp_fw[fw].caps[i].id;
2486406abc95SCharles Keepax 
2487406abc95SCharles Keepax 		caps->num_codecs = i;
2488406abc95SCharles Keepax 		caps->direction = wm_adsp_fw[fw].compr_direction;
2489406abc95SCharles Keepax 
2490406abc95SCharles Keepax 		caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE;
2491406abc95SCharles Keepax 		caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE;
2492406abc95SCharles Keepax 		caps->min_fragments = WM_ADSP_MIN_FRAGMENTS;
2493406abc95SCharles Keepax 		caps->max_fragments = WM_ADSP_MAX_FRAGMENTS;
2494406abc95SCharles Keepax 	}
2495406abc95SCharles Keepax 
2496406abc95SCharles Keepax 	return 0;
2497406abc95SCharles Keepax }
2498406abc95SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps);
2499406abc95SCharles Keepax 
25002cd19bdbSCharles Keepax static int wm_adsp_read_data_block(struct wm_adsp *dsp, int mem_type,
25012cd19bdbSCharles Keepax 				   unsigned int mem_addr,
25022cd19bdbSCharles Keepax 				   unsigned int num_words, u32 *data)
25032cd19bdbSCharles Keepax {
25042cd19bdbSCharles Keepax 	struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
25052cd19bdbSCharles Keepax 	unsigned int i, reg;
25062cd19bdbSCharles Keepax 	int ret;
25072cd19bdbSCharles Keepax 
25082cd19bdbSCharles Keepax 	if (!mem)
25092cd19bdbSCharles Keepax 		return -EINVAL;
25102cd19bdbSCharles Keepax 
25112cd19bdbSCharles Keepax 	reg = wm_adsp_region_to_reg(mem, mem_addr);
25122cd19bdbSCharles Keepax 
25132cd19bdbSCharles Keepax 	ret = regmap_raw_read(dsp->regmap, reg, data,
25142cd19bdbSCharles Keepax 			      sizeof(*data) * num_words);
25152cd19bdbSCharles Keepax 	if (ret < 0)
25162cd19bdbSCharles Keepax 		return ret;
25172cd19bdbSCharles Keepax 
25182cd19bdbSCharles Keepax 	for (i = 0; i < num_words; ++i)
25192cd19bdbSCharles Keepax 		data[i] = be32_to_cpu(data[i]) & 0x00ffffffu;
25202cd19bdbSCharles Keepax 
25212cd19bdbSCharles Keepax 	return 0;
25222cd19bdbSCharles Keepax }
25232cd19bdbSCharles Keepax 
25242cd19bdbSCharles Keepax static inline int wm_adsp_read_data_word(struct wm_adsp *dsp, int mem_type,
25252cd19bdbSCharles Keepax 					 unsigned int mem_addr, u32 *data)
25262cd19bdbSCharles Keepax {
25272cd19bdbSCharles Keepax 	return wm_adsp_read_data_block(dsp, mem_type, mem_addr, 1, data);
25282cd19bdbSCharles Keepax }
25292cd19bdbSCharles Keepax 
25302cd19bdbSCharles Keepax static int wm_adsp_write_data_word(struct wm_adsp *dsp, int mem_type,
25312cd19bdbSCharles Keepax 				   unsigned int mem_addr, u32 data)
25322cd19bdbSCharles Keepax {
25332cd19bdbSCharles Keepax 	struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type);
25342cd19bdbSCharles Keepax 	unsigned int reg;
25352cd19bdbSCharles Keepax 
25362cd19bdbSCharles Keepax 	if (!mem)
25372cd19bdbSCharles Keepax 		return -EINVAL;
25382cd19bdbSCharles Keepax 
25392cd19bdbSCharles Keepax 	reg = wm_adsp_region_to_reg(mem, mem_addr);
25402cd19bdbSCharles Keepax 
25412cd19bdbSCharles Keepax 	data = cpu_to_be32(data & 0x00ffffffu);
25422cd19bdbSCharles Keepax 
25432cd19bdbSCharles Keepax 	return regmap_raw_write(dsp->regmap, reg, &data, sizeof(data));
25442cd19bdbSCharles Keepax }
25452cd19bdbSCharles Keepax 
25462cd19bdbSCharles Keepax static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf,
25472cd19bdbSCharles Keepax 				      unsigned int field_offset, u32 *data)
25482cd19bdbSCharles Keepax {
25492cd19bdbSCharles Keepax 	return wm_adsp_read_data_word(buf->dsp, WMFW_ADSP2_XM,
25502cd19bdbSCharles Keepax 				      buf->host_buf_ptr + field_offset, data);
25512cd19bdbSCharles Keepax }
25522cd19bdbSCharles Keepax 
25532cd19bdbSCharles Keepax static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf,
25542cd19bdbSCharles Keepax 				       unsigned int field_offset, u32 data)
25552cd19bdbSCharles Keepax {
25562cd19bdbSCharles Keepax 	return wm_adsp_write_data_word(buf->dsp, WMFW_ADSP2_XM,
25572cd19bdbSCharles Keepax 				       buf->host_buf_ptr + field_offset, data);
25582cd19bdbSCharles Keepax }
25592cd19bdbSCharles Keepax 
25602cd19bdbSCharles Keepax static int wm_adsp_buffer_locate(struct wm_adsp_compr_buf *buf)
25612cd19bdbSCharles Keepax {
25622cd19bdbSCharles Keepax 	struct wm_adsp_alg_region *alg_region;
25632cd19bdbSCharles Keepax 	struct wm_adsp *dsp = buf->dsp;
25642cd19bdbSCharles Keepax 	u32 xmalg, addr, magic;
25652cd19bdbSCharles Keepax 	int i, ret;
25662cd19bdbSCharles Keepax 
25672cd19bdbSCharles Keepax 	alg_region = wm_adsp_find_alg_region(dsp, WMFW_ADSP2_XM, dsp->fw_id);
25682cd19bdbSCharles Keepax 	xmalg = sizeof(struct wm_adsp_system_config_xm_hdr) / sizeof(__be32);
25692cd19bdbSCharles Keepax 
25702cd19bdbSCharles Keepax 	addr = alg_region->base + xmalg + ALG_XM_FIELD(magic);
25712cd19bdbSCharles Keepax 	ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr, &magic);
25722cd19bdbSCharles Keepax 	if (ret < 0)
25732cd19bdbSCharles Keepax 		return ret;
25742cd19bdbSCharles Keepax 
25752cd19bdbSCharles Keepax 	if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC)
25762cd19bdbSCharles Keepax 		return -EINVAL;
25772cd19bdbSCharles Keepax 
25782cd19bdbSCharles Keepax 	addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr);
25792cd19bdbSCharles Keepax 	for (i = 0; i < 5; ++i) {
25802cd19bdbSCharles Keepax 		ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr,
25812cd19bdbSCharles Keepax 					     &buf->host_buf_ptr);
25822cd19bdbSCharles Keepax 		if (ret < 0)
25832cd19bdbSCharles Keepax 			return ret;
25842cd19bdbSCharles Keepax 
25852cd19bdbSCharles Keepax 		if (buf->host_buf_ptr)
25862cd19bdbSCharles Keepax 			break;
25872cd19bdbSCharles Keepax 
25882cd19bdbSCharles Keepax 		usleep_range(1000, 2000);
25892cd19bdbSCharles Keepax 	}
25902cd19bdbSCharles Keepax 
25912cd19bdbSCharles Keepax 	if (!buf->host_buf_ptr)
25922cd19bdbSCharles Keepax 		return -EIO;
25932cd19bdbSCharles Keepax 
25942cd19bdbSCharles Keepax 	adsp_dbg(dsp, "host_buf_ptr=%x\n", buf->host_buf_ptr);
25952cd19bdbSCharles Keepax 
25962cd19bdbSCharles Keepax 	return 0;
25972cd19bdbSCharles Keepax }
25982cd19bdbSCharles Keepax 
25992cd19bdbSCharles Keepax static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf)
26002cd19bdbSCharles Keepax {
26012cd19bdbSCharles Keepax 	const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps;
26022cd19bdbSCharles Keepax 	struct wm_adsp_buffer_region *region;
26032cd19bdbSCharles Keepax 	u32 offset = 0;
26042cd19bdbSCharles Keepax 	int i, ret;
26052cd19bdbSCharles Keepax 
26062cd19bdbSCharles Keepax 	for (i = 0; i < caps->num_regions; ++i) {
26072cd19bdbSCharles Keepax 		region = &buf->regions[i];
26082cd19bdbSCharles Keepax 
26092cd19bdbSCharles Keepax 		region->offset = offset;
26102cd19bdbSCharles Keepax 		region->mem_type = caps->region_defs[i].mem_type;
26112cd19bdbSCharles Keepax 
26122cd19bdbSCharles Keepax 		ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset,
26132cd19bdbSCharles Keepax 					  &region->base_addr);
26142cd19bdbSCharles Keepax 		if (ret < 0)
26152cd19bdbSCharles Keepax 			return ret;
26162cd19bdbSCharles Keepax 
26172cd19bdbSCharles Keepax 		ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset,
26182cd19bdbSCharles Keepax 					  &offset);
26192cd19bdbSCharles Keepax 		if (ret < 0)
26202cd19bdbSCharles Keepax 			return ret;
26212cd19bdbSCharles Keepax 
26222cd19bdbSCharles Keepax 		region->cumulative_size = offset;
26232cd19bdbSCharles Keepax 
26242cd19bdbSCharles Keepax 		adsp_dbg(buf->dsp,
26252cd19bdbSCharles Keepax 			 "region=%d type=%d base=%04x off=%04x size=%04x\n",
26262cd19bdbSCharles Keepax 			 i, region->mem_type, region->base_addr,
26272cd19bdbSCharles Keepax 			 region->offset, region->cumulative_size);
26282cd19bdbSCharles Keepax 	}
26292cd19bdbSCharles Keepax 
26302cd19bdbSCharles Keepax 	return 0;
26312cd19bdbSCharles Keepax }
26322cd19bdbSCharles Keepax 
26332cd19bdbSCharles Keepax static int wm_adsp_buffer_init(struct wm_adsp *dsp)
26342cd19bdbSCharles Keepax {
26352cd19bdbSCharles Keepax 	struct wm_adsp_compr_buf *buf;
26362cd19bdbSCharles Keepax 	int ret;
26372cd19bdbSCharles Keepax 
26382cd19bdbSCharles Keepax 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
26392cd19bdbSCharles Keepax 	if (!buf)
26402cd19bdbSCharles Keepax 		return -ENOMEM;
26412cd19bdbSCharles Keepax 
26422cd19bdbSCharles Keepax 	buf->dsp = dsp;
2643565ace46SCharles Keepax 	buf->read_index = -1;
2644565ace46SCharles Keepax 	buf->irq_count = 0xFFFFFFFF;
26452cd19bdbSCharles Keepax 
26462cd19bdbSCharles Keepax 	ret = wm_adsp_buffer_locate(buf);
26472cd19bdbSCharles Keepax 	if (ret < 0) {
26482cd19bdbSCharles Keepax 		adsp_err(dsp, "Failed to acquire host buffer: %d\n", ret);
26492cd19bdbSCharles Keepax 		goto err_buffer;
26502cd19bdbSCharles Keepax 	}
26512cd19bdbSCharles Keepax 
26522cd19bdbSCharles Keepax 	buf->regions = kcalloc(wm_adsp_fw[dsp->fw].caps->num_regions,
26532cd19bdbSCharles Keepax 			       sizeof(*buf->regions), GFP_KERNEL);
26542cd19bdbSCharles Keepax 	if (!buf->regions) {
26552cd19bdbSCharles Keepax 		ret = -ENOMEM;
26562cd19bdbSCharles Keepax 		goto err_buffer;
26572cd19bdbSCharles Keepax 	}
26582cd19bdbSCharles Keepax 
26592cd19bdbSCharles Keepax 	ret = wm_adsp_buffer_populate(buf);
26602cd19bdbSCharles Keepax 	if (ret < 0) {
26612cd19bdbSCharles Keepax 		adsp_err(dsp, "Failed to populate host buffer: %d\n", ret);
26622cd19bdbSCharles Keepax 		goto err_regions;
26632cd19bdbSCharles Keepax 	}
26642cd19bdbSCharles Keepax 
26652cd19bdbSCharles Keepax 	dsp->buffer = buf;
26662cd19bdbSCharles Keepax 
26672cd19bdbSCharles Keepax 	return 0;
26682cd19bdbSCharles Keepax 
26692cd19bdbSCharles Keepax err_regions:
26702cd19bdbSCharles Keepax 	kfree(buf->regions);
26712cd19bdbSCharles Keepax err_buffer:
26722cd19bdbSCharles Keepax 	kfree(buf);
26732cd19bdbSCharles Keepax 	return ret;
26742cd19bdbSCharles Keepax }
26752cd19bdbSCharles Keepax 
26762cd19bdbSCharles Keepax static int wm_adsp_buffer_free(struct wm_adsp *dsp)
26772cd19bdbSCharles Keepax {
26782cd19bdbSCharles Keepax 	if (dsp->buffer) {
26792cd19bdbSCharles Keepax 		kfree(dsp->buffer->regions);
26802cd19bdbSCharles Keepax 		kfree(dsp->buffer);
26812cd19bdbSCharles Keepax 
26822cd19bdbSCharles Keepax 		dsp->buffer = NULL;
26832cd19bdbSCharles Keepax 	}
26842cd19bdbSCharles Keepax 
26852cd19bdbSCharles Keepax 	return 0;
26862cd19bdbSCharles Keepax }
26872cd19bdbSCharles Keepax 
268895fe9597SCharles Keepax static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr)
268995fe9597SCharles Keepax {
269095fe9597SCharles Keepax 	return compr->buf != NULL;
269195fe9597SCharles Keepax }
269295fe9597SCharles Keepax 
269395fe9597SCharles Keepax static int wm_adsp_compr_attach(struct wm_adsp_compr *compr)
269495fe9597SCharles Keepax {
269595fe9597SCharles Keepax 	/*
269695fe9597SCharles Keepax 	 * Note this will be more complex once each DSP can support multiple
269795fe9597SCharles Keepax 	 * streams
269895fe9597SCharles Keepax 	 */
269995fe9597SCharles Keepax 	if (!compr->dsp->buffer)
270095fe9597SCharles Keepax 		return -EINVAL;
270195fe9597SCharles Keepax 
270295fe9597SCharles Keepax 	compr->buf = compr->dsp->buffer;
270395fe9597SCharles Keepax 
270495fe9597SCharles Keepax 	return 0;
270595fe9597SCharles Keepax }
270695fe9597SCharles Keepax 
270795fe9597SCharles Keepax int wm_adsp_compr_trigger(struct snd_compr_stream *stream, int cmd)
270895fe9597SCharles Keepax {
270995fe9597SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
271095fe9597SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
271195fe9597SCharles Keepax 	int ret = 0;
271295fe9597SCharles Keepax 
271395fe9597SCharles Keepax 	adsp_dbg(dsp, "Trigger: %d\n", cmd);
271495fe9597SCharles Keepax 
271595fe9597SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
271695fe9597SCharles Keepax 
271795fe9597SCharles Keepax 	switch (cmd) {
271895fe9597SCharles Keepax 	case SNDRV_PCM_TRIGGER_START:
271995fe9597SCharles Keepax 		if (wm_adsp_compr_attached(compr))
272095fe9597SCharles Keepax 			break;
272195fe9597SCharles Keepax 
272295fe9597SCharles Keepax 		ret = wm_adsp_compr_attach(compr);
272395fe9597SCharles Keepax 		if (ret < 0) {
272495fe9597SCharles Keepax 			adsp_err(dsp, "Failed to link buffer and stream: %d\n",
272595fe9597SCharles Keepax 				 ret);
272695fe9597SCharles Keepax 			break;
272795fe9597SCharles Keepax 		}
2728565ace46SCharles Keepax 
2729565ace46SCharles Keepax 		/* Trigger the IRQ at one fragment of data */
2730565ace46SCharles Keepax 		ret = wm_adsp_buffer_write(compr->buf,
2731565ace46SCharles Keepax 					   HOST_BUFFER_FIELD(high_water_mark),
2732565ace46SCharles Keepax 					   wm_adsp_compr_frag_words(compr));
2733565ace46SCharles Keepax 		if (ret < 0) {
2734565ace46SCharles Keepax 			adsp_err(dsp, "Failed to set high water mark: %d\n",
2735565ace46SCharles Keepax 				 ret);
2736565ace46SCharles Keepax 			break;
2737565ace46SCharles Keepax 		}
273895fe9597SCharles Keepax 		break;
273995fe9597SCharles Keepax 	case SNDRV_PCM_TRIGGER_STOP:
274095fe9597SCharles Keepax 		break;
274195fe9597SCharles Keepax 	default:
274295fe9597SCharles Keepax 		ret = -EINVAL;
274395fe9597SCharles Keepax 		break;
274495fe9597SCharles Keepax 	}
274595fe9597SCharles Keepax 
274695fe9597SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
274795fe9597SCharles Keepax 
274895fe9597SCharles Keepax 	return ret;
274995fe9597SCharles Keepax }
275095fe9597SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger);
275195fe9597SCharles Keepax 
2752565ace46SCharles Keepax static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf)
2753565ace46SCharles Keepax {
2754565ace46SCharles Keepax 	int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1;
2755565ace46SCharles Keepax 
2756565ace46SCharles Keepax 	return buf->regions[last_region].cumulative_size;
2757565ace46SCharles Keepax }
2758565ace46SCharles Keepax 
2759565ace46SCharles Keepax static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf)
2760565ace46SCharles Keepax {
2761565ace46SCharles Keepax 	u32 next_read_index, next_write_index;
2762565ace46SCharles Keepax 	int write_index, read_index, avail;
2763565ace46SCharles Keepax 	int ret;
2764565ace46SCharles Keepax 
2765565ace46SCharles Keepax 	/* Only sync read index if we haven't already read a valid index */
2766565ace46SCharles Keepax 	if (buf->read_index < 0) {
2767565ace46SCharles Keepax 		ret = wm_adsp_buffer_read(buf,
2768565ace46SCharles Keepax 				HOST_BUFFER_FIELD(next_read_index),
2769565ace46SCharles Keepax 				&next_read_index);
2770565ace46SCharles Keepax 		if (ret < 0)
2771565ace46SCharles Keepax 			return ret;
2772565ace46SCharles Keepax 
2773565ace46SCharles Keepax 		read_index = sign_extend32(next_read_index, 23);
2774565ace46SCharles Keepax 
2775565ace46SCharles Keepax 		if (read_index < 0) {
2776565ace46SCharles Keepax 			adsp_dbg(buf->dsp, "Avail check on unstarted stream\n");
2777565ace46SCharles Keepax 			return 0;
2778565ace46SCharles Keepax 		}
2779565ace46SCharles Keepax 
2780565ace46SCharles Keepax 		buf->read_index = read_index;
2781565ace46SCharles Keepax 	}
2782565ace46SCharles Keepax 
2783565ace46SCharles Keepax 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index),
2784565ace46SCharles Keepax 			&next_write_index);
2785565ace46SCharles Keepax 	if (ret < 0)
2786565ace46SCharles Keepax 		return ret;
2787565ace46SCharles Keepax 
2788565ace46SCharles Keepax 	write_index = sign_extend32(next_write_index, 23);
2789565ace46SCharles Keepax 
2790565ace46SCharles Keepax 	avail = write_index - buf->read_index;
2791565ace46SCharles Keepax 	if (avail < 0)
2792565ace46SCharles Keepax 		avail += wm_adsp_buffer_size(buf);
2793565ace46SCharles Keepax 
2794565ace46SCharles Keepax 	adsp_dbg(buf->dsp, "readindex=0x%x, writeindex=0x%x, avail=%d\n",
2795565ace46SCharles Keepax 		 buf->read_index, write_index, avail);
2796565ace46SCharles Keepax 
2797565ace46SCharles Keepax 	buf->avail = avail;
2798565ace46SCharles Keepax 
2799565ace46SCharles Keepax 	return 0;
2800565ace46SCharles Keepax }
2801565ace46SCharles Keepax 
2802565ace46SCharles Keepax int wm_adsp_compr_handle_irq(struct wm_adsp *dsp)
2803565ace46SCharles Keepax {
2804565ace46SCharles Keepax 	struct wm_adsp_compr_buf *buf = dsp->buffer;
280583a40ce9SCharles Keepax 	struct wm_adsp_compr *compr = dsp->compr;
2806565ace46SCharles Keepax 	int ret = 0;
2807565ace46SCharles Keepax 
2808565ace46SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2809565ace46SCharles Keepax 
2810565ace46SCharles Keepax 	if (!buf) {
2811565ace46SCharles Keepax 		adsp_err(dsp, "Spurious buffer IRQ\n");
2812565ace46SCharles Keepax 		ret = -ENODEV;
2813565ace46SCharles Keepax 		goto out;
2814565ace46SCharles Keepax 	}
2815565ace46SCharles Keepax 
2816565ace46SCharles Keepax 	adsp_dbg(dsp, "Handling buffer IRQ\n");
2817565ace46SCharles Keepax 
2818565ace46SCharles Keepax 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error);
2819565ace46SCharles Keepax 	if (ret < 0) {
2820565ace46SCharles Keepax 		adsp_err(dsp, "Failed to check buffer error: %d\n", ret);
2821565ace46SCharles Keepax 		goto out;
2822565ace46SCharles Keepax 	}
2823565ace46SCharles Keepax 	if (buf->error != 0) {
2824565ace46SCharles Keepax 		adsp_err(dsp, "Buffer error occurred: %d\n", buf->error);
2825565ace46SCharles Keepax 		ret = -EIO;
2826565ace46SCharles Keepax 		goto out;
2827565ace46SCharles Keepax 	}
2828565ace46SCharles Keepax 
2829565ace46SCharles Keepax 	ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count),
2830565ace46SCharles Keepax 				  &buf->irq_count);
2831565ace46SCharles Keepax 	if (ret < 0) {
2832565ace46SCharles Keepax 		adsp_err(dsp, "Failed to get irq_count: %d\n", ret);
2833565ace46SCharles Keepax 		goto out;
2834565ace46SCharles Keepax 	}
2835565ace46SCharles Keepax 
2836565ace46SCharles Keepax 	ret = wm_adsp_buffer_update_avail(buf);
2837565ace46SCharles Keepax 	if (ret < 0) {
2838565ace46SCharles Keepax 		adsp_err(dsp, "Error reading avail: %d\n", ret);
2839565ace46SCharles Keepax 		goto out;
2840565ace46SCharles Keepax 	}
2841565ace46SCharles Keepax 
2842c7dae7c4SCharles Keepax 	if (compr && compr->stream)
284383a40ce9SCharles Keepax 		snd_compr_fragment_elapsed(compr->stream);
284483a40ce9SCharles Keepax 
2845565ace46SCharles Keepax out:
2846565ace46SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2847565ace46SCharles Keepax 
2848565ace46SCharles Keepax 	return ret;
2849565ace46SCharles Keepax }
2850565ace46SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq);
2851565ace46SCharles Keepax 
2852565ace46SCharles Keepax static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf)
2853565ace46SCharles Keepax {
2854565ace46SCharles Keepax 	if (buf->irq_count & 0x01)
2855565ace46SCharles Keepax 		return 0;
2856565ace46SCharles Keepax 
2857565ace46SCharles Keepax 	adsp_dbg(buf->dsp, "Enable IRQ(0x%x) for next fragment\n",
2858565ace46SCharles Keepax 		 buf->irq_count);
2859565ace46SCharles Keepax 
2860565ace46SCharles Keepax 	buf->irq_count |= 0x01;
2861565ace46SCharles Keepax 
2862565ace46SCharles Keepax 	return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack),
2863565ace46SCharles Keepax 				    buf->irq_count);
2864565ace46SCharles Keepax }
2865565ace46SCharles Keepax 
2866565ace46SCharles Keepax int wm_adsp_compr_pointer(struct snd_compr_stream *stream,
2867565ace46SCharles Keepax 			  struct snd_compr_tstamp *tstamp)
2868565ace46SCharles Keepax {
2869565ace46SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
2870565ace46SCharles Keepax 	struct wm_adsp_compr_buf *buf = compr->buf;
2871565ace46SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
2872565ace46SCharles Keepax 	int ret = 0;
2873565ace46SCharles Keepax 
2874565ace46SCharles Keepax 	adsp_dbg(dsp, "Pointer request\n");
2875565ace46SCharles Keepax 
2876565ace46SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
2877565ace46SCharles Keepax 
2878565ace46SCharles Keepax 	if (!compr->buf) {
2879565ace46SCharles Keepax 		ret = -ENXIO;
2880565ace46SCharles Keepax 		goto out;
2881565ace46SCharles Keepax 	}
2882565ace46SCharles Keepax 
2883565ace46SCharles Keepax 	if (compr->buf->error) {
2884565ace46SCharles Keepax 		ret = -EIO;
2885565ace46SCharles Keepax 		goto out;
2886565ace46SCharles Keepax 	}
2887565ace46SCharles Keepax 
2888565ace46SCharles Keepax 	if (buf->avail < wm_adsp_compr_frag_words(compr)) {
2889565ace46SCharles Keepax 		ret = wm_adsp_buffer_update_avail(buf);
2890565ace46SCharles Keepax 		if (ret < 0) {
2891565ace46SCharles Keepax 			adsp_err(dsp, "Error reading avail: %d\n", ret);
2892565ace46SCharles Keepax 			goto out;
2893565ace46SCharles Keepax 		}
2894565ace46SCharles Keepax 
2895565ace46SCharles Keepax 		/*
2896565ace46SCharles Keepax 		 * If we really have less than 1 fragment available tell the
2897565ace46SCharles Keepax 		 * DSP to inform us once a whole fragment is available.
2898565ace46SCharles Keepax 		 */
2899565ace46SCharles Keepax 		if (buf->avail < wm_adsp_compr_frag_words(compr)) {
2900565ace46SCharles Keepax 			ret = wm_adsp_buffer_reenable_irq(buf);
2901565ace46SCharles Keepax 			if (ret < 0) {
2902565ace46SCharles Keepax 				adsp_err(dsp,
2903565ace46SCharles Keepax 					 "Failed to re-enable buffer IRQ: %d\n",
2904565ace46SCharles Keepax 					 ret);
2905565ace46SCharles Keepax 				goto out;
2906565ace46SCharles Keepax 			}
2907565ace46SCharles Keepax 		}
2908565ace46SCharles Keepax 	}
2909565ace46SCharles Keepax 
2910565ace46SCharles Keepax 	tstamp->copied_total = compr->copied_total;
2911565ace46SCharles Keepax 	tstamp->copied_total += buf->avail * WM_ADSP_DATA_WORD_SIZE;
2912da2b3358SCharles Keepax 	tstamp->sampling_rate = compr->sample_rate;
2913565ace46SCharles Keepax 
2914565ace46SCharles Keepax out:
2915565ace46SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
2916565ace46SCharles Keepax 
2917565ace46SCharles Keepax 	return ret;
2918565ace46SCharles Keepax }
2919565ace46SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer);
2920565ace46SCharles Keepax 
292183a40ce9SCharles Keepax static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target)
292283a40ce9SCharles Keepax {
292383a40ce9SCharles Keepax 	struct wm_adsp_compr_buf *buf = compr->buf;
292483a40ce9SCharles Keepax 	u8 *pack_in = (u8 *)compr->raw_buf;
292583a40ce9SCharles Keepax 	u8 *pack_out = (u8 *)compr->raw_buf;
292683a40ce9SCharles Keepax 	unsigned int adsp_addr;
292783a40ce9SCharles Keepax 	int mem_type, nwords, max_read;
292883a40ce9SCharles Keepax 	int i, j, ret;
292983a40ce9SCharles Keepax 
293083a40ce9SCharles Keepax 	/* Calculate read parameters */
293183a40ce9SCharles Keepax 	for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i)
293283a40ce9SCharles Keepax 		if (buf->read_index < buf->regions[i].cumulative_size)
293383a40ce9SCharles Keepax 			break;
293483a40ce9SCharles Keepax 
293583a40ce9SCharles Keepax 	if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions)
293683a40ce9SCharles Keepax 		return -EINVAL;
293783a40ce9SCharles Keepax 
293883a40ce9SCharles Keepax 	mem_type = buf->regions[i].mem_type;
293983a40ce9SCharles Keepax 	adsp_addr = buf->regions[i].base_addr +
294083a40ce9SCharles Keepax 		    (buf->read_index - buf->regions[i].offset);
294183a40ce9SCharles Keepax 
294283a40ce9SCharles Keepax 	max_read = wm_adsp_compr_frag_words(compr);
294383a40ce9SCharles Keepax 	nwords = buf->regions[i].cumulative_size - buf->read_index;
294483a40ce9SCharles Keepax 
294583a40ce9SCharles Keepax 	if (nwords > target)
294683a40ce9SCharles Keepax 		nwords = target;
294783a40ce9SCharles Keepax 	if (nwords > buf->avail)
294883a40ce9SCharles Keepax 		nwords = buf->avail;
294983a40ce9SCharles Keepax 	if (nwords > max_read)
295083a40ce9SCharles Keepax 		nwords = max_read;
295183a40ce9SCharles Keepax 	if (!nwords)
295283a40ce9SCharles Keepax 		return 0;
295383a40ce9SCharles Keepax 
295483a40ce9SCharles Keepax 	/* Read data from DSP */
295583a40ce9SCharles Keepax 	ret = wm_adsp_read_data_block(buf->dsp, mem_type, adsp_addr,
295683a40ce9SCharles Keepax 				      nwords, compr->raw_buf);
295783a40ce9SCharles Keepax 	if (ret < 0)
295883a40ce9SCharles Keepax 		return ret;
295983a40ce9SCharles Keepax 
296083a40ce9SCharles Keepax 	/* Remove the padding bytes from the data read from the DSP */
296183a40ce9SCharles Keepax 	for (i = 0; i < nwords; i++) {
296283a40ce9SCharles Keepax 		for (j = 0; j < WM_ADSP_DATA_WORD_SIZE; j++)
296383a40ce9SCharles Keepax 			*pack_out++ = *pack_in++;
296483a40ce9SCharles Keepax 
296583a40ce9SCharles Keepax 		pack_in += sizeof(*(compr->raw_buf)) - WM_ADSP_DATA_WORD_SIZE;
296683a40ce9SCharles Keepax 	}
296783a40ce9SCharles Keepax 
296883a40ce9SCharles Keepax 	/* update read index to account for words read */
296983a40ce9SCharles Keepax 	buf->read_index += nwords;
297083a40ce9SCharles Keepax 	if (buf->read_index == wm_adsp_buffer_size(buf))
297183a40ce9SCharles Keepax 		buf->read_index = 0;
297283a40ce9SCharles Keepax 
297383a40ce9SCharles Keepax 	ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index),
297483a40ce9SCharles Keepax 				   buf->read_index);
297583a40ce9SCharles Keepax 	if (ret < 0)
297683a40ce9SCharles Keepax 		return ret;
297783a40ce9SCharles Keepax 
297883a40ce9SCharles Keepax 	/* update avail to account for words read */
297983a40ce9SCharles Keepax 	buf->avail -= nwords;
298083a40ce9SCharles Keepax 
298183a40ce9SCharles Keepax 	return nwords;
298283a40ce9SCharles Keepax }
298383a40ce9SCharles Keepax 
298483a40ce9SCharles Keepax static int wm_adsp_compr_read(struct wm_adsp_compr *compr,
298583a40ce9SCharles Keepax 			      char __user *buf, size_t count)
298683a40ce9SCharles Keepax {
298783a40ce9SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
298883a40ce9SCharles Keepax 	int ntotal = 0;
298983a40ce9SCharles Keepax 	int nwords, nbytes;
299083a40ce9SCharles Keepax 
299183a40ce9SCharles Keepax 	adsp_dbg(dsp, "Requested read of %zu bytes\n", count);
299283a40ce9SCharles Keepax 
299383a40ce9SCharles Keepax 	if (!compr->buf)
299483a40ce9SCharles Keepax 		return -ENXIO;
299583a40ce9SCharles Keepax 
299683a40ce9SCharles Keepax 	if (compr->buf->error)
299783a40ce9SCharles Keepax 		return -EIO;
299883a40ce9SCharles Keepax 
299983a40ce9SCharles Keepax 	count /= WM_ADSP_DATA_WORD_SIZE;
300083a40ce9SCharles Keepax 
300183a40ce9SCharles Keepax 	do {
300283a40ce9SCharles Keepax 		nwords = wm_adsp_buffer_capture_block(compr, count);
300383a40ce9SCharles Keepax 		if (nwords < 0) {
300483a40ce9SCharles Keepax 			adsp_err(dsp, "Failed to capture block: %d\n", nwords);
300583a40ce9SCharles Keepax 			return nwords;
300683a40ce9SCharles Keepax 		}
300783a40ce9SCharles Keepax 
300883a40ce9SCharles Keepax 		nbytes = nwords * WM_ADSP_DATA_WORD_SIZE;
300983a40ce9SCharles Keepax 
301083a40ce9SCharles Keepax 		adsp_dbg(dsp, "Read %d bytes\n", nbytes);
301183a40ce9SCharles Keepax 
301283a40ce9SCharles Keepax 		if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) {
301383a40ce9SCharles Keepax 			adsp_err(dsp, "Failed to copy data to user: %d, %d\n",
301483a40ce9SCharles Keepax 				 ntotal, nbytes);
301583a40ce9SCharles Keepax 			return -EFAULT;
301683a40ce9SCharles Keepax 		}
301783a40ce9SCharles Keepax 
301883a40ce9SCharles Keepax 		count -= nwords;
301983a40ce9SCharles Keepax 		ntotal += nbytes;
302083a40ce9SCharles Keepax 	} while (nwords > 0 && count > 0);
302183a40ce9SCharles Keepax 
302283a40ce9SCharles Keepax 	compr->copied_total += ntotal;
302383a40ce9SCharles Keepax 
302483a40ce9SCharles Keepax 	return ntotal;
302583a40ce9SCharles Keepax }
302683a40ce9SCharles Keepax 
302783a40ce9SCharles Keepax int wm_adsp_compr_copy(struct snd_compr_stream *stream, char __user *buf,
302883a40ce9SCharles Keepax 		       size_t count)
302983a40ce9SCharles Keepax {
303083a40ce9SCharles Keepax 	struct wm_adsp_compr *compr = stream->runtime->private_data;
303183a40ce9SCharles Keepax 	struct wm_adsp *dsp = compr->dsp;
303283a40ce9SCharles Keepax 	int ret;
303383a40ce9SCharles Keepax 
303483a40ce9SCharles Keepax 	mutex_lock(&dsp->pwr_lock);
303583a40ce9SCharles Keepax 
303683a40ce9SCharles Keepax 	if (stream->direction == SND_COMPRESS_CAPTURE)
303783a40ce9SCharles Keepax 		ret = wm_adsp_compr_read(compr, buf, count);
303883a40ce9SCharles Keepax 	else
303983a40ce9SCharles Keepax 		ret = -ENOTSUPP;
304083a40ce9SCharles Keepax 
304183a40ce9SCharles Keepax 	mutex_unlock(&dsp->pwr_lock);
304283a40ce9SCharles Keepax 
304383a40ce9SCharles Keepax 	return ret;
304483a40ce9SCharles Keepax }
304583a40ce9SCharles Keepax EXPORT_SYMBOL_GPL(wm_adsp_compr_copy);
304683a40ce9SCharles Keepax 
30470a37c6efSPraveen Diwakar MODULE_LICENSE("GPL v2");
3048