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