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