Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1 | /* |
| 2 | * wm_adsp.c -- Wolfson ADSP support |
| 3 | * |
| 4 | * Copyright 2012 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/firmware.h> |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 18 | #include <linux/list.h> |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 19 | #include <linux/pm.h> |
| 20 | #include <linux/pm_runtime.h> |
| 21 | #include <linux/regmap.h> |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 22 | #include <linux/regulator/consumer.h> |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 25 | #include <linux/workqueue.h> |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 26 | #include <linux/debugfs.h> |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 27 | #include <sound/core.h> |
| 28 | #include <sound/pcm.h> |
| 29 | #include <sound/pcm_params.h> |
| 30 | #include <sound/soc.h> |
| 31 | #include <sound/jack.h> |
| 32 | #include <sound/initval.h> |
| 33 | #include <sound/tlv.h> |
| 34 | |
| 35 | #include <linux/mfd/arizona/registers.h> |
| 36 | |
Mark Brown | dc91428 | 2013-02-18 19:09:23 +0000 | [diff] [blame] | 37 | #include "arizona.h" |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 38 | #include "wm_adsp.h" |
| 39 | |
| 40 | #define adsp_crit(_dsp, fmt, ...) \ |
| 41 | dev_crit(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 42 | #define adsp_err(_dsp, fmt, ...) \ |
| 43 | dev_err(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 44 | #define adsp_warn(_dsp, fmt, ...) \ |
| 45 | dev_warn(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 46 | #define adsp_info(_dsp, fmt, ...) \ |
| 47 | dev_info(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 48 | #define adsp_dbg(_dsp, fmt, ...) \ |
| 49 | dev_dbg(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__) |
| 50 | |
| 51 | #define ADSP1_CONTROL_1 0x00 |
| 52 | #define ADSP1_CONTROL_2 0x02 |
| 53 | #define ADSP1_CONTROL_3 0x03 |
| 54 | #define ADSP1_CONTROL_4 0x04 |
| 55 | #define ADSP1_CONTROL_5 0x06 |
| 56 | #define ADSP1_CONTROL_6 0x07 |
| 57 | #define ADSP1_CONTROL_7 0x08 |
| 58 | #define ADSP1_CONTROL_8 0x09 |
| 59 | #define ADSP1_CONTROL_9 0x0A |
| 60 | #define ADSP1_CONTROL_10 0x0B |
| 61 | #define ADSP1_CONTROL_11 0x0C |
| 62 | #define ADSP1_CONTROL_12 0x0D |
| 63 | #define ADSP1_CONTROL_13 0x0F |
| 64 | #define ADSP1_CONTROL_14 0x10 |
| 65 | #define ADSP1_CONTROL_15 0x11 |
| 66 | #define ADSP1_CONTROL_16 0x12 |
| 67 | #define ADSP1_CONTROL_17 0x13 |
| 68 | #define ADSP1_CONTROL_18 0x14 |
| 69 | #define ADSP1_CONTROL_19 0x16 |
| 70 | #define ADSP1_CONTROL_20 0x17 |
| 71 | #define ADSP1_CONTROL_21 0x18 |
| 72 | #define ADSP1_CONTROL_22 0x1A |
| 73 | #define ADSP1_CONTROL_23 0x1B |
| 74 | #define ADSP1_CONTROL_24 0x1C |
| 75 | #define ADSP1_CONTROL_25 0x1E |
| 76 | #define ADSP1_CONTROL_26 0x20 |
| 77 | #define ADSP1_CONTROL_27 0x21 |
| 78 | #define ADSP1_CONTROL_28 0x22 |
| 79 | #define ADSP1_CONTROL_29 0x23 |
| 80 | #define ADSP1_CONTROL_30 0x24 |
| 81 | #define ADSP1_CONTROL_31 0x26 |
| 82 | |
| 83 | /* |
| 84 | * ADSP1 Control 19 |
| 85 | */ |
| 86 | #define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */ |
| 87 | #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */ |
| 88 | #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */ |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | * ADSP1 Control 30 |
| 93 | */ |
| 94 | #define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */ |
| 95 | #define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */ |
| 96 | #define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */ |
| 97 | #define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */ |
| 98 | #define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */ |
| 99 | #define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */ |
| 100 | #define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */ |
| 101 | #define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */ |
| 102 | #define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */ |
| 103 | #define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */ |
| 104 | #define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */ |
| 105 | #define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */ |
| 106 | #define ADSP1_START 0x0001 /* DSP1_START */ |
| 107 | #define ADSP1_START_MASK 0x0001 /* DSP1_START */ |
| 108 | #define ADSP1_START_SHIFT 0 /* DSP1_START */ |
| 109 | #define ADSP1_START_WIDTH 1 /* DSP1_START */ |
| 110 | |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 111 | /* |
| 112 | * ADSP1 Control 31 |
| 113 | */ |
| 114 | #define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */ |
| 115 | #define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */ |
| 116 | #define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */ |
| 117 | |
Mark Brown | 2d30b57 | 2013-01-28 20:18:17 +0800 | [diff] [blame] | 118 | #define ADSP2_CONTROL 0x0 |
| 119 | #define ADSP2_CLOCKING 0x1 |
| 120 | #define ADSP2_STATUS1 0x4 |
| 121 | #define ADSP2_WDMA_CONFIG_1 0x30 |
| 122 | #define ADSP2_WDMA_CONFIG_2 0x31 |
| 123 | #define ADSP2_RDMA_CONFIG_1 0x34 |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 124 | |
Richard Fitzgerald | 10337b0 | 2015-05-29 10:23:07 +0100 | [diff] [blame] | 125 | #define ADSP2_SCRATCH0 0x40 |
| 126 | #define ADSP2_SCRATCH1 0x41 |
| 127 | #define ADSP2_SCRATCH2 0x42 |
| 128 | #define ADSP2_SCRATCH3 0x43 |
| 129 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 130 | /* |
| 131 | * ADSP2 Control |
| 132 | */ |
| 133 | |
| 134 | #define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */ |
| 135 | #define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */ |
| 136 | #define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */ |
| 137 | #define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */ |
| 138 | #define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */ |
| 139 | #define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */ |
| 140 | #define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */ |
| 141 | #define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */ |
| 142 | #define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */ |
| 143 | #define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */ |
| 144 | #define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */ |
| 145 | #define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */ |
| 146 | #define ADSP2_START 0x0001 /* DSP1_START */ |
| 147 | #define ADSP2_START_MASK 0x0001 /* DSP1_START */ |
| 148 | #define ADSP2_START_SHIFT 0 /* DSP1_START */ |
| 149 | #define ADSP2_START_WIDTH 1 /* DSP1_START */ |
| 150 | |
| 151 | /* |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 152 | * ADSP2 clocking |
| 153 | */ |
| 154 | #define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */ |
| 155 | #define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */ |
| 156 | #define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */ |
| 157 | |
| 158 | /* |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 159 | * ADSP2 Status 1 |
| 160 | */ |
| 161 | #define ADSP2_RAM_RDY 0x0001 |
| 162 | #define ADSP2_RAM_RDY_MASK 0x0001 |
| 163 | #define ADSP2_RAM_RDY_SHIFT 0 |
| 164 | #define ADSP2_RAM_RDY_WIDTH 1 |
| 165 | |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 166 | struct wm_adsp_buf { |
| 167 | struct list_head list; |
| 168 | void *buf; |
| 169 | }; |
| 170 | |
| 171 | static struct wm_adsp_buf *wm_adsp_buf_alloc(const void *src, size_t len, |
| 172 | struct list_head *list) |
| 173 | { |
| 174 | struct wm_adsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
| 175 | |
| 176 | if (buf == NULL) |
| 177 | return NULL; |
| 178 | |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 179 | buf->buf = vmalloc(len); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 180 | if (!buf->buf) { |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 181 | vfree(buf); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 184 | memcpy(buf->buf, src, len); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 185 | |
| 186 | if (list) |
| 187 | list_add_tail(&buf->list, list); |
| 188 | |
| 189 | return buf; |
| 190 | } |
| 191 | |
| 192 | static void wm_adsp_buf_free(struct list_head *list) |
| 193 | { |
| 194 | while (!list_empty(list)) { |
| 195 | struct wm_adsp_buf *buf = list_first_entry(list, |
| 196 | struct wm_adsp_buf, |
| 197 | list); |
| 198 | list_del(&buf->list); |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 199 | vfree(buf->buf); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 200 | kfree(buf); |
| 201 | } |
| 202 | } |
| 203 | |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 204 | #define WM_ADSP_FW_MBC_VSS 0 |
| 205 | #define WM_ADSP_FW_HIFI 1 |
| 206 | #define WM_ADSP_FW_TX 2 |
| 207 | #define WM_ADSP_FW_TX_SPK 3 |
| 208 | #define WM_ADSP_FW_RX 4 |
| 209 | #define WM_ADSP_FW_RX_ANC 5 |
| 210 | #define WM_ADSP_FW_CTRL 6 |
| 211 | #define WM_ADSP_FW_ASR 7 |
| 212 | #define WM_ADSP_FW_TRACE 8 |
| 213 | #define WM_ADSP_FW_SPK_PROT 9 |
| 214 | #define WM_ADSP_FW_MISC 10 |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 215 | |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 216 | #define WM_ADSP_NUM_FW 11 |
Mark Brown | dd84f92 | 2013-03-08 15:25:58 +0800 | [diff] [blame] | 217 | |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 218 | static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = { |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 219 | [WM_ADSP_FW_MBC_VSS] = "MBC/VSS", |
| 220 | [WM_ADSP_FW_HIFI] = "MasterHiFi", |
| 221 | [WM_ADSP_FW_TX] = "Tx", |
| 222 | [WM_ADSP_FW_TX_SPK] = "Tx Speaker", |
| 223 | [WM_ADSP_FW_RX] = "Rx", |
| 224 | [WM_ADSP_FW_RX_ANC] = "Rx ANC", |
| 225 | [WM_ADSP_FW_CTRL] = "Voice Ctrl", |
| 226 | [WM_ADSP_FW_ASR] = "ASR Assist", |
| 227 | [WM_ADSP_FW_TRACE] = "Dbg Trace", |
| 228 | [WM_ADSP_FW_SPK_PROT] = "Protection", |
| 229 | [WM_ADSP_FW_MISC] = "Misc", |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
Charles Keepax | 2cd19bd | 2015-12-15 11:29:46 +0000 | [diff] [blame] | 232 | struct wm_adsp_system_config_xm_hdr { |
| 233 | __be32 sys_enable; |
| 234 | __be32 fw_id; |
| 235 | __be32 fw_rev; |
| 236 | __be32 boot_status; |
| 237 | __be32 watchdog; |
| 238 | __be32 dma_buffer_size; |
| 239 | __be32 rdma[6]; |
| 240 | __be32 wdma[8]; |
| 241 | __be32 build_job_name[3]; |
| 242 | __be32 build_job_number; |
| 243 | }; |
| 244 | |
| 245 | struct wm_adsp_alg_xm_struct { |
| 246 | __be32 magic; |
| 247 | __be32 smoothing; |
| 248 | __be32 threshold; |
| 249 | __be32 host_buf_ptr; |
| 250 | __be32 start_seq; |
| 251 | __be32 high_water_mark; |
| 252 | __be32 low_water_mark; |
| 253 | __be64 smoothed_power; |
| 254 | }; |
| 255 | |
| 256 | struct wm_adsp_buffer { |
| 257 | __be32 X_buf_base; /* XM base addr of first X area */ |
| 258 | __be32 X_buf_size; /* Size of 1st X area in words */ |
| 259 | __be32 X_buf_base2; /* XM base addr of 2nd X area */ |
| 260 | __be32 X_buf_brk; /* Total X size in words */ |
| 261 | __be32 Y_buf_base; /* YM base addr of Y area */ |
| 262 | __be32 wrap; /* Total size X and Y in words */ |
| 263 | __be32 high_water_mark; /* Point at which IRQ is asserted */ |
| 264 | __be32 irq_count; /* bits 1-31 count IRQ assertions */ |
| 265 | __be32 irq_ack; /* acked IRQ count, bit 0 enables IRQ */ |
| 266 | __be32 next_write_index; /* word index of next write */ |
| 267 | __be32 next_read_index; /* word index of next read */ |
| 268 | __be32 error; /* error if any */ |
| 269 | __be32 oldest_block_index; /* word index of oldest surviving */ |
| 270 | __be32 requested_rewind; /* how many blocks rewind was done */ |
| 271 | __be32 reserved_space; /* internal */ |
| 272 | __be32 min_free; /* min free space since stream start */ |
| 273 | __be32 blocks_written[2]; /* total blocks written (64 bit) */ |
| 274 | __be32 words_written[2]; /* total words written (64 bit) */ |
| 275 | }; |
| 276 | |
| 277 | struct wm_adsp_compr_buf { |
| 278 | struct wm_adsp *dsp; |
| 279 | |
| 280 | struct wm_adsp_buffer_region *regions; |
| 281 | u32 host_buf_ptr; |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 282 | |
| 283 | u32 error; |
| 284 | u32 irq_count; |
| 285 | int read_index; |
| 286 | int avail; |
Charles Keepax | 2cd19bd | 2015-12-15 11:29:46 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 289 | struct wm_adsp_compr { |
| 290 | struct wm_adsp *dsp; |
Charles Keepax | 95fe959 | 2015-12-15 11:29:47 +0000 | [diff] [blame] | 291 | struct wm_adsp_compr_buf *buf; |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 292 | |
| 293 | struct snd_compr_stream *stream; |
| 294 | struct snd_compressed_buffer size; |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 295 | |
Charles Keepax | 83a40ce | 2016-01-06 12:33:19 +0000 | [diff] [blame] | 296 | u32 *raw_buf; |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 297 | unsigned int copied_total; |
Charles Keepax | da2b335 | 2016-02-02 16:41:36 +0000 | [diff] [blame^] | 298 | |
| 299 | unsigned int sample_rate; |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | #define WM_ADSP_DATA_WORD_SIZE 3 |
| 303 | |
| 304 | #define WM_ADSP_MIN_FRAGMENTS 1 |
| 305 | #define WM_ADSP_MAX_FRAGMENTS 256 |
| 306 | #define WM_ADSP_MIN_FRAGMENT_SIZE (64 * WM_ADSP_DATA_WORD_SIZE) |
| 307 | #define WM_ADSP_MAX_FRAGMENT_SIZE (4096 * WM_ADSP_DATA_WORD_SIZE) |
| 308 | |
Charles Keepax | 2cd19bd | 2015-12-15 11:29:46 +0000 | [diff] [blame] | 309 | #define WM_ADSP_ALG_XM_STRUCT_MAGIC 0x49aec7 |
| 310 | |
| 311 | #define HOST_BUFFER_FIELD(field) \ |
| 312 | (offsetof(struct wm_adsp_buffer, field) / sizeof(__be32)) |
| 313 | |
| 314 | #define ALG_XM_FIELD(field) \ |
| 315 | (offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32)) |
| 316 | |
| 317 | static int wm_adsp_buffer_init(struct wm_adsp *dsp); |
| 318 | static int wm_adsp_buffer_free(struct wm_adsp *dsp); |
| 319 | |
| 320 | struct wm_adsp_buffer_region { |
| 321 | unsigned int offset; |
| 322 | unsigned int cumulative_size; |
| 323 | unsigned int mem_type; |
| 324 | unsigned int base_addr; |
| 325 | }; |
| 326 | |
| 327 | struct wm_adsp_buffer_region_def { |
| 328 | unsigned int mem_type; |
| 329 | unsigned int base_offset; |
| 330 | unsigned int size_offset; |
| 331 | }; |
| 332 | |
| 333 | static struct wm_adsp_buffer_region_def ez2control_regions[] = { |
| 334 | { |
| 335 | .mem_type = WMFW_ADSP2_XM, |
| 336 | .base_offset = HOST_BUFFER_FIELD(X_buf_base), |
| 337 | .size_offset = HOST_BUFFER_FIELD(X_buf_size), |
| 338 | }, |
| 339 | { |
| 340 | .mem_type = WMFW_ADSP2_XM, |
| 341 | .base_offset = HOST_BUFFER_FIELD(X_buf_base2), |
| 342 | .size_offset = HOST_BUFFER_FIELD(X_buf_brk), |
| 343 | }, |
| 344 | { |
| 345 | .mem_type = WMFW_ADSP2_YM, |
| 346 | .base_offset = HOST_BUFFER_FIELD(Y_buf_base), |
| 347 | .size_offset = HOST_BUFFER_FIELD(wrap), |
| 348 | }, |
| 349 | }; |
| 350 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 351 | struct wm_adsp_fw_caps { |
| 352 | u32 id; |
| 353 | struct snd_codec_desc desc; |
Charles Keepax | 2cd19bd | 2015-12-15 11:29:46 +0000 | [diff] [blame] | 354 | int num_regions; |
| 355 | struct wm_adsp_buffer_region_def *region_defs; |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | static const struct wm_adsp_fw_caps ez2control_caps[] = { |
| 359 | { |
| 360 | .id = SND_AUDIOCODEC_BESPOKE, |
| 361 | .desc = { |
| 362 | .max_ch = 1, |
| 363 | .sample_rates = { 16000 }, |
| 364 | .num_sample_rates = 1, |
| 365 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 366 | }, |
Charles Keepax | 2cd19bd | 2015-12-15 11:29:46 +0000 | [diff] [blame] | 367 | .num_regions = ARRAY_SIZE(ez2control_regions), |
| 368 | .region_defs = ez2control_regions, |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 369 | }, |
| 370 | }; |
| 371 | |
| 372 | static const struct { |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 373 | const char *file; |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 374 | int compr_direction; |
| 375 | int num_caps; |
| 376 | const struct wm_adsp_fw_caps *caps; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 377 | } wm_adsp_fw[WM_ADSP_NUM_FW] = { |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 378 | [WM_ADSP_FW_MBC_VSS] = { .file = "mbc-vss" }, |
| 379 | [WM_ADSP_FW_HIFI] = { .file = "hifi" }, |
| 380 | [WM_ADSP_FW_TX] = { .file = "tx" }, |
| 381 | [WM_ADSP_FW_TX_SPK] = { .file = "tx-spk" }, |
| 382 | [WM_ADSP_FW_RX] = { .file = "rx" }, |
| 383 | [WM_ADSP_FW_RX_ANC] = { .file = "rx-anc" }, |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 384 | [WM_ADSP_FW_CTRL] = { |
| 385 | .file = "ctrl", |
| 386 | .compr_direction = SND_COMPRESS_CAPTURE, |
| 387 | .num_caps = ARRAY_SIZE(ez2control_caps), |
| 388 | .caps = ez2control_caps, |
| 389 | }, |
Charles Keepax | 04d1300 | 2015-11-26 14:01:52 +0000 | [diff] [blame] | 390 | [WM_ADSP_FW_ASR] = { .file = "asr" }, |
| 391 | [WM_ADSP_FW_TRACE] = { .file = "trace" }, |
| 392 | [WM_ADSP_FW_SPK_PROT] = { .file = "spk-prot" }, |
| 393 | [WM_ADSP_FW_MISC] = { .file = "misc" }, |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 394 | }; |
| 395 | |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 396 | struct wm_coeff_ctl_ops { |
| 397 | int (*xget)(struct snd_kcontrol *kcontrol, |
| 398 | struct snd_ctl_elem_value *ucontrol); |
| 399 | int (*xput)(struct snd_kcontrol *kcontrol, |
| 400 | struct snd_ctl_elem_value *ucontrol); |
| 401 | int (*xinfo)(struct snd_kcontrol *kcontrol, |
| 402 | struct snd_ctl_elem_info *uinfo); |
| 403 | }; |
| 404 | |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 405 | struct wm_coeff_ctl { |
| 406 | const char *name; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 407 | const char *fw_name; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 408 | struct wm_adsp_alg_region alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 409 | struct wm_coeff_ctl_ops ops; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 410 | struct wm_adsp *dsp; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 411 | unsigned int enabled:1; |
| 412 | struct list_head list; |
| 413 | void *cache; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 414 | unsigned int offset; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 415 | size_t len; |
Dimitris Papastamos | 0c2e3f3 | 2013-05-28 12:01:50 +0100 | [diff] [blame] | 416 | unsigned int set:1; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 417 | struct snd_kcontrol *kcontrol; |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 418 | unsigned int flags; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 419 | }; |
| 420 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 421 | #ifdef CONFIG_DEBUG_FS |
| 422 | static void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, const char *s) |
| 423 | { |
| 424 | char *tmp = kasprintf(GFP_KERNEL, "%s\n", s); |
| 425 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 426 | kfree(dsp->wmfw_file_name); |
| 427 | dsp->wmfw_file_name = tmp; |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, const char *s) |
| 431 | { |
| 432 | char *tmp = kasprintf(GFP_KERNEL, "%s\n", s); |
| 433 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 434 | kfree(dsp->bin_file_name); |
| 435 | dsp->bin_file_name = tmp; |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static void wm_adsp_debugfs_clear(struct wm_adsp *dsp) |
| 439 | { |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 440 | kfree(dsp->wmfw_file_name); |
| 441 | kfree(dsp->bin_file_name); |
| 442 | dsp->wmfw_file_name = NULL; |
| 443 | dsp->bin_file_name = NULL; |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static ssize_t wm_adsp_debugfs_wmfw_read(struct file *file, |
| 447 | char __user *user_buf, |
| 448 | size_t count, loff_t *ppos) |
| 449 | { |
| 450 | struct wm_adsp *dsp = file->private_data; |
| 451 | ssize_t ret; |
| 452 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 453 | mutex_lock(&dsp->pwr_lock); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 454 | |
| 455 | if (!dsp->wmfw_file_name || !dsp->running) |
| 456 | ret = 0; |
| 457 | else |
| 458 | ret = simple_read_from_buffer(user_buf, count, ppos, |
| 459 | dsp->wmfw_file_name, |
| 460 | strlen(dsp->wmfw_file_name)); |
| 461 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 462 | mutex_unlock(&dsp->pwr_lock); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | static ssize_t wm_adsp_debugfs_bin_read(struct file *file, |
| 467 | char __user *user_buf, |
| 468 | size_t count, loff_t *ppos) |
| 469 | { |
| 470 | struct wm_adsp *dsp = file->private_data; |
| 471 | ssize_t ret; |
| 472 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 473 | mutex_lock(&dsp->pwr_lock); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 474 | |
| 475 | if (!dsp->bin_file_name || !dsp->running) |
| 476 | ret = 0; |
| 477 | else |
| 478 | ret = simple_read_from_buffer(user_buf, count, ppos, |
| 479 | dsp->bin_file_name, |
| 480 | strlen(dsp->bin_file_name)); |
| 481 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 482 | mutex_unlock(&dsp->pwr_lock); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | static const struct { |
| 487 | const char *name; |
| 488 | const struct file_operations fops; |
| 489 | } wm_adsp_debugfs_fops[] = { |
| 490 | { |
| 491 | .name = "wmfw_file_name", |
| 492 | .fops = { |
| 493 | .open = simple_open, |
| 494 | .read = wm_adsp_debugfs_wmfw_read, |
| 495 | }, |
| 496 | }, |
| 497 | { |
| 498 | .name = "bin_file_name", |
| 499 | .fops = { |
| 500 | .open = simple_open, |
| 501 | .read = wm_adsp_debugfs_bin_read, |
| 502 | }, |
| 503 | }, |
| 504 | }; |
| 505 | |
| 506 | static void wm_adsp2_init_debugfs(struct wm_adsp *dsp, |
| 507 | struct snd_soc_codec *codec) |
| 508 | { |
| 509 | struct dentry *root = NULL; |
| 510 | char *root_name; |
| 511 | int i; |
| 512 | |
| 513 | if (!codec->component.debugfs_root) { |
| 514 | adsp_err(dsp, "No codec debugfs root\n"); |
| 515 | goto err; |
| 516 | } |
| 517 | |
| 518 | root_name = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 519 | if (!root_name) |
| 520 | goto err; |
| 521 | |
| 522 | snprintf(root_name, PAGE_SIZE, "dsp%d", dsp->num); |
| 523 | root = debugfs_create_dir(root_name, codec->component.debugfs_root); |
| 524 | kfree(root_name); |
| 525 | |
| 526 | if (!root) |
| 527 | goto err; |
| 528 | |
| 529 | if (!debugfs_create_bool("running", S_IRUGO, root, &dsp->running)) |
| 530 | goto err; |
| 531 | |
| 532 | if (!debugfs_create_x32("fw_id", S_IRUGO, root, &dsp->fw_id)) |
| 533 | goto err; |
| 534 | |
| 535 | if (!debugfs_create_x32("fw_version", S_IRUGO, root, |
| 536 | &dsp->fw_id_version)) |
| 537 | goto err; |
| 538 | |
| 539 | for (i = 0; i < ARRAY_SIZE(wm_adsp_debugfs_fops); ++i) { |
| 540 | if (!debugfs_create_file(wm_adsp_debugfs_fops[i].name, |
| 541 | S_IRUGO, root, dsp, |
| 542 | &wm_adsp_debugfs_fops[i].fops)) |
| 543 | goto err; |
| 544 | } |
| 545 | |
| 546 | dsp->debugfs_root = root; |
| 547 | return; |
| 548 | |
| 549 | err: |
| 550 | debugfs_remove_recursive(root); |
| 551 | adsp_err(dsp, "Failed to create debugfs\n"); |
| 552 | } |
| 553 | |
| 554 | static void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp) |
| 555 | { |
| 556 | wm_adsp_debugfs_clear(dsp); |
| 557 | debugfs_remove_recursive(dsp->debugfs_root); |
| 558 | } |
| 559 | #else |
| 560 | static inline void wm_adsp2_init_debugfs(struct wm_adsp *dsp, |
| 561 | struct snd_soc_codec *codec) |
| 562 | { |
| 563 | } |
| 564 | |
| 565 | static inline void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp) |
| 566 | { |
| 567 | } |
| 568 | |
| 569 | static inline void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, |
| 570 | const char *s) |
| 571 | { |
| 572 | } |
| 573 | |
| 574 | static inline void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, |
| 575 | const char *s) |
| 576 | { |
| 577 | } |
| 578 | |
| 579 | static inline void wm_adsp_debugfs_clear(struct wm_adsp *dsp) |
| 580 | { |
| 581 | } |
| 582 | #endif |
| 583 | |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 584 | static int wm_adsp_fw_get(struct snd_kcontrol *kcontrol, |
| 585 | struct snd_ctl_elem_value *ucontrol) |
| 586 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame] | 587 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 588 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 589 | struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 590 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 591 | ucontrol->value.integer.value[0] = dsp[e->shift_l].fw; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static int wm_adsp_fw_put(struct snd_kcontrol *kcontrol, |
| 597 | struct snd_ctl_elem_value *ucontrol) |
| 598 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame] | 599 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 600 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 601 | struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec); |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 602 | int ret = 0; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 603 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 604 | if (ucontrol->value.integer.value[0] == dsp[e->shift_l].fw) |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 605 | return 0; |
| 606 | |
| 607 | if (ucontrol->value.integer.value[0] >= WM_ADSP_NUM_FW) |
| 608 | return -EINVAL; |
| 609 | |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 610 | mutex_lock(&dsp[e->shift_l].pwr_lock); |
| 611 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 612 | if (dsp[e->shift_l].running || dsp[e->shift_l].compr) |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 613 | ret = -EBUSY; |
| 614 | else |
| 615 | dsp[e->shift_l].fw = ucontrol->value.integer.value[0]; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 616 | |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 617 | mutex_unlock(&dsp[e->shift_l].pwr_lock); |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 618 | |
Charles Keepax | d27c5e1 | 2015-12-08 16:08:28 +0000 | [diff] [blame] | 619 | return ret; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static const struct soc_enum wm_adsp_fw_enum[] = { |
| 623 | SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
| 624 | SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
| 625 | SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
| 626 | SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
| 627 | }; |
| 628 | |
Richard Fitzgerald | 336d044 | 2015-06-18 13:43:19 +0100 | [diff] [blame] | 629 | const struct snd_kcontrol_new wm_adsp_fw_controls[] = { |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 630 | SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum[0], |
| 631 | wm_adsp_fw_get, wm_adsp_fw_put), |
| 632 | SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum[1], |
| 633 | wm_adsp_fw_get, wm_adsp_fw_put), |
| 634 | SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum[2], |
| 635 | wm_adsp_fw_get, wm_adsp_fw_put), |
Richard Fitzgerald | 336d044 | 2015-06-18 13:43:19 +0100 | [diff] [blame] | 636 | SOC_ENUM_EXT("DSP4 Firmware", wm_adsp_fw_enum[3], |
| 637 | wm_adsp_fw_get, wm_adsp_fw_put), |
Mark Brown | b6ed61cf | 2013-03-29 18:00:24 +0000 | [diff] [blame] | 638 | }; |
Richard Fitzgerald | 336d044 | 2015-06-18 13:43:19 +0100 | [diff] [blame] | 639 | EXPORT_SYMBOL_GPL(wm_adsp_fw_controls); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 640 | |
| 641 | static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp, |
| 642 | int type) |
| 643 | { |
| 644 | int i; |
| 645 | |
| 646 | for (i = 0; i < dsp->num_mems; i++) |
| 647 | if (dsp->mem[i].type == type) |
| 648 | return &dsp->mem[i]; |
| 649 | |
| 650 | return NULL; |
| 651 | } |
| 652 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 653 | static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *mem, |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 654 | unsigned int offset) |
| 655 | { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 656 | if (WARN_ON(!mem)) |
Takashi Iwai | 6c452bd | 2013-11-05 18:40:00 +0100 | [diff] [blame] | 657 | return offset; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 658 | switch (mem->type) { |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 659 | case WMFW_ADSP1_PM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 660 | return mem->base + (offset * 3); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 661 | case WMFW_ADSP1_DM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 662 | return mem->base + (offset * 2); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 663 | case WMFW_ADSP2_XM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 664 | return mem->base + (offset * 2); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 665 | case WMFW_ADSP2_YM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 666 | return mem->base + (offset * 2); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 667 | case WMFW_ADSP1_ZM: |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 668 | return mem->base + (offset * 2); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 669 | default: |
Takashi Iwai | 6c452bd | 2013-11-05 18:40:00 +0100 | [diff] [blame] | 670 | WARN(1, "Unknown memory region type"); |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 671 | return offset; |
| 672 | } |
| 673 | } |
| 674 | |
Richard Fitzgerald | 10337b0 | 2015-05-29 10:23:07 +0100 | [diff] [blame] | 675 | static void wm_adsp2_show_fw_status(struct wm_adsp *dsp) |
| 676 | { |
| 677 | u16 scratch[4]; |
| 678 | int ret; |
| 679 | |
| 680 | ret = regmap_raw_read(dsp->regmap, dsp->base + ADSP2_SCRATCH0, |
| 681 | scratch, sizeof(scratch)); |
| 682 | if (ret) { |
| 683 | adsp_err(dsp, "Failed to read SCRATCH regs: %d\n", ret); |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n", |
| 688 | be16_to_cpu(scratch[0]), |
| 689 | be16_to_cpu(scratch[1]), |
| 690 | be16_to_cpu(scratch[2]), |
| 691 | be16_to_cpu(scratch[3])); |
| 692 | } |
| 693 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 694 | static int wm_coeff_info(struct snd_kcontrol *kctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 695 | struct snd_ctl_elem_info *uinfo) |
| 696 | { |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 697 | struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kctl->private_value; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 698 | |
| 699 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; |
| 700 | uinfo->count = ctl->len; |
| 701 | return 0; |
| 702 | } |
| 703 | |
Charles Keepax | c9f8dd7 | 2015-04-13 13:27:58 +0100 | [diff] [blame] | 704 | static int wm_coeff_write_control(struct wm_coeff_ctl *ctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 705 | const void *buf, size_t len) |
| 706 | { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 707 | struct wm_adsp_alg_region *alg_region = &ctl->alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 708 | const struct wm_adsp_region *mem; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 709 | struct wm_adsp *dsp = ctl->dsp; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 710 | void *scratch; |
| 711 | int ret; |
| 712 | unsigned int reg; |
| 713 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 714 | mem = wm_adsp_find_region(dsp, alg_region->type); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 715 | if (!mem) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 716 | adsp_err(dsp, "No base for region %x\n", |
| 717 | alg_region->type); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 718 | return -EINVAL; |
| 719 | } |
| 720 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 721 | reg = ctl->alg_region.base + ctl->offset; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 722 | reg = wm_adsp_region_to_reg(mem, reg); |
| 723 | |
| 724 | scratch = kmemdup(buf, ctl->len, GFP_KERNEL | GFP_DMA); |
| 725 | if (!scratch) |
| 726 | return -ENOMEM; |
| 727 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 728 | ret = regmap_raw_write(dsp->regmap, reg, scratch, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 729 | ctl->len); |
| 730 | if (ret) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 731 | adsp_err(dsp, "Failed to write %zu bytes to %x: %d\n", |
Dimitris Papastamos | 43bc3bf | 2013-11-01 15:56:52 +0000 | [diff] [blame] | 732 | ctl->len, reg, ret); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 733 | kfree(scratch); |
| 734 | return ret; |
| 735 | } |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 736 | adsp_dbg(dsp, "Wrote %zu bytes to %x\n", ctl->len, reg); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 737 | |
| 738 | kfree(scratch); |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 743 | static int wm_coeff_put(struct snd_kcontrol *kctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 744 | struct snd_ctl_elem_value *ucontrol) |
| 745 | { |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 746 | struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kctl->private_value; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 747 | char *p = ucontrol->value.bytes.data; |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 748 | int ret = 0; |
| 749 | |
| 750 | mutex_lock(&ctl->dsp->pwr_lock); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 751 | |
| 752 | memcpy(ctl->cache, p, ctl->len); |
| 753 | |
Nikesh Oswal | 65d17a9 | 2015-02-16 15:25:48 +0000 | [diff] [blame] | 754 | ctl->set = 1; |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 755 | if (ctl->enabled) |
| 756 | ret = wm_coeff_write_control(ctl, p, ctl->len); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 757 | |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 758 | mutex_unlock(&ctl->dsp->pwr_lock); |
| 759 | |
| 760 | return ret; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Charles Keepax | c9f8dd7 | 2015-04-13 13:27:58 +0100 | [diff] [blame] | 763 | static int wm_coeff_read_control(struct wm_coeff_ctl *ctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 764 | void *buf, size_t len) |
| 765 | { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 766 | struct wm_adsp_alg_region *alg_region = &ctl->alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 767 | const struct wm_adsp_region *mem; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 768 | struct wm_adsp *dsp = ctl->dsp; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 769 | void *scratch; |
| 770 | int ret; |
| 771 | unsigned int reg; |
| 772 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 773 | mem = wm_adsp_find_region(dsp, alg_region->type); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 774 | if (!mem) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 775 | adsp_err(dsp, "No base for region %x\n", |
| 776 | alg_region->type); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 777 | return -EINVAL; |
| 778 | } |
| 779 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 780 | reg = ctl->alg_region.base + ctl->offset; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 781 | reg = wm_adsp_region_to_reg(mem, reg); |
| 782 | |
| 783 | scratch = kmalloc(ctl->len, GFP_KERNEL | GFP_DMA); |
| 784 | if (!scratch) |
| 785 | return -ENOMEM; |
| 786 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 787 | ret = regmap_raw_read(dsp->regmap, reg, scratch, ctl->len); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 788 | if (ret) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 789 | adsp_err(dsp, "Failed to read %zu bytes from %x: %d\n", |
Dimitris Papastamos | 43bc3bf | 2013-11-01 15:56:52 +0000 | [diff] [blame] | 790 | ctl->len, reg, ret); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 791 | kfree(scratch); |
| 792 | return ret; |
| 793 | } |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 794 | adsp_dbg(dsp, "Read %zu bytes from %x\n", ctl->len, reg); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 795 | |
| 796 | memcpy(buf, scratch, ctl->len); |
| 797 | kfree(scratch); |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 802 | static int wm_coeff_get(struct snd_kcontrol *kctl, |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 803 | struct snd_ctl_elem_value *ucontrol) |
| 804 | { |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 805 | struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kctl->private_value; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 806 | char *p = ucontrol->value.bytes.data; |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 807 | int ret = 0; |
| 808 | |
| 809 | mutex_lock(&ctl->dsp->pwr_lock); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 810 | |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 811 | if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) { |
| 812 | if (ctl->enabled) |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 813 | ret = wm_coeff_read_control(ctl, p, ctl->len); |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 814 | else |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 815 | ret = -EPERM; |
| 816 | } else { |
Charles Keepax | bc1765d | 2015-12-17 10:05:59 +0000 | [diff] [blame] | 817 | if (!ctl->flags && ctl->enabled) |
| 818 | ret = wm_coeff_read_control(ctl, ctl->cache, ctl->len); |
| 819 | |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 820 | memcpy(p, ctl->cache, ctl->len); |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 823 | mutex_unlock(&ctl->dsp->pwr_lock); |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 824 | |
Charles Keepax | 168d10e | 2015-12-08 16:08:27 +0000 | [diff] [blame] | 825 | return ret; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 828 | struct wmfw_ctl_work { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 829 | struct wm_adsp *dsp; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 830 | struct wm_coeff_ctl *ctl; |
| 831 | struct work_struct work; |
| 832 | }; |
| 833 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 834 | static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl) |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 835 | { |
| 836 | struct snd_kcontrol_new *kcontrol; |
| 837 | int ret; |
| 838 | |
Dimitris Papastamos | 92bb4c3 | 2013-08-01 11:11:28 +0100 | [diff] [blame] | 839 | if (!ctl || !ctl->name) |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 840 | return -EINVAL; |
| 841 | |
| 842 | kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL); |
| 843 | if (!kcontrol) |
| 844 | return -ENOMEM; |
| 845 | kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 846 | |
| 847 | kcontrol->name = ctl->name; |
| 848 | kcontrol->info = wm_coeff_info; |
| 849 | kcontrol->get = wm_coeff_get; |
| 850 | kcontrol->put = wm_coeff_put; |
| 851 | kcontrol->private_value = (unsigned long)ctl; |
| 852 | |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 853 | if (ctl->flags) { |
| 854 | if (ctl->flags & WMFW_CTL_FLAG_WRITEABLE) |
| 855 | kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_WRITE; |
| 856 | if (ctl->flags & WMFW_CTL_FLAG_READABLE) |
| 857 | kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_READ; |
| 858 | if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) |
| 859 | kcontrol->access |= SNDRV_CTL_ELEM_ACCESS_VOLATILE; |
| 860 | } |
| 861 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 862 | ret = snd_soc_add_card_controls(dsp->card, |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 863 | kcontrol, 1); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 864 | if (ret < 0) |
| 865 | goto err_kcontrol; |
| 866 | |
| 867 | kfree(kcontrol); |
| 868 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 869 | ctl->kcontrol = snd_soc_card_get_kcontrol(dsp->card, |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 870 | ctl->name); |
| 871 | |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 872 | return 0; |
| 873 | |
| 874 | err_kcontrol: |
| 875 | kfree(kcontrol); |
| 876 | return ret; |
| 877 | } |
| 878 | |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 879 | static int wm_coeff_init_control_caches(struct wm_adsp *dsp) |
| 880 | { |
| 881 | struct wm_coeff_ctl *ctl; |
| 882 | int ret; |
| 883 | |
| 884 | list_for_each_entry(ctl, &dsp->ctl_list, list) { |
| 885 | if (!ctl->enabled || ctl->set) |
| 886 | continue; |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 887 | if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) |
| 888 | continue; |
| 889 | |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 890 | ret = wm_coeff_read_control(ctl, |
| 891 | ctl->cache, |
| 892 | ctl->len); |
| 893 | if (ret < 0) |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | static int wm_coeff_sync_controls(struct wm_adsp *dsp) |
| 901 | { |
| 902 | struct wm_coeff_ctl *ctl; |
| 903 | int ret; |
| 904 | |
| 905 | list_for_each_entry(ctl, &dsp->ctl_list, list) { |
| 906 | if (!ctl->enabled) |
| 907 | continue; |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 908 | if (ctl->set && !(ctl->flags & WMFW_CTL_FLAG_VOLATILE)) { |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 909 | ret = wm_coeff_write_control(ctl, |
| 910 | ctl->cache, |
| 911 | ctl->len); |
| 912 | if (ret < 0) |
| 913 | return ret; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | static void wm_adsp_ctl_work(struct work_struct *work) |
| 921 | { |
| 922 | struct wmfw_ctl_work *ctl_work = container_of(work, |
| 923 | struct wmfw_ctl_work, |
| 924 | work); |
| 925 | |
| 926 | wmfw_add_ctl(ctl_work->dsp, ctl_work->ctl); |
| 927 | kfree(ctl_work); |
| 928 | } |
| 929 | |
| 930 | static int wm_adsp_create_control(struct wm_adsp *dsp, |
| 931 | const struct wm_adsp_alg_region *alg_region, |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 932 | unsigned int offset, unsigned int len, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 933 | const char *subname, unsigned int subname_len, |
| 934 | unsigned int flags) |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 935 | { |
| 936 | struct wm_coeff_ctl *ctl; |
| 937 | struct wmfw_ctl_work *ctl_work; |
| 938 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
| 939 | char *region_name; |
| 940 | int ret; |
| 941 | |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 942 | if (flags & WMFW_CTL_FLAG_SYS) |
| 943 | return 0; |
| 944 | |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 945 | switch (alg_region->type) { |
| 946 | case WMFW_ADSP1_PM: |
| 947 | region_name = "PM"; |
| 948 | break; |
| 949 | case WMFW_ADSP1_DM: |
| 950 | region_name = "DM"; |
| 951 | break; |
| 952 | case WMFW_ADSP2_XM: |
| 953 | region_name = "XM"; |
| 954 | break; |
| 955 | case WMFW_ADSP2_YM: |
| 956 | region_name = "YM"; |
| 957 | break; |
| 958 | case WMFW_ADSP1_ZM: |
| 959 | region_name = "ZM"; |
| 960 | break; |
| 961 | default: |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 962 | adsp_err(dsp, "Unknown region type: %d\n", alg_region->type); |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 963 | return -EINVAL; |
| 964 | } |
| 965 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 966 | switch (dsp->fw_ver) { |
| 967 | case 0: |
| 968 | case 1: |
| 969 | snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "DSP%d %s %x", |
| 970 | dsp->num, region_name, alg_region->alg); |
| 971 | break; |
| 972 | default: |
| 973 | ret = snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, |
| 974 | "DSP%d%c %.12s %x", dsp->num, *region_name, |
| 975 | wm_adsp_fw_text[dsp->fw], alg_region->alg); |
| 976 | |
| 977 | /* Truncate the subname from the start if it is too long */ |
| 978 | if (subname) { |
| 979 | int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2; |
| 980 | int skip = 0; |
| 981 | |
| 982 | if (subname_len > avail) |
| 983 | skip = subname_len - avail; |
| 984 | |
| 985 | snprintf(name + ret, |
| 986 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret, " %.*s", |
| 987 | subname_len - skip, subname + skip); |
| 988 | } |
| 989 | break; |
| 990 | } |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 991 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 992 | list_for_each_entry(ctl, &dsp->ctl_list, list) { |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 993 | if (!strcmp(ctl->name, name)) { |
| 994 | if (!ctl->enabled) |
| 995 | ctl->enabled = 1; |
| 996 | return 0; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); |
| 1001 | if (!ctl) |
| 1002 | return -ENOMEM; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1003 | ctl->fw_name = wm_adsp_fw_text[dsp->fw]; |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 1004 | ctl->alg_region = *alg_region; |
| 1005 | ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL); |
| 1006 | if (!ctl->name) { |
| 1007 | ret = -ENOMEM; |
| 1008 | goto err_ctl; |
| 1009 | } |
| 1010 | ctl->enabled = 1; |
| 1011 | ctl->set = 0; |
| 1012 | ctl->ops.xget = wm_coeff_get; |
| 1013 | ctl->ops.xput = wm_coeff_put; |
| 1014 | ctl->dsp = dsp; |
| 1015 | |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1016 | ctl->flags = flags; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1017 | ctl->offset = offset; |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 1018 | if (len > 512) { |
| 1019 | adsp_warn(dsp, "Truncating control %s from %d\n", |
| 1020 | ctl->name, len); |
| 1021 | len = 512; |
| 1022 | } |
| 1023 | ctl->len = len; |
| 1024 | ctl->cache = kzalloc(ctl->len, GFP_KERNEL); |
| 1025 | if (!ctl->cache) { |
| 1026 | ret = -ENOMEM; |
| 1027 | goto err_ctl_name; |
| 1028 | } |
| 1029 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1030 | list_add(&ctl->list, &dsp->ctl_list); |
| 1031 | |
Charles Keepax | b21acc1 | 2015-04-13 13:28:01 +0100 | [diff] [blame] | 1032 | ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL); |
| 1033 | if (!ctl_work) { |
| 1034 | ret = -ENOMEM; |
| 1035 | goto err_ctl_cache; |
| 1036 | } |
| 1037 | |
| 1038 | ctl_work->dsp = dsp; |
| 1039 | ctl_work->ctl = ctl; |
| 1040 | INIT_WORK(&ctl_work->work, wm_adsp_ctl_work); |
| 1041 | schedule_work(&ctl_work->work); |
| 1042 | |
| 1043 | return 0; |
| 1044 | |
| 1045 | err_ctl_cache: |
| 1046 | kfree(ctl->cache); |
| 1047 | err_ctl_name: |
| 1048 | kfree(ctl->name); |
| 1049 | err_ctl: |
| 1050 | kfree(ctl); |
| 1051 | |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1055 | struct wm_coeff_parsed_alg { |
| 1056 | int id; |
| 1057 | const u8 *name; |
| 1058 | int name_len; |
| 1059 | int ncoeff; |
| 1060 | }; |
| 1061 | |
| 1062 | struct wm_coeff_parsed_coeff { |
| 1063 | int offset; |
| 1064 | int mem_type; |
| 1065 | const u8 *name; |
| 1066 | int name_len; |
| 1067 | int ctl_type; |
| 1068 | int flags; |
| 1069 | int len; |
| 1070 | }; |
| 1071 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1072 | static int wm_coeff_parse_string(int bytes, const u8 **pos, const u8 **str) |
| 1073 | { |
| 1074 | int length; |
| 1075 | |
| 1076 | switch (bytes) { |
| 1077 | case 1: |
| 1078 | length = **pos; |
| 1079 | break; |
| 1080 | case 2: |
Charles Keepax | 8299ee8 | 2015-04-20 13:52:44 +0100 | [diff] [blame] | 1081 | length = le16_to_cpu(*((__le16 *)*pos)); |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1082 | break; |
| 1083 | default: |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | if (str) |
| 1088 | *str = *pos + bytes; |
| 1089 | |
| 1090 | *pos += ((length + bytes) + 3) & ~0x03; |
| 1091 | |
| 1092 | return length; |
| 1093 | } |
| 1094 | |
| 1095 | static int wm_coeff_parse_int(int bytes, const u8 **pos) |
| 1096 | { |
| 1097 | int val = 0; |
| 1098 | |
| 1099 | switch (bytes) { |
| 1100 | case 2: |
Charles Keepax | 8299ee8 | 2015-04-20 13:52:44 +0100 | [diff] [blame] | 1101 | val = le16_to_cpu(*((__le16 *)*pos)); |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1102 | break; |
| 1103 | case 4: |
Charles Keepax | 8299ee8 | 2015-04-20 13:52:44 +0100 | [diff] [blame] | 1104 | val = le32_to_cpu(*((__le32 *)*pos)); |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1105 | break; |
| 1106 | default: |
| 1107 | break; |
| 1108 | } |
| 1109 | |
| 1110 | *pos += bytes; |
| 1111 | |
| 1112 | return val; |
| 1113 | } |
| 1114 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1115 | static inline void wm_coeff_parse_alg(struct wm_adsp *dsp, const u8 **data, |
| 1116 | struct wm_coeff_parsed_alg *blk) |
| 1117 | { |
| 1118 | const struct wmfw_adsp_alg_data *raw; |
| 1119 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1120 | switch (dsp->fw_ver) { |
| 1121 | case 0: |
| 1122 | case 1: |
| 1123 | raw = (const struct wmfw_adsp_alg_data *)*data; |
| 1124 | *data = raw->data; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1125 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1126 | blk->id = le32_to_cpu(raw->id); |
| 1127 | blk->name = raw->name; |
| 1128 | blk->name_len = strlen(raw->name); |
| 1129 | blk->ncoeff = le32_to_cpu(raw->ncoeff); |
| 1130 | break; |
| 1131 | default: |
| 1132 | blk->id = wm_coeff_parse_int(sizeof(raw->id), data); |
| 1133 | blk->name_len = wm_coeff_parse_string(sizeof(u8), data, |
| 1134 | &blk->name); |
| 1135 | wm_coeff_parse_string(sizeof(u16), data, NULL); |
| 1136 | blk->ncoeff = wm_coeff_parse_int(sizeof(raw->ncoeff), data); |
| 1137 | break; |
| 1138 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1139 | |
| 1140 | adsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id); |
| 1141 | adsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name); |
| 1142 | adsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff); |
| 1143 | } |
| 1144 | |
| 1145 | static inline void wm_coeff_parse_coeff(struct wm_adsp *dsp, const u8 **data, |
| 1146 | struct wm_coeff_parsed_coeff *blk) |
| 1147 | { |
| 1148 | const struct wmfw_adsp_coeff_data *raw; |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1149 | const u8 *tmp; |
| 1150 | int length; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1151 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1152 | switch (dsp->fw_ver) { |
| 1153 | case 0: |
| 1154 | case 1: |
| 1155 | raw = (const struct wmfw_adsp_coeff_data *)*data; |
| 1156 | *data = *data + sizeof(raw->hdr) + le32_to_cpu(raw->hdr.size); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1157 | |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1158 | blk->offset = le16_to_cpu(raw->hdr.offset); |
| 1159 | blk->mem_type = le16_to_cpu(raw->hdr.type); |
| 1160 | blk->name = raw->name; |
| 1161 | blk->name_len = strlen(raw->name); |
| 1162 | blk->ctl_type = le16_to_cpu(raw->ctl_type); |
| 1163 | blk->flags = le16_to_cpu(raw->flags); |
| 1164 | blk->len = le32_to_cpu(raw->len); |
| 1165 | break; |
| 1166 | default: |
| 1167 | tmp = *data; |
| 1168 | blk->offset = wm_coeff_parse_int(sizeof(raw->hdr.offset), &tmp); |
| 1169 | blk->mem_type = wm_coeff_parse_int(sizeof(raw->hdr.type), &tmp); |
| 1170 | length = wm_coeff_parse_int(sizeof(raw->hdr.size), &tmp); |
| 1171 | blk->name_len = wm_coeff_parse_string(sizeof(u8), &tmp, |
| 1172 | &blk->name); |
| 1173 | wm_coeff_parse_string(sizeof(u8), &tmp, NULL); |
| 1174 | wm_coeff_parse_string(sizeof(u16), &tmp, NULL); |
| 1175 | blk->ctl_type = wm_coeff_parse_int(sizeof(raw->ctl_type), &tmp); |
| 1176 | blk->flags = wm_coeff_parse_int(sizeof(raw->flags), &tmp); |
| 1177 | blk->len = wm_coeff_parse_int(sizeof(raw->len), &tmp); |
| 1178 | |
| 1179 | *data = *data + sizeof(raw->hdr) + length; |
| 1180 | break; |
| 1181 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1182 | |
| 1183 | adsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type); |
| 1184 | adsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset); |
| 1185 | adsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name); |
| 1186 | adsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags); |
| 1187 | adsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type); |
| 1188 | adsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len); |
| 1189 | } |
| 1190 | |
| 1191 | static int wm_adsp_parse_coeff(struct wm_adsp *dsp, |
| 1192 | const struct wmfw_region *region) |
| 1193 | { |
| 1194 | struct wm_adsp_alg_region alg_region = {}; |
| 1195 | struct wm_coeff_parsed_alg alg_blk; |
| 1196 | struct wm_coeff_parsed_coeff coeff_blk; |
| 1197 | const u8 *data = region->data; |
| 1198 | int i, ret; |
| 1199 | |
| 1200 | wm_coeff_parse_alg(dsp, &data, &alg_blk); |
| 1201 | for (i = 0; i < alg_blk.ncoeff; i++) { |
| 1202 | wm_coeff_parse_coeff(dsp, &data, &coeff_blk); |
| 1203 | |
| 1204 | switch (coeff_blk.ctl_type) { |
| 1205 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 1206 | break; |
| 1207 | default: |
| 1208 | adsp_err(dsp, "Unknown control type: %d\n", |
| 1209 | coeff_blk.ctl_type); |
| 1210 | return -EINVAL; |
| 1211 | } |
| 1212 | |
| 1213 | alg_region.type = coeff_blk.mem_type; |
| 1214 | alg_region.alg = alg_blk.id; |
| 1215 | |
| 1216 | ret = wm_adsp_create_control(dsp, &alg_region, |
| 1217 | coeff_blk.offset, |
| 1218 | coeff_blk.len, |
| 1219 | coeff_blk.name, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1220 | coeff_blk.name_len, |
| 1221 | coeff_blk.flags); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1222 | if (ret < 0) |
| 1223 | adsp_err(dsp, "Failed to create control: %.*s, %d\n", |
| 1224 | coeff_blk.name_len, coeff_blk.name, ret); |
| 1225 | } |
| 1226 | |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1230 | static int wm_adsp_load(struct wm_adsp *dsp) |
| 1231 | { |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1232 | LIST_HEAD(buf_list); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1233 | const struct firmware *firmware; |
| 1234 | struct regmap *regmap = dsp->regmap; |
| 1235 | unsigned int pos = 0; |
| 1236 | const struct wmfw_header *header; |
| 1237 | const struct wmfw_adsp1_sizes *adsp1_sizes; |
| 1238 | const struct wmfw_adsp2_sizes *adsp2_sizes; |
| 1239 | const struct wmfw_footer *footer; |
| 1240 | const struct wmfw_region *region; |
| 1241 | const struct wm_adsp_region *mem; |
| 1242 | const char *region_name; |
| 1243 | char *file, *text; |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1244 | struct wm_adsp_buf *buf; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1245 | unsigned int reg; |
| 1246 | int regions = 0; |
| 1247 | int ret, offset, type, sizes; |
| 1248 | |
| 1249 | file = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 1250 | if (file == NULL) |
| 1251 | return -ENOMEM; |
| 1252 | |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 1253 | snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.wmfw", dsp->part, dsp->num, |
| 1254 | wm_adsp_fw[dsp->fw].file); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1255 | file[PAGE_SIZE - 1] = '\0'; |
| 1256 | |
| 1257 | ret = request_firmware(&firmware, file, dsp->dev); |
| 1258 | if (ret != 0) { |
| 1259 | adsp_err(dsp, "Failed to request '%s'\n", file); |
| 1260 | goto out; |
| 1261 | } |
| 1262 | ret = -EINVAL; |
| 1263 | |
| 1264 | pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer); |
| 1265 | if (pos >= firmware->size) { |
| 1266 | adsp_err(dsp, "%s: file too short, %zu bytes\n", |
| 1267 | file, firmware->size); |
| 1268 | goto out_fw; |
| 1269 | } |
| 1270 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1271 | header = (void *)&firmware->data[0]; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1272 | |
| 1273 | if (memcmp(&header->magic[0], "WMFW", 4) != 0) { |
| 1274 | adsp_err(dsp, "%s: invalid magic\n", file); |
| 1275 | goto out_fw; |
| 1276 | } |
| 1277 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1278 | switch (header->ver) { |
| 1279 | case 0: |
Charles Keepax | c61e59f | 2015-04-13 13:28:05 +0100 | [diff] [blame] | 1280 | adsp_warn(dsp, "%s: Depreciated file format %d\n", |
| 1281 | file, header->ver); |
| 1282 | break; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1283 | case 1: |
Charles Keepax | cb5b57a | 2015-04-13 13:28:04 +0100 | [diff] [blame] | 1284 | case 2: |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1285 | break; |
| 1286 | default: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1287 | adsp_err(dsp, "%s: unknown file format %d\n", |
| 1288 | file, header->ver); |
| 1289 | goto out_fw; |
| 1290 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1291 | |
Dimitris Papastamos | 3626992 | 2013-11-01 15:56:57 +0000 | [diff] [blame] | 1292 | adsp_info(dsp, "Firmware version: %d\n", header->ver); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1293 | dsp->fw_ver = header->ver; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1294 | |
| 1295 | if (header->core != dsp->type) { |
| 1296 | adsp_err(dsp, "%s: invalid core %d != %d\n", |
| 1297 | file, header->core, dsp->type); |
| 1298 | goto out_fw; |
| 1299 | } |
| 1300 | |
| 1301 | switch (dsp->type) { |
| 1302 | case WMFW_ADSP1: |
| 1303 | pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer); |
| 1304 | adsp1_sizes = (void *)&(header[1]); |
| 1305 | footer = (void *)&(adsp1_sizes[1]); |
| 1306 | sizes = sizeof(*adsp1_sizes); |
| 1307 | |
| 1308 | adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n", |
| 1309 | file, le32_to_cpu(adsp1_sizes->dm), |
| 1310 | le32_to_cpu(adsp1_sizes->pm), |
| 1311 | le32_to_cpu(adsp1_sizes->zm)); |
| 1312 | break; |
| 1313 | |
| 1314 | case WMFW_ADSP2: |
| 1315 | pos = sizeof(*header) + sizeof(*adsp2_sizes) + sizeof(*footer); |
| 1316 | adsp2_sizes = (void *)&(header[1]); |
| 1317 | footer = (void *)&(adsp2_sizes[1]); |
| 1318 | sizes = sizeof(*adsp2_sizes); |
| 1319 | |
| 1320 | adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n", |
| 1321 | file, le32_to_cpu(adsp2_sizes->xm), |
| 1322 | le32_to_cpu(adsp2_sizes->ym), |
| 1323 | le32_to_cpu(adsp2_sizes->pm), |
| 1324 | le32_to_cpu(adsp2_sizes->zm)); |
| 1325 | break; |
| 1326 | |
| 1327 | default: |
Takashi Iwai | 6c452bd | 2013-11-05 18:40:00 +0100 | [diff] [blame] | 1328 | WARN(1, "Unknown DSP type"); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1329 | goto out_fw; |
| 1330 | } |
| 1331 | |
| 1332 | if (le32_to_cpu(header->len) != sizeof(*header) + |
| 1333 | sizes + sizeof(*footer)) { |
| 1334 | adsp_err(dsp, "%s: unexpected header length %d\n", |
| 1335 | file, le32_to_cpu(header->len)); |
| 1336 | goto out_fw; |
| 1337 | } |
| 1338 | |
| 1339 | adsp_dbg(dsp, "%s: timestamp %llu\n", file, |
| 1340 | le64_to_cpu(footer->timestamp)); |
| 1341 | |
| 1342 | while (pos < firmware->size && |
| 1343 | pos - firmware->size > sizeof(*region)) { |
| 1344 | region = (void *)&(firmware->data[pos]); |
| 1345 | region_name = "Unknown"; |
| 1346 | reg = 0; |
| 1347 | text = NULL; |
| 1348 | offset = le32_to_cpu(region->offset) & 0xffffff; |
| 1349 | type = be32_to_cpu(region->type) & 0xff; |
| 1350 | mem = wm_adsp_find_region(dsp, type); |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1351 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1352 | switch (type) { |
| 1353 | case WMFW_NAME_TEXT: |
| 1354 | region_name = "Firmware name"; |
| 1355 | text = kzalloc(le32_to_cpu(region->len) + 1, |
| 1356 | GFP_KERNEL); |
| 1357 | break; |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1358 | case WMFW_ALGORITHM_DATA: |
| 1359 | region_name = "Algorithm"; |
| 1360 | ret = wm_adsp_parse_coeff(dsp, region); |
| 1361 | if (ret != 0) |
| 1362 | goto out_fw; |
| 1363 | break; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1364 | case WMFW_INFO_TEXT: |
| 1365 | region_name = "Information"; |
| 1366 | text = kzalloc(le32_to_cpu(region->len) + 1, |
| 1367 | GFP_KERNEL); |
| 1368 | break; |
| 1369 | case WMFW_ABSOLUTE: |
| 1370 | region_name = "Absolute"; |
| 1371 | reg = offset; |
| 1372 | break; |
| 1373 | case WMFW_ADSP1_PM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1374 | region_name = "PM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1375 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1376 | break; |
| 1377 | case WMFW_ADSP1_DM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1378 | region_name = "DM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1379 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1380 | break; |
| 1381 | case WMFW_ADSP2_XM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1382 | region_name = "XM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1383 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1384 | break; |
| 1385 | case WMFW_ADSP2_YM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1386 | region_name = "YM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1387 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1388 | break; |
| 1389 | case WMFW_ADSP1_ZM: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1390 | region_name = "ZM"; |
Mark Brown | 45b9ee7 | 2013-01-08 16:02:06 +0000 | [diff] [blame] | 1391 | reg = wm_adsp_region_to_reg(mem, offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1392 | break; |
| 1393 | default: |
| 1394 | adsp_warn(dsp, |
| 1395 | "%s.%d: Unknown region type %x at %d(%x)\n", |
| 1396 | file, regions, type, pos, pos); |
| 1397 | break; |
| 1398 | } |
| 1399 | |
| 1400 | adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file, |
| 1401 | regions, le32_to_cpu(region->len), offset, |
| 1402 | region_name); |
| 1403 | |
| 1404 | if (text) { |
| 1405 | memcpy(text, region->data, le32_to_cpu(region->len)); |
| 1406 | adsp_info(dsp, "%s: %s\n", file, text); |
| 1407 | kfree(text); |
| 1408 | } |
| 1409 | |
| 1410 | if (reg) { |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 1411 | buf = wm_adsp_buf_alloc(region->data, |
| 1412 | le32_to_cpu(region->len), |
| 1413 | &buf_list); |
| 1414 | if (!buf) { |
| 1415 | adsp_err(dsp, "Out of memory\n"); |
| 1416 | ret = -ENOMEM; |
| 1417 | goto out_fw; |
| 1418 | } |
Mark Brown | a76fefa | 2013-01-07 19:03:17 +0000 | [diff] [blame] | 1419 | |
Charles Keepax | cdcd7f7 | 2014-11-14 15:40:45 +0000 | [diff] [blame] | 1420 | ret = regmap_raw_write_async(regmap, reg, buf->buf, |
| 1421 | le32_to_cpu(region->len)); |
| 1422 | if (ret != 0) { |
| 1423 | adsp_err(dsp, |
| 1424 | "%s.%d: Failed to write %d bytes at %d in %s: %d\n", |
| 1425 | file, regions, |
| 1426 | le32_to_cpu(region->len), offset, |
| 1427 | region_name, ret); |
| 1428 | goto out_fw; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | pos += le32_to_cpu(region->len) + sizeof(*region); |
| 1433 | regions++; |
| 1434 | } |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1435 | |
| 1436 | ret = regmap_async_complete(regmap); |
| 1437 | if (ret != 0) { |
| 1438 | adsp_err(dsp, "Failed to complete async write: %d\n", ret); |
| 1439 | goto out_fw; |
| 1440 | } |
| 1441 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1442 | if (pos > firmware->size) |
| 1443 | adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n", |
| 1444 | file, regions, pos - firmware->size); |
| 1445 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 1446 | wm_adsp_debugfs_save_wmfwname(dsp, file); |
| 1447 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1448 | out_fw: |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1449 | regmap_async_complete(regmap); |
| 1450 | wm_adsp_buf_free(&buf_list); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1451 | release_firmware(firmware); |
| 1452 | out: |
| 1453 | kfree(file); |
| 1454 | |
| 1455 | return ret; |
| 1456 | } |
| 1457 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1458 | static void wm_adsp_ctl_fixup_base(struct wm_adsp *dsp, |
| 1459 | const struct wm_adsp_alg_region *alg_region) |
| 1460 | { |
| 1461 | struct wm_coeff_ctl *ctl; |
| 1462 | |
| 1463 | list_for_each_entry(ctl, &dsp->ctl_list, list) { |
| 1464 | if (ctl->fw_name == wm_adsp_fw_text[dsp->fw] && |
| 1465 | alg_region->alg == ctl->alg_region.alg && |
| 1466 | alg_region->type == ctl->alg_region.type) { |
| 1467 | ctl->alg_region.base = alg_region->base; |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1472 | static void *wm_adsp_read_algs(struct wm_adsp *dsp, size_t n_algs, |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1473 | unsigned int pos, unsigned int len) |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1474 | { |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1475 | void *alg; |
| 1476 | int ret; |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1477 | __be32 val; |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1478 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1479 | if (n_algs == 0) { |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1480 | adsp_err(dsp, "No algorithms\n"); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1481 | return ERR_PTR(-EINVAL); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1482 | } |
| 1483 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1484 | if (n_algs > 1024) { |
| 1485 | adsp_err(dsp, "Algorithm count %zx excessive\n", n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1486 | return ERR_PTR(-EINVAL); |
Mark Brown | d62f4bc | 2012-12-19 14:00:30 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1489 | /* Read the terminator first to validate the length */ |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1490 | ret = regmap_raw_read(dsp->regmap, pos + len, &val, sizeof(val)); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1491 | if (ret != 0) { |
| 1492 | adsp_err(dsp, "Failed to read algorithm list end: %d\n", |
| 1493 | ret); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1494 | return ERR_PTR(ret); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | if (be32_to_cpu(val) != 0xbedead) |
| 1498 | adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbeadead\n", |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1499 | pos + len, be32_to_cpu(val)); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1500 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1501 | alg = kzalloc(len * 2, GFP_KERNEL | GFP_DMA); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1502 | if (!alg) |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1503 | return ERR_PTR(-ENOMEM); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1504 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1505 | ret = regmap_raw_read(dsp->regmap, pos, alg, len * 2); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1506 | if (ret != 0) { |
| 1507 | adsp_err(dsp, "Failed to read algorithm list: %d\n", |
| 1508 | ret); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1509 | kfree(alg); |
| 1510 | return ERR_PTR(ret); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1511 | } |
| 1512 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1513 | return alg; |
| 1514 | } |
| 1515 | |
Charles Keepax | 1419709 | 2015-12-15 11:29:43 +0000 | [diff] [blame] | 1516 | static struct wm_adsp_alg_region * |
| 1517 | wm_adsp_find_alg_region(struct wm_adsp *dsp, int type, unsigned int id) |
| 1518 | { |
| 1519 | struct wm_adsp_alg_region *alg_region; |
| 1520 | |
| 1521 | list_for_each_entry(alg_region, &dsp->alg_regions, list) { |
| 1522 | if (id == alg_region->alg && type == alg_region->type) |
| 1523 | return alg_region; |
| 1524 | } |
| 1525 | |
| 1526 | return NULL; |
| 1527 | } |
| 1528 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1529 | static struct wm_adsp_alg_region *wm_adsp_create_region(struct wm_adsp *dsp, |
| 1530 | int type, __be32 id, |
| 1531 | __be32 base) |
| 1532 | { |
| 1533 | struct wm_adsp_alg_region *alg_region; |
| 1534 | |
| 1535 | alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL); |
| 1536 | if (!alg_region) |
| 1537 | return ERR_PTR(-ENOMEM); |
| 1538 | |
| 1539 | alg_region->type = type; |
| 1540 | alg_region->alg = be32_to_cpu(id); |
| 1541 | alg_region->base = be32_to_cpu(base); |
| 1542 | |
| 1543 | list_add_tail(&alg_region->list, &dsp->alg_regions); |
| 1544 | |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1545 | if (dsp->fw_ver > 0) |
| 1546 | wm_adsp_ctl_fixup_base(dsp, alg_region); |
| 1547 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1548 | return alg_region; |
| 1549 | } |
| 1550 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1551 | static int wm_adsp1_setup_algs(struct wm_adsp *dsp) |
| 1552 | { |
| 1553 | struct wmfw_adsp1_id_hdr adsp1_id; |
| 1554 | struct wmfw_adsp1_alg_hdr *adsp1_alg; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1555 | struct wm_adsp_alg_region *alg_region; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1556 | const struct wm_adsp_region *mem; |
| 1557 | unsigned int pos, len; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1558 | size_t n_algs; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1559 | int i, ret; |
| 1560 | |
| 1561 | mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM); |
| 1562 | if (WARN_ON(!mem)) |
| 1563 | return -EINVAL; |
| 1564 | |
| 1565 | ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id, |
| 1566 | sizeof(adsp1_id)); |
| 1567 | if (ret != 0) { |
| 1568 | adsp_err(dsp, "Failed to read algorithm info: %d\n", |
| 1569 | ret); |
| 1570 | return ret; |
| 1571 | } |
| 1572 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1573 | n_algs = be32_to_cpu(adsp1_id.n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1574 | dsp->fw_id = be32_to_cpu(adsp1_id.fw.id); |
| 1575 | adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n", |
| 1576 | dsp->fw_id, |
| 1577 | (be32_to_cpu(adsp1_id.fw.ver) & 0xff0000) >> 16, |
| 1578 | (be32_to_cpu(adsp1_id.fw.ver) & 0xff00) >> 8, |
| 1579 | be32_to_cpu(adsp1_id.fw.ver) & 0xff, |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1580 | n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1581 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1582 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM, |
| 1583 | adsp1_id.fw.id, adsp1_id.zm); |
| 1584 | if (IS_ERR(alg_region)) |
| 1585 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1586 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1587 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM, |
| 1588 | adsp1_id.fw.id, adsp1_id.dm); |
| 1589 | if (IS_ERR(alg_region)) |
| 1590 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1591 | |
| 1592 | pos = sizeof(adsp1_id) / 2; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1593 | len = (sizeof(*adsp1_alg) * n_algs) / 2; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1594 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1595 | adsp1_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1596 | if (IS_ERR(adsp1_alg)) |
| 1597 | return PTR_ERR(adsp1_alg); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1598 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1599 | for (i = 0; i < n_algs; i++) { |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1600 | adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n", |
| 1601 | i, be32_to_cpu(adsp1_alg[i].alg.id), |
| 1602 | (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16, |
| 1603 | (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8, |
| 1604 | be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff, |
| 1605 | be32_to_cpu(adsp1_alg[i].dm), |
| 1606 | be32_to_cpu(adsp1_alg[i].zm)); |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1607 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1608 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM, |
| 1609 | adsp1_alg[i].alg.id, |
| 1610 | adsp1_alg[i].dm); |
| 1611 | if (IS_ERR(alg_region)) { |
| 1612 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1613 | goto out; |
| 1614 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1615 | if (dsp->fw_ver == 0) { |
| 1616 | if (i + 1 < n_algs) { |
| 1617 | len = be32_to_cpu(adsp1_alg[i + 1].dm); |
| 1618 | len -= be32_to_cpu(adsp1_alg[i].dm); |
| 1619 | len *= 4; |
| 1620 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1621 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1622 | } else { |
| 1623 | adsp_warn(dsp, "Missing length info for region DM with ID %x\n", |
| 1624 | be32_to_cpu(adsp1_alg[i].alg.id)); |
| 1625 | } |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1626 | } |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1627 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1628 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM, |
| 1629 | adsp1_alg[i].alg.id, |
| 1630 | adsp1_alg[i].zm); |
| 1631 | if (IS_ERR(alg_region)) { |
| 1632 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1633 | goto out; |
| 1634 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1635 | if (dsp->fw_ver == 0) { |
| 1636 | if (i + 1 < n_algs) { |
| 1637 | len = be32_to_cpu(adsp1_alg[i + 1].zm); |
| 1638 | len -= be32_to_cpu(adsp1_alg[i].zm); |
| 1639 | len *= 4; |
| 1640 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1641 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1642 | } else { |
| 1643 | adsp_warn(dsp, "Missing length info for region ZM with ID %x\n", |
| 1644 | be32_to_cpu(adsp1_alg[i].alg.id)); |
| 1645 | } |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | out: |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1650 | kfree(adsp1_alg); |
| 1651 | return ret; |
| 1652 | } |
| 1653 | |
| 1654 | static int wm_adsp2_setup_algs(struct wm_adsp *dsp) |
| 1655 | { |
| 1656 | struct wmfw_adsp2_id_hdr adsp2_id; |
| 1657 | struct wmfw_adsp2_alg_hdr *adsp2_alg; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1658 | struct wm_adsp_alg_region *alg_region; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1659 | const struct wm_adsp_region *mem; |
| 1660 | unsigned int pos, len; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1661 | size_t n_algs; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1662 | int i, ret; |
| 1663 | |
| 1664 | mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM); |
| 1665 | if (WARN_ON(!mem)) |
| 1666 | return -EINVAL; |
| 1667 | |
| 1668 | ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id, |
| 1669 | sizeof(adsp2_id)); |
| 1670 | if (ret != 0) { |
| 1671 | adsp_err(dsp, "Failed to read algorithm info: %d\n", |
| 1672 | ret); |
| 1673 | return ret; |
| 1674 | } |
| 1675 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1676 | n_algs = be32_to_cpu(adsp2_id.n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1677 | dsp->fw_id = be32_to_cpu(adsp2_id.fw.id); |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 1678 | dsp->fw_id_version = be32_to_cpu(adsp2_id.fw.ver); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1679 | adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n", |
| 1680 | dsp->fw_id, |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 1681 | (dsp->fw_id_version & 0xff0000) >> 16, |
| 1682 | (dsp->fw_id_version & 0xff00) >> 8, |
| 1683 | dsp->fw_id_version & 0xff, |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1684 | n_algs); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1685 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1686 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM, |
| 1687 | adsp2_id.fw.id, adsp2_id.xm); |
| 1688 | if (IS_ERR(alg_region)) |
| 1689 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1690 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1691 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM, |
| 1692 | adsp2_id.fw.id, adsp2_id.ym); |
| 1693 | if (IS_ERR(alg_region)) |
| 1694 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1695 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1696 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM, |
| 1697 | adsp2_id.fw.id, adsp2_id.zm); |
| 1698 | if (IS_ERR(alg_region)) |
| 1699 | return PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1700 | |
| 1701 | pos = sizeof(adsp2_id) / 2; |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1702 | len = (sizeof(*adsp2_alg) * n_algs) / 2; |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1703 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1704 | adsp2_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1705 | if (IS_ERR(adsp2_alg)) |
| 1706 | return PTR_ERR(adsp2_alg); |
| 1707 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1708 | for (i = 0; i < n_algs; i++) { |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1709 | adsp_info(dsp, |
| 1710 | "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n", |
| 1711 | i, be32_to_cpu(adsp2_alg[i].alg.id), |
| 1712 | (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16, |
| 1713 | (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8, |
| 1714 | be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff, |
| 1715 | be32_to_cpu(adsp2_alg[i].xm), |
| 1716 | be32_to_cpu(adsp2_alg[i].ym), |
| 1717 | be32_to_cpu(adsp2_alg[i].zm)); |
| 1718 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1719 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM, |
| 1720 | adsp2_alg[i].alg.id, |
| 1721 | adsp2_alg[i].xm); |
| 1722 | if (IS_ERR(alg_region)) { |
| 1723 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1724 | goto out; |
| 1725 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1726 | if (dsp->fw_ver == 0) { |
| 1727 | if (i + 1 < n_algs) { |
| 1728 | len = be32_to_cpu(adsp2_alg[i + 1].xm); |
| 1729 | len -= be32_to_cpu(adsp2_alg[i].xm); |
| 1730 | len *= 4; |
| 1731 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1732 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1733 | } else { |
| 1734 | adsp_warn(dsp, "Missing length info for region XM with ID %x\n", |
| 1735 | be32_to_cpu(adsp2_alg[i].alg.id)); |
| 1736 | } |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1739 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM, |
| 1740 | adsp2_alg[i].alg.id, |
| 1741 | adsp2_alg[i].ym); |
| 1742 | if (IS_ERR(alg_region)) { |
| 1743 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1744 | goto out; |
| 1745 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1746 | if (dsp->fw_ver == 0) { |
| 1747 | if (i + 1 < n_algs) { |
| 1748 | len = be32_to_cpu(adsp2_alg[i + 1].ym); |
| 1749 | len -= be32_to_cpu(adsp2_alg[i].ym); |
| 1750 | len *= 4; |
| 1751 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1752 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1753 | } else { |
| 1754 | adsp_warn(dsp, "Missing length info for region YM with ID %x\n", |
| 1755 | be32_to_cpu(adsp2_alg[i].alg.id)); |
| 1756 | } |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1757 | } |
| 1758 | |
Charles Keepax | d9d20e1 | 2015-04-13 13:27:59 +0100 | [diff] [blame] | 1759 | alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM, |
| 1760 | adsp2_alg[i].alg.id, |
| 1761 | adsp2_alg[i].zm); |
| 1762 | if (IS_ERR(alg_region)) { |
| 1763 | ret = PTR_ERR(alg_region); |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1764 | goto out; |
| 1765 | } |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1766 | if (dsp->fw_ver == 0) { |
| 1767 | if (i + 1 < n_algs) { |
| 1768 | len = be32_to_cpu(adsp2_alg[i + 1].zm); |
| 1769 | len -= be32_to_cpu(adsp2_alg[i].zm); |
| 1770 | len *= 4; |
| 1771 | wm_adsp_create_control(dsp, alg_region, 0, |
Charles Keepax | 26c22a1 | 2015-04-20 13:52:45 +0100 | [diff] [blame] | 1772 | len, NULL, 0, 0); |
Charles Keepax | 2323736 | 2015-04-13 13:28:02 +0100 | [diff] [blame] | 1773 | } else { |
| 1774 | adsp_warn(dsp, "Missing length info for region ZM with ID %x\n", |
| 1775 | be32_to_cpu(adsp2_alg[i].alg.id)); |
| 1776 | } |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | out: |
| 1781 | kfree(adsp2_alg); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 1782 | return ret; |
| 1783 | } |
| 1784 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1785 | static int wm_adsp_load_coeff(struct wm_adsp *dsp) |
| 1786 | { |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1787 | LIST_HEAD(buf_list); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1788 | struct regmap *regmap = dsp->regmap; |
| 1789 | struct wmfw_coeff_hdr *hdr; |
| 1790 | struct wmfw_coeff_item *blk; |
| 1791 | const struct firmware *firmware; |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1792 | const struct wm_adsp_region *mem; |
| 1793 | struct wm_adsp_alg_region *alg_region; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1794 | const char *region_name; |
| 1795 | int ret, pos, blocks, type, offset, reg; |
| 1796 | char *file; |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1797 | struct wm_adsp_buf *buf; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1798 | |
| 1799 | file = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 1800 | if (file == NULL) |
| 1801 | return -ENOMEM; |
| 1802 | |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 1803 | snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.bin", dsp->part, dsp->num, |
| 1804 | wm_adsp_fw[dsp->fw].file); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1805 | file[PAGE_SIZE - 1] = '\0'; |
| 1806 | |
| 1807 | ret = request_firmware(&firmware, file, dsp->dev); |
| 1808 | if (ret != 0) { |
| 1809 | adsp_warn(dsp, "Failed to request '%s'\n", file); |
| 1810 | ret = 0; |
| 1811 | goto out; |
| 1812 | } |
| 1813 | ret = -EINVAL; |
| 1814 | |
| 1815 | if (sizeof(*hdr) >= firmware->size) { |
| 1816 | adsp_err(dsp, "%s: file too short, %zu bytes\n", |
| 1817 | file, firmware->size); |
| 1818 | goto out_fw; |
| 1819 | } |
| 1820 | |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1821 | hdr = (void *)&firmware->data[0]; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1822 | if (memcmp(hdr->magic, "WMDR", 4) != 0) { |
| 1823 | adsp_err(dsp, "%s: invalid magic\n", file); |
Charles Keepax | a4cdbec | 2013-01-21 09:02:31 +0000 | [diff] [blame] | 1824 | goto out_fw; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1825 | } |
| 1826 | |
Mark Brown | c712326 | 2013-01-16 16:59:04 +0900 | [diff] [blame] | 1827 | switch (be32_to_cpu(hdr->rev) & 0xff) { |
| 1828 | case 1: |
| 1829 | break; |
| 1830 | default: |
| 1831 | adsp_err(dsp, "%s: Unsupported coefficient file format %d\n", |
| 1832 | file, be32_to_cpu(hdr->rev) & 0xff); |
| 1833 | ret = -EINVAL; |
| 1834 | goto out_fw; |
| 1835 | } |
| 1836 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1837 | adsp_dbg(dsp, "%s: v%d.%d.%d\n", file, |
| 1838 | (le32_to_cpu(hdr->ver) >> 16) & 0xff, |
| 1839 | (le32_to_cpu(hdr->ver) >> 8) & 0xff, |
| 1840 | le32_to_cpu(hdr->ver) & 0xff); |
| 1841 | |
| 1842 | pos = le32_to_cpu(hdr->len); |
| 1843 | |
| 1844 | blocks = 0; |
| 1845 | while (pos < firmware->size && |
| 1846 | pos - firmware->size > sizeof(*blk)) { |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1847 | blk = (void *)(&firmware->data[pos]); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1848 | |
Mark Brown | c712326 | 2013-01-16 16:59:04 +0900 | [diff] [blame] | 1849 | type = le16_to_cpu(blk->type); |
| 1850 | offset = le16_to_cpu(blk->offset); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1851 | |
| 1852 | adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n", |
| 1853 | file, blocks, le32_to_cpu(blk->id), |
| 1854 | (le32_to_cpu(blk->ver) >> 16) & 0xff, |
| 1855 | (le32_to_cpu(blk->ver) >> 8) & 0xff, |
| 1856 | le32_to_cpu(blk->ver) & 0xff); |
| 1857 | adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n", |
| 1858 | file, blocks, le32_to_cpu(blk->len), offset, type); |
| 1859 | |
| 1860 | reg = 0; |
| 1861 | region_name = "Unknown"; |
| 1862 | switch (type) { |
Mark Brown | c712326 | 2013-01-16 16:59:04 +0900 | [diff] [blame] | 1863 | case (WMFW_NAME_TEXT << 8): |
| 1864 | case (WMFW_INFO_TEXT << 8): |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1865 | break; |
Mark Brown | c712326 | 2013-01-16 16:59:04 +0900 | [diff] [blame] | 1866 | case (WMFW_ABSOLUTE << 8): |
Mark Brown | f395a21 | 2013-03-05 22:39:54 +0800 | [diff] [blame] | 1867 | /* |
| 1868 | * Old files may use this for global |
| 1869 | * coefficients. |
| 1870 | */ |
| 1871 | if (le32_to_cpu(blk->id) == dsp->fw_id && |
| 1872 | offset == 0) { |
| 1873 | region_name = "global coefficients"; |
| 1874 | mem = wm_adsp_find_region(dsp, type); |
| 1875 | if (!mem) { |
| 1876 | adsp_err(dsp, "No ZM\n"); |
| 1877 | break; |
| 1878 | } |
| 1879 | reg = wm_adsp_region_to_reg(mem, 0); |
| 1880 | |
| 1881 | } else { |
| 1882 | region_name = "register"; |
| 1883 | reg = offset; |
| 1884 | } |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1885 | break; |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1886 | |
| 1887 | case WMFW_ADSP1_DM: |
| 1888 | case WMFW_ADSP1_ZM: |
| 1889 | case WMFW_ADSP2_XM: |
| 1890 | case WMFW_ADSP2_YM: |
| 1891 | adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n", |
| 1892 | file, blocks, le32_to_cpu(blk->len), |
| 1893 | type, le32_to_cpu(blk->id)); |
| 1894 | |
| 1895 | mem = wm_adsp_find_region(dsp, type); |
| 1896 | if (!mem) { |
| 1897 | adsp_err(dsp, "No base for region %x\n", type); |
| 1898 | break; |
| 1899 | } |
| 1900 | |
Charles Keepax | 1419709 | 2015-12-15 11:29:43 +0000 | [diff] [blame] | 1901 | alg_region = wm_adsp_find_alg_region(dsp, type, |
| 1902 | le32_to_cpu(blk->id)); |
| 1903 | if (alg_region) { |
| 1904 | reg = alg_region->base; |
| 1905 | reg = wm_adsp_region_to_reg(mem, reg); |
| 1906 | reg += offset; |
| 1907 | } else { |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1908 | adsp_err(dsp, "No %x for algorithm %x\n", |
| 1909 | type, le32_to_cpu(blk->id)); |
Charles Keepax | 1419709 | 2015-12-15 11:29:43 +0000 | [diff] [blame] | 1910 | } |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 1911 | break; |
| 1912 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1913 | default: |
Mark Brown | 25c62f7e | 2013-01-20 19:02:19 +0900 | [diff] [blame] | 1914 | adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n", |
| 1915 | file, blocks, type, pos); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1916 | break; |
| 1917 | } |
| 1918 | |
| 1919 | if (reg) { |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1920 | buf = wm_adsp_buf_alloc(blk->data, |
| 1921 | le32_to_cpu(blk->len), |
| 1922 | &buf_list); |
Mark Brown | a76fefa | 2013-01-07 19:03:17 +0000 | [diff] [blame] | 1923 | if (!buf) { |
| 1924 | adsp_err(dsp, "Out of memory\n"); |
Wei Yongjun | f4b8281 | 2013-03-12 00:23:15 +0800 | [diff] [blame] | 1925 | ret = -ENOMEM; |
| 1926 | goto out_fw; |
Mark Brown | a76fefa | 2013-01-07 19:03:17 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Mark Brown | 20da6d5 | 2013-01-12 19:58:17 +0000 | [diff] [blame] | 1929 | adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n", |
| 1930 | file, blocks, le32_to_cpu(blk->len), |
| 1931 | reg); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1932 | ret = regmap_raw_write_async(regmap, reg, buf->buf, |
| 1933 | le32_to_cpu(blk->len)); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1934 | if (ret != 0) { |
| 1935 | adsp_err(dsp, |
Dimitris Papastamos | 43bc3bf | 2013-11-01 15:56:52 +0000 | [diff] [blame] | 1936 | "%s.%d: Failed to write to %x in %s: %d\n", |
| 1937 | file, blocks, reg, region_name, ret); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | |
Charles Keepax | be95101 | 2015-02-16 15:25:49 +0000 | [diff] [blame] | 1941 | pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1942 | blocks++; |
| 1943 | } |
| 1944 | |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1945 | ret = regmap_async_complete(regmap); |
| 1946 | if (ret != 0) |
| 1947 | adsp_err(dsp, "Failed to complete async write: %d\n", ret); |
| 1948 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1949 | if (pos > firmware->size) |
| 1950 | adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n", |
| 1951 | file, blocks, pos - firmware->size); |
| 1952 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 1953 | wm_adsp_debugfs_save_binname(dsp, file); |
| 1954 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1955 | out_fw: |
Charles Keepax | 9da7a5a | 2014-11-17 10:48:21 +0000 | [diff] [blame] | 1956 | regmap_async_complete(regmap); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1957 | release_firmware(firmware); |
Mark Brown | cf17c83 | 2013-01-30 14:37:23 +0800 | [diff] [blame] | 1958 | wm_adsp_buf_free(&buf_list); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1959 | out: |
| 1960 | kfree(file); |
Wei Yongjun | f4b8281 | 2013-03-12 00:23:15 +0800 | [diff] [blame] | 1961 | return ret; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1962 | } |
| 1963 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1964 | int wm_adsp1_init(struct wm_adsp *dsp) |
Mark Brown | 5e7a7a2 | 2013-01-16 10:03:56 +0900 | [diff] [blame] | 1965 | { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 1966 | INIT_LIST_HEAD(&dsp->alg_regions); |
Mark Brown | 5e7a7a2 | 2013-01-16 10:03:56 +0900 | [diff] [blame] | 1967 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1968 | mutex_init(&dsp->pwr_lock); |
| 1969 | |
Mark Brown | 5e7a7a2 | 2013-01-16 10:03:56 +0900 | [diff] [blame] | 1970 | return 0; |
| 1971 | } |
| 1972 | EXPORT_SYMBOL_GPL(wm_adsp1_init); |
| 1973 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1974 | int wm_adsp1_event(struct snd_soc_dapm_widget *w, |
| 1975 | struct snd_kcontrol *kcontrol, |
| 1976 | int event) |
| 1977 | { |
Lars-Peter Clausen | 7271851 | 2015-01-13 10:27:34 +0100 | [diff] [blame] | 1978 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1979 | struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec); |
| 1980 | struct wm_adsp *dsp = &dsps[w->shift]; |
Dimitris Papastamos | b0101b4 | 2013-11-01 15:56:56 +0000 | [diff] [blame] | 1981 | struct wm_adsp_alg_region *alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 1982 | struct wm_coeff_ctl *ctl; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1983 | int ret; |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1984 | unsigned int val; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1985 | |
Lars-Peter Clausen | 0020010 | 2014-07-17 22:01:07 +0200 | [diff] [blame] | 1986 | dsp->card = codec->component.card; |
Dimitris Papastamos | 92bb4c3 | 2013-08-01 11:11:28 +0100 | [diff] [blame] | 1987 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 1988 | mutex_lock(&dsp->pwr_lock); |
| 1989 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 1990 | switch (event) { |
| 1991 | case SND_SOC_DAPM_POST_PMU: |
| 1992 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 1993 | ADSP1_SYS_ENA, ADSP1_SYS_ENA); |
| 1994 | |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 1995 | /* |
| 1996 | * For simplicity set the DSP clock rate to be the |
| 1997 | * SYSCLK rate rather than making it configurable. |
| 1998 | */ |
Charles Keepax | 7585a5b | 2015-12-08 16:08:25 +0000 | [diff] [blame] | 1999 | if (dsp->sysclk_reg) { |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 2000 | ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val); |
| 2001 | if (ret != 0) { |
| 2002 | adsp_err(dsp, "Failed to read SYSCLK state: %d\n", |
| 2003 | ret); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2004 | goto err_mutex; |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | val = (val & dsp->sysclk_mask) |
| 2008 | >> dsp->sysclk_shift; |
| 2009 | |
| 2010 | ret = regmap_update_bits(dsp->regmap, |
| 2011 | dsp->base + ADSP1_CONTROL_31, |
| 2012 | ADSP1_CLK_SEL_MASK, val); |
| 2013 | if (ret != 0) { |
| 2014 | adsp_err(dsp, "Failed to set clock rate: %d\n", |
| 2015 | ret); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2016 | goto err_mutex; |
Chris Rattray | 94e205b | 2013-01-18 08:43:09 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2020 | ret = wm_adsp_load(dsp); |
| 2021 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2022 | goto err_ena; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2023 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 2024 | ret = wm_adsp1_setup_algs(dsp); |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 2025 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2026 | goto err_ena; |
Mark Brown | db40517 | 2012-10-26 19:30:40 +0100 | [diff] [blame] | 2027 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2028 | ret = wm_adsp_load_coeff(dsp); |
| 2029 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2030 | goto err_ena; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2031 | |
Dimitris Papastamos | 0c2e3f3 | 2013-05-28 12:01:50 +0100 | [diff] [blame] | 2032 | /* Initialize caches for enabled and unset controls */ |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 2033 | ret = wm_coeff_init_control_caches(dsp); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2034 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2035 | goto err_ena; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2036 | |
Dimitris Papastamos | 0c2e3f3 | 2013-05-28 12:01:50 +0100 | [diff] [blame] | 2037 | /* Sync set controls */ |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 2038 | ret = wm_coeff_sync_controls(dsp); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2039 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2040 | goto err_ena; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2041 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2042 | /* Start the core running */ |
| 2043 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 2044 | ADSP1_CORE_ENA | ADSP1_START, |
| 2045 | ADSP1_CORE_ENA | ADSP1_START); |
| 2046 | break; |
| 2047 | |
| 2048 | case SND_SOC_DAPM_PRE_PMD: |
| 2049 | /* Halt the core */ |
| 2050 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 2051 | ADSP1_CORE_ENA | ADSP1_START, 0); |
| 2052 | |
| 2053 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19, |
| 2054 | ADSP1_WDMA_BUFFER_LENGTH_MASK, 0); |
| 2055 | |
| 2056 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 2057 | ADSP1_SYS_ENA, 0); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2058 | |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 2059 | list_for_each_entry(ctl, &dsp->ctl_list, list) |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2060 | ctl->enabled = 0; |
Dimitris Papastamos | b0101b4 | 2013-11-01 15:56:56 +0000 | [diff] [blame] | 2061 | |
| 2062 | while (!list_empty(&dsp->alg_regions)) { |
| 2063 | alg_region = list_first_entry(&dsp->alg_regions, |
| 2064 | struct wm_adsp_alg_region, |
| 2065 | list); |
| 2066 | list_del(&alg_region->list); |
| 2067 | kfree(alg_region); |
| 2068 | } |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2069 | break; |
| 2070 | |
| 2071 | default: |
| 2072 | break; |
| 2073 | } |
| 2074 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2075 | mutex_unlock(&dsp->pwr_lock); |
| 2076 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2077 | return 0; |
| 2078 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2079 | err_ena: |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2080 | regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, |
| 2081 | ADSP1_SYS_ENA, 0); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2082 | err_mutex: |
| 2083 | mutex_unlock(&dsp->pwr_lock); |
| 2084 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2085 | return ret; |
| 2086 | } |
| 2087 | EXPORT_SYMBOL_GPL(wm_adsp1_event); |
| 2088 | |
| 2089 | static int wm_adsp2_ena(struct wm_adsp *dsp) |
| 2090 | { |
| 2091 | unsigned int val; |
| 2092 | int ret, count; |
| 2093 | |
Mark Brown | 1552c32 | 2013-11-28 18:11:38 +0000 | [diff] [blame] | 2094 | ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL, |
| 2095 | ADSP2_SYS_ENA, ADSP2_SYS_ENA); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2096 | if (ret != 0) |
| 2097 | return ret; |
| 2098 | |
| 2099 | /* Wait for the RAM to start, should be near instantaneous */ |
Charles Keepax | 939fd1e | 2013-12-18 09:25:49 +0000 | [diff] [blame] | 2100 | for (count = 0; count < 10; ++count) { |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2101 | ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1, |
| 2102 | &val); |
| 2103 | if (ret != 0) |
| 2104 | return ret; |
Charles Keepax | 939fd1e | 2013-12-18 09:25:49 +0000 | [diff] [blame] | 2105 | |
| 2106 | if (val & ADSP2_RAM_RDY) |
| 2107 | break; |
| 2108 | |
| 2109 | msleep(1); |
| 2110 | } |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2111 | |
| 2112 | if (!(val & ADSP2_RAM_RDY)) { |
| 2113 | adsp_err(dsp, "Failed to start DSP RAM\n"); |
| 2114 | return -EBUSY; |
| 2115 | } |
| 2116 | |
| 2117 | adsp_dbg(dsp, "RAM ready after %d polls\n", count); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2118 | |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
Charles Keepax | 18b1a90 | 2014-01-09 09:06:54 +0000 | [diff] [blame] | 2122 | static void wm_adsp2_boot_work(struct work_struct *work) |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2123 | { |
| 2124 | struct wm_adsp *dsp = container_of(work, |
| 2125 | struct wm_adsp, |
| 2126 | boot_work); |
| 2127 | int ret; |
| 2128 | unsigned int val; |
| 2129 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2130 | mutex_lock(&dsp->pwr_lock); |
| 2131 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2132 | /* |
| 2133 | * For simplicity set the DSP clock rate to be the |
| 2134 | * SYSCLK rate rather than making it configurable. |
| 2135 | */ |
| 2136 | ret = regmap_read(dsp->regmap, ARIZONA_SYSTEM_CLOCK_1, &val); |
| 2137 | if (ret != 0) { |
| 2138 | adsp_err(dsp, "Failed to read SYSCLK state: %d\n", ret); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2139 | goto err_mutex; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2140 | } |
| 2141 | val = (val & ARIZONA_SYSCLK_FREQ_MASK) |
| 2142 | >> ARIZONA_SYSCLK_FREQ_SHIFT; |
| 2143 | |
| 2144 | ret = regmap_update_bits_async(dsp->regmap, |
| 2145 | dsp->base + ADSP2_CLOCKING, |
| 2146 | ADSP2_CLK_SEL_MASK, val); |
| 2147 | if (ret != 0) { |
| 2148 | adsp_err(dsp, "Failed to set clock rate: %d\n", ret); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2149 | goto err_mutex; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2152 | ret = wm_adsp2_ena(dsp); |
| 2153 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2154 | goto err_mutex; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2155 | |
| 2156 | ret = wm_adsp_load(dsp); |
| 2157 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2158 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2159 | |
Charles Keepax | b618a185 | 2015-04-13 13:27:53 +0100 | [diff] [blame] | 2160 | ret = wm_adsp2_setup_algs(dsp); |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2161 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2162 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2163 | |
| 2164 | ret = wm_adsp_load_coeff(dsp); |
| 2165 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2166 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2167 | |
| 2168 | /* Initialize caches for enabled and unset controls */ |
| 2169 | ret = wm_coeff_init_control_caches(dsp); |
| 2170 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2171 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2172 | |
| 2173 | /* Sync set controls */ |
| 2174 | ret = wm_coeff_sync_controls(dsp); |
| 2175 | if (ret != 0) |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2176 | goto err_ena; |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2177 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2178 | dsp->running = true; |
| 2179 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2180 | mutex_unlock(&dsp->pwr_lock); |
| 2181 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2182 | return; |
| 2183 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2184 | err_ena: |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2185 | regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, |
| 2186 | ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0); |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2187 | err_mutex: |
| 2188 | mutex_unlock(&dsp->pwr_lock); |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Charles Keepax | 12db5ed | 2014-01-08 17:42:19 +0000 | [diff] [blame] | 2191 | int wm_adsp2_early_event(struct snd_soc_dapm_widget *w, |
| 2192 | struct snd_kcontrol *kcontrol, int event) |
| 2193 | { |
Lars-Peter Clausen | 7271851 | 2015-01-13 10:27:34 +0100 | [diff] [blame] | 2194 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); |
Charles Keepax | 12db5ed | 2014-01-08 17:42:19 +0000 | [diff] [blame] | 2195 | struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec); |
| 2196 | struct wm_adsp *dsp = &dsps[w->shift]; |
| 2197 | |
Lars-Peter Clausen | 0020010 | 2014-07-17 22:01:07 +0200 | [diff] [blame] | 2198 | dsp->card = codec->component.card; |
Charles Keepax | 12db5ed | 2014-01-08 17:42:19 +0000 | [diff] [blame] | 2199 | |
| 2200 | switch (event) { |
| 2201 | case SND_SOC_DAPM_PRE_PMU: |
| 2202 | queue_work(system_unbound_wq, &dsp->boot_work); |
| 2203 | break; |
| 2204 | default: |
| 2205 | break; |
Charles Keepax | cab2725 | 2014-04-17 13:42:54 +0100 | [diff] [blame] | 2206 | } |
Charles Keepax | 12db5ed | 2014-01-08 17:42:19 +0000 | [diff] [blame] | 2207 | |
| 2208 | return 0; |
| 2209 | } |
| 2210 | EXPORT_SYMBOL_GPL(wm_adsp2_early_event); |
| 2211 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2212 | int wm_adsp2_event(struct snd_soc_dapm_widget *w, |
| 2213 | struct snd_kcontrol *kcontrol, int event) |
| 2214 | { |
Lars-Peter Clausen | 7271851 | 2015-01-13 10:27:34 +0100 | [diff] [blame] | 2215 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2216 | struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec); |
| 2217 | struct wm_adsp *dsp = &dsps[w->shift]; |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 2218 | struct wm_adsp_alg_region *alg_region; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2219 | struct wm_coeff_ctl *ctl; |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2220 | int ret; |
| 2221 | |
| 2222 | switch (event) { |
| 2223 | case SND_SOC_DAPM_POST_PMU: |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2224 | flush_work(&dsp->boot_work); |
Mark Brown | dd49e2c | 2012-12-02 21:50:46 +0900 | [diff] [blame] | 2225 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2226 | if (!dsp->running) |
| 2227 | return -EIO; |
Mark Brown | dd49e2c | 2012-12-02 21:50:46 +0900 | [diff] [blame] | 2228 | |
Charles Keepax | d8a64d6 | 2014-01-08 17:42:18 +0000 | [diff] [blame] | 2229 | ret = regmap_update_bits(dsp->regmap, |
| 2230 | dsp->base + ADSP2_CONTROL, |
Charles Keepax | 00e4c3b | 2014-11-18 16:25:27 +0000 | [diff] [blame] | 2231 | ADSP2_CORE_ENA | ADSP2_START, |
| 2232 | ADSP2_CORE_ENA | ADSP2_START); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2233 | if (ret != 0) |
| 2234 | goto err; |
Charles Keepax | 2cd19bd | 2015-12-15 11:29:46 +0000 | [diff] [blame] | 2235 | |
| 2236 | if (wm_adsp_fw[dsp->fw].num_caps != 0) |
| 2237 | ret = wm_adsp_buffer_init(dsp); |
| 2238 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2239 | break; |
| 2240 | |
| 2241 | case SND_SOC_DAPM_PRE_PMD: |
Richard Fitzgerald | 10337b0 | 2015-05-29 10:23:07 +0100 | [diff] [blame] | 2242 | /* Log firmware state, it can be useful for analysis */ |
| 2243 | wm_adsp2_show_fw_status(dsp); |
| 2244 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2245 | mutex_lock(&dsp->pwr_lock); |
| 2246 | |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 2247 | wm_adsp_debugfs_clear(dsp); |
| 2248 | |
| 2249 | dsp->fw_id = 0; |
| 2250 | dsp->fw_id_version = 0; |
Mark Brown | 1023dbd | 2013-01-11 22:58:28 +0000 | [diff] [blame] | 2251 | dsp->running = false; |
| 2252 | |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2253 | regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, |
Mark Brown | a7f9be7 | 2012-11-28 19:53:59 +0000 | [diff] [blame] | 2254 | ADSP2_SYS_ENA | ADSP2_CORE_ENA | |
| 2255 | ADSP2_START, 0); |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 2256 | |
Mark Brown | 2d30b57 | 2013-01-28 20:18:17 +0800 | [diff] [blame] | 2257 | /* Make sure DMAs are quiesced */ |
| 2258 | regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0); |
| 2259 | regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0); |
| 2260 | regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0); |
| 2261 | |
Dimitris Papastamos | 81ad93e | 2013-07-29 13:51:59 +0100 | [diff] [blame] | 2262 | list_for_each_entry(ctl, &dsp->ctl_list, list) |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2263 | ctl->enabled = 0; |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2264 | |
Mark Brown | 471f488 | 2013-01-08 16:09:31 +0000 | [diff] [blame] | 2265 | while (!list_empty(&dsp->alg_regions)) { |
| 2266 | alg_region = list_first_entry(&dsp->alg_regions, |
| 2267 | struct wm_adsp_alg_region, |
| 2268 | list); |
| 2269 | list_del(&alg_region->list); |
| 2270 | kfree(alg_region); |
| 2271 | } |
Charles Keepax | ddbc5ef | 2014-01-22 10:09:11 +0000 | [diff] [blame] | 2272 | |
Charles Keepax | 2cd19bd | 2015-12-15 11:29:46 +0000 | [diff] [blame] | 2273 | if (wm_adsp_fw[dsp->fw].num_caps != 0) |
| 2274 | wm_adsp_buffer_free(dsp); |
| 2275 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2276 | mutex_unlock(&dsp->pwr_lock); |
| 2277 | |
Charles Keepax | ddbc5ef | 2014-01-22 10:09:11 +0000 | [diff] [blame] | 2278 | adsp_dbg(dsp, "Shutdown complete\n"); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2279 | break; |
| 2280 | |
| 2281 | default: |
| 2282 | break; |
| 2283 | } |
| 2284 | |
| 2285 | return 0; |
| 2286 | err: |
| 2287 | regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, |
Mark Brown | a7f9be7 | 2012-11-28 19:53:59 +0000 | [diff] [blame] | 2288 | ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0); |
Mark Brown | 2159ad9 | 2012-10-11 11:54:02 +0900 | [diff] [blame] | 2289 | return ret; |
| 2290 | } |
| 2291 | EXPORT_SYMBOL_GPL(wm_adsp2_event); |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 2292 | |
Richard Fitzgerald | f5e2ce9 | 2015-06-11 11:32:30 +0100 | [diff] [blame] | 2293 | int wm_adsp2_codec_probe(struct wm_adsp *dsp, struct snd_soc_codec *codec) |
| 2294 | { |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 2295 | wm_adsp2_init_debugfs(dsp, codec); |
| 2296 | |
Richard Fitzgerald | 218e508 | 2015-06-11 11:32:31 +0100 | [diff] [blame] | 2297 | return snd_soc_add_codec_controls(codec, |
Richard Fitzgerald | 336d044 | 2015-06-18 13:43:19 +0100 | [diff] [blame] | 2298 | &wm_adsp_fw_controls[dsp->num - 1], |
| 2299 | 1); |
Richard Fitzgerald | f5e2ce9 | 2015-06-11 11:32:30 +0100 | [diff] [blame] | 2300 | } |
| 2301 | EXPORT_SYMBOL_GPL(wm_adsp2_codec_probe); |
| 2302 | |
| 2303 | int wm_adsp2_codec_remove(struct wm_adsp *dsp, struct snd_soc_codec *codec) |
| 2304 | { |
Richard Fitzgerald | f9f55e3 | 2015-06-11 11:32:32 +0100 | [diff] [blame] | 2305 | wm_adsp2_cleanup_debugfs(dsp); |
| 2306 | |
Richard Fitzgerald | f5e2ce9 | 2015-06-11 11:32:30 +0100 | [diff] [blame] | 2307 | return 0; |
| 2308 | } |
| 2309 | EXPORT_SYMBOL_GPL(wm_adsp2_codec_remove); |
| 2310 | |
Richard Fitzgerald | 81ac58b | 2015-06-02 11:53:34 +0100 | [diff] [blame] | 2311 | int wm_adsp2_init(struct wm_adsp *dsp) |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 2312 | { |
| 2313 | int ret; |
| 2314 | |
Mark Brown | 10a2b66 | 2012-12-02 21:37:00 +0900 | [diff] [blame] | 2315 | /* |
| 2316 | * Disable the DSP memory by default when in reset for a small |
| 2317 | * power saving. |
| 2318 | */ |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 2319 | ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, |
Mark Brown | 10a2b66 | 2012-12-02 21:37:00 +0900 | [diff] [blame] | 2320 | ADSP2_MEM_ENA, 0); |
| 2321 | if (ret != 0) { |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 2322 | adsp_err(dsp, "Failed to clear memory retention: %d\n", ret); |
Mark Brown | 10a2b66 | 2012-12-02 21:37:00 +0900 | [diff] [blame] | 2323 | return ret; |
| 2324 | } |
| 2325 | |
Charles Keepax | 3809f00 | 2015-04-13 13:27:54 +0100 | [diff] [blame] | 2326 | INIT_LIST_HEAD(&dsp->alg_regions); |
| 2327 | INIT_LIST_HEAD(&dsp->ctl_list); |
| 2328 | INIT_WORK(&dsp->boot_work, wm_adsp2_boot_work); |
Dimitris Papastamos | 6ab2b7b | 2013-05-08 14:15:35 +0100 | [diff] [blame] | 2329 | |
Charles Keepax | 078e718 | 2015-12-08 16:08:26 +0000 | [diff] [blame] | 2330 | mutex_init(&dsp->pwr_lock); |
| 2331 | |
Mark Brown | 973838a | 2012-11-28 17:20:32 +0000 | [diff] [blame] | 2332 | return 0; |
| 2333 | } |
| 2334 | EXPORT_SYMBOL_GPL(wm_adsp2_init); |
Praveen Diwakar | 0a37c6e | 2014-07-04 11:17:41 +0530 | [diff] [blame] | 2335 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 2336 | int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream) |
| 2337 | { |
| 2338 | struct wm_adsp_compr *compr; |
| 2339 | int ret = 0; |
| 2340 | |
| 2341 | mutex_lock(&dsp->pwr_lock); |
| 2342 | |
| 2343 | if (wm_adsp_fw[dsp->fw].num_caps == 0) { |
| 2344 | adsp_err(dsp, "Firmware does not support compressed API\n"); |
| 2345 | ret = -ENXIO; |
| 2346 | goto out; |
| 2347 | } |
| 2348 | |
| 2349 | if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) { |
| 2350 | adsp_err(dsp, "Firmware does not support stream direction\n"); |
| 2351 | ret = -EINVAL; |
| 2352 | goto out; |
| 2353 | } |
| 2354 | |
Charles Keepax | 95fe959 | 2015-12-15 11:29:47 +0000 | [diff] [blame] | 2355 | if (dsp->compr) { |
| 2356 | /* It is expect this limitation will be removed in future */ |
| 2357 | adsp_err(dsp, "Only a single stream supported per DSP\n"); |
| 2358 | ret = -EBUSY; |
| 2359 | goto out; |
| 2360 | } |
| 2361 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 2362 | compr = kzalloc(sizeof(*compr), GFP_KERNEL); |
| 2363 | if (!compr) { |
| 2364 | ret = -ENOMEM; |
| 2365 | goto out; |
| 2366 | } |
| 2367 | |
| 2368 | compr->dsp = dsp; |
| 2369 | compr->stream = stream; |
| 2370 | |
| 2371 | dsp->compr = compr; |
| 2372 | |
| 2373 | stream->runtime->private_data = compr; |
| 2374 | |
| 2375 | out: |
| 2376 | mutex_unlock(&dsp->pwr_lock); |
| 2377 | |
| 2378 | return ret; |
| 2379 | } |
| 2380 | EXPORT_SYMBOL_GPL(wm_adsp_compr_open); |
| 2381 | |
| 2382 | int wm_adsp_compr_free(struct snd_compr_stream *stream) |
| 2383 | { |
| 2384 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 2385 | struct wm_adsp *dsp = compr->dsp; |
| 2386 | |
| 2387 | mutex_lock(&dsp->pwr_lock); |
| 2388 | |
| 2389 | dsp->compr = NULL; |
| 2390 | |
Charles Keepax | 83a40ce | 2016-01-06 12:33:19 +0000 | [diff] [blame] | 2391 | kfree(compr->raw_buf); |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 2392 | kfree(compr); |
| 2393 | |
| 2394 | mutex_unlock(&dsp->pwr_lock); |
| 2395 | |
| 2396 | return 0; |
| 2397 | } |
| 2398 | EXPORT_SYMBOL_GPL(wm_adsp_compr_free); |
| 2399 | |
| 2400 | static int wm_adsp_compr_check_params(struct snd_compr_stream *stream, |
| 2401 | struct snd_compr_params *params) |
| 2402 | { |
| 2403 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 2404 | struct wm_adsp *dsp = compr->dsp; |
| 2405 | const struct wm_adsp_fw_caps *caps; |
| 2406 | const struct snd_codec_desc *desc; |
| 2407 | int i, j; |
| 2408 | |
| 2409 | if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE || |
| 2410 | params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE || |
| 2411 | params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS || |
| 2412 | params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS || |
| 2413 | params->buffer.fragment_size % WM_ADSP_DATA_WORD_SIZE) { |
| 2414 | adsp_err(dsp, "Invalid buffer fragsize=%d fragments=%d\n", |
| 2415 | params->buffer.fragment_size, |
| 2416 | params->buffer.fragments); |
| 2417 | |
| 2418 | return -EINVAL; |
| 2419 | } |
| 2420 | |
| 2421 | for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) { |
| 2422 | caps = &wm_adsp_fw[dsp->fw].caps[i]; |
| 2423 | desc = &caps->desc; |
| 2424 | |
| 2425 | if (caps->id != params->codec.id) |
| 2426 | continue; |
| 2427 | |
| 2428 | if (stream->direction == SND_COMPRESS_PLAYBACK) { |
| 2429 | if (desc->max_ch < params->codec.ch_out) |
| 2430 | continue; |
| 2431 | } else { |
| 2432 | if (desc->max_ch < params->codec.ch_in) |
| 2433 | continue; |
| 2434 | } |
| 2435 | |
| 2436 | if (!(desc->formats & (1 << params->codec.format))) |
| 2437 | continue; |
| 2438 | |
| 2439 | for (j = 0; j < desc->num_sample_rates; ++j) |
| 2440 | if (desc->sample_rates[j] == params->codec.sample_rate) |
| 2441 | return 0; |
| 2442 | } |
| 2443 | |
| 2444 | adsp_err(dsp, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n", |
| 2445 | params->codec.id, params->codec.ch_in, params->codec.ch_out, |
| 2446 | params->codec.sample_rate, params->codec.format); |
| 2447 | return -EINVAL; |
| 2448 | } |
| 2449 | |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 2450 | static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr) |
| 2451 | { |
| 2452 | return compr->size.fragment_size / WM_ADSP_DATA_WORD_SIZE; |
| 2453 | } |
| 2454 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 2455 | int wm_adsp_compr_set_params(struct snd_compr_stream *stream, |
| 2456 | struct snd_compr_params *params) |
| 2457 | { |
| 2458 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
Charles Keepax | 83a40ce | 2016-01-06 12:33:19 +0000 | [diff] [blame] | 2459 | unsigned int size; |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 2460 | int ret; |
| 2461 | |
| 2462 | ret = wm_adsp_compr_check_params(stream, params); |
| 2463 | if (ret) |
| 2464 | return ret; |
| 2465 | |
| 2466 | compr->size = params->buffer; |
| 2467 | |
| 2468 | adsp_dbg(compr->dsp, "fragment_size=%d fragments=%d\n", |
| 2469 | compr->size.fragment_size, compr->size.fragments); |
| 2470 | |
Charles Keepax | 83a40ce | 2016-01-06 12:33:19 +0000 | [diff] [blame] | 2471 | size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf); |
| 2472 | compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL); |
| 2473 | if (!compr->raw_buf) |
| 2474 | return -ENOMEM; |
| 2475 | |
Charles Keepax | da2b335 | 2016-02-02 16:41:36 +0000 | [diff] [blame^] | 2476 | compr->sample_rate = params->codec.sample_rate; |
| 2477 | |
Charles Keepax | 406abc9 | 2015-12-15 11:29:45 +0000 | [diff] [blame] | 2478 | return 0; |
| 2479 | } |
| 2480 | EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params); |
| 2481 | |
| 2482 | int wm_adsp_compr_get_caps(struct snd_compr_stream *stream, |
| 2483 | struct snd_compr_caps *caps) |
| 2484 | { |
| 2485 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 2486 | int fw = compr->dsp->fw; |
| 2487 | int i; |
| 2488 | |
| 2489 | if (wm_adsp_fw[fw].caps) { |
| 2490 | for (i = 0; i < wm_adsp_fw[fw].num_caps; i++) |
| 2491 | caps->codecs[i] = wm_adsp_fw[fw].caps[i].id; |
| 2492 | |
| 2493 | caps->num_codecs = i; |
| 2494 | caps->direction = wm_adsp_fw[fw].compr_direction; |
| 2495 | |
| 2496 | caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE; |
| 2497 | caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE; |
| 2498 | caps->min_fragments = WM_ADSP_MIN_FRAGMENTS; |
| 2499 | caps->max_fragments = WM_ADSP_MAX_FRAGMENTS; |
| 2500 | } |
| 2501 | |
| 2502 | return 0; |
| 2503 | } |
| 2504 | EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps); |
| 2505 | |
Charles Keepax | 2cd19bd | 2015-12-15 11:29:46 +0000 | [diff] [blame] | 2506 | static int wm_adsp_read_data_block(struct wm_adsp *dsp, int mem_type, |
| 2507 | unsigned int mem_addr, |
| 2508 | unsigned int num_words, u32 *data) |
| 2509 | { |
| 2510 | struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type); |
| 2511 | unsigned int i, reg; |
| 2512 | int ret; |
| 2513 | |
| 2514 | if (!mem) |
| 2515 | return -EINVAL; |
| 2516 | |
| 2517 | reg = wm_adsp_region_to_reg(mem, mem_addr); |
| 2518 | |
| 2519 | ret = regmap_raw_read(dsp->regmap, reg, data, |
| 2520 | sizeof(*data) * num_words); |
| 2521 | if (ret < 0) |
| 2522 | return ret; |
| 2523 | |
| 2524 | for (i = 0; i < num_words; ++i) |
| 2525 | data[i] = be32_to_cpu(data[i]) & 0x00ffffffu; |
| 2526 | |
| 2527 | return 0; |
| 2528 | } |
| 2529 | |
| 2530 | static inline int wm_adsp_read_data_word(struct wm_adsp *dsp, int mem_type, |
| 2531 | unsigned int mem_addr, u32 *data) |
| 2532 | { |
| 2533 | return wm_adsp_read_data_block(dsp, mem_type, mem_addr, 1, data); |
| 2534 | } |
| 2535 | |
| 2536 | static int wm_adsp_write_data_word(struct wm_adsp *dsp, int mem_type, |
| 2537 | unsigned int mem_addr, u32 data) |
| 2538 | { |
| 2539 | struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type); |
| 2540 | unsigned int reg; |
| 2541 | |
| 2542 | if (!mem) |
| 2543 | return -EINVAL; |
| 2544 | |
| 2545 | reg = wm_adsp_region_to_reg(mem, mem_addr); |
| 2546 | |
| 2547 | data = cpu_to_be32(data & 0x00ffffffu); |
| 2548 | |
| 2549 | return regmap_raw_write(dsp->regmap, reg, &data, sizeof(data)); |
| 2550 | } |
| 2551 | |
| 2552 | static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf, |
| 2553 | unsigned int field_offset, u32 *data) |
| 2554 | { |
| 2555 | return wm_adsp_read_data_word(buf->dsp, WMFW_ADSP2_XM, |
| 2556 | buf->host_buf_ptr + field_offset, data); |
| 2557 | } |
| 2558 | |
| 2559 | static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf, |
| 2560 | unsigned int field_offset, u32 data) |
| 2561 | { |
| 2562 | return wm_adsp_write_data_word(buf->dsp, WMFW_ADSP2_XM, |
| 2563 | buf->host_buf_ptr + field_offset, data); |
| 2564 | } |
| 2565 | |
| 2566 | static int wm_adsp_buffer_locate(struct wm_adsp_compr_buf *buf) |
| 2567 | { |
| 2568 | struct wm_adsp_alg_region *alg_region; |
| 2569 | struct wm_adsp *dsp = buf->dsp; |
| 2570 | u32 xmalg, addr, magic; |
| 2571 | int i, ret; |
| 2572 | |
| 2573 | alg_region = wm_adsp_find_alg_region(dsp, WMFW_ADSP2_XM, dsp->fw_id); |
| 2574 | xmalg = sizeof(struct wm_adsp_system_config_xm_hdr) / sizeof(__be32); |
| 2575 | |
| 2576 | addr = alg_region->base + xmalg + ALG_XM_FIELD(magic); |
| 2577 | ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr, &magic); |
| 2578 | if (ret < 0) |
| 2579 | return ret; |
| 2580 | |
| 2581 | if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC) |
| 2582 | return -EINVAL; |
| 2583 | |
| 2584 | addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr); |
| 2585 | for (i = 0; i < 5; ++i) { |
| 2586 | ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr, |
| 2587 | &buf->host_buf_ptr); |
| 2588 | if (ret < 0) |
| 2589 | return ret; |
| 2590 | |
| 2591 | if (buf->host_buf_ptr) |
| 2592 | break; |
| 2593 | |
| 2594 | usleep_range(1000, 2000); |
| 2595 | } |
| 2596 | |
| 2597 | if (!buf->host_buf_ptr) |
| 2598 | return -EIO; |
| 2599 | |
| 2600 | adsp_dbg(dsp, "host_buf_ptr=%x\n", buf->host_buf_ptr); |
| 2601 | |
| 2602 | return 0; |
| 2603 | } |
| 2604 | |
| 2605 | static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf) |
| 2606 | { |
| 2607 | const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps; |
| 2608 | struct wm_adsp_buffer_region *region; |
| 2609 | u32 offset = 0; |
| 2610 | int i, ret; |
| 2611 | |
| 2612 | for (i = 0; i < caps->num_regions; ++i) { |
| 2613 | region = &buf->regions[i]; |
| 2614 | |
| 2615 | region->offset = offset; |
| 2616 | region->mem_type = caps->region_defs[i].mem_type; |
| 2617 | |
| 2618 | ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset, |
| 2619 | ®ion->base_addr); |
| 2620 | if (ret < 0) |
| 2621 | return ret; |
| 2622 | |
| 2623 | ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset, |
| 2624 | &offset); |
| 2625 | if (ret < 0) |
| 2626 | return ret; |
| 2627 | |
| 2628 | region->cumulative_size = offset; |
| 2629 | |
| 2630 | adsp_dbg(buf->dsp, |
| 2631 | "region=%d type=%d base=%04x off=%04x size=%04x\n", |
| 2632 | i, region->mem_type, region->base_addr, |
| 2633 | region->offset, region->cumulative_size); |
| 2634 | } |
| 2635 | |
| 2636 | return 0; |
| 2637 | } |
| 2638 | |
| 2639 | static int wm_adsp_buffer_init(struct wm_adsp *dsp) |
| 2640 | { |
| 2641 | struct wm_adsp_compr_buf *buf; |
| 2642 | int ret; |
| 2643 | |
| 2644 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
| 2645 | if (!buf) |
| 2646 | return -ENOMEM; |
| 2647 | |
| 2648 | buf->dsp = dsp; |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 2649 | buf->read_index = -1; |
| 2650 | buf->irq_count = 0xFFFFFFFF; |
Charles Keepax | 2cd19bd | 2015-12-15 11:29:46 +0000 | [diff] [blame] | 2651 | |
| 2652 | ret = wm_adsp_buffer_locate(buf); |
| 2653 | if (ret < 0) { |
| 2654 | adsp_err(dsp, "Failed to acquire host buffer: %d\n", ret); |
| 2655 | goto err_buffer; |
| 2656 | } |
| 2657 | |
| 2658 | buf->regions = kcalloc(wm_adsp_fw[dsp->fw].caps->num_regions, |
| 2659 | sizeof(*buf->regions), GFP_KERNEL); |
| 2660 | if (!buf->regions) { |
| 2661 | ret = -ENOMEM; |
| 2662 | goto err_buffer; |
| 2663 | } |
| 2664 | |
| 2665 | ret = wm_adsp_buffer_populate(buf); |
| 2666 | if (ret < 0) { |
| 2667 | adsp_err(dsp, "Failed to populate host buffer: %d\n", ret); |
| 2668 | goto err_regions; |
| 2669 | } |
| 2670 | |
| 2671 | dsp->buffer = buf; |
| 2672 | |
| 2673 | return 0; |
| 2674 | |
| 2675 | err_regions: |
| 2676 | kfree(buf->regions); |
| 2677 | err_buffer: |
| 2678 | kfree(buf); |
| 2679 | return ret; |
| 2680 | } |
| 2681 | |
| 2682 | static int wm_adsp_buffer_free(struct wm_adsp *dsp) |
| 2683 | { |
| 2684 | if (dsp->buffer) { |
| 2685 | kfree(dsp->buffer->regions); |
| 2686 | kfree(dsp->buffer); |
| 2687 | |
| 2688 | dsp->buffer = NULL; |
| 2689 | } |
| 2690 | |
| 2691 | return 0; |
| 2692 | } |
| 2693 | |
Charles Keepax | 95fe959 | 2015-12-15 11:29:47 +0000 | [diff] [blame] | 2694 | static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr) |
| 2695 | { |
| 2696 | return compr->buf != NULL; |
| 2697 | } |
| 2698 | |
| 2699 | static int wm_adsp_compr_attach(struct wm_adsp_compr *compr) |
| 2700 | { |
| 2701 | /* |
| 2702 | * Note this will be more complex once each DSP can support multiple |
| 2703 | * streams |
| 2704 | */ |
| 2705 | if (!compr->dsp->buffer) |
| 2706 | return -EINVAL; |
| 2707 | |
| 2708 | compr->buf = compr->dsp->buffer; |
| 2709 | |
| 2710 | return 0; |
| 2711 | } |
| 2712 | |
| 2713 | int wm_adsp_compr_trigger(struct snd_compr_stream *stream, int cmd) |
| 2714 | { |
| 2715 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 2716 | struct wm_adsp *dsp = compr->dsp; |
| 2717 | int ret = 0; |
| 2718 | |
| 2719 | adsp_dbg(dsp, "Trigger: %d\n", cmd); |
| 2720 | |
| 2721 | mutex_lock(&dsp->pwr_lock); |
| 2722 | |
| 2723 | switch (cmd) { |
| 2724 | case SNDRV_PCM_TRIGGER_START: |
| 2725 | if (wm_adsp_compr_attached(compr)) |
| 2726 | break; |
| 2727 | |
| 2728 | ret = wm_adsp_compr_attach(compr); |
| 2729 | if (ret < 0) { |
| 2730 | adsp_err(dsp, "Failed to link buffer and stream: %d\n", |
| 2731 | ret); |
| 2732 | break; |
| 2733 | } |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 2734 | |
| 2735 | /* Trigger the IRQ at one fragment of data */ |
| 2736 | ret = wm_adsp_buffer_write(compr->buf, |
| 2737 | HOST_BUFFER_FIELD(high_water_mark), |
| 2738 | wm_adsp_compr_frag_words(compr)); |
| 2739 | if (ret < 0) { |
| 2740 | adsp_err(dsp, "Failed to set high water mark: %d\n", |
| 2741 | ret); |
| 2742 | break; |
| 2743 | } |
Charles Keepax | 95fe959 | 2015-12-15 11:29:47 +0000 | [diff] [blame] | 2744 | break; |
| 2745 | case SNDRV_PCM_TRIGGER_STOP: |
| 2746 | break; |
| 2747 | default: |
| 2748 | ret = -EINVAL; |
| 2749 | break; |
| 2750 | } |
| 2751 | |
| 2752 | mutex_unlock(&dsp->pwr_lock); |
| 2753 | |
| 2754 | return ret; |
| 2755 | } |
| 2756 | EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger); |
| 2757 | |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 2758 | static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf) |
| 2759 | { |
| 2760 | int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1; |
| 2761 | |
| 2762 | return buf->regions[last_region].cumulative_size; |
| 2763 | } |
| 2764 | |
| 2765 | static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf) |
| 2766 | { |
| 2767 | u32 next_read_index, next_write_index; |
| 2768 | int write_index, read_index, avail; |
| 2769 | int ret; |
| 2770 | |
| 2771 | /* Only sync read index if we haven't already read a valid index */ |
| 2772 | if (buf->read_index < 0) { |
| 2773 | ret = wm_adsp_buffer_read(buf, |
| 2774 | HOST_BUFFER_FIELD(next_read_index), |
| 2775 | &next_read_index); |
| 2776 | if (ret < 0) |
| 2777 | return ret; |
| 2778 | |
| 2779 | read_index = sign_extend32(next_read_index, 23); |
| 2780 | |
| 2781 | if (read_index < 0) { |
| 2782 | adsp_dbg(buf->dsp, "Avail check on unstarted stream\n"); |
| 2783 | return 0; |
| 2784 | } |
| 2785 | |
| 2786 | buf->read_index = read_index; |
| 2787 | } |
| 2788 | |
| 2789 | ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index), |
| 2790 | &next_write_index); |
| 2791 | if (ret < 0) |
| 2792 | return ret; |
| 2793 | |
| 2794 | write_index = sign_extend32(next_write_index, 23); |
| 2795 | |
| 2796 | avail = write_index - buf->read_index; |
| 2797 | if (avail < 0) |
| 2798 | avail += wm_adsp_buffer_size(buf); |
| 2799 | |
| 2800 | adsp_dbg(buf->dsp, "readindex=0x%x, writeindex=0x%x, avail=%d\n", |
| 2801 | buf->read_index, write_index, avail); |
| 2802 | |
| 2803 | buf->avail = avail; |
| 2804 | |
| 2805 | return 0; |
| 2806 | } |
| 2807 | |
| 2808 | int wm_adsp_compr_handle_irq(struct wm_adsp *dsp) |
| 2809 | { |
| 2810 | struct wm_adsp_compr_buf *buf = dsp->buffer; |
Charles Keepax | 83a40ce | 2016-01-06 12:33:19 +0000 | [diff] [blame] | 2811 | struct wm_adsp_compr *compr = dsp->compr; |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 2812 | int ret = 0; |
| 2813 | |
| 2814 | mutex_lock(&dsp->pwr_lock); |
| 2815 | |
| 2816 | if (!buf) { |
| 2817 | adsp_err(dsp, "Spurious buffer IRQ\n"); |
| 2818 | ret = -ENODEV; |
| 2819 | goto out; |
| 2820 | } |
| 2821 | |
| 2822 | adsp_dbg(dsp, "Handling buffer IRQ\n"); |
| 2823 | |
| 2824 | ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error); |
| 2825 | if (ret < 0) { |
| 2826 | adsp_err(dsp, "Failed to check buffer error: %d\n", ret); |
| 2827 | goto out; |
| 2828 | } |
| 2829 | if (buf->error != 0) { |
| 2830 | adsp_err(dsp, "Buffer error occurred: %d\n", buf->error); |
| 2831 | ret = -EIO; |
| 2832 | goto out; |
| 2833 | } |
| 2834 | |
| 2835 | ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count), |
| 2836 | &buf->irq_count); |
| 2837 | if (ret < 0) { |
| 2838 | adsp_err(dsp, "Failed to get irq_count: %d\n", ret); |
| 2839 | goto out; |
| 2840 | } |
| 2841 | |
| 2842 | ret = wm_adsp_buffer_update_avail(buf); |
| 2843 | if (ret < 0) { |
| 2844 | adsp_err(dsp, "Error reading avail: %d\n", ret); |
| 2845 | goto out; |
| 2846 | } |
| 2847 | |
Charles Keepax | 83a40ce | 2016-01-06 12:33:19 +0000 | [diff] [blame] | 2848 | if (compr->stream) |
| 2849 | snd_compr_fragment_elapsed(compr->stream); |
| 2850 | |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 2851 | out: |
| 2852 | mutex_unlock(&dsp->pwr_lock); |
| 2853 | |
| 2854 | return ret; |
| 2855 | } |
| 2856 | EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq); |
| 2857 | |
| 2858 | static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf) |
| 2859 | { |
| 2860 | if (buf->irq_count & 0x01) |
| 2861 | return 0; |
| 2862 | |
| 2863 | adsp_dbg(buf->dsp, "Enable IRQ(0x%x) for next fragment\n", |
| 2864 | buf->irq_count); |
| 2865 | |
| 2866 | buf->irq_count |= 0x01; |
| 2867 | |
| 2868 | return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack), |
| 2869 | buf->irq_count); |
| 2870 | } |
| 2871 | |
| 2872 | int wm_adsp_compr_pointer(struct snd_compr_stream *stream, |
| 2873 | struct snd_compr_tstamp *tstamp) |
| 2874 | { |
| 2875 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 2876 | struct wm_adsp_compr_buf *buf = compr->buf; |
| 2877 | struct wm_adsp *dsp = compr->dsp; |
| 2878 | int ret = 0; |
| 2879 | |
| 2880 | adsp_dbg(dsp, "Pointer request\n"); |
| 2881 | |
| 2882 | mutex_lock(&dsp->pwr_lock); |
| 2883 | |
| 2884 | if (!compr->buf) { |
| 2885 | ret = -ENXIO; |
| 2886 | goto out; |
| 2887 | } |
| 2888 | |
| 2889 | if (compr->buf->error) { |
| 2890 | ret = -EIO; |
| 2891 | goto out; |
| 2892 | } |
| 2893 | |
| 2894 | if (buf->avail < wm_adsp_compr_frag_words(compr)) { |
| 2895 | ret = wm_adsp_buffer_update_avail(buf); |
| 2896 | if (ret < 0) { |
| 2897 | adsp_err(dsp, "Error reading avail: %d\n", ret); |
| 2898 | goto out; |
| 2899 | } |
| 2900 | |
| 2901 | /* |
| 2902 | * If we really have less than 1 fragment available tell the |
| 2903 | * DSP to inform us once a whole fragment is available. |
| 2904 | */ |
| 2905 | if (buf->avail < wm_adsp_compr_frag_words(compr)) { |
| 2906 | ret = wm_adsp_buffer_reenable_irq(buf); |
| 2907 | if (ret < 0) { |
| 2908 | adsp_err(dsp, |
| 2909 | "Failed to re-enable buffer IRQ: %d\n", |
| 2910 | ret); |
| 2911 | goto out; |
| 2912 | } |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | tstamp->copied_total = compr->copied_total; |
| 2917 | tstamp->copied_total += buf->avail * WM_ADSP_DATA_WORD_SIZE; |
Charles Keepax | da2b335 | 2016-02-02 16:41:36 +0000 | [diff] [blame^] | 2918 | tstamp->sampling_rate = compr->sample_rate; |
Charles Keepax | 565ace4 | 2016-01-06 12:33:18 +0000 | [diff] [blame] | 2919 | |
| 2920 | out: |
| 2921 | mutex_unlock(&dsp->pwr_lock); |
| 2922 | |
| 2923 | return ret; |
| 2924 | } |
| 2925 | EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer); |
| 2926 | |
Charles Keepax | 83a40ce | 2016-01-06 12:33:19 +0000 | [diff] [blame] | 2927 | static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target) |
| 2928 | { |
| 2929 | struct wm_adsp_compr_buf *buf = compr->buf; |
| 2930 | u8 *pack_in = (u8 *)compr->raw_buf; |
| 2931 | u8 *pack_out = (u8 *)compr->raw_buf; |
| 2932 | unsigned int adsp_addr; |
| 2933 | int mem_type, nwords, max_read; |
| 2934 | int i, j, ret; |
| 2935 | |
| 2936 | /* Calculate read parameters */ |
| 2937 | for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i) |
| 2938 | if (buf->read_index < buf->regions[i].cumulative_size) |
| 2939 | break; |
| 2940 | |
| 2941 | if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions) |
| 2942 | return -EINVAL; |
| 2943 | |
| 2944 | mem_type = buf->regions[i].mem_type; |
| 2945 | adsp_addr = buf->regions[i].base_addr + |
| 2946 | (buf->read_index - buf->regions[i].offset); |
| 2947 | |
| 2948 | max_read = wm_adsp_compr_frag_words(compr); |
| 2949 | nwords = buf->regions[i].cumulative_size - buf->read_index; |
| 2950 | |
| 2951 | if (nwords > target) |
| 2952 | nwords = target; |
| 2953 | if (nwords > buf->avail) |
| 2954 | nwords = buf->avail; |
| 2955 | if (nwords > max_read) |
| 2956 | nwords = max_read; |
| 2957 | if (!nwords) |
| 2958 | return 0; |
| 2959 | |
| 2960 | /* Read data from DSP */ |
| 2961 | ret = wm_adsp_read_data_block(buf->dsp, mem_type, adsp_addr, |
| 2962 | nwords, compr->raw_buf); |
| 2963 | if (ret < 0) |
| 2964 | return ret; |
| 2965 | |
| 2966 | /* Remove the padding bytes from the data read from the DSP */ |
| 2967 | for (i = 0; i < nwords; i++) { |
| 2968 | for (j = 0; j < WM_ADSP_DATA_WORD_SIZE; j++) |
| 2969 | *pack_out++ = *pack_in++; |
| 2970 | |
| 2971 | pack_in += sizeof(*(compr->raw_buf)) - WM_ADSP_DATA_WORD_SIZE; |
| 2972 | } |
| 2973 | |
| 2974 | /* update read index to account for words read */ |
| 2975 | buf->read_index += nwords; |
| 2976 | if (buf->read_index == wm_adsp_buffer_size(buf)) |
| 2977 | buf->read_index = 0; |
| 2978 | |
| 2979 | ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index), |
| 2980 | buf->read_index); |
| 2981 | if (ret < 0) |
| 2982 | return ret; |
| 2983 | |
| 2984 | /* update avail to account for words read */ |
| 2985 | buf->avail -= nwords; |
| 2986 | |
| 2987 | return nwords; |
| 2988 | } |
| 2989 | |
| 2990 | static int wm_adsp_compr_read(struct wm_adsp_compr *compr, |
| 2991 | char __user *buf, size_t count) |
| 2992 | { |
| 2993 | struct wm_adsp *dsp = compr->dsp; |
| 2994 | int ntotal = 0; |
| 2995 | int nwords, nbytes; |
| 2996 | |
| 2997 | adsp_dbg(dsp, "Requested read of %zu bytes\n", count); |
| 2998 | |
| 2999 | if (!compr->buf) |
| 3000 | return -ENXIO; |
| 3001 | |
| 3002 | if (compr->buf->error) |
| 3003 | return -EIO; |
| 3004 | |
| 3005 | count /= WM_ADSP_DATA_WORD_SIZE; |
| 3006 | |
| 3007 | do { |
| 3008 | nwords = wm_adsp_buffer_capture_block(compr, count); |
| 3009 | if (nwords < 0) { |
| 3010 | adsp_err(dsp, "Failed to capture block: %d\n", nwords); |
| 3011 | return nwords; |
| 3012 | } |
| 3013 | |
| 3014 | nbytes = nwords * WM_ADSP_DATA_WORD_SIZE; |
| 3015 | |
| 3016 | adsp_dbg(dsp, "Read %d bytes\n", nbytes); |
| 3017 | |
| 3018 | if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) { |
| 3019 | adsp_err(dsp, "Failed to copy data to user: %d, %d\n", |
| 3020 | ntotal, nbytes); |
| 3021 | return -EFAULT; |
| 3022 | } |
| 3023 | |
| 3024 | count -= nwords; |
| 3025 | ntotal += nbytes; |
| 3026 | } while (nwords > 0 && count > 0); |
| 3027 | |
| 3028 | compr->copied_total += ntotal; |
| 3029 | |
| 3030 | return ntotal; |
| 3031 | } |
| 3032 | |
| 3033 | int wm_adsp_compr_copy(struct snd_compr_stream *stream, char __user *buf, |
| 3034 | size_t count) |
| 3035 | { |
| 3036 | struct wm_adsp_compr *compr = stream->runtime->private_data; |
| 3037 | struct wm_adsp *dsp = compr->dsp; |
| 3038 | int ret; |
| 3039 | |
| 3040 | mutex_lock(&dsp->pwr_lock); |
| 3041 | |
| 3042 | if (stream->direction == SND_COMPRESS_CAPTURE) |
| 3043 | ret = wm_adsp_compr_read(compr, buf, count); |
| 3044 | else |
| 3045 | ret = -ENOTSUPP; |
| 3046 | |
| 3047 | mutex_unlock(&dsp->pwr_lock); |
| 3048 | |
| 3049 | return ret; |
| 3050 | } |
| 3051 | EXPORT_SYMBOL_GPL(wm_adsp_compr_copy); |
| 3052 | |
Praveen Diwakar | 0a37c6e | 2014-07-04 11:17:41 +0530 | [diff] [blame] | 3053 | MODULE_LICENSE("GPL v2"); |