1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * wm_adsp.c -- Wolfson ADSP support 4 * 5 * Copyright 2012 Wolfson Microelectronics plc 6 * 7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 8 */ 9 10 #include <linux/ctype.h> 11 #include <linux/module.h> 12 #include <linux/moduleparam.h> 13 #include <linux/init.h> 14 #include <linux/delay.h> 15 #include <linux/firmware.h> 16 #include <linux/list.h> 17 #include <linux/pm.h> 18 #include <linux/pm_runtime.h> 19 #include <linux/regmap.h> 20 #include <linux/regulator/consumer.h> 21 #include <linux/slab.h> 22 #include <linux/vmalloc.h> 23 #include <linux/workqueue.h> 24 #include <linux/debugfs.h> 25 #include <sound/core.h> 26 #include <sound/pcm.h> 27 #include <sound/pcm_params.h> 28 #include <sound/soc.h> 29 #include <sound/jack.h> 30 #include <sound/initval.h> 31 #include <sound/tlv.h> 32 33 #include "wm_adsp.h" 34 35 #define adsp_crit(_dsp, fmt, ...) \ 36 dev_crit(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__) 37 #define adsp_err(_dsp, fmt, ...) \ 38 dev_err(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__) 39 #define adsp_warn(_dsp, fmt, ...) \ 40 dev_warn(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__) 41 #define adsp_info(_dsp, fmt, ...) \ 42 dev_info(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__) 43 #define adsp_dbg(_dsp, fmt, ...) \ 44 dev_dbg(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__) 45 46 #define compr_err(_obj, fmt, ...) \ 47 adsp_err(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \ 48 ##__VA_ARGS__) 49 #define compr_dbg(_obj, fmt, ...) \ 50 adsp_dbg(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \ 51 ##__VA_ARGS__) 52 53 #define ADSP1_CONTROL_1 0x00 54 #define ADSP1_CONTROL_2 0x02 55 #define ADSP1_CONTROL_3 0x03 56 #define ADSP1_CONTROL_4 0x04 57 #define ADSP1_CONTROL_5 0x06 58 #define ADSP1_CONTROL_6 0x07 59 #define ADSP1_CONTROL_7 0x08 60 #define ADSP1_CONTROL_8 0x09 61 #define ADSP1_CONTROL_9 0x0A 62 #define ADSP1_CONTROL_10 0x0B 63 #define ADSP1_CONTROL_11 0x0C 64 #define ADSP1_CONTROL_12 0x0D 65 #define ADSP1_CONTROL_13 0x0F 66 #define ADSP1_CONTROL_14 0x10 67 #define ADSP1_CONTROL_15 0x11 68 #define ADSP1_CONTROL_16 0x12 69 #define ADSP1_CONTROL_17 0x13 70 #define ADSP1_CONTROL_18 0x14 71 #define ADSP1_CONTROL_19 0x16 72 #define ADSP1_CONTROL_20 0x17 73 #define ADSP1_CONTROL_21 0x18 74 #define ADSP1_CONTROL_22 0x1A 75 #define ADSP1_CONTROL_23 0x1B 76 #define ADSP1_CONTROL_24 0x1C 77 #define ADSP1_CONTROL_25 0x1E 78 #define ADSP1_CONTROL_26 0x20 79 #define ADSP1_CONTROL_27 0x21 80 #define ADSP1_CONTROL_28 0x22 81 #define ADSP1_CONTROL_29 0x23 82 #define ADSP1_CONTROL_30 0x24 83 #define ADSP1_CONTROL_31 0x26 84 85 /* 86 * ADSP1 Control 19 87 */ 88 #define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */ 89 #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */ 90 #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */ 91 92 93 /* 94 * ADSP1 Control 30 95 */ 96 #define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */ 97 #define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */ 98 #define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */ 99 #define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */ 100 #define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */ 101 #define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */ 102 #define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */ 103 #define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */ 104 #define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */ 105 #define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */ 106 #define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */ 107 #define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */ 108 #define ADSP1_START 0x0001 /* DSP1_START */ 109 #define ADSP1_START_MASK 0x0001 /* DSP1_START */ 110 #define ADSP1_START_SHIFT 0 /* DSP1_START */ 111 #define ADSP1_START_WIDTH 1 /* DSP1_START */ 112 113 /* 114 * ADSP1 Control 31 115 */ 116 #define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */ 117 #define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */ 118 #define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */ 119 120 #define ADSP2_CONTROL 0x0 121 #define ADSP2_CLOCKING 0x1 122 #define ADSP2V2_CLOCKING 0x2 123 #define ADSP2_STATUS1 0x4 124 #define ADSP2_WDMA_CONFIG_1 0x30 125 #define ADSP2_WDMA_CONFIG_2 0x31 126 #define ADSP2V2_WDMA_CONFIG_2 0x32 127 #define ADSP2_RDMA_CONFIG_1 0x34 128 129 #define ADSP2_SCRATCH0 0x40 130 #define ADSP2_SCRATCH1 0x41 131 #define ADSP2_SCRATCH2 0x42 132 #define ADSP2_SCRATCH3 0x43 133 134 #define ADSP2V2_SCRATCH0_1 0x40 135 #define ADSP2V2_SCRATCH2_3 0x42 136 137 /* 138 * ADSP2 Control 139 */ 140 141 #define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */ 142 #define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */ 143 #define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */ 144 #define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */ 145 #define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */ 146 #define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */ 147 #define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */ 148 #define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */ 149 #define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */ 150 #define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */ 151 #define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */ 152 #define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */ 153 #define ADSP2_START 0x0001 /* DSP1_START */ 154 #define ADSP2_START_MASK 0x0001 /* DSP1_START */ 155 #define ADSP2_START_SHIFT 0 /* DSP1_START */ 156 #define ADSP2_START_WIDTH 1 /* DSP1_START */ 157 158 /* 159 * ADSP2 clocking 160 */ 161 #define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */ 162 #define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */ 163 #define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */ 164 165 /* 166 * ADSP2V2 clocking 167 */ 168 #define ADSP2V2_CLK_SEL_MASK 0x70000 /* CLK_SEL_ENA */ 169 #define ADSP2V2_CLK_SEL_SHIFT 16 /* CLK_SEL_ENA */ 170 #define ADSP2V2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */ 171 172 #define ADSP2V2_RATE_MASK 0x7800 /* DSP_RATE */ 173 #define ADSP2V2_RATE_SHIFT 11 /* DSP_RATE */ 174 #define ADSP2V2_RATE_WIDTH 4 /* DSP_RATE */ 175 176 /* 177 * ADSP2 Status 1 178 */ 179 #define ADSP2_RAM_RDY 0x0001 180 #define ADSP2_RAM_RDY_MASK 0x0001 181 #define ADSP2_RAM_RDY_SHIFT 0 182 #define ADSP2_RAM_RDY_WIDTH 1 183 184 /* 185 * ADSP2 Lock support 186 */ 187 #define ADSP2_LOCK_CODE_0 0x5555 188 #define ADSP2_LOCK_CODE_1 0xAAAA 189 190 #define ADSP2_WATCHDOG 0x0A 191 #define ADSP2_BUS_ERR_ADDR 0x52 192 #define ADSP2_REGION_LOCK_STATUS 0x64 193 #define ADSP2_LOCK_REGION_1_LOCK_REGION_0 0x66 194 #define ADSP2_LOCK_REGION_3_LOCK_REGION_2 0x68 195 #define ADSP2_LOCK_REGION_5_LOCK_REGION_4 0x6A 196 #define ADSP2_LOCK_REGION_7_LOCK_REGION_6 0x6C 197 #define ADSP2_LOCK_REGION_9_LOCK_REGION_8 0x6E 198 #define ADSP2_LOCK_REGION_CTRL 0x7A 199 #define ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR 0x7C 200 201 #define ADSP2_REGION_LOCK_ERR_MASK 0x8000 202 #define ADSP2_ADDR_ERR_MASK 0x4000 203 #define ADSP2_WDT_TIMEOUT_STS_MASK 0x2000 204 #define ADSP2_CTRL_ERR_PAUSE_ENA 0x0002 205 #define ADSP2_CTRL_ERR_EINT 0x0001 206 207 #define ADSP2_BUS_ERR_ADDR_MASK 0x00FFFFFF 208 #define ADSP2_XMEM_ERR_ADDR_MASK 0x0000FFFF 209 #define ADSP2_PMEM_ERR_ADDR_MASK 0x7FFF0000 210 #define ADSP2_PMEM_ERR_ADDR_SHIFT 16 211 #define ADSP2_WDT_ENA_MASK 0xFFFFFFFD 212 213 #define ADSP2_LOCK_REGION_SHIFT 16 214 215 #define ADSP_MAX_STD_CTRL_SIZE 512 216 217 #define WM_ADSP_ACKED_CTL_TIMEOUT_MS 100 218 #define WM_ADSP_ACKED_CTL_N_QUICKPOLLS 10 219 #define WM_ADSP_ACKED_CTL_MIN_VALUE 0 220 #define WM_ADSP_ACKED_CTL_MAX_VALUE 0xFFFFFF 221 222 /* 223 * Event control messages 224 */ 225 #define WM_ADSP_FW_EVENT_SHUTDOWN 0x000001 226 227 /* 228 * HALO system info 229 */ 230 #define HALO_AHBM_WINDOW_DEBUG_0 0x02040 231 #define HALO_AHBM_WINDOW_DEBUG_1 0x02044 232 233 /* 234 * HALO core 235 */ 236 #define HALO_SCRATCH1 0x005c0 237 #define HALO_SCRATCH2 0x005c8 238 #define HALO_SCRATCH3 0x005d0 239 #define HALO_SCRATCH4 0x005d8 240 #define HALO_CCM_CORE_CONTROL 0x41000 241 #define HALO_CORE_SOFT_RESET 0x00010 242 #define HALO_WDT_CONTROL 0x47000 243 244 /* 245 * HALO MPU banks 246 */ 247 #define HALO_MPU_XMEM_ACCESS_0 0x43000 248 #define HALO_MPU_YMEM_ACCESS_0 0x43004 249 #define HALO_MPU_WINDOW_ACCESS_0 0x43008 250 #define HALO_MPU_XREG_ACCESS_0 0x4300C 251 #define HALO_MPU_YREG_ACCESS_0 0x43014 252 #define HALO_MPU_XMEM_ACCESS_1 0x43018 253 #define HALO_MPU_YMEM_ACCESS_1 0x4301C 254 #define HALO_MPU_WINDOW_ACCESS_1 0x43020 255 #define HALO_MPU_XREG_ACCESS_1 0x43024 256 #define HALO_MPU_YREG_ACCESS_1 0x4302C 257 #define HALO_MPU_XMEM_ACCESS_2 0x43030 258 #define HALO_MPU_YMEM_ACCESS_2 0x43034 259 #define HALO_MPU_WINDOW_ACCESS_2 0x43038 260 #define HALO_MPU_XREG_ACCESS_2 0x4303C 261 #define HALO_MPU_YREG_ACCESS_2 0x43044 262 #define HALO_MPU_XMEM_ACCESS_3 0x43048 263 #define HALO_MPU_YMEM_ACCESS_3 0x4304C 264 #define HALO_MPU_WINDOW_ACCESS_3 0x43050 265 #define HALO_MPU_XREG_ACCESS_3 0x43054 266 #define HALO_MPU_YREG_ACCESS_3 0x4305C 267 #define HALO_MPU_XM_VIO_ADDR 0x43100 268 #define HALO_MPU_XM_VIO_STATUS 0x43104 269 #define HALO_MPU_YM_VIO_ADDR 0x43108 270 #define HALO_MPU_YM_VIO_STATUS 0x4310C 271 #define HALO_MPU_PM_VIO_ADDR 0x43110 272 #define HALO_MPU_PM_VIO_STATUS 0x43114 273 #define HALO_MPU_LOCK_CONFIG 0x43140 274 275 /* 276 * HALO_AHBM_WINDOW_DEBUG_1 277 */ 278 #define HALO_AHBM_CORE_ERR_ADDR_MASK 0x0fffff00 279 #define HALO_AHBM_CORE_ERR_ADDR_SHIFT 8 280 #define HALO_AHBM_FLAGS_ERR_MASK 0x000000ff 281 282 /* 283 * HALO_CCM_CORE_CONTROL 284 */ 285 #define HALO_CORE_EN 0x00000001 286 287 /* 288 * HALO_CORE_SOFT_RESET 289 */ 290 #define HALO_CORE_SOFT_RESET_MASK 0x00000001 291 292 /* 293 * HALO_WDT_CONTROL 294 */ 295 #define HALO_WDT_EN_MASK 0x00000001 296 297 /* 298 * HALO_MPU_?M_VIO_STATUS 299 */ 300 #define HALO_MPU_VIO_STS_MASK 0x007e0000 301 #define HALO_MPU_VIO_STS_SHIFT 17 302 #define HALO_MPU_VIO_ERR_WR_MASK 0x00008000 303 #define HALO_MPU_VIO_ERR_SRC_MASK 0x00007fff 304 #define HALO_MPU_VIO_ERR_SRC_SHIFT 0 305 306 static struct wm_adsp_ops wm_adsp1_ops; 307 static struct wm_adsp_ops wm_adsp2_ops[]; 308 static struct wm_adsp_ops wm_halo_ops; 309 310 struct wm_adsp_buf { 311 struct list_head list; 312 void *buf; 313 }; 314 315 static struct wm_adsp_buf *wm_adsp_buf_alloc(const void *src, size_t len, 316 struct list_head *list) 317 { 318 struct wm_adsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL); 319 320 if (buf == NULL) 321 return NULL; 322 323 buf->buf = vmalloc(len); 324 if (!buf->buf) { 325 kfree(buf); 326 return NULL; 327 } 328 memcpy(buf->buf, src, len); 329 330 if (list) 331 list_add_tail(&buf->list, list); 332 333 return buf; 334 } 335 336 static void wm_adsp_buf_free(struct list_head *list) 337 { 338 while (!list_empty(list)) { 339 struct wm_adsp_buf *buf = list_first_entry(list, 340 struct wm_adsp_buf, 341 list); 342 list_del(&buf->list); 343 vfree(buf->buf); 344 kfree(buf); 345 } 346 } 347 348 #define WM_ADSP_FW_MBC_VSS 0 349 #define WM_ADSP_FW_HIFI 1 350 #define WM_ADSP_FW_TX 2 351 #define WM_ADSP_FW_TX_SPK 3 352 #define WM_ADSP_FW_RX 4 353 #define WM_ADSP_FW_RX_ANC 5 354 #define WM_ADSP_FW_CTRL 6 355 #define WM_ADSP_FW_ASR 7 356 #define WM_ADSP_FW_TRACE 8 357 #define WM_ADSP_FW_SPK_PROT 9 358 #define WM_ADSP_FW_SPK_CALI 10 359 #define WM_ADSP_FW_SPK_DIAG 11 360 #define WM_ADSP_FW_MISC 12 361 362 #define WM_ADSP_NUM_FW 13 363 364 static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = { 365 [WM_ADSP_FW_MBC_VSS] = "MBC/VSS", 366 [WM_ADSP_FW_HIFI] = "MasterHiFi", 367 [WM_ADSP_FW_TX] = "Tx", 368 [WM_ADSP_FW_TX_SPK] = "Tx Speaker", 369 [WM_ADSP_FW_RX] = "Rx", 370 [WM_ADSP_FW_RX_ANC] = "Rx ANC", 371 [WM_ADSP_FW_CTRL] = "Voice Ctrl", 372 [WM_ADSP_FW_ASR] = "ASR Assist", 373 [WM_ADSP_FW_TRACE] = "Dbg Trace", 374 [WM_ADSP_FW_SPK_PROT] = "Protection", 375 [WM_ADSP_FW_SPK_CALI] = "Calibration", 376 [WM_ADSP_FW_SPK_DIAG] = "Diagnostic", 377 [WM_ADSP_FW_MISC] = "Misc", 378 }; 379 380 struct wm_adsp_system_config_xm_hdr { 381 __be32 sys_enable; 382 __be32 fw_id; 383 __be32 fw_rev; 384 __be32 boot_status; 385 __be32 watchdog; 386 __be32 dma_buffer_size; 387 __be32 rdma[6]; 388 __be32 wdma[8]; 389 __be32 build_job_name[3]; 390 __be32 build_job_number; 391 }; 392 393 struct wm_halo_system_config_xm_hdr { 394 __be32 halo_heartbeat; 395 __be32 build_job_name[3]; 396 __be32 build_job_number; 397 }; 398 399 struct wm_adsp_alg_xm_struct { 400 __be32 magic; 401 __be32 smoothing; 402 __be32 threshold; 403 __be32 host_buf_ptr; 404 __be32 start_seq; 405 __be32 high_water_mark; 406 __be32 low_water_mark; 407 __be64 smoothed_power; 408 }; 409 410 struct wm_adsp_host_buf_coeff_v1 { 411 __be32 host_buf_ptr; /* Host buffer pointer */ 412 __be32 versions; /* Version numbers */ 413 __be32 name[4]; /* The buffer name */ 414 }; 415 416 struct wm_adsp_buffer { 417 __be32 buf1_base; /* Base addr of first buffer area */ 418 __be32 buf1_size; /* Size of buf1 area in DSP words */ 419 __be32 buf2_base; /* Base addr of 2nd buffer area */ 420 __be32 buf1_buf2_size; /* Size of buf1+buf2 in DSP words */ 421 __be32 buf3_base; /* Base addr of buf3 area */ 422 __be32 buf_total_size; /* Size of buf1+buf2+buf3 in DSP words */ 423 __be32 high_water_mark; /* Point at which IRQ is asserted */ 424 __be32 irq_count; /* bits 1-31 count IRQ assertions */ 425 __be32 irq_ack; /* acked IRQ count, bit 0 enables IRQ */ 426 __be32 next_write_index; /* word index of next write */ 427 __be32 next_read_index; /* word index of next read */ 428 __be32 error; /* error if any */ 429 __be32 oldest_block_index; /* word index of oldest surviving */ 430 __be32 requested_rewind; /* how many blocks rewind was done */ 431 __be32 reserved_space; /* internal */ 432 __be32 min_free; /* min free space since stream start */ 433 __be32 blocks_written[2]; /* total blocks written (64 bit) */ 434 __be32 words_written[2]; /* total words written (64 bit) */ 435 }; 436 437 struct wm_adsp_compr; 438 439 struct wm_adsp_compr_buf { 440 struct list_head list; 441 struct wm_adsp *dsp; 442 struct wm_adsp_compr *compr; 443 444 struct wm_adsp_buffer_region *regions; 445 u32 host_buf_ptr; 446 447 u32 error; 448 u32 irq_count; 449 int read_index; 450 int avail; 451 int host_buf_mem_type; 452 453 char *name; 454 }; 455 456 struct wm_adsp_compr { 457 struct list_head list; 458 struct wm_adsp *dsp; 459 struct wm_adsp_compr_buf *buf; 460 461 struct snd_compr_stream *stream; 462 struct snd_compressed_buffer size; 463 464 u32 *raw_buf; 465 unsigned int copied_total; 466 467 unsigned int sample_rate; 468 469 const char *name; 470 }; 471 472 #define WM_ADSP_DATA_WORD_SIZE 3 473 474 #define WM_ADSP_MIN_FRAGMENTS 1 475 #define WM_ADSP_MAX_FRAGMENTS 256 476 #define WM_ADSP_MIN_FRAGMENT_SIZE (64 * WM_ADSP_DATA_WORD_SIZE) 477 #define WM_ADSP_MAX_FRAGMENT_SIZE (4096 * WM_ADSP_DATA_WORD_SIZE) 478 479 #define WM_ADSP_ALG_XM_STRUCT_MAGIC 0x49aec7 480 481 #define HOST_BUFFER_FIELD(field) \ 482 (offsetof(struct wm_adsp_buffer, field) / sizeof(__be32)) 483 484 #define ALG_XM_FIELD(field) \ 485 (offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32)) 486 487 #define HOST_BUF_COEFF_SUPPORTED_COMPAT_VER 1 488 489 #define HOST_BUF_COEFF_COMPAT_VER_MASK 0xFF00 490 #define HOST_BUF_COEFF_COMPAT_VER_SHIFT 8 491 492 static int wm_adsp_buffer_init(struct wm_adsp *dsp); 493 static int wm_adsp_buffer_free(struct wm_adsp *dsp); 494 495 struct wm_adsp_buffer_region { 496 unsigned int offset; 497 unsigned int cumulative_size; 498 unsigned int mem_type; 499 unsigned int base_addr; 500 }; 501 502 struct wm_adsp_buffer_region_def { 503 unsigned int mem_type; 504 unsigned int base_offset; 505 unsigned int size_offset; 506 }; 507 508 static const struct wm_adsp_buffer_region_def default_regions[] = { 509 { 510 .mem_type = WMFW_ADSP2_XM, 511 .base_offset = HOST_BUFFER_FIELD(buf1_base), 512 .size_offset = HOST_BUFFER_FIELD(buf1_size), 513 }, 514 { 515 .mem_type = WMFW_ADSP2_XM, 516 .base_offset = HOST_BUFFER_FIELD(buf2_base), 517 .size_offset = HOST_BUFFER_FIELD(buf1_buf2_size), 518 }, 519 { 520 .mem_type = WMFW_ADSP2_YM, 521 .base_offset = HOST_BUFFER_FIELD(buf3_base), 522 .size_offset = HOST_BUFFER_FIELD(buf_total_size), 523 }, 524 }; 525 526 struct wm_adsp_fw_caps { 527 u32 id; 528 struct snd_codec_desc desc; 529 int num_regions; 530 const struct wm_adsp_buffer_region_def *region_defs; 531 }; 532 533 static const struct wm_adsp_fw_caps ctrl_caps[] = { 534 { 535 .id = SND_AUDIOCODEC_BESPOKE, 536 .desc = { 537 .max_ch = 8, 538 .sample_rates = { 16000 }, 539 .num_sample_rates = 1, 540 .formats = SNDRV_PCM_FMTBIT_S16_LE, 541 }, 542 .num_regions = ARRAY_SIZE(default_regions), 543 .region_defs = default_regions, 544 }, 545 }; 546 547 static const struct wm_adsp_fw_caps trace_caps[] = { 548 { 549 .id = SND_AUDIOCODEC_BESPOKE, 550 .desc = { 551 .max_ch = 8, 552 .sample_rates = { 553 4000, 8000, 11025, 12000, 16000, 22050, 554 24000, 32000, 44100, 48000, 64000, 88200, 555 96000, 176400, 192000 556 }, 557 .num_sample_rates = 15, 558 .formats = SNDRV_PCM_FMTBIT_S16_LE, 559 }, 560 .num_regions = ARRAY_SIZE(default_regions), 561 .region_defs = default_regions, 562 }, 563 }; 564 565 static const struct { 566 const char *file; 567 int compr_direction; 568 int num_caps; 569 const struct wm_adsp_fw_caps *caps; 570 bool voice_trigger; 571 } wm_adsp_fw[WM_ADSP_NUM_FW] = { 572 [WM_ADSP_FW_MBC_VSS] = { .file = "mbc-vss" }, 573 [WM_ADSP_FW_HIFI] = { .file = "hifi" }, 574 [WM_ADSP_FW_TX] = { .file = "tx" }, 575 [WM_ADSP_FW_TX_SPK] = { .file = "tx-spk" }, 576 [WM_ADSP_FW_RX] = { .file = "rx" }, 577 [WM_ADSP_FW_RX_ANC] = { .file = "rx-anc" }, 578 [WM_ADSP_FW_CTRL] = { 579 .file = "ctrl", 580 .compr_direction = SND_COMPRESS_CAPTURE, 581 .num_caps = ARRAY_SIZE(ctrl_caps), 582 .caps = ctrl_caps, 583 .voice_trigger = true, 584 }, 585 [WM_ADSP_FW_ASR] = { .file = "asr" }, 586 [WM_ADSP_FW_TRACE] = { 587 .file = "trace", 588 .compr_direction = SND_COMPRESS_CAPTURE, 589 .num_caps = ARRAY_SIZE(trace_caps), 590 .caps = trace_caps, 591 }, 592 [WM_ADSP_FW_SPK_PROT] = { .file = "spk-prot" }, 593 [WM_ADSP_FW_SPK_CALI] = { .file = "spk-cali" }, 594 [WM_ADSP_FW_SPK_DIAG] = { .file = "spk-diag" }, 595 [WM_ADSP_FW_MISC] = { .file = "misc" }, 596 }; 597 598 struct wm_coeff_ctl_ops { 599 int (*xget)(struct snd_kcontrol *kcontrol, 600 struct snd_ctl_elem_value *ucontrol); 601 int (*xput)(struct snd_kcontrol *kcontrol, 602 struct snd_ctl_elem_value *ucontrol); 603 }; 604 605 struct wm_coeff_ctl { 606 const char *name; 607 const char *fw_name; 608 /* Subname is needed to match with firmware */ 609 const char *subname; 610 unsigned int subname_len; 611 struct wm_adsp_alg_region alg_region; 612 struct wm_coeff_ctl_ops ops; 613 struct wm_adsp *dsp; 614 unsigned int enabled:1; 615 struct list_head list; 616 void *cache; 617 unsigned int offset; 618 size_t len; 619 unsigned int set:1; 620 struct soc_bytes_ext bytes_ext; 621 unsigned int flags; 622 unsigned int type; 623 }; 624 625 static const char *wm_adsp_mem_region_name(unsigned int type) 626 { 627 switch (type) { 628 case WMFW_ADSP1_PM: 629 return "PM"; 630 case WMFW_HALO_PM_PACKED: 631 return "PM_PACKED"; 632 case WMFW_ADSP1_DM: 633 return "DM"; 634 case WMFW_ADSP2_XM: 635 return "XM"; 636 case WMFW_HALO_XM_PACKED: 637 return "XM_PACKED"; 638 case WMFW_ADSP2_YM: 639 return "YM"; 640 case WMFW_HALO_YM_PACKED: 641 return "YM_PACKED"; 642 case WMFW_ADSP1_ZM: 643 return "ZM"; 644 default: 645 return NULL; 646 } 647 } 648 649 #ifdef CONFIG_DEBUG_FS 650 static void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, const char *s) 651 { 652 char *tmp = kasprintf(GFP_KERNEL, "%s\n", s); 653 654 kfree(dsp->wmfw_file_name); 655 dsp->wmfw_file_name = tmp; 656 } 657 658 static void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, const char *s) 659 { 660 char *tmp = kasprintf(GFP_KERNEL, "%s\n", s); 661 662 kfree(dsp->bin_file_name); 663 dsp->bin_file_name = tmp; 664 } 665 666 static void wm_adsp_debugfs_clear(struct wm_adsp *dsp) 667 { 668 kfree(dsp->wmfw_file_name); 669 kfree(dsp->bin_file_name); 670 dsp->wmfw_file_name = NULL; 671 dsp->bin_file_name = NULL; 672 } 673 674 static ssize_t wm_adsp_debugfs_wmfw_read(struct file *file, 675 char __user *user_buf, 676 size_t count, loff_t *ppos) 677 { 678 struct wm_adsp *dsp = file->private_data; 679 ssize_t ret; 680 681 mutex_lock(&dsp->pwr_lock); 682 683 if (!dsp->wmfw_file_name || !dsp->booted) 684 ret = 0; 685 else 686 ret = simple_read_from_buffer(user_buf, count, ppos, 687 dsp->wmfw_file_name, 688 strlen(dsp->wmfw_file_name)); 689 690 mutex_unlock(&dsp->pwr_lock); 691 return ret; 692 } 693 694 static ssize_t wm_adsp_debugfs_bin_read(struct file *file, 695 char __user *user_buf, 696 size_t count, loff_t *ppos) 697 { 698 struct wm_adsp *dsp = file->private_data; 699 ssize_t ret; 700 701 mutex_lock(&dsp->pwr_lock); 702 703 if (!dsp->bin_file_name || !dsp->booted) 704 ret = 0; 705 else 706 ret = simple_read_from_buffer(user_buf, count, ppos, 707 dsp->bin_file_name, 708 strlen(dsp->bin_file_name)); 709 710 mutex_unlock(&dsp->pwr_lock); 711 return ret; 712 } 713 714 static const struct { 715 const char *name; 716 const struct file_operations fops; 717 } wm_adsp_debugfs_fops[] = { 718 { 719 .name = "wmfw_file_name", 720 .fops = { 721 .open = simple_open, 722 .read = wm_adsp_debugfs_wmfw_read, 723 }, 724 }, 725 { 726 .name = "bin_file_name", 727 .fops = { 728 .open = simple_open, 729 .read = wm_adsp_debugfs_bin_read, 730 }, 731 }, 732 }; 733 734 static void wm_adsp2_init_debugfs(struct wm_adsp *dsp, 735 struct snd_soc_component *component) 736 { 737 struct dentry *root = NULL; 738 int i; 739 740 root = debugfs_create_dir(dsp->name, component->debugfs_root); 741 742 debugfs_create_bool("booted", 0444, root, &dsp->booted); 743 debugfs_create_bool("running", 0444, root, &dsp->running); 744 debugfs_create_x32("fw_id", 0444, root, &dsp->fw_id); 745 debugfs_create_x32("fw_version", 0444, root, &dsp->fw_id_version); 746 747 for (i = 0; i < ARRAY_SIZE(wm_adsp_debugfs_fops); ++i) 748 debugfs_create_file(wm_adsp_debugfs_fops[i].name, 0444, root, 749 dsp, &wm_adsp_debugfs_fops[i].fops); 750 751 dsp->debugfs_root = root; 752 } 753 754 static void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp) 755 { 756 wm_adsp_debugfs_clear(dsp); 757 debugfs_remove_recursive(dsp->debugfs_root); 758 } 759 #else 760 static inline void wm_adsp2_init_debugfs(struct wm_adsp *dsp, 761 struct snd_soc_component *component) 762 { 763 } 764 765 static inline void wm_adsp2_cleanup_debugfs(struct wm_adsp *dsp) 766 { 767 } 768 769 static inline void wm_adsp_debugfs_save_wmfwname(struct wm_adsp *dsp, 770 const char *s) 771 { 772 } 773 774 static inline void wm_adsp_debugfs_save_binname(struct wm_adsp *dsp, 775 const char *s) 776 { 777 } 778 779 static inline void wm_adsp_debugfs_clear(struct wm_adsp *dsp) 780 { 781 } 782 #endif 783 784 int wm_adsp_fw_get(struct snd_kcontrol *kcontrol, 785 struct snd_ctl_elem_value *ucontrol) 786 { 787 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 788 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 789 struct wm_adsp *dsp = snd_soc_component_get_drvdata(component); 790 791 ucontrol->value.enumerated.item[0] = dsp[e->shift_l].fw; 792 793 return 0; 794 } 795 EXPORT_SYMBOL_GPL(wm_adsp_fw_get); 796 797 int wm_adsp_fw_put(struct snd_kcontrol *kcontrol, 798 struct snd_ctl_elem_value *ucontrol) 799 { 800 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 801 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 802 struct wm_adsp *dsp = snd_soc_component_get_drvdata(component); 803 int ret = 0; 804 805 if (ucontrol->value.enumerated.item[0] == dsp[e->shift_l].fw) 806 return 0; 807 808 if (ucontrol->value.enumerated.item[0] >= WM_ADSP_NUM_FW) 809 return -EINVAL; 810 811 mutex_lock(&dsp[e->shift_l].pwr_lock); 812 813 if (dsp[e->shift_l].booted || !list_empty(&dsp[e->shift_l].compr_list)) 814 ret = -EBUSY; 815 else 816 dsp[e->shift_l].fw = ucontrol->value.enumerated.item[0]; 817 818 mutex_unlock(&dsp[e->shift_l].pwr_lock); 819 820 return ret; 821 } 822 EXPORT_SYMBOL_GPL(wm_adsp_fw_put); 823 824 const struct soc_enum wm_adsp_fw_enum[] = { 825 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 826 SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 827 SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 828 SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 829 SOC_ENUM_SINGLE(0, 4, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 830 SOC_ENUM_SINGLE(0, 5, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 831 SOC_ENUM_SINGLE(0, 6, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 832 }; 833 EXPORT_SYMBOL_GPL(wm_adsp_fw_enum); 834 835 static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp, 836 int type) 837 { 838 int i; 839 840 for (i = 0; i < dsp->num_mems; i++) 841 if (dsp->mem[i].type == type) 842 return &dsp->mem[i]; 843 844 return NULL; 845 } 846 847 static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *mem, 848 unsigned int offset) 849 { 850 switch (mem->type) { 851 case WMFW_ADSP1_PM: 852 return mem->base + (offset * 3); 853 case WMFW_ADSP1_DM: 854 case WMFW_ADSP2_XM: 855 case WMFW_ADSP2_YM: 856 case WMFW_ADSP1_ZM: 857 return mem->base + (offset * 2); 858 default: 859 WARN(1, "Unknown memory region type"); 860 return offset; 861 } 862 } 863 864 static unsigned int wm_halo_region_to_reg(struct wm_adsp_region const *mem, 865 unsigned int offset) 866 { 867 switch (mem->type) { 868 case WMFW_ADSP2_XM: 869 case WMFW_ADSP2_YM: 870 return mem->base + (offset * 4); 871 case WMFW_HALO_XM_PACKED: 872 case WMFW_HALO_YM_PACKED: 873 return (mem->base + (offset * 3)) & ~0x3; 874 case WMFW_HALO_PM_PACKED: 875 return mem->base + (offset * 5); 876 default: 877 WARN(1, "Unknown memory region type"); 878 return offset; 879 } 880 } 881 882 static void wm_adsp_read_fw_status(struct wm_adsp *dsp, 883 int noffs, unsigned int *offs) 884 { 885 unsigned int i; 886 int ret; 887 888 for (i = 0; i < noffs; ++i) { 889 ret = regmap_read(dsp->regmap, dsp->base + offs[i], &offs[i]); 890 if (ret) { 891 adsp_err(dsp, "Failed to read SCRATCH%u: %d\n", i, ret); 892 return; 893 } 894 } 895 } 896 897 static void wm_adsp2_show_fw_status(struct wm_adsp *dsp) 898 { 899 unsigned int offs[] = { 900 ADSP2_SCRATCH0, ADSP2_SCRATCH1, ADSP2_SCRATCH2, ADSP2_SCRATCH3, 901 }; 902 903 wm_adsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs); 904 905 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n", 906 offs[0], offs[1], offs[2], offs[3]); 907 } 908 909 static void wm_adsp2v2_show_fw_status(struct wm_adsp *dsp) 910 { 911 unsigned int offs[] = { ADSP2V2_SCRATCH0_1, ADSP2V2_SCRATCH2_3 }; 912 913 wm_adsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs); 914 915 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n", 916 offs[0] & 0xFFFF, offs[0] >> 16, 917 offs[1] & 0xFFFF, offs[1] >> 16); 918 } 919 920 static void wm_halo_show_fw_status(struct wm_adsp *dsp) 921 { 922 unsigned int offs[] = { 923 HALO_SCRATCH1, HALO_SCRATCH2, HALO_SCRATCH3, HALO_SCRATCH4, 924 }; 925 926 wm_adsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs); 927 928 adsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n", 929 offs[0], offs[1], offs[2], offs[3]); 930 } 931 932 static inline struct wm_coeff_ctl *bytes_ext_to_ctl(struct soc_bytes_ext *ext) 933 { 934 return container_of(ext, struct wm_coeff_ctl, bytes_ext); 935 } 936 937 static int wm_coeff_base_reg(struct wm_coeff_ctl *ctl, unsigned int *reg) 938 { 939 const struct wm_adsp_alg_region *alg_region = &ctl->alg_region; 940 struct wm_adsp *dsp = ctl->dsp; 941 const struct wm_adsp_region *mem; 942 943 mem = wm_adsp_find_region(dsp, alg_region->type); 944 if (!mem) { 945 adsp_err(dsp, "No base for region %x\n", 946 alg_region->type); 947 return -EINVAL; 948 } 949 950 *reg = dsp->ops->region_to_reg(mem, ctl->alg_region.base + ctl->offset); 951 952 return 0; 953 } 954 955 static int wm_coeff_info(struct snd_kcontrol *kctl, 956 struct snd_ctl_elem_info *uinfo) 957 { 958 struct soc_bytes_ext *bytes_ext = 959 (struct soc_bytes_ext *)kctl->private_value; 960 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 961 962 switch (ctl->type) { 963 case WMFW_CTL_TYPE_ACKED: 964 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 965 uinfo->value.integer.min = WM_ADSP_ACKED_CTL_MIN_VALUE; 966 uinfo->value.integer.max = WM_ADSP_ACKED_CTL_MAX_VALUE; 967 uinfo->value.integer.step = 1; 968 uinfo->count = 1; 969 break; 970 default: 971 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; 972 uinfo->count = ctl->len; 973 break; 974 } 975 976 return 0; 977 } 978 979 static int wm_coeff_write_acked_control(struct wm_coeff_ctl *ctl, 980 unsigned int event_id) 981 { 982 struct wm_adsp *dsp = ctl->dsp; 983 u32 val = cpu_to_be32(event_id); 984 unsigned int reg; 985 int i, ret; 986 987 ret = wm_coeff_base_reg(ctl, ®); 988 if (ret) 989 return ret; 990 991 adsp_dbg(dsp, "Sending 0x%x to acked control alg 0x%x %s:0x%x\n", 992 event_id, ctl->alg_region.alg, 993 wm_adsp_mem_region_name(ctl->alg_region.type), ctl->offset); 994 995 ret = regmap_raw_write(dsp->regmap, reg, &val, sizeof(val)); 996 if (ret) { 997 adsp_err(dsp, "Failed to write %x: %d\n", reg, ret); 998 return ret; 999 } 1000 1001 /* 1002 * Poll for ack, we initially poll at ~1ms intervals for firmwares 1003 * that respond quickly, then go to ~10ms polls. A firmware is unlikely 1004 * to ack instantly so we do the first 1ms delay before reading the 1005 * control to avoid a pointless bus transaction 1006 */ 1007 for (i = 0; i < WM_ADSP_ACKED_CTL_TIMEOUT_MS;) { 1008 switch (i) { 1009 case 0 ... WM_ADSP_ACKED_CTL_N_QUICKPOLLS - 1: 1010 usleep_range(1000, 2000); 1011 i++; 1012 break; 1013 default: 1014 usleep_range(10000, 20000); 1015 i += 10; 1016 break; 1017 } 1018 1019 ret = regmap_raw_read(dsp->regmap, reg, &val, sizeof(val)); 1020 if (ret) { 1021 adsp_err(dsp, "Failed to read %x: %d\n", reg, ret); 1022 return ret; 1023 } 1024 1025 if (val == 0) { 1026 adsp_dbg(dsp, "Acked control ACKED at poll %u\n", i); 1027 return 0; 1028 } 1029 } 1030 1031 adsp_warn(dsp, "Acked control @0x%x alg:0x%x %s:0x%x timed out\n", 1032 reg, ctl->alg_region.alg, 1033 wm_adsp_mem_region_name(ctl->alg_region.type), 1034 ctl->offset); 1035 1036 return -ETIMEDOUT; 1037 } 1038 1039 static int wm_coeff_write_ctrl_raw(struct wm_coeff_ctl *ctl, 1040 const void *buf, size_t len) 1041 { 1042 struct wm_adsp *dsp = ctl->dsp; 1043 void *scratch; 1044 int ret; 1045 unsigned int reg; 1046 1047 ret = wm_coeff_base_reg(ctl, ®); 1048 if (ret) 1049 return ret; 1050 1051 scratch = kmemdup(buf, len, GFP_KERNEL | GFP_DMA); 1052 if (!scratch) 1053 return -ENOMEM; 1054 1055 ret = regmap_raw_write(dsp->regmap, reg, scratch, 1056 len); 1057 if (ret) { 1058 adsp_err(dsp, "Failed to write %zu bytes to %x: %d\n", 1059 len, reg, ret); 1060 kfree(scratch); 1061 return ret; 1062 } 1063 adsp_dbg(dsp, "Wrote %zu bytes to %x\n", len, reg); 1064 1065 kfree(scratch); 1066 1067 return 0; 1068 } 1069 1070 static int wm_coeff_write_ctrl(struct wm_coeff_ctl *ctl, 1071 const void *buf, size_t len) 1072 { 1073 int ret = 0; 1074 1075 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) 1076 ret = -EPERM; 1077 else if (buf != ctl->cache) 1078 memcpy(ctl->cache, buf, len); 1079 1080 ctl->set = 1; 1081 if (ctl->enabled && ctl->dsp->running) 1082 ret = wm_coeff_write_ctrl_raw(ctl, buf, len); 1083 1084 return ret; 1085 } 1086 1087 static int wm_coeff_put(struct snd_kcontrol *kctl, 1088 struct snd_ctl_elem_value *ucontrol) 1089 { 1090 struct soc_bytes_ext *bytes_ext = 1091 (struct soc_bytes_ext *)kctl->private_value; 1092 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 1093 char *p = ucontrol->value.bytes.data; 1094 int ret = 0; 1095 1096 mutex_lock(&ctl->dsp->pwr_lock); 1097 ret = wm_coeff_write_ctrl(ctl, p, ctl->len); 1098 mutex_unlock(&ctl->dsp->pwr_lock); 1099 1100 return ret; 1101 } 1102 1103 static int wm_coeff_tlv_put(struct snd_kcontrol *kctl, 1104 const unsigned int __user *bytes, unsigned int size) 1105 { 1106 struct soc_bytes_ext *bytes_ext = 1107 (struct soc_bytes_ext *)kctl->private_value; 1108 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 1109 int ret = 0; 1110 1111 mutex_lock(&ctl->dsp->pwr_lock); 1112 1113 if (copy_from_user(ctl->cache, bytes, size)) 1114 ret = -EFAULT; 1115 else 1116 ret = wm_coeff_write_ctrl(ctl, ctl->cache, size); 1117 1118 mutex_unlock(&ctl->dsp->pwr_lock); 1119 1120 return ret; 1121 } 1122 1123 static int wm_coeff_put_acked(struct snd_kcontrol *kctl, 1124 struct snd_ctl_elem_value *ucontrol) 1125 { 1126 struct soc_bytes_ext *bytes_ext = 1127 (struct soc_bytes_ext *)kctl->private_value; 1128 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 1129 unsigned int val = ucontrol->value.integer.value[0]; 1130 int ret; 1131 1132 if (val == 0) 1133 return 0; /* 0 means no event */ 1134 1135 mutex_lock(&ctl->dsp->pwr_lock); 1136 1137 if (ctl->enabled && ctl->dsp->running) 1138 ret = wm_coeff_write_acked_control(ctl, val); 1139 else 1140 ret = -EPERM; 1141 1142 mutex_unlock(&ctl->dsp->pwr_lock); 1143 1144 return ret; 1145 } 1146 1147 static int wm_coeff_read_ctrl_raw(struct wm_coeff_ctl *ctl, 1148 void *buf, size_t len) 1149 { 1150 struct wm_adsp *dsp = ctl->dsp; 1151 void *scratch; 1152 int ret; 1153 unsigned int reg; 1154 1155 ret = wm_coeff_base_reg(ctl, ®); 1156 if (ret) 1157 return ret; 1158 1159 scratch = kmalloc(len, GFP_KERNEL | GFP_DMA); 1160 if (!scratch) 1161 return -ENOMEM; 1162 1163 ret = regmap_raw_read(dsp->regmap, reg, scratch, len); 1164 if (ret) { 1165 adsp_err(dsp, "Failed to read %zu bytes from %x: %d\n", 1166 len, reg, ret); 1167 kfree(scratch); 1168 return ret; 1169 } 1170 adsp_dbg(dsp, "Read %zu bytes from %x\n", len, reg); 1171 1172 memcpy(buf, scratch, len); 1173 kfree(scratch); 1174 1175 return 0; 1176 } 1177 1178 static int wm_coeff_read_ctrl(struct wm_coeff_ctl *ctl, void *buf, size_t len) 1179 { 1180 int ret = 0; 1181 1182 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) { 1183 if (ctl->enabled && ctl->dsp->running) 1184 return wm_coeff_read_ctrl_raw(ctl, buf, len); 1185 else 1186 return -EPERM; 1187 } else { 1188 if (!ctl->flags && ctl->enabled && ctl->dsp->running) 1189 ret = wm_coeff_read_ctrl_raw(ctl, ctl->cache, ctl->len); 1190 1191 if (buf != ctl->cache) 1192 memcpy(buf, ctl->cache, len); 1193 } 1194 1195 return ret; 1196 } 1197 1198 static int wm_coeff_get(struct snd_kcontrol *kctl, 1199 struct snd_ctl_elem_value *ucontrol) 1200 { 1201 struct soc_bytes_ext *bytes_ext = 1202 (struct soc_bytes_ext *)kctl->private_value; 1203 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 1204 char *p = ucontrol->value.bytes.data; 1205 int ret; 1206 1207 mutex_lock(&ctl->dsp->pwr_lock); 1208 ret = wm_coeff_read_ctrl(ctl, p, ctl->len); 1209 mutex_unlock(&ctl->dsp->pwr_lock); 1210 1211 return ret; 1212 } 1213 1214 static int wm_coeff_tlv_get(struct snd_kcontrol *kctl, 1215 unsigned int __user *bytes, unsigned int size) 1216 { 1217 struct soc_bytes_ext *bytes_ext = 1218 (struct soc_bytes_ext *)kctl->private_value; 1219 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 1220 int ret = 0; 1221 1222 mutex_lock(&ctl->dsp->pwr_lock); 1223 1224 ret = wm_coeff_read_ctrl_raw(ctl, ctl->cache, size); 1225 1226 if (!ret && copy_to_user(bytes, ctl->cache, size)) 1227 ret = -EFAULT; 1228 1229 mutex_unlock(&ctl->dsp->pwr_lock); 1230 1231 return ret; 1232 } 1233 1234 static int wm_coeff_get_acked(struct snd_kcontrol *kcontrol, 1235 struct snd_ctl_elem_value *ucontrol) 1236 { 1237 /* 1238 * Although it's not useful to read an acked control, we must satisfy 1239 * user-side assumptions that all controls are readable and that a 1240 * write of the same value should be filtered out (it's valid to send 1241 * the same event number again to the firmware). We therefore return 0, 1242 * meaning "no event" so valid event numbers will always be a change 1243 */ 1244 ucontrol->value.integer.value[0] = 0; 1245 1246 return 0; 1247 } 1248 1249 struct wmfw_ctl_work { 1250 struct wm_adsp *dsp; 1251 struct wm_coeff_ctl *ctl; 1252 struct work_struct work; 1253 }; 1254 1255 static unsigned int wmfw_convert_flags(unsigned int in, unsigned int len) 1256 { 1257 unsigned int out, rd, wr, vol; 1258 1259 if (len > ADSP_MAX_STD_CTRL_SIZE) { 1260 rd = SNDRV_CTL_ELEM_ACCESS_TLV_READ; 1261 wr = SNDRV_CTL_ELEM_ACCESS_TLV_WRITE; 1262 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE; 1263 1264 out = SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; 1265 } else { 1266 rd = SNDRV_CTL_ELEM_ACCESS_READ; 1267 wr = SNDRV_CTL_ELEM_ACCESS_WRITE; 1268 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE; 1269 1270 out = 0; 1271 } 1272 1273 if (in) { 1274 out |= rd; 1275 if (in & WMFW_CTL_FLAG_WRITEABLE) 1276 out |= wr; 1277 if (in & WMFW_CTL_FLAG_VOLATILE) 1278 out |= vol; 1279 } else { 1280 out |= rd | wr | vol; 1281 } 1282 1283 return out; 1284 } 1285 1286 static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl) 1287 { 1288 struct snd_kcontrol_new *kcontrol; 1289 int ret; 1290 1291 if (!ctl || !ctl->name) 1292 return -EINVAL; 1293 1294 kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL); 1295 if (!kcontrol) 1296 return -ENOMEM; 1297 1298 kcontrol->name = ctl->name; 1299 kcontrol->info = wm_coeff_info; 1300 kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1301 kcontrol->tlv.c = snd_soc_bytes_tlv_callback; 1302 kcontrol->private_value = (unsigned long)&ctl->bytes_ext; 1303 kcontrol->access = wmfw_convert_flags(ctl->flags, ctl->len); 1304 1305 switch (ctl->type) { 1306 case WMFW_CTL_TYPE_ACKED: 1307 kcontrol->get = wm_coeff_get_acked; 1308 kcontrol->put = wm_coeff_put_acked; 1309 break; 1310 default: 1311 if (kcontrol->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 1312 ctl->bytes_ext.max = ctl->len; 1313 ctl->bytes_ext.get = wm_coeff_tlv_get; 1314 ctl->bytes_ext.put = wm_coeff_tlv_put; 1315 } else { 1316 kcontrol->get = wm_coeff_get; 1317 kcontrol->put = wm_coeff_put; 1318 } 1319 break; 1320 } 1321 1322 ret = snd_soc_add_component_controls(dsp->component, kcontrol, 1); 1323 if (ret < 0) 1324 goto err_kcontrol; 1325 1326 kfree(kcontrol); 1327 1328 return 0; 1329 1330 err_kcontrol: 1331 kfree(kcontrol); 1332 return ret; 1333 } 1334 1335 static int wm_coeff_init_control_caches(struct wm_adsp *dsp) 1336 { 1337 struct wm_coeff_ctl *ctl; 1338 int ret; 1339 1340 list_for_each_entry(ctl, &dsp->ctl_list, list) { 1341 if (!ctl->enabled || ctl->set) 1342 continue; 1343 if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) 1344 continue; 1345 1346 /* 1347 * For readable controls populate the cache from the DSP memory. 1348 * For non-readable controls the cache was zero-filled when 1349 * created so we don't need to do anything. 1350 */ 1351 if (!ctl->flags || (ctl->flags & WMFW_CTL_FLAG_READABLE)) { 1352 ret = wm_coeff_read_ctrl_raw(ctl, ctl->cache, ctl->len); 1353 if (ret < 0) 1354 return ret; 1355 } 1356 } 1357 1358 return 0; 1359 } 1360 1361 static int wm_coeff_sync_controls(struct wm_adsp *dsp) 1362 { 1363 struct wm_coeff_ctl *ctl; 1364 int ret; 1365 1366 list_for_each_entry(ctl, &dsp->ctl_list, list) { 1367 if (!ctl->enabled) 1368 continue; 1369 if (ctl->set && !(ctl->flags & WMFW_CTL_FLAG_VOLATILE)) { 1370 ret = wm_coeff_write_ctrl_raw(ctl, ctl->cache, 1371 ctl->len); 1372 if (ret < 0) 1373 return ret; 1374 } 1375 } 1376 1377 return 0; 1378 } 1379 1380 static void wm_adsp_signal_event_controls(struct wm_adsp *dsp, 1381 unsigned int event) 1382 { 1383 struct wm_coeff_ctl *ctl; 1384 int ret; 1385 1386 list_for_each_entry(ctl, &dsp->ctl_list, list) { 1387 if (ctl->type != WMFW_CTL_TYPE_HOSTEVENT) 1388 continue; 1389 1390 if (!ctl->enabled) 1391 continue; 1392 1393 ret = wm_coeff_write_acked_control(ctl, event); 1394 if (ret) 1395 adsp_warn(dsp, 1396 "Failed to send 0x%x event to alg 0x%x (%d)\n", 1397 event, ctl->alg_region.alg, ret); 1398 } 1399 } 1400 1401 static void wm_adsp_ctl_work(struct work_struct *work) 1402 { 1403 struct wmfw_ctl_work *ctl_work = container_of(work, 1404 struct wmfw_ctl_work, 1405 work); 1406 1407 wmfw_add_ctl(ctl_work->dsp, ctl_work->ctl); 1408 kfree(ctl_work); 1409 } 1410 1411 static void wm_adsp_free_ctl_blk(struct wm_coeff_ctl *ctl) 1412 { 1413 kfree(ctl->cache); 1414 kfree(ctl->name); 1415 kfree(ctl->subname); 1416 kfree(ctl); 1417 } 1418 1419 static int wm_adsp_create_control(struct wm_adsp *dsp, 1420 const struct wm_adsp_alg_region *alg_region, 1421 unsigned int offset, unsigned int len, 1422 const char *subname, unsigned int subname_len, 1423 unsigned int flags, unsigned int type) 1424 { 1425 struct wm_coeff_ctl *ctl; 1426 struct wmfw_ctl_work *ctl_work; 1427 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 1428 const char *region_name; 1429 int ret; 1430 1431 region_name = wm_adsp_mem_region_name(alg_region->type); 1432 if (!region_name) { 1433 adsp_err(dsp, "Unknown region type: %d\n", alg_region->type); 1434 return -EINVAL; 1435 } 1436 1437 switch (dsp->fw_ver) { 1438 case 0: 1439 case 1: 1440 snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s %x", 1441 dsp->name, region_name, alg_region->alg); 1442 subname = NULL; /* don't append subname */ 1443 break; 1444 case 2: 1445 ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, 1446 "%s%c %.12s %x", dsp->name, *region_name, 1447 wm_adsp_fw_text[dsp->fw], alg_region->alg); 1448 break; 1449 default: 1450 ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, 1451 "%s %.12s %x", dsp->name, 1452 wm_adsp_fw_text[dsp->fw], alg_region->alg); 1453 break; 1454 } 1455 1456 if (subname) { 1457 int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2; 1458 int skip = 0; 1459 1460 if (dsp->component->name_prefix) 1461 avail -= strlen(dsp->component->name_prefix) + 1; 1462 1463 /* Truncate the subname from the start if it is too long */ 1464 if (subname_len > avail) 1465 skip = subname_len - avail; 1466 1467 snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret, 1468 " %.*s", subname_len - skip, subname + skip); 1469 } 1470 1471 list_for_each_entry(ctl, &dsp->ctl_list, list) { 1472 if (!strcmp(ctl->name, name)) { 1473 if (!ctl->enabled) 1474 ctl->enabled = 1; 1475 return 0; 1476 } 1477 } 1478 1479 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); 1480 if (!ctl) 1481 return -ENOMEM; 1482 ctl->fw_name = wm_adsp_fw_text[dsp->fw]; 1483 ctl->alg_region = *alg_region; 1484 ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL); 1485 if (!ctl->name) { 1486 ret = -ENOMEM; 1487 goto err_ctl; 1488 } 1489 if (subname) { 1490 ctl->subname_len = subname_len; 1491 ctl->subname = kmemdup(subname, 1492 strlen(subname) + 1, GFP_KERNEL); 1493 if (!ctl->subname) { 1494 ret = -ENOMEM; 1495 goto err_ctl_name; 1496 } 1497 } 1498 ctl->enabled = 1; 1499 ctl->set = 0; 1500 ctl->ops.xget = wm_coeff_get; 1501 ctl->ops.xput = wm_coeff_put; 1502 ctl->dsp = dsp; 1503 1504 ctl->flags = flags; 1505 ctl->type = type; 1506 ctl->offset = offset; 1507 ctl->len = len; 1508 ctl->cache = kzalloc(ctl->len, GFP_KERNEL); 1509 if (!ctl->cache) { 1510 ret = -ENOMEM; 1511 goto err_ctl_subname; 1512 } 1513 1514 list_add(&ctl->list, &dsp->ctl_list); 1515 1516 if (flags & WMFW_CTL_FLAG_SYS) 1517 return 0; 1518 1519 ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL); 1520 if (!ctl_work) { 1521 ret = -ENOMEM; 1522 goto err_ctl_cache; 1523 } 1524 1525 ctl_work->dsp = dsp; 1526 ctl_work->ctl = ctl; 1527 INIT_WORK(&ctl_work->work, wm_adsp_ctl_work); 1528 schedule_work(&ctl_work->work); 1529 1530 return 0; 1531 1532 err_ctl_cache: 1533 kfree(ctl->cache); 1534 err_ctl_subname: 1535 kfree(ctl->subname); 1536 err_ctl_name: 1537 kfree(ctl->name); 1538 err_ctl: 1539 kfree(ctl); 1540 1541 return ret; 1542 } 1543 1544 struct wm_coeff_parsed_alg { 1545 int id; 1546 const u8 *name; 1547 int name_len; 1548 int ncoeff; 1549 }; 1550 1551 struct wm_coeff_parsed_coeff { 1552 int offset; 1553 int mem_type; 1554 const u8 *name; 1555 int name_len; 1556 int ctl_type; 1557 int flags; 1558 int len; 1559 }; 1560 1561 static int wm_coeff_parse_string(int bytes, const u8 **pos, const u8 **str) 1562 { 1563 int length; 1564 1565 switch (bytes) { 1566 case 1: 1567 length = **pos; 1568 break; 1569 case 2: 1570 length = le16_to_cpu(*((__le16 *)*pos)); 1571 break; 1572 default: 1573 return 0; 1574 } 1575 1576 if (str) 1577 *str = *pos + bytes; 1578 1579 *pos += ((length + bytes) + 3) & ~0x03; 1580 1581 return length; 1582 } 1583 1584 static int wm_coeff_parse_int(int bytes, const u8 **pos) 1585 { 1586 int val = 0; 1587 1588 switch (bytes) { 1589 case 2: 1590 val = le16_to_cpu(*((__le16 *)*pos)); 1591 break; 1592 case 4: 1593 val = le32_to_cpu(*((__le32 *)*pos)); 1594 break; 1595 default: 1596 break; 1597 } 1598 1599 *pos += bytes; 1600 1601 return val; 1602 } 1603 1604 static inline void wm_coeff_parse_alg(struct wm_adsp *dsp, const u8 **data, 1605 struct wm_coeff_parsed_alg *blk) 1606 { 1607 const struct wmfw_adsp_alg_data *raw; 1608 1609 switch (dsp->fw_ver) { 1610 case 0: 1611 case 1: 1612 raw = (const struct wmfw_adsp_alg_data *)*data; 1613 *data = raw->data; 1614 1615 blk->id = le32_to_cpu(raw->id); 1616 blk->name = raw->name; 1617 blk->name_len = strlen(raw->name); 1618 blk->ncoeff = le32_to_cpu(raw->ncoeff); 1619 break; 1620 default: 1621 blk->id = wm_coeff_parse_int(sizeof(raw->id), data); 1622 blk->name_len = wm_coeff_parse_string(sizeof(u8), data, 1623 &blk->name); 1624 wm_coeff_parse_string(sizeof(u16), data, NULL); 1625 blk->ncoeff = wm_coeff_parse_int(sizeof(raw->ncoeff), data); 1626 break; 1627 } 1628 1629 adsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id); 1630 adsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name); 1631 adsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff); 1632 } 1633 1634 static inline void wm_coeff_parse_coeff(struct wm_adsp *dsp, const u8 **data, 1635 struct wm_coeff_parsed_coeff *blk) 1636 { 1637 const struct wmfw_adsp_coeff_data *raw; 1638 const u8 *tmp; 1639 int length; 1640 1641 switch (dsp->fw_ver) { 1642 case 0: 1643 case 1: 1644 raw = (const struct wmfw_adsp_coeff_data *)*data; 1645 *data = *data + sizeof(raw->hdr) + le32_to_cpu(raw->hdr.size); 1646 1647 blk->offset = le16_to_cpu(raw->hdr.offset); 1648 blk->mem_type = le16_to_cpu(raw->hdr.type); 1649 blk->name = raw->name; 1650 blk->name_len = strlen(raw->name); 1651 blk->ctl_type = le16_to_cpu(raw->ctl_type); 1652 blk->flags = le16_to_cpu(raw->flags); 1653 blk->len = le32_to_cpu(raw->len); 1654 break; 1655 default: 1656 tmp = *data; 1657 blk->offset = wm_coeff_parse_int(sizeof(raw->hdr.offset), &tmp); 1658 blk->mem_type = wm_coeff_parse_int(sizeof(raw->hdr.type), &tmp); 1659 length = wm_coeff_parse_int(sizeof(raw->hdr.size), &tmp); 1660 blk->name_len = wm_coeff_parse_string(sizeof(u8), &tmp, 1661 &blk->name); 1662 wm_coeff_parse_string(sizeof(u8), &tmp, NULL); 1663 wm_coeff_parse_string(sizeof(u16), &tmp, NULL); 1664 blk->ctl_type = wm_coeff_parse_int(sizeof(raw->ctl_type), &tmp); 1665 blk->flags = wm_coeff_parse_int(sizeof(raw->flags), &tmp); 1666 blk->len = wm_coeff_parse_int(sizeof(raw->len), &tmp); 1667 1668 *data = *data + sizeof(raw->hdr) + length; 1669 break; 1670 } 1671 1672 adsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type); 1673 adsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset); 1674 adsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name); 1675 adsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags); 1676 adsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type); 1677 adsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len); 1678 } 1679 1680 static int wm_adsp_check_coeff_flags(struct wm_adsp *dsp, 1681 const struct wm_coeff_parsed_coeff *coeff_blk, 1682 unsigned int f_required, 1683 unsigned int f_illegal) 1684 { 1685 if ((coeff_blk->flags & f_illegal) || 1686 ((coeff_blk->flags & f_required) != f_required)) { 1687 adsp_err(dsp, "Illegal flags 0x%x for control type 0x%x\n", 1688 coeff_blk->flags, coeff_blk->ctl_type); 1689 return -EINVAL; 1690 } 1691 1692 return 0; 1693 } 1694 1695 static int wm_adsp_parse_coeff(struct wm_adsp *dsp, 1696 const struct wmfw_region *region) 1697 { 1698 struct wm_adsp_alg_region alg_region = {}; 1699 struct wm_coeff_parsed_alg alg_blk; 1700 struct wm_coeff_parsed_coeff coeff_blk; 1701 const u8 *data = region->data; 1702 int i, ret; 1703 1704 wm_coeff_parse_alg(dsp, &data, &alg_blk); 1705 for (i = 0; i < alg_blk.ncoeff; i++) { 1706 wm_coeff_parse_coeff(dsp, &data, &coeff_blk); 1707 1708 switch (coeff_blk.ctl_type) { 1709 case SNDRV_CTL_ELEM_TYPE_BYTES: 1710 break; 1711 case WMFW_CTL_TYPE_ACKED: 1712 if (coeff_blk.flags & WMFW_CTL_FLAG_SYS) 1713 continue; /* ignore */ 1714 1715 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk, 1716 WMFW_CTL_FLAG_VOLATILE | 1717 WMFW_CTL_FLAG_WRITEABLE | 1718 WMFW_CTL_FLAG_READABLE, 1719 0); 1720 if (ret) 1721 return -EINVAL; 1722 break; 1723 case WMFW_CTL_TYPE_HOSTEVENT: 1724 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk, 1725 WMFW_CTL_FLAG_SYS | 1726 WMFW_CTL_FLAG_VOLATILE | 1727 WMFW_CTL_FLAG_WRITEABLE | 1728 WMFW_CTL_FLAG_READABLE, 1729 0); 1730 if (ret) 1731 return -EINVAL; 1732 break; 1733 case WMFW_CTL_TYPE_HOST_BUFFER: 1734 ret = wm_adsp_check_coeff_flags(dsp, &coeff_blk, 1735 WMFW_CTL_FLAG_SYS | 1736 WMFW_CTL_FLAG_VOLATILE | 1737 WMFW_CTL_FLAG_READABLE, 1738 0); 1739 if (ret) 1740 return -EINVAL; 1741 break; 1742 default: 1743 adsp_err(dsp, "Unknown control type: %d\n", 1744 coeff_blk.ctl_type); 1745 return -EINVAL; 1746 } 1747 1748 alg_region.type = coeff_blk.mem_type; 1749 alg_region.alg = alg_blk.id; 1750 1751 ret = wm_adsp_create_control(dsp, &alg_region, 1752 coeff_blk.offset, 1753 coeff_blk.len, 1754 coeff_blk.name, 1755 coeff_blk.name_len, 1756 coeff_blk.flags, 1757 coeff_blk.ctl_type); 1758 if (ret < 0) 1759 adsp_err(dsp, "Failed to create control: %.*s, %d\n", 1760 coeff_blk.name_len, coeff_blk.name, ret); 1761 } 1762 1763 return 0; 1764 } 1765 1766 static unsigned int wm_adsp1_parse_sizes(struct wm_adsp *dsp, 1767 const char * const file, 1768 unsigned int pos, 1769 const struct firmware *firmware) 1770 { 1771 const struct wmfw_adsp1_sizes *adsp1_sizes; 1772 1773 adsp1_sizes = (void *)&firmware->data[pos]; 1774 1775 adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n", file, 1776 le32_to_cpu(adsp1_sizes->dm), le32_to_cpu(adsp1_sizes->pm), 1777 le32_to_cpu(adsp1_sizes->zm)); 1778 1779 return pos + sizeof(*adsp1_sizes); 1780 } 1781 1782 static unsigned int wm_adsp2_parse_sizes(struct wm_adsp *dsp, 1783 const char * const file, 1784 unsigned int pos, 1785 const struct firmware *firmware) 1786 { 1787 const struct wmfw_adsp2_sizes *adsp2_sizes; 1788 1789 adsp2_sizes = (void *)&firmware->data[pos]; 1790 1791 adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n", file, 1792 le32_to_cpu(adsp2_sizes->xm), le32_to_cpu(adsp2_sizes->ym), 1793 le32_to_cpu(adsp2_sizes->pm), le32_to_cpu(adsp2_sizes->zm)); 1794 1795 return pos + sizeof(*adsp2_sizes); 1796 } 1797 1798 static bool wm_adsp_validate_version(struct wm_adsp *dsp, unsigned int version) 1799 { 1800 switch (version) { 1801 case 0: 1802 adsp_warn(dsp, "Deprecated file format %d\n", version); 1803 return true; 1804 case 1: 1805 case 2: 1806 return true; 1807 default: 1808 return false; 1809 } 1810 } 1811 1812 static bool wm_halo_validate_version(struct wm_adsp *dsp, unsigned int version) 1813 { 1814 switch (version) { 1815 case 3: 1816 return true; 1817 default: 1818 return false; 1819 } 1820 } 1821 1822 static int wm_adsp_load(struct wm_adsp *dsp) 1823 { 1824 LIST_HEAD(buf_list); 1825 const struct firmware *firmware; 1826 struct regmap *regmap = dsp->regmap; 1827 unsigned int pos = 0; 1828 const struct wmfw_header *header; 1829 const struct wmfw_adsp1_sizes *adsp1_sizes; 1830 const struct wmfw_footer *footer; 1831 const struct wmfw_region *region; 1832 const struct wm_adsp_region *mem; 1833 const char *region_name; 1834 char *file, *text = NULL; 1835 struct wm_adsp_buf *buf; 1836 unsigned int reg; 1837 int regions = 0; 1838 int ret, offset, type; 1839 1840 file = kzalloc(PAGE_SIZE, GFP_KERNEL); 1841 if (file == NULL) 1842 return -ENOMEM; 1843 1844 snprintf(file, PAGE_SIZE, "%s-%s-%s.wmfw", dsp->part, dsp->fwf_name, 1845 wm_adsp_fw[dsp->fw].file); 1846 file[PAGE_SIZE - 1] = '\0'; 1847 1848 ret = request_firmware(&firmware, file, dsp->dev); 1849 if (ret != 0) { 1850 adsp_err(dsp, "Failed to request '%s'\n", file); 1851 goto out; 1852 } 1853 ret = -EINVAL; 1854 1855 pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer); 1856 if (pos >= firmware->size) { 1857 adsp_err(dsp, "%s: file too short, %zu bytes\n", 1858 file, firmware->size); 1859 goto out_fw; 1860 } 1861 1862 header = (void *)&firmware->data[0]; 1863 1864 if (memcmp(&header->magic[0], "WMFW", 4) != 0) { 1865 adsp_err(dsp, "%s: invalid magic\n", file); 1866 goto out_fw; 1867 } 1868 1869 if (!dsp->ops->validate_version(dsp, header->ver)) { 1870 adsp_err(dsp, "%s: unknown file format %d\n", 1871 file, header->ver); 1872 goto out_fw; 1873 } 1874 1875 adsp_info(dsp, "Firmware version: %d\n", header->ver); 1876 dsp->fw_ver = header->ver; 1877 1878 if (header->core != dsp->type) { 1879 adsp_err(dsp, "%s: invalid core %d != %d\n", 1880 file, header->core, dsp->type); 1881 goto out_fw; 1882 } 1883 1884 pos = sizeof(*header); 1885 pos = dsp->ops->parse_sizes(dsp, file, pos, firmware); 1886 1887 footer = (void *)&firmware->data[pos]; 1888 pos += sizeof(*footer); 1889 1890 if (le32_to_cpu(header->len) != pos) { 1891 adsp_err(dsp, "%s: unexpected header length %d\n", 1892 file, le32_to_cpu(header->len)); 1893 goto out_fw; 1894 } 1895 1896 adsp_dbg(dsp, "%s: timestamp %llu\n", file, 1897 le64_to_cpu(footer->timestamp)); 1898 1899 while (pos < firmware->size && 1900 sizeof(*region) < firmware->size - pos) { 1901 region = (void *)&(firmware->data[pos]); 1902 region_name = "Unknown"; 1903 reg = 0; 1904 text = NULL; 1905 offset = le32_to_cpu(region->offset) & 0xffffff; 1906 type = be32_to_cpu(region->type) & 0xff; 1907 1908 switch (type) { 1909 case WMFW_NAME_TEXT: 1910 region_name = "Firmware name"; 1911 text = kzalloc(le32_to_cpu(region->len) + 1, 1912 GFP_KERNEL); 1913 break; 1914 case WMFW_ALGORITHM_DATA: 1915 region_name = "Algorithm"; 1916 ret = wm_adsp_parse_coeff(dsp, region); 1917 if (ret != 0) 1918 goto out_fw; 1919 break; 1920 case WMFW_INFO_TEXT: 1921 region_name = "Information"; 1922 text = kzalloc(le32_to_cpu(region->len) + 1, 1923 GFP_KERNEL); 1924 break; 1925 case WMFW_ABSOLUTE: 1926 region_name = "Absolute"; 1927 reg = offset; 1928 break; 1929 case WMFW_ADSP1_PM: 1930 case WMFW_ADSP1_DM: 1931 case WMFW_ADSP2_XM: 1932 case WMFW_ADSP2_YM: 1933 case WMFW_ADSP1_ZM: 1934 case WMFW_HALO_PM_PACKED: 1935 case WMFW_HALO_XM_PACKED: 1936 case WMFW_HALO_YM_PACKED: 1937 mem = wm_adsp_find_region(dsp, type); 1938 if (!mem) { 1939 adsp_err(dsp, "No region of type: %x\n", type); 1940 goto out_fw; 1941 } 1942 1943 region_name = wm_adsp_mem_region_name(type); 1944 reg = dsp->ops->region_to_reg(mem, offset); 1945 break; 1946 default: 1947 adsp_warn(dsp, 1948 "%s.%d: Unknown region type %x at %d(%x)\n", 1949 file, regions, type, pos, pos); 1950 break; 1951 } 1952 1953 adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file, 1954 regions, le32_to_cpu(region->len), offset, 1955 region_name); 1956 1957 if (le32_to_cpu(region->len) > 1958 firmware->size - pos - sizeof(*region)) { 1959 adsp_err(dsp, 1960 "%s.%d: %s region len %d bytes exceeds file length %zu\n", 1961 file, regions, region_name, 1962 le32_to_cpu(region->len), firmware->size); 1963 ret = -EINVAL; 1964 goto out_fw; 1965 } 1966 1967 if (text) { 1968 memcpy(text, region->data, le32_to_cpu(region->len)); 1969 adsp_info(dsp, "%s: %s\n", file, text); 1970 kfree(text); 1971 text = NULL; 1972 } 1973 1974 if (reg) { 1975 buf = wm_adsp_buf_alloc(region->data, 1976 le32_to_cpu(region->len), 1977 &buf_list); 1978 if (!buf) { 1979 adsp_err(dsp, "Out of memory\n"); 1980 ret = -ENOMEM; 1981 goto out_fw; 1982 } 1983 1984 ret = regmap_raw_write_async(regmap, reg, buf->buf, 1985 le32_to_cpu(region->len)); 1986 if (ret != 0) { 1987 adsp_err(dsp, 1988 "%s.%d: Failed to write %d bytes at %d in %s: %d\n", 1989 file, regions, 1990 le32_to_cpu(region->len), offset, 1991 region_name, ret); 1992 goto out_fw; 1993 } 1994 } 1995 1996 pos += le32_to_cpu(region->len) + sizeof(*region); 1997 regions++; 1998 } 1999 2000 ret = regmap_async_complete(regmap); 2001 if (ret != 0) { 2002 adsp_err(dsp, "Failed to complete async write: %d\n", ret); 2003 goto out_fw; 2004 } 2005 2006 if (pos > firmware->size) 2007 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n", 2008 file, regions, pos - firmware->size); 2009 2010 wm_adsp_debugfs_save_wmfwname(dsp, file); 2011 2012 out_fw: 2013 regmap_async_complete(regmap); 2014 wm_adsp_buf_free(&buf_list); 2015 release_firmware(firmware); 2016 kfree(text); 2017 out: 2018 kfree(file); 2019 2020 return ret; 2021 } 2022 2023 /* 2024 * Find wm_coeff_ctl with input name as its subname 2025 * If not found, return NULL 2026 */ 2027 static struct wm_coeff_ctl *wm_adsp_get_ctl(struct wm_adsp *dsp, 2028 const char *name, int type, 2029 unsigned int alg) 2030 { 2031 struct wm_coeff_ctl *pos, *rslt = NULL; 2032 2033 list_for_each_entry(pos, &dsp->ctl_list, list) { 2034 if (!pos->subname) 2035 continue; 2036 if (strncmp(pos->subname, name, pos->subname_len) == 0 && 2037 pos->alg_region.alg == alg && 2038 pos->alg_region.type == type) { 2039 rslt = pos; 2040 break; 2041 } 2042 } 2043 2044 return rslt; 2045 } 2046 2047 int wm_adsp_write_ctl(struct wm_adsp *dsp, const char *name, int type, 2048 unsigned int alg, void *buf, size_t len) 2049 { 2050 struct wm_coeff_ctl *ctl; 2051 struct snd_kcontrol *kcontrol; 2052 char ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 2053 int ret; 2054 2055 ctl = wm_adsp_get_ctl(dsp, name, type, alg); 2056 if (!ctl) 2057 return -EINVAL; 2058 2059 if (len > ctl->len) 2060 return -EINVAL; 2061 2062 ret = wm_coeff_write_ctrl(ctl, buf, len); 2063 if (ret) 2064 return ret; 2065 2066 if (ctl->flags & WMFW_CTL_FLAG_SYS) 2067 return 0; 2068 2069 if (dsp->component->name_prefix) 2070 snprintf(ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s", 2071 dsp->component->name_prefix, ctl->name); 2072 else 2073 snprintf(ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s", 2074 ctl->name); 2075 2076 kcontrol = snd_soc_card_get_kcontrol(dsp->component->card, ctl_name); 2077 if (!kcontrol) { 2078 adsp_err(dsp, "Can't find kcontrol %s\n", ctl_name); 2079 return -EINVAL; 2080 } 2081 2082 snd_ctl_notify(dsp->component->card->snd_card, 2083 SNDRV_CTL_EVENT_MASK_VALUE, &kcontrol->id); 2084 2085 return ret; 2086 } 2087 EXPORT_SYMBOL_GPL(wm_adsp_write_ctl); 2088 2089 int wm_adsp_read_ctl(struct wm_adsp *dsp, const char *name, int type, 2090 unsigned int alg, void *buf, size_t len) 2091 { 2092 struct wm_coeff_ctl *ctl; 2093 2094 ctl = wm_adsp_get_ctl(dsp, name, type, alg); 2095 if (!ctl) 2096 return -EINVAL; 2097 2098 if (len > ctl->len) 2099 return -EINVAL; 2100 2101 return wm_coeff_read_ctrl(ctl, buf, len); 2102 } 2103 EXPORT_SYMBOL_GPL(wm_adsp_read_ctl); 2104 2105 static void wm_adsp_ctl_fixup_base(struct wm_adsp *dsp, 2106 const struct wm_adsp_alg_region *alg_region) 2107 { 2108 struct wm_coeff_ctl *ctl; 2109 2110 list_for_each_entry(ctl, &dsp->ctl_list, list) { 2111 if (ctl->fw_name == wm_adsp_fw_text[dsp->fw] && 2112 alg_region->alg == ctl->alg_region.alg && 2113 alg_region->type == ctl->alg_region.type) { 2114 ctl->alg_region.base = alg_region->base; 2115 } 2116 } 2117 } 2118 2119 static void *wm_adsp_read_algs(struct wm_adsp *dsp, size_t n_algs, 2120 const struct wm_adsp_region *mem, 2121 unsigned int pos, unsigned int len) 2122 { 2123 void *alg; 2124 unsigned int reg; 2125 int ret; 2126 __be32 val; 2127 2128 if (n_algs == 0) { 2129 adsp_err(dsp, "No algorithms\n"); 2130 return ERR_PTR(-EINVAL); 2131 } 2132 2133 if (n_algs > 1024) { 2134 adsp_err(dsp, "Algorithm count %zx excessive\n", n_algs); 2135 return ERR_PTR(-EINVAL); 2136 } 2137 2138 /* Read the terminator first to validate the length */ 2139 reg = dsp->ops->region_to_reg(mem, pos + len); 2140 2141 ret = regmap_raw_read(dsp->regmap, reg, &val, sizeof(val)); 2142 if (ret != 0) { 2143 adsp_err(dsp, "Failed to read algorithm list end: %d\n", 2144 ret); 2145 return ERR_PTR(ret); 2146 } 2147 2148 if (be32_to_cpu(val) != 0xbedead) 2149 adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbedead\n", 2150 reg, be32_to_cpu(val)); 2151 2152 /* Convert length from DSP words to bytes */ 2153 len *= sizeof(u32); 2154 2155 alg = kzalloc(len, GFP_KERNEL | GFP_DMA); 2156 if (!alg) 2157 return ERR_PTR(-ENOMEM); 2158 2159 reg = dsp->ops->region_to_reg(mem, pos); 2160 2161 ret = regmap_raw_read(dsp->regmap, reg, alg, len); 2162 if (ret != 0) { 2163 adsp_err(dsp, "Failed to read algorithm list: %d\n", ret); 2164 kfree(alg); 2165 return ERR_PTR(ret); 2166 } 2167 2168 return alg; 2169 } 2170 2171 static struct wm_adsp_alg_region * 2172 wm_adsp_find_alg_region(struct wm_adsp *dsp, int type, unsigned int id) 2173 { 2174 struct wm_adsp_alg_region *alg_region; 2175 2176 list_for_each_entry(alg_region, &dsp->alg_regions, list) { 2177 if (id == alg_region->alg && type == alg_region->type) 2178 return alg_region; 2179 } 2180 2181 return NULL; 2182 } 2183 2184 static struct wm_adsp_alg_region *wm_adsp_create_region(struct wm_adsp *dsp, 2185 int type, __be32 id, 2186 __be32 base) 2187 { 2188 struct wm_adsp_alg_region *alg_region; 2189 2190 alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL); 2191 if (!alg_region) 2192 return ERR_PTR(-ENOMEM); 2193 2194 alg_region->type = type; 2195 alg_region->alg = be32_to_cpu(id); 2196 alg_region->base = be32_to_cpu(base); 2197 2198 list_add_tail(&alg_region->list, &dsp->alg_regions); 2199 2200 if (dsp->fw_ver > 0) 2201 wm_adsp_ctl_fixup_base(dsp, alg_region); 2202 2203 return alg_region; 2204 } 2205 2206 static void wm_adsp_free_alg_regions(struct wm_adsp *dsp) 2207 { 2208 struct wm_adsp_alg_region *alg_region; 2209 2210 while (!list_empty(&dsp->alg_regions)) { 2211 alg_region = list_first_entry(&dsp->alg_regions, 2212 struct wm_adsp_alg_region, 2213 list); 2214 list_del(&alg_region->list); 2215 kfree(alg_region); 2216 } 2217 } 2218 2219 static void wmfw_parse_id_header(struct wm_adsp *dsp, 2220 struct wmfw_id_hdr *fw, int nalgs) 2221 { 2222 dsp->fw_id = be32_to_cpu(fw->id); 2223 dsp->fw_id_version = be32_to_cpu(fw->ver); 2224 2225 adsp_info(dsp, "Firmware: %x v%d.%d.%d, %d algorithms\n", 2226 dsp->fw_id, (dsp->fw_id_version & 0xff0000) >> 16, 2227 (dsp->fw_id_version & 0xff00) >> 8, dsp->fw_id_version & 0xff, 2228 nalgs); 2229 } 2230 2231 static void wmfw_v3_parse_id_header(struct wm_adsp *dsp, 2232 struct wmfw_v3_id_hdr *fw, int nalgs) 2233 { 2234 dsp->fw_id = be32_to_cpu(fw->id); 2235 dsp->fw_id_version = be32_to_cpu(fw->ver); 2236 dsp->fw_vendor_id = be32_to_cpu(fw->vendor_id); 2237 2238 adsp_info(dsp, "Firmware: %x vendor: 0x%x v%d.%d.%d, %d algorithms\n", 2239 dsp->fw_id, dsp->fw_vendor_id, 2240 (dsp->fw_id_version & 0xff0000) >> 16, 2241 (dsp->fw_id_version & 0xff00) >> 8, dsp->fw_id_version & 0xff, 2242 nalgs); 2243 } 2244 2245 static int wm_adsp_create_regions(struct wm_adsp *dsp, __be32 id, int nregions, 2246 int *type, __be32 *base) 2247 { 2248 struct wm_adsp_alg_region *alg_region; 2249 int i; 2250 2251 for (i = 0; i < nregions; i++) { 2252 alg_region = wm_adsp_create_region(dsp, type[i], id, base[i]); 2253 if (IS_ERR(alg_region)) 2254 return PTR_ERR(alg_region); 2255 } 2256 2257 return 0; 2258 } 2259 2260 static int wm_adsp1_setup_algs(struct wm_adsp *dsp) 2261 { 2262 struct wmfw_adsp1_id_hdr adsp1_id; 2263 struct wmfw_adsp1_alg_hdr *adsp1_alg; 2264 struct wm_adsp_alg_region *alg_region; 2265 const struct wm_adsp_region *mem; 2266 unsigned int pos, len; 2267 size_t n_algs; 2268 int i, ret; 2269 2270 mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM); 2271 if (WARN_ON(!mem)) 2272 return -EINVAL; 2273 2274 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id, 2275 sizeof(adsp1_id)); 2276 if (ret != 0) { 2277 adsp_err(dsp, "Failed to read algorithm info: %d\n", 2278 ret); 2279 return ret; 2280 } 2281 2282 n_algs = be32_to_cpu(adsp1_id.n_algs); 2283 2284 wmfw_parse_id_header(dsp, &adsp1_id.fw, n_algs); 2285 2286 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM, 2287 adsp1_id.fw.id, adsp1_id.zm); 2288 if (IS_ERR(alg_region)) 2289 return PTR_ERR(alg_region); 2290 2291 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM, 2292 adsp1_id.fw.id, adsp1_id.dm); 2293 if (IS_ERR(alg_region)) 2294 return PTR_ERR(alg_region); 2295 2296 /* Calculate offset and length in DSP words */ 2297 pos = sizeof(adsp1_id) / sizeof(u32); 2298 len = (sizeof(*adsp1_alg) * n_algs) / sizeof(u32); 2299 2300 adsp1_alg = wm_adsp_read_algs(dsp, n_algs, mem, pos, len); 2301 if (IS_ERR(adsp1_alg)) 2302 return PTR_ERR(adsp1_alg); 2303 2304 for (i = 0; i < n_algs; i++) { 2305 adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n", 2306 i, be32_to_cpu(adsp1_alg[i].alg.id), 2307 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16, 2308 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8, 2309 be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff, 2310 be32_to_cpu(adsp1_alg[i].dm), 2311 be32_to_cpu(adsp1_alg[i].zm)); 2312 2313 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM, 2314 adsp1_alg[i].alg.id, 2315 adsp1_alg[i].dm); 2316 if (IS_ERR(alg_region)) { 2317 ret = PTR_ERR(alg_region); 2318 goto out; 2319 } 2320 if (dsp->fw_ver == 0) { 2321 if (i + 1 < n_algs) { 2322 len = be32_to_cpu(adsp1_alg[i + 1].dm); 2323 len -= be32_to_cpu(adsp1_alg[i].dm); 2324 len *= 4; 2325 wm_adsp_create_control(dsp, alg_region, 0, 2326 len, NULL, 0, 0, 2327 SNDRV_CTL_ELEM_TYPE_BYTES); 2328 } else { 2329 adsp_warn(dsp, "Missing length info for region DM with ID %x\n", 2330 be32_to_cpu(adsp1_alg[i].alg.id)); 2331 } 2332 } 2333 2334 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM, 2335 adsp1_alg[i].alg.id, 2336 adsp1_alg[i].zm); 2337 if (IS_ERR(alg_region)) { 2338 ret = PTR_ERR(alg_region); 2339 goto out; 2340 } 2341 if (dsp->fw_ver == 0) { 2342 if (i + 1 < n_algs) { 2343 len = be32_to_cpu(adsp1_alg[i + 1].zm); 2344 len -= be32_to_cpu(adsp1_alg[i].zm); 2345 len *= 4; 2346 wm_adsp_create_control(dsp, alg_region, 0, 2347 len, NULL, 0, 0, 2348 SNDRV_CTL_ELEM_TYPE_BYTES); 2349 } else { 2350 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n", 2351 be32_to_cpu(adsp1_alg[i].alg.id)); 2352 } 2353 } 2354 } 2355 2356 out: 2357 kfree(adsp1_alg); 2358 return ret; 2359 } 2360 2361 static int wm_adsp2_setup_algs(struct wm_adsp *dsp) 2362 { 2363 struct wmfw_adsp2_id_hdr adsp2_id; 2364 struct wmfw_adsp2_alg_hdr *adsp2_alg; 2365 struct wm_adsp_alg_region *alg_region; 2366 const struct wm_adsp_region *mem; 2367 unsigned int pos, len; 2368 size_t n_algs; 2369 int i, ret; 2370 2371 mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM); 2372 if (WARN_ON(!mem)) 2373 return -EINVAL; 2374 2375 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id, 2376 sizeof(adsp2_id)); 2377 if (ret != 0) { 2378 adsp_err(dsp, "Failed to read algorithm info: %d\n", 2379 ret); 2380 return ret; 2381 } 2382 2383 n_algs = be32_to_cpu(adsp2_id.n_algs); 2384 2385 wmfw_parse_id_header(dsp, &adsp2_id.fw, n_algs); 2386 2387 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM, 2388 adsp2_id.fw.id, adsp2_id.xm); 2389 if (IS_ERR(alg_region)) 2390 return PTR_ERR(alg_region); 2391 2392 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM, 2393 adsp2_id.fw.id, adsp2_id.ym); 2394 if (IS_ERR(alg_region)) 2395 return PTR_ERR(alg_region); 2396 2397 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM, 2398 adsp2_id.fw.id, adsp2_id.zm); 2399 if (IS_ERR(alg_region)) 2400 return PTR_ERR(alg_region); 2401 2402 /* Calculate offset and length in DSP words */ 2403 pos = sizeof(adsp2_id) / sizeof(u32); 2404 len = (sizeof(*adsp2_alg) * n_algs) / sizeof(u32); 2405 2406 adsp2_alg = wm_adsp_read_algs(dsp, n_algs, mem, pos, len); 2407 if (IS_ERR(adsp2_alg)) 2408 return PTR_ERR(adsp2_alg); 2409 2410 for (i = 0; i < n_algs; i++) { 2411 adsp_info(dsp, 2412 "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n", 2413 i, be32_to_cpu(adsp2_alg[i].alg.id), 2414 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16, 2415 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8, 2416 be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff, 2417 be32_to_cpu(adsp2_alg[i].xm), 2418 be32_to_cpu(adsp2_alg[i].ym), 2419 be32_to_cpu(adsp2_alg[i].zm)); 2420 2421 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM, 2422 adsp2_alg[i].alg.id, 2423 adsp2_alg[i].xm); 2424 if (IS_ERR(alg_region)) { 2425 ret = PTR_ERR(alg_region); 2426 goto out; 2427 } 2428 if (dsp->fw_ver == 0) { 2429 if (i + 1 < n_algs) { 2430 len = be32_to_cpu(adsp2_alg[i + 1].xm); 2431 len -= be32_to_cpu(adsp2_alg[i].xm); 2432 len *= 4; 2433 wm_adsp_create_control(dsp, alg_region, 0, 2434 len, NULL, 0, 0, 2435 SNDRV_CTL_ELEM_TYPE_BYTES); 2436 } else { 2437 adsp_warn(dsp, "Missing length info for region XM with ID %x\n", 2438 be32_to_cpu(adsp2_alg[i].alg.id)); 2439 } 2440 } 2441 2442 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM, 2443 adsp2_alg[i].alg.id, 2444 adsp2_alg[i].ym); 2445 if (IS_ERR(alg_region)) { 2446 ret = PTR_ERR(alg_region); 2447 goto out; 2448 } 2449 if (dsp->fw_ver == 0) { 2450 if (i + 1 < n_algs) { 2451 len = be32_to_cpu(adsp2_alg[i + 1].ym); 2452 len -= be32_to_cpu(adsp2_alg[i].ym); 2453 len *= 4; 2454 wm_adsp_create_control(dsp, alg_region, 0, 2455 len, NULL, 0, 0, 2456 SNDRV_CTL_ELEM_TYPE_BYTES); 2457 } else { 2458 adsp_warn(dsp, "Missing length info for region YM with ID %x\n", 2459 be32_to_cpu(adsp2_alg[i].alg.id)); 2460 } 2461 } 2462 2463 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM, 2464 adsp2_alg[i].alg.id, 2465 adsp2_alg[i].zm); 2466 if (IS_ERR(alg_region)) { 2467 ret = PTR_ERR(alg_region); 2468 goto out; 2469 } 2470 if (dsp->fw_ver == 0) { 2471 if (i + 1 < n_algs) { 2472 len = be32_to_cpu(adsp2_alg[i + 1].zm); 2473 len -= be32_to_cpu(adsp2_alg[i].zm); 2474 len *= 4; 2475 wm_adsp_create_control(dsp, alg_region, 0, 2476 len, NULL, 0, 0, 2477 SNDRV_CTL_ELEM_TYPE_BYTES); 2478 } else { 2479 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n", 2480 be32_to_cpu(adsp2_alg[i].alg.id)); 2481 } 2482 } 2483 } 2484 2485 out: 2486 kfree(adsp2_alg); 2487 return ret; 2488 } 2489 2490 static int wm_halo_create_regions(struct wm_adsp *dsp, __be32 id, 2491 __be32 xm_base, __be32 ym_base) 2492 { 2493 int types[] = { 2494 WMFW_ADSP2_XM, WMFW_HALO_XM_PACKED, 2495 WMFW_ADSP2_YM, WMFW_HALO_YM_PACKED 2496 }; 2497 __be32 bases[] = { xm_base, xm_base, ym_base, ym_base }; 2498 2499 return wm_adsp_create_regions(dsp, id, ARRAY_SIZE(types), types, bases); 2500 } 2501 2502 static int wm_halo_setup_algs(struct wm_adsp *dsp) 2503 { 2504 struct wmfw_halo_id_hdr halo_id; 2505 struct wmfw_halo_alg_hdr *halo_alg; 2506 const struct wm_adsp_region *mem; 2507 unsigned int pos, len; 2508 size_t n_algs; 2509 int i, ret; 2510 2511 mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM); 2512 if (WARN_ON(!mem)) 2513 return -EINVAL; 2514 2515 ret = regmap_raw_read(dsp->regmap, mem->base, &halo_id, 2516 sizeof(halo_id)); 2517 if (ret != 0) { 2518 adsp_err(dsp, "Failed to read algorithm info: %d\n", 2519 ret); 2520 return ret; 2521 } 2522 2523 n_algs = be32_to_cpu(halo_id.n_algs); 2524 2525 wmfw_v3_parse_id_header(dsp, &halo_id.fw, n_algs); 2526 2527 ret = wm_halo_create_regions(dsp, halo_id.fw.id, 2528 halo_id.xm_base, halo_id.ym_base); 2529 if (ret) 2530 return ret; 2531 2532 /* Calculate offset and length in DSP words */ 2533 pos = sizeof(halo_id) / sizeof(u32); 2534 len = (sizeof(*halo_alg) * n_algs) / sizeof(u32); 2535 2536 halo_alg = wm_adsp_read_algs(dsp, n_algs, mem, pos, len); 2537 if (IS_ERR(halo_alg)) 2538 return PTR_ERR(halo_alg); 2539 2540 for (i = 0; i < n_algs; i++) { 2541 adsp_info(dsp, 2542 "%d: ID %x v%d.%d.%d XM@%x YM@%x\n", 2543 i, be32_to_cpu(halo_alg[i].alg.id), 2544 (be32_to_cpu(halo_alg[i].alg.ver) & 0xff0000) >> 16, 2545 (be32_to_cpu(halo_alg[i].alg.ver) & 0xff00) >> 8, 2546 be32_to_cpu(halo_alg[i].alg.ver) & 0xff, 2547 be32_to_cpu(halo_alg[i].xm_base), 2548 be32_to_cpu(halo_alg[i].ym_base)); 2549 2550 ret = wm_halo_create_regions(dsp, halo_alg[i].alg.id, 2551 halo_alg[i].xm_base, 2552 halo_alg[i].ym_base); 2553 if (ret) 2554 goto out; 2555 } 2556 2557 out: 2558 kfree(halo_alg); 2559 return ret; 2560 } 2561 2562 static int wm_adsp_load_coeff(struct wm_adsp *dsp) 2563 { 2564 LIST_HEAD(buf_list); 2565 struct regmap *regmap = dsp->regmap; 2566 struct wmfw_coeff_hdr *hdr; 2567 struct wmfw_coeff_item *blk; 2568 const struct firmware *firmware; 2569 const struct wm_adsp_region *mem; 2570 struct wm_adsp_alg_region *alg_region; 2571 const char *region_name; 2572 int ret, pos, blocks, type, offset, reg; 2573 char *file; 2574 struct wm_adsp_buf *buf; 2575 2576 file = kzalloc(PAGE_SIZE, GFP_KERNEL); 2577 if (file == NULL) 2578 return -ENOMEM; 2579 2580 snprintf(file, PAGE_SIZE, "%s-%s-%s.bin", dsp->part, dsp->fwf_name, 2581 wm_adsp_fw[dsp->fw].file); 2582 file[PAGE_SIZE - 1] = '\0'; 2583 2584 ret = request_firmware(&firmware, file, dsp->dev); 2585 if (ret != 0) { 2586 adsp_warn(dsp, "Failed to request '%s'\n", file); 2587 ret = 0; 2588 goto out; 2589 } 2590 ret = -EINVAL; 2591 2592 if (sizeof(*hdr) >= firmware->size) { 2593 adsp_err(dsp, "%s: file too short, %zu bytes\n", 2594 file, firmware->size); 2595 goto out_fw; 2596 } 2597 2598 hdr = (void *)&firmware->data[0]; 2599 if (memcmp(hdr->magic, "WMDR", 4) != 0) { 2600 adsp_err(dsp, "%s: invalid magic\n", file); 2601 goto out_fw; 2602 } 2603 2604 switch (be32_to_cpu(hdr->rev) & 0xff) { 2605 case 1: 2606 break; 2607 default: 2608 adsp_err(dsp, "%s: Unsupported coefficient file format %d\n", 2609 file, be32_to_cpu(hdr->rev) & 0xff); 2610 ret = -EINVAL; 2611 goto out_fw; 2612 } 2613 2614 adsp_dbg(dsp, "%s: v%d.%d.%d\n", file, 2615 (le32_to_cpu(hdr->ver) >> 16) & 0xff, 2616 (le32_to_cpu(hdr->ver) >> 8) & 0xff, 2617 le32_to_cpu(hdr->ver) & 0xff); 2618 2619 pos = le32_to_cpu(hdr->len); 2620 2621 blocks = 0; 2622 while (pos < firmware->size && 2623 sizeof(*blk) < firmware->size - pos) { 2624 blk = (void *)(&firmware->data[pos]); 2625 2626 type = le16_to_cpu(blk->type); 2627 offset = le16_to_cpu(blk->offset); 2628 2629 adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n", 2630 file, blocks, le32_to_cpu(blk->id), 2631 (le32_to_cpu(blk->ver) >> 16) & 0xff, 2632 (le32_to_cpu(blk->ver) >> 8) & 0xff, 2633 le32_to_cpu(blk->ver) & 0xff); 2634 adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n", 2635 file, blocks, le32_to_cpu(blk->len), offset, type); 2636 2637 reg = 0; 2638 region_name = "Unknown"; 2639 switch (type) { 2640 case (WMFW_NAME_TEXT << 8): 2641 case (WMFW_INFO_TEXT << 8): 2642 case (WMFW_METADATA << 8): 2643 break; 2644 case (WMFW_ABSOLUTE << 8): 2645 /* 2646 * Old files may use this for global 2647 * coefficients. 2648 */ 2649 if (le32_to_cpu(blk->id) == dsp->fw_id && 2650 offset == 0) { 2651 region_name = "global coefficients"; 2652 mem = wm_adsp_find_region(dsp, type); 2653 if (!mem) { 2654 adsp_err(dsp, "No ZM\n"); 2655 break; 2656 } 2657 reg = dsp->ops->region_to_reg(mem, 0); 2658 2659 } else { 2660 region_name = "register"; 2661 reg = offset; 2662 } 2663 break; 2664 2665 case WMFW_ADSP1_DM: 2666 case WMFW_ADSP1_ZM: 2667 case WMFW_ADSP2_XM: 2668 case WMFW_ADSP2_YM: 2669 case WMFW_HALO_XM_PACKED: 2670 case WMFW_HALO_YM_PACKED: 2671 case WMFW_HALO_PM_PACKED: 2672 adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n", 2673 file, blocks, le32_to_cpu(blk->len), 2674 type, le32_to_cpu(blk->id)); 2675 2676 mem = wm_adsp_find_region(dsp, type); 2677 if (!mem) { 2678 adsp_err(dsp, "No base for region %x\n", type); 2679 break; 2680 } 2681 2682 alg_region = wm_adsp_find_alg_region(dsp, type, 2683 le32_to_cpu(blk->id)); 2684 if (alg_region) { 2685 reg = alg_region->base; 2686 reg = dsp->ops->region_to_reg(mem, reg); 2687 reg += offset; 2688 } else { 2689 adsp_err(dsp, "No %x for algorithm %x\n", 2690 type, le32_to_cpu(blk->id)); 2691 } 2692 break; 2693 2694 default: 2695 adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n", 2696 file, blocks, type, pos); 2697 break; 2698 } 2699 2700 if (reg) { 2701 if (le32_to_cpu(blk->len) > 2702 firmware->size - pos - sizeof(*blk)) { 2703 adsp_err(dsp, 2704 "%s.%d: %s region len %d bytes exceeds file length %zu\n", 2705 file, blocks, region_name, 2706 le32_to_cpu(blk->len), 2707 firmware->size); 2708 ret = -EINVAL; 2709 goto out_fw; 2710 } 2711 2712 buf = wm_adsp_buf_alloc(blk->data, 2713 le32_to_cpu(blk->len), 2714 &buf_list); 2715 if (!buf) { 2716 adsp_err(dsp, "Out of memory\n"); 2717 ret = -ENOMEM; 2718 goto out_fw; 2719 } 2720 2721 adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n", 2722 file, blocks, le32_to_cpu(blk->len), 2723 reg); 2724 ret = regmap_raw_write_async(regmap, reg, buf->buf, 2725 le32_to_cpu(blk->len)); 2726 if (ret != 0) { 2727 adsp_err(dsp, 2728 "%s.%d: Failed to write to %x in %s: %d\n", 2729 file, blocks, reg, region_name, ret); 2730 } 2731 } 2732 2733 pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03; 2734 blocks++; 2735 } 2736 2737 ret = regmap_async_complete(regmap); 2738 if (ret != 0) 2739 adsp_err(dsp, "Failed to complete async write: %d\n", ret); 2740 2741 if (pos > firmware->size) 2742 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n", 2743 file, blocks, pos - firmware->size); 2744 2745 wm_adsp_debugfs_save_binname(dsp, file); 2746 2747 out_fw: 2748 regmap_async_complete(regmap); 2749 release_firmware(firmware); 2750 wm_adsp_buf_free(&buf_list); 2751 out: 2752 kfree(file); 2753 return ret; 2754 } 2755 2756 static int wm_adsp_create_name(struct wm_adsp *dsp) 2757 { 2758 char *p; 2759 2760 if (!dsp->name) { 2761 dsp->name = devm_kasprintf(dsp->dev, GFP_KERNEL, "DSP%d", 2762 dsp->num); 2763 if (!dsp->name) 2764 return -ENOMEM; 2765 } 2766 2767 if (!dsp->fwf_name) { 2768 p = devm_kstrdup(dsp->dev, dsp->name, GFP_KERNEL); 2769 if (!p) 2770 return -ENOMEM; 2771 2772 dsp->fwf_name = p; 2773 for (; *p != 0; ++p) 2774 *p = tolower(*p); 2775 } 2776 2777 return 0; 2778 } 2779 2780 static int wm_adsp_common_init(struct wm_adsp *dsp) 2781 { 2782 int ret; 2783 2784 ret = wm_adsp_create_name(dsp); 2785 if (ret) 2786 return ret; 2787 2788 INIT_LIST_HEAD(&dsp->alg_regions); 2789 INIT_LIST_HEAD(&dsp->ctl_list); 2790 INIT_LIST_HEAD(&dsp->compr_list); 2791 INIT_LIST_HEAD(&dsp->buffer_list); 2792 2793 mutex_init(&dsp->pwr_lock); 2794 2795 return 0; 2796 } 2797 2798 int wm_adsp1_init(struct wm_adsp *dsp) 2799 { 2800 dsp->ops = &wm_adsp1_ops; 2801 2802 return wm_adsp_common_init(dsp); 2803 } 2804 EXPORT_SYMBOL_GPL(wm_adsp1_init); 2805 2806 int wm_adsp1_event(struct snd_soc_dapm_widget *w, 2807 struct snd_kcontrol *kcontrol, 2808 int event) 2809 { 2810 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 2811 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 2812 struct wm_adsp *dsp = &dsps[w->shift]; 2813 struct wm_coeff_ctl *ctl; 2814 int ret; 2815 unsigned int val; 2816 2817 dsp->component = component; 2818 2819 mutex_lock(&dsp->pwr_lock); 2820 2821 switch (event) { 2822 case SND_SOC_DAPM_POST_PMU: 2823 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, 2824 ADSP1_SYS_ENA, ADSP1_SYS_ENA); 2825 2826 /* 2827 * For simplicity set the DSP clock rate to be the 2828 * SYSCLK rate rather than making it configurable. 2829 */ 2830 if (dsp->sysclk_reg) { 2831 ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val); 2832 if (ret != 0) { 2833 adsp_err(dsp, "Failed to read SYSCLK state: %d\n", 2834 ret); 2835 goto err_mutex; 2836 } 2837 2838 val = (val & dsp->sysclk_mask) >> dsp->sysclk_shift; 2839 2840 ret = regmap_update_bits(dsp->regmap, 2841 dsp->base + ADSP1_CONTROL_31, 2842 ADSP1_CLK_SEL_MASK, val); 2843 if (ret != 0) { 2844 adsp_err(dsp, "Failed to set clock rate: %d\n", 2845 ret); 2846 goto err_mutex; 2847 } 2848 } 2849 2850 ret = wm_adsp_load(dsp); 2851 if (ret != 0) 2852 goto err_ena; 2853 2854 ret = wm_adsp1_setup_algs(dsp); 2855 if (ret != 0) 2856 goto err_ena; 2857 2858 ret = wm_adsp_load_coeff(dsp); 2859 if (ret != 0) 2860 goto err_ena; 2861 2862 /* Initialize caches for enabled and unset controls */ 2863 ret = wm_coeff_init_control_caches(dsp); 2864 if (ret != 0) 2865 goto err_ena; 2866 2867 /* Sync set controls */ 2868 ret = wm_coeff_sync_controls(dsp); 2869 if (ret != 0) 2870 goto err_ena; 2871 2872 dsp->booted = true; 2873 2874 /* Start the core running */ 2875 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, 2876 ADSP1_CORE_ENA | ADSP1_START, 2877 ADSP1_CORE_ENA | ADSP1_START); 2878 2879 dsp->running = true; 2880 break; 2881 2882 case SND_SOC_DAPM_PRE_PMD: 2883 dsp->running = false; 2884 dsp->booted = false; 2885 2886 /* Halt the core */ 2887 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, 2888 ADSP1_CORE_ENA | ADSP1_START, 0); 2889 2890 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19, 2891 ADSP1_WDMA_BUFFER_LENGTH_MASK, 0); 2892 2893 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, 2894 ADSP1_SYS_ENA, 0); 2895 2896 list_for_each_entry(ctl, &dsp->ctl_list, list) 2897 ctl->enabled = 0; 2898 2899 2900 wm_adsp_free_alg_regions(dsp); 2901 break; 2902 2903 default: 2904 break; 2905 } 2906 2907 mutex_unlock(&dsp->pwr_lock); 2908 2909 return 0; 2910 2911 err_ena: 2912 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30, 2913 ADSP1_SYS_ENA, 0); 2914 err_mutex: 2915 mutex_unlock(&dsp->pwr_lock); 2916 2917 return ret; 2918 } 2919 EXPORT_SYMBOL_GPL(wm_adsp1_event); 2920 2921 static int wm_adsp2v2_enable_core(struct wm_adsp *dsp) 2922 { 2923 unsigned int val; 2924 int ret, count; 2925 2926 /* Wait for the RAM to start, should be near instantaneous */ 2927 for (count = 0; count < 10; ++count) { 2928 ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1, &val); 2929 if (ret != 0) 2930 return ret; 2931 2932 if (val & ADSP2_RAM_RDY) 2933 break; 2934 2935 usleep_range(250, 500); 2936 } 2937 2938 if (!(val & ADSP2_RAM_RDY)) { 2939 adsp_err(dsp, "Failed to start DSP RAM\n"); 2940 return -EBUSY; 2941 } 2942 2943 adsp_dbg(dsp, "RAM ready after %d polls\n", count); 2944 2945 return 0; 2946 } 2947 2948 static int wm_adsp2_enable_core(struct wm_adsp *dsp) 2949 { 2950 int ret; 2951 2952 ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL, 2953 ADSP2_SYS_ENA, ADSP2_SYS_ENA); 2954 if (ret != 0) 2955 return ret; 2956 2957 return wm_adsp2v2_enable_core(dsp); 2958 } 2959 2960 static int wm_adsp2_lock(struct wm_adsp *dsp, unsigned int lock_regions) 2961 { 2962 struct regmap *regmap = dsp->regmap; 2963 unsigned int code0, code1, lock_reg; 2964 2965 if (!(lock_regions & WM_ADSP2_REGION_ALL)) 2966 return 0; 2967 2968 lock_regions &= WM_ADSP2_REGION_ALL; 2969 lock_reg = dsp->base + ADSP2_LOCK_REGION_1_LOCK_REGION_0; 2970 2971 while (lock_regions) { 2972 code0 = code1 = 0; 2973 if (lock_regions & BIT(0)) { 2974 code0 = ADSP2_LOCK_CODE_0; 2975 code1 = ADSP2_LOCK_CODE_1; 2976 } 2977 if (lock_regions & BIT(1)) { 2978 code0 |= ADSP2_LOCK_CODE_0 << ADSP2_LOCK_REGION_SHIFT; 2979 code1 |= ADSP2_LOCK_CODE_1 << ADSP2_LOCK_REGION_SHIFT; 2980 } 2981 regmap_write(regmap, lock_reg, code0); 2982 regmap_write(regmap, lock_reg, code1); 2983 lock_regions >>= 2; 2984 lock_reg += 2; 2985 } 2986 2987 return 0; 2988 } 2989 2990 static int wm_adsp2_enable_memory(struct wm_adsp *dsp) 2991 { 2992 return regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, 2993 ADSP2_MEM_ENA, ADSP2_MEM_ENA); 2994 } 2995 2996 static void wm_adsp2_disable_memory(struct wm_adsp *dsp) 2997 { 2998 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, 2999 ADSP2_MEM_ENA, 0); 3000 } 3001 3002 static void wm_adsp2_disable_core(struct wm_adsp *dsp) 3003 { 3004 regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0); 3005 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0); 3006 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0); 3007 3008 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, 3009 ADSP2_SYS_ENA, 0); 3010 } 3011 3012 static void wm_adsp2v2_disable_core(struct wm_adsp *dsp) 3013 { 3014 regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0); 3015 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0); 3016 regmap_write(dsp->regmap, dsp->base + ADSP2V2_WDMA_CONFIG_2, 0); 3017 } 3018 3019 static void wm_adsp_boot_work(struct work_struct *work) 3020 { 3021 struct wm_adsp *dsp = container_of(work, 3022 struct wm_adsp, 3023 boot_work); 3024 int ret; 3025 3026 mutex_lock(&dsp->pwr_lock); 3027 3028 if (dsp->ops->enable_memory) { 3029 ret = dsp->ops->enable_memory(dsp); 3030 if (ret != 0) 3031 goto err_mutex; 3032 } 3033 3034 if (dsp->ops->enable_core) { 3035 ret = dsp->ops->enable_core(dsp); 3036 if (ret != 0) 3037 goto err_mem; 3038 } 3039 3040 ret = wm_adsp_load(dsp); 3041 if (ret != 0) 3042 goto err_ena; 3043 3044 ret = dsp->ops->setup_algs(dsp); 3045 if (ret != 0) 3046 goto err_ena; 3047 3048 ret = wm_adsp_load_coeff(dsp); 3049 if (ret != 0) 3050 goto err_ena; 3051 3052 /* Initialize caches for enabled and unset controls */ 3053 ret = wm_coeff_init_control_caches(dsp); 3054 if (ret != 0) 3055 goto err_ena; 3056 3057 if (dsp->ops->disable_core) 3058 dsp->ops->disable_core(dsp); 3059 3060 dsp->booted = true; 3061 3062 mutex_unlock(&dsp->pwr_lock); 3063 3064 return; 3065 3066 err_ena: 3067 if (dsp->ops->disable_core) 3068 dsp->ops->disable_core(dsp); 3069 err_mem: 3070 if (dsp->ops->disable_memory) 3071 dsp->ops->disable_memory(dsp); 3072 err_mutex: 3073 mutex_unlock(&dsp->pwr_lock); 3074 } 3075 3076 static int wm_halo_configure_mpu(struct wm_adsp *dsp, unsigned int lock_regions) 3077 { 3078 struct reg_sequence config[] = { 3079 { dsp->base + HALO_MPU_LOCK_CONFIG, 0x5555 }, 3080 { dsp->base + HALO_MPU_LOCK_CONFIG, 0xAAAA }, 3081 { dsp->base + HALO_MPU_XMEM_ACCESS_0, 0xFFFFFFFF }, 3082 { dsp->base + HALO_MPU_YMEM_ACCESS_0, 0xFFFFFFFF }, 3083 { dsp->base + HALO_MPU_WINDOW_ACCESS_0, lock_regions }, 3084 { dsp->base + HALO_MPU_XREG_ACCESS_0, lock_regions }, 3085 { dsp->base + HALO_MPU_YREG_ACCESS_0, lock_regions }, 3086 { dsp->base + HALO_MPU_XMEM_ACCESS_1, 0xFFFFFFFF }, 3087 { dsp->base + HALO_MPU_YMEM_ACCESS_1, 0xFFFFFFFF }, 3088 { dsp->base + HALO_MPU_WINDOW_ACCESS_1, lock_regions }, 3089 { dsp->base + HALO_MPU_XREG_ACCESS_1, lock_regions }, 3090 { dsp->base + HALO_MPU_YREG_ACCESS_1, lock_regions }, 3091 { dsp->base + HALO_MPU_XMEM_ACCESS_2, 0xFFFFFFFF }, 3092 { dsp->base + HALO_MPU_YMEM_ACCESS_2, 0xFFFFFFFF }, 3093 { dsp->base + HALO_MPU_WINDOW_ACCESS_2, lock_regions }, 3094 { dsp->base + HALO_MPU_XREG_ACCESS_2, lock_regions }, 3095 { dsp->base + HALO_MPU_YREG_ACCESS_2, lock_regions }, 3096 { dsp->base + HALO_MPU_XMEM_ACCESS_3, 0xFFFFFFFF }, 3097 { dsp->base + HALO_MPU_YMEM_ACCESS_3, 0xFFFFFFFF }, 3098 { dsp->base + HALO_MPU_WINDOW_ACCESS_3, lock_regions }, 3099 { dsp->base + HALO_MPU_XREG_ACCESS_3, lock_regions }, 3100 { dsp->base + HALO_MPU_YREG_ACCESS_3, lock_regions }, 3101 { dsp->base + HALO_MPU_LOCK_CONFIG, 0 }, 3102 }; 3103 3104 return regmap_multi_reg_write(dsp->regmap, config, ARRAY_SIZE(config)); 3105 } 3106 3107 int wm_adsp2_set_dspclk(struct snd_soc_dapm_widget *w, unsigned int freq) 3108 { 3109 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 3110 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 3111 struct wm_adsp *dsp = &dsps[w->shift]; 3112 int ret; 3113 3114 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CLOCKING, 3115 ADSP2_CLK_SEL_MASK, 3116 freq << ADSP2_CLK_SEL_SHIFT); 3117 if (ret) 3118 adsp_err(dsp, "Failed to set clock rate: %d\n", ret); 3119 3120 return ret; 3121 } 3122 EXPORT_SYMBOL_GPL(wm_adsp2_set_dspclk); 3123 3124 int wm_adsp2_preloader_get(struct snd_kcontrol *kcontrol, 3125 struct snd_ctl_elem_value *ucontrol) 3126 { 3127 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 3128 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 3129 struct soc_mixer_control *mc = 3130 (struct soc_mixer_control *)kcontrol->private_value; 3131 struct wm_adsp *dsp = &dsps[mc->shift - 1]; 3132 3133 ucontrol->value.integer.value[0] = dsp->preloaded; 3134 3135 return 0; 3136 } 3137 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_get); 3138 3139 int wm_adsp2_preloader_put(struct snd_kcontrol *kcontrol, 3140 struct snd_ctl_elem_value *ucontrol) 3141 { 3142 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 3143 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 3144 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 3145 struct soc_mixer_control *mc = 3146 (struct soc_mixer_control *)kcontrol->private_value; 3147 struct wm_adsp *dsp = &dsps[mc->shift - 1]; 3148 char preload[32]; 3149 3150 snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->name); 3151 3152 dsp->preloaded = ucontrol->value.integer.value[0]; 3153 3154 if (ucontrol->value.integer.value[0]) 3155 snd_soc_component_force_enable_pin(component, preload); 3156 else 3157 snd_soc_component_disable_pin(component, preload); 3158 3159 snd_soc_dapm_sync(dapm); 3160 3161 flush_work(&dsp->boot_work); 3162 3163 return 0; 3164 } 3165 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_put); 3166 3167 static void wm_adsp_stop_watchdog(struct wm_adsp *dsp) 3168 { 3169 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_WATCHDOG, 3170 ADSP2_WDT_ENA_MASK, 0); 3171 } 3172 3173 static void wm_halo_stop_watchdog(struct wm_adsp *dsp) 3174 { 3175 regmap_update_bits(dsp->regmap, dsp->base + HALO_WDT_CONTROL, 3176 HALO_WDT_EN_MASK, 0); 3177 } 3178 3179 int wm_adsp_early_event(struct snd_soc_dapm_widget *w, 3180 struct snd_kcontrol *kcontrol, int event) 3181 { 3182 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 3183 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 3184 struct wm_adsp *dsp = &dsps[w->shift]; 3185 struct wm_coeff_ctl *ctl; 3186 3187 switch (event) { 3188 case SND_SOC_DAPM_PRE_PMU: 3189 queue_work(system_unbound_wq, &dsp->boot_work); 3190 break; 3191 case SND_SOC_DAPM_PRE_PMD: 3192 mutex_lock(&dsp->pwr_lock); 3193 3194 wm_adsp_debugfs_clear(dsp); 3195 3196 dsp->fw_id = 0; 3197 dsp->fw_id_version = 0; 3198 3199 dsp->booted = false; 3200 3201 if (dsp->ops->disable_memory) 3202 dsp->ops->disable_memory(dsp); 3203 3204 list_for_each_entry(ctl, &dsp->ctl_list, list) 3205 ctl->enabled = 0; 3206 3207 wm_adsp_free_alg_regions(dsp); 3208 3209 mutex_unlock(&dsp->pwr_lock); 3210 3211 adsp_dbg(dsp, "Shutdown complete\n"); 3212 break; 3213 default: 3214 break; 3215 } 3216 3217 return 0; 3218 } 3219 EXPORT_SYMBOL_GPL(wm_adsp_early_event); 3220 3221 static int wm_adsp2_start_core(struct wm_adsp *dsp) 3222 { 3223 return regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, 3224 ADSP2_CORE_ENA | ADSP2_START, 3225 ADSP2_CORE_ENA | ADSP2_START); 3226 } 3227 3228 static void wm_adsp2_stop_core(struct wm_adsp *dsp) 3229 { 3230 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, 3231 ADSP2_CORE_ENA | ADSP2_START, 0); 3232 } 3233 3234 int wm_adsp_event(struct snd_soc_dapm_widget *w, 3235 struct snd_kcontrol *kcontrol, int event) 3236 { 3237 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 3238 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 3239 struct wm_adsp *dsp = &dsps[w->shift]; 3240 int ret; 3241 3242 switch (event) { 3243 case SND_SOC_DAPM_POST_PMU: 3244 flush_work(&dsp->boot_work); 3245 3246 mutex_lock(&dsp->pwr_lock); 3247 3248 if (!dsp->booted) { 3249 ret = -EIO; 3250 goto err; 3251 } 3252 3253 if (dsp->ops->enable_core) { 3254 ret = dsp->ops->enable_core(dsp); 3255 if (ret != 0) 3256 goto err; 3257 } 3258 3259 /* Sync set controls */ 3260 ret = wm_coeff_sync_controls(dsp); 3261 if (ret != 0) 3262 goto err; 3263 3264 if (dsp->ops->lock_memory) { 3265 ret = dsp->ops->lock_memory(dsp, dsp->lock_regions); 3266 if (ret != 0) { 3267 adsp_err(dsp, "Error configuring MPU: %d\n", 3268 ret); 3269 goto err; 3270 } 3271 } 3272 3273 if (dsp->ops->start_core) { 3274 ret = dsp->ops->start_core(dsp); 3275 if (ret != 0) 3276 goto err; 3277 } 3278 3279 if (wm_adsp_fw[dsp->fw].num_caps != 0) { 3280 ret = wm_adsp_buffer_init(dsp); 3281 if (ret < 0) 3282 goto err; 3283 } 3284 3285 dsp->running = true; 3286 3287 mutex_unlock(&dsp->pwr_lock); 3288 break; 3289 3290 case SND_SOC_DAPM_PRE_PMD: 3291 /* Tell the firmware to cleanup */ 3292 wm_adsp_signal_event_controls(dsp, WM_ADSP_FW_EVENT_SHUTDOWN); 3293 3294 if (dsp->ops->stop_watchdog) 3295 dsp->ops->stop_watchdog(dsp); 3296 3297 /* Log firmware state, it can be useful for analysis */ 3298 if (dsp->ops->show_fw_status) 3299 dsp->ops->show_fw_status(dsp); 3300 3301 mutex_lock(&dsp->pwr_lock); 3302 3303 dsp->running = false; 3304 3305 if (dsp->ops->stop_core) 3306 dsp->ops->stop_core(dsp); 3307 if (dsp->ops->disable_core) 3308 dsp->ops->disable_core(dsp); 3309 3310 if (wm_adsp_fw[dsp->fw].num_caps != 0) 3311 wm_adsp_buffer_free(dsp); 3312 3313 dsp->fatal_error = false; 3314 3315 mutex_unlock(&dsp->pwr_lock); 3316 3317 adsp_dbg(dsp, "Execution stopped\n"); 3318 break; 3319 3320 default: 3321 break; 3322 } 3323 3324 return 0; 3325 err: 3326 if (dsp->ops->stop_core) 3327 dsp->ops->stop_core(dsp); 3328 if (dsp->ops->disable_core) 3329 dsp->ops->disable_core(dsp); 3330 mutex_unlock(&dsp->pwr_lock); 3331 return ret; 3332 } 3333 EXPORT_SYMBOL_GPL(wm_adsp_event); 3334 3335 static int wm_halo_start_core(struct wm_adsp *dsp) 3336 { 3337 return regmap_update_bits(dsp->regmap, 3338 dsp->base + HALO_CCM_CORE_CONTROL, 3339 HALO_CORE_EN, HALO_CORE_EN); 3340 } 3341 3342 static void wm_halo_stop_core(struct wm_adsp *dsp) 3343 { 3344 regmap_update_bits(dsp->regmap, dsp->base + HALO_CCM_CORE_CONTROL, 3345 HALO_CORE_EN, 0); 3346 3347 /* reset halo core with CORE_SOFT_RESET */ 3348 regmap_update_bits(dsp->regmap, dsp->base + HALO_CORE_SOFT_RESET, 3349 HALO_CORE_SOFT_RESET_MASK, 1); 3350 } 3351 3352 int wm_adsp2_component_probe(struct wm_adsp *dsp, struct snd_soc_component *component) 3353 { 3354 char preload[32]; 3355 3356 snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->name); 3357 snd_soc_component_disable_pin(component, preload); 3358 3359 wm_adsp2_init_debugfs(dsp, component); 3360 3361 dsp->component = component; 3362 3363 return 0; 3364 } 3365 EXPORT_SYMBOL_GPL(wm_adsp2_component_probe); 3366 3367 int wm_adsp2_component_remove(struct wm_adsp *dsp, struct snd_soc_component *component) 3368 { 3369 wm_adsp2_cleanup_debugfs(dsp); 3370 3371 return 0; 3372 } 3373 EXPORT_SYMBOL_GPL(wm_adsp2_component_remove); 3374 3375 int wm_adsp2_init(struct wm_adsp *dsp) 3376 { 3377 int ret; 3378 3379 ret = wm_adsp_common_init(dsp); 3380 if (ret) 3381 return ret; 3382 3383 switch (dsp->rev) { 3384 case 0: 3385 /* 3386 * Disable the DSP memory by default when in reset for a small 3387 * power saving. 3388 */ 3389 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL, 3390 ADSP2_MEM_ENA, 0); 3391 if (ret) { 3392 adsp_err(dsp, 3393 "Failed to clear memory retention: %d\n", ret); 3394 return ret; 3395 } 3396 3397 dsp->ops = &wm_adsp2_ops[0]; 3398 break; 3399 case 1: 3400 dsp->ops = &wm_adsp2_ops[1]; 3401 break; 3402 default: 3403 dsp->ops = &wm_adsp2_ops[2]; 3404 break; 3405 } 3406 3407 INIT_WORK(&dsp->boot_work, wm_adsp_boot_work); 3408 3409 return 0; 3410 } 3411 EXPORT_SYMBOL_GPL(wm_adsp2_init); 3412 3413 int wm_halo_init(struct wm_adsp *dsp) 3414 { 3415 int ret; 3416 3417 ret = wm_adsp_common_init(dsp); 3418 if (ret) 3419 return ret; 3420 3421 dsp->ops = &wm_halo_ops; 3422 3423 INIT_WORK(&dsp->boot_work, wm_adsp_boot_work); 3424 3425 return 0; 3426 } 3427 EXPORT_SYMBOL_GPL(wm_halo_init); 3428 3429 void wm_adsp2_remove(struct wm_adsp *dsp) 3430 { 3431 struct wm_coeff_ctl *ctl; 3432 3433 while (!list_empty(&dsp->ctl_list)) { 3434 ctl = list_first_entry(&dsp->ctl_list, struct wm_coeff_ctl, 3435 list); 3436 list_del(&ctl->list); 3437 wm_adsp_free_ctl_blk(ctl); 3438 } 3439 } 3440 EXPORT_SYMBOL_GPL(wm_adsp2_remove); 3441 3442 static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr) 3443 { 3444 return compr->buf != NULL; 3445 } 3446 3447 static int wm_adsp_compr_attach(struct wm_adsp_compr *compr) 3448 { 3449 struct wm_adsp_compr_buf *buf = NULL, *tmp; 3450 3451 if (compr->dsp->fatal_error) 3452 return -EINVAL; 3453 3454 list_for_each_entry(tmp, &compr->dsp->buffer_list, list) { 3455 if (!tmp->name || !strcmp(compr->name, tmp->name)) { 3456 buf = tmp; 3457 break; 3458 } 3459 } 3460 3461 if (!buf) 3462 return -EINVAL; 3463 3464 compr->buf = buf; 3465 buf->compr = compr; 3466 3467 return 0; 3468 } 3469 3470 static void wm_adsp_compr_detach(struct wm_adsp_compr *compr) 3471 { 3472 if (!compr) 3473 return; 3474 3475 /* Wake the poll so it can see buffer is no longer attached */ 3476 if (compr->stream) 3477 snd_compr_fragment_elapsed(compr->stream); 3478 3479 if (wm_adsp_compr_attached(compr)) { 3480 compr->buf->compr = NULL; 3481 compr->buf = NULL; 3482 } 3483 } 3484 3485 int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream) 3486 { 3487 struct wm_adsp_compr *compr, *tmp; 3488 struct snd_soc_pcm_runtime *rtd = stream->private_data; 3489 int ret = 0; 3490 3491 mutex_lock(&dsp->pwr_lock); 3492 3493 if (wm_adsp_fw[dsp->fw].num_caps == 0) { 3494 adsp_err(dsp, "%s: Firmware does not support compressed API\n", 3495 asoc_rtd_to_codec(rtd, 0)->name); 3496 ret = -ENXIO; 3497 goto out; 3498 } 3499 3500 if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) { 3501 adsp_err(dsp, "%s: Firmware does not support stream direction\n", 3502 asoc_rtd_to_codec(rtd, 0)->name); 3503 ret = -EINVAL; 3504 goto out; 3505 } 3506 3507 list_for_each_entry(tmp, &dsp->compr_list, list) { 3508 if (!strcmp(tmp->name, asoc_rtd_to_codec(rtd, 0)->name)) { 3509 adsp_err(dsp, "%s: Only a single stream supported per dai\n", 3510 asoc_rtd_to_codec(rtd, 0)->name); 3511 ret = -EBUSY; 3512 goto out; 3513 } 3514 } 3515 3516 compr = kzalloc(sizeof(*compr), GFP_KERNEL); 3517 if (!compr) { 3518 ret = -ENOMEM; 3519 goto out; 3520 } 3521 3522 compr->dsp = dsp; 3523 compr->stream = stream; 3524 compr->name = asoc_rtd_to_codec(rtd, 0)->name; 3525 3526 list_add_tail(&compr->list, &dsp->compr_list); 3527 3528 stream->runtime->private_data = compr; 3529 3530 out: 3531 mutex_unlock(&dsp->pwr_lock); 3532 3533 return ret; 3534 } 3535 EXPORT_SYMBOL_GPL(wm_adsp_compr_open); 3536 3537 int wm_adsp_compr_free(struct snd_soc_component *component, 3538 struct snd_compr_stream *stream) 3539 { 3540 struct wm_adsp_compr *compr = stream->runtime->private_data; 3541 struct wm_adsp *dsp = compr->dsp; 3542 3543 mutex_lock(&dsp->pwr_lock); 3544 3545 wm_adsp_compr_detach(compr); 3546 list_del(&compr->list); 3547 3548 kfree(compr->raw_buf); 3549 kfree(compr); 3550 3551 mutex_unlock(&dsp->pwr_lock); 3552 3553 return 0; 3554 } 3555 EXPORT_SYMBOL_GPL(wm_adsp_compr_free); 3556 3557 static int wm_adsp_compr_check_params(struct snd_compr_stream *stream, 3558 struct snd_compr_params *params) 3559 { 3560 struct wm_adsp_compr *compr = stream->runtime->private_data; 3561 struct wm_adsp *dsp = compr->dsp; 3562 const struct wm_adsp_fw_caps *caps; 3563 const struct snd_codec_desc *desc; 3564 int i, j; 3565 3566 if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE || 3567 params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE || 3568 params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS || 3569 params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS || 3570 params->buffer.fragment_size % WM_ADSP_DATA_WORD_SIZE) { 3571 compr_err(compr, "Invalid buffer fragsize=%d fragments=%d\n", 3572 params->buffer.fragment_size, 3573 params->buffer.fragments); 3574 3575 return -EINVAL; 3576 } 3577 3578 for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) { 3579 caps = &wm_adsp_fw[dsp->fw].caps[i]; 3580 desc = &caps->desc; 3581 3582 if (caps->id != params->codec.id) 3583 continue; 3584 3585 if (stream->direction == SND_COMPRESS_PLAYBACK) { 3586 if (desc->max_ch < params->codec.ch_out) 3587 continue; 3588 } else { 3589 if (desc->max_ch < params->codec.ch_in) 3590 continue; 3591 } 3592 3593 if (!(desc->formats & (1 << params->codec.format))) 3594 continue; 3595 3596 for (j = 0; j < desc->num_sample_rates; ++j) 3597 if (desc->sample_rates[j] == params->codec.sample_rate) 3598 return 0; 3599 } 3600 3601 compr_err(compr, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n", 3602 params->codec.id, params->codec.ch_in, params->codec.ch_out, 3603 params->codec.sample_rate, params->codec.format); 3604 return -EINVAL; 3605 } 3606 3607 static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr) 3608 { 3609 return compr->size.fragment_size / WM_ADSP_DATA_WORD_SIZE; 3610 } 3611 3612 int wm_adsp_compr_set_params(struct snd_soc_component *component, 3613 struct snd_compr_stream *stream, 3614 struct snd_compr_params *params) 3615 { 3616 struct wm_adsp_compr *compr = stream->runtime->private_data; 3617 unsigned int size; 3618 int ret; 3619 3620 ret = wm_adsp_compr_check_params(stream, params); 3621 if (ret) 3622 return ret; 3623 3624 compr->size = params->buffer; 3625 3626 compr_dbg(compr, "fragment_size=%d fragments=%d\n", 3627 compr->size.fragment_size, compr->size.fragments); 3628 3629 size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf); 3630 compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL); 3631 if (!compr->raw_buf) 3632 return -ENOMEM; 3633 3634 compr->sample_rate = params->codec.sample_rate; 3635 3636 return 0; 3637 } 3638 EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params); 3639 3640 int wm_adsp_compr_get_caps(struct snd_soc_component *component, 3641 struct snd_compr_stream *stream, 3642 struct snd_compr_caps *caps) 3643 { 3644 struct wm_adsp_compr *compr = stream->runtime->private_data; 3645 int fw = compr->dsp->fw; 3646 int i; 3647 3648 if (wm_adsp_fw[fw].caps) { 3649 for (i = 0; i < wm_adsp_fw[fw].num_caps; i++) 3650 caps->codecs[i] = wm_adsp_fw[fw].caps[i].id; 3651 3652 caps->num_codecs = i; 3653 caps->direction = wm_adsp_fw[fw].compr_direction; 3654 3655 caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE; 3656 caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE; 3657 caps->min_fragments = WM_ADSP_MIN_FRAGMENTS; 3658 caps->max_fragments = WM_ADSP_MAX_FRAGMENTS; 3659 } 3660 3661 return 0; 3662 } 3663 EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps); 3664 3665 static int wm_adsp_read_data_block(struct wm_adsp *dsp, int mem_type, 3666 unsigned int mem_addr, 3667 unsigned int num_words, u32 *data) 3668 { 3669 struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type); 3670 unsigned int i, reg; 3671 int ret; 3672 3673 if (!mem) 3674 return -EINVAL; 3675 3676 reg = dsp->ops->region_to_reg(mem, mem_addr); 3677 3678 ret = regmap_raw_read(dsp->regmap, reg, data, 3679 sizeof(*data) * num_words); 3680 if (ret < 0) 3681 return ret; 3682 3683 for (i = 0; i < num_words; ++i) 3684 data[i] = be32_to_cpu(data[i]) & 0x00ffffffu; 3685 3686 return 0; 3687 } 3688 3689 static inline int wm_adsp_read_data_word(struct wm_adsp *dsp, int mem_type, 3690 unsigned int mem_addr, u32 *data) 3691 { 3692 return wm_adsp_read_data_block(dsp, mem_type, mem_addr, 1, data); 3693 } 3694 3695 static int wm_adsp_write_data_word(struct wm_adsp *dsp, int mem_type, 3696 unsigned int mem_addr, u32 data) 3697 { 3698 struct wm_adsp_region const *mem = wm_adsp_find_region(dsp, mem_type); 3699 unsigned int reg; 3700 3701 if (!mem) 3702 return -EINVAL; 3703 3704 reg = dsp->ops->region_to_reg(mem, mem_addr); 3705 3706 data = cpu_to_be32(data & 0x00ffffffu); 3707 3708 return regmap_raw_write(dsp->regmap, reg, &data, sizeof(data)); 3709 } 3710 3711 static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf, 3712 unsigned int field_offset, u32 *data) 3713 { 3714 return wm_adsp_read_data_word(buf->dsp, buf->host_buf_mem_type, 3715 buf->host_buf_ptr + field_offset, data); 3716 } 3717 3718 static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf, 3719 unsigned int field_offset, u32 data) 3720 { 3721 return wm_adsp_write_data_word(buf->dsp, buf->host_buf_mem_type, 3722 buf->host_buf_ptr + field_offset, data); 3723 } 3724 3725 static void wm_adsp_remove_padding(u32 *buf, int nwords, int data_word_size) 3726 { 3727 u8 *pack_in = (u8 *)buf; 3728 u8 *pack_out = (u8 *)buf; 3729 int i, j; 3730 3731 /* Remove the padding bytes from the data read from the DSP */ 3732 for (i = 0; i < nwords; i++) { 3733 for (j = 0; j < data_word_size; j++) 3734 *pack_out++ = *pack_in++; 3735 3736 pack_in += sizeof(*buf) - data_word_size; 3737 } 3738 } 3739 3740 static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf) 3741 { 3742 const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps; 3743 struct wm_adsp_buffer_region *region; 3744 u32 offset = 0; 3745 int i, ret; 3746 3747 buf->regions = kcalloc(caps->num_regions, sizeof(*buf->regions), 3748 GFP_KERNEL); 3749 if (!buf->regions) 3750 return -ENOMEM; 3751 3752 for (i = 0; i < caps->num_regions; ++i) { 3753 region = &buf->regions[i]; 3754 3755 region->offset = offset; 3756 region->mem_type = caps->region_defs[i].mem_type; 3757 3758 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset, 3759 ®ion->base_addr); 3760 if (ret < 0) 3761 return ret; 3762 3763 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset, 3764 &offset); 3765 if (ret < 0) 3766 return ret; 3767 3768 region->cumulative_size = offset; 3769 3770 compr_dbg(buf, 3771 "region=%d type=%d base=%08x off=%08x size=%08x\n", 3772 i, region->mem_type, region->base_addr, 3773 region->offset, region->cumulative_size); 3774 } 3775 3776 return 0; 3777 } 3778 3779 static void wm_adsp_buffer_clear(struct wm_adsp_compr_buf *buf) 3780 { 3781 buf->irq_count = 0xFFFFFFFF; 3782 buf->read_index = -1; 3783 buf->avail = 0; 3784 } 3785 3786 static struct wm_adsp_compr_buf *wm_adsp_buffer_alloc(struct wm_adsp *dsp) 3787 { 3788 struct wm_adsp_compr_buf *buf; 3789 3790 buf = kzalloc(sizeof(*buf), GFP_KERNEL); 3791 if (!buf) 3792 return NULL; 3793 3794 buf->dsp = dsp; 3795 3796 wm_adsp_buffer_clear(buf); 3797 3798 list_add_tail(&buf->list, &dsp->buffer_list); 3799 3800 return buf; 3801 } 3802 3803 static int wm_adsp_buffer_parse_legacy(struct wm_adsp *dsp) 3804 { 3805 struct wm_adsp_alg_region *alg_region; 3806 struct wm_adsp_compr_buf *buf; 3807 u32 xmalg, addr, magic; 3808 int i, ret; 3809 3810 alg_region = wm_adsp_find_alg_region(dsp, WMFW_ADSP2_XM, dsp->fw_id); 3811 if (!alg_region) { 3812 adsp_err(dsp, "No algorithm region found\n"); 3813 return -EINVAL; 3814 } 3815 3816 buf = wm_adsp_buffer_alloc(dsp); 3817 if (!buf) 3818 return -ENOMEM; 3819 3820 xmalg = dsp->ops->sys_config_size / sizeof(__be32); 3821 3822 addr = alg_region->base + xmalg + ALG_XM_FIELD(magic); 3823 ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr, &magic); 3824 if (ret < 0) 3825 return ret; 3826 3827 if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC) 3828 return -ENODEV; 3829 3830 addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr); 3831 for (i = 0; i < 5; ++i) { 3832 ret = wm_adsp_read_data_word(dsp, WMFW_ADSP2_XM, addr, 3833 &buf->host_buf_ptr); 3834 if (ret < 0) 3835 return ret; 3836 3837 if (buf->host_buf_ptr) 3838 break; 3839 3840 usleep_range(1000, 2000); 3841 } 3842 3843 if (!buf->host_buf_ptr) 3844 return -EIO; 3845 3846 buf->host_buf_mem_type = WMFW_ADSP2_XM; 3847 3848 ret = wm_adsp_buffer_populate(buf); 3849 if (ret < 0) 3850 return ret; 3851 3852 compr_dbg(buf, "legacy host_buf_ptr=%x\n", buf->host_buf_ptr); 3853 3854 return 0; 3855 } 3856 3857 static int wm_adsp_buffer_parse_coeff(struct wm_coeff_ctl *ctl) 3858 { 3859 struct wm_adsp_host_buf_coeff_v1 coeff_v1; 3860 struct wm_adsp_compr_buf *buf; 3861 unsigned int val, reg; 3862 int ret, i; 3863 3864 ret = wm_coeff_base_reg(ctl, ®); 3865 if (ret) 3866 return ret; 3867 3868 for (i = 0; i < 5; ++i) { 3869 ret = regmap_raw_read(ctl->dsp->regmap, reg, &val, sizeof(val)); 3870 if (ret < 0) 3871 return ret; 3872 3873 if (val) 3874 break; 3875 3876 usleep_range(1000, 2000); 3877 } 3878 3879 if (!val) { 3880 adsp_err(ctl->dsp, "Failed to acquire host buffer\n"); 3881 return -EIO; 3882 } 3883 3884 buf = wm_adsp_buffer_alloc(ctl->dsp); 3885 if (!buf) 3886 return -ENOMEM; 3887 3888 buf->host_buf_mem_type = ctl->alg_region.type; 3889 buf->host_buf_ptr = be32_to_cpu(val); 3890 3891 ret = wm_adsp_buffer_populate(buf); 3892 if (ret < 0) 3893 return ret; 3894 3895 /* 3896 * v0 host_buffer coefficients didn't have versioning, so if the 3897 * control is one word, assume version 0. 3898 */ 3899 if (ctl->len == 4) { 3900 compr_dbg(buf, "host_buf_ptr=%x\n", buf->host_buf_ptr); 3901 return 0; 3902 } 3903 3904 ret = regmap_raw_read(ctl->dsp->regmap, reg, &coeff_v1, 3905 sizeof(coeff_v1)); 3906 if (ret < 0) 3907 return ret; 3908 3909 coeff_v1.versions = be32_to_cpu(coeff_v1.versions); 3910 val = coeff_v1.versions & HOST_BUF_COEFF_COMPAT_VER_MASK; 3911 val >>= HOST_BUF_COEFF_COMPAT_VER_SHIFT; 3912 3913 if (val > HOST_BUF_COEFF_SUPPORTED_COMPAT_VER) { 3914 adsp_err(ctl->dsp, 3915 "Host buffer coeff ver %u > supported version %u\n", 3916 val, HOST_BUF_COEFF_SUPPORTED_COMPAT_VER); 3917 return -EINVAL; 3918 } 3919 3920 for (i = 0; i < ARRAY_SIZE(coeff_v1.name); i++) 3921 coeff_v1.name[i] = be32_to_cpu(coeff_v1.name[i]); 3922 3923 wm_adsp_remove_padding((u32 *)&coeff_v1.name, 3924 ARRAY_SIZE(coeff_v1.name), 3925 WM_ADSP_DATA_WORD_SIZE); 3926 3927 buf->name = kasprintf(GFP_KERNEL, "%s-dsp-%s", ctl->dsp->part, 3928 (char *)&coeff_v1.name); 3929 3930 compr_dbg(buf, "host_buf_ptr=%x coeff version %u\n", 3931 buf->host_buf_ptr, val); 3932 3933 return val; 3934 } 3935 3936 static int wm_adsp_buffer_init(struct wm_adsp *dsp) 3937 { 3938 struct wm_coeff_ctl *ctl; 3939 int ret; 3940 3941 list_for_each_entry(ctl, &dsp->ctl_list, list) { 3942 if (ctl->type != WMFW_CTL_TYPE_HOST_BUFFER) 3943 continue; 3944 3945 if (!ctl->enabled) 3946 continue; 3947 3948 ret = wm_adsp_buffer_parse_coeff(ctl); 3949 if (ret < 0) { 3950 adsp_err(dsp, "Failed to parse coeff: %d\n", ret); 3951 goto error; 3952 } else if (ret == 0) { 3953 /* Only one buffer supported for version 0 */ 3954 return 0; 3955 } 3956 } 3957 3958 if (list_empty(&dsp->buffer_list)) { 3959 /* Fall back to legacy support */ 3960 ret = wm_adsp_buffer_parse_legacy(dsp); 3961 if (ret) { 3962 adsp_err(dsp, "Failed to parse legacy: %d\n", ret); 3963 goto error; 3964 } 3965 } 3966 3967 return 0; 3968 3969 error: 3970 wm_adsp_buffer_free(dsp); 3971 return ret; 3972 } 3973 3974 static int wm_adsp_buffer_free(struct wm_adsp *dsp) 3975 { 3976 struct wm_adsp_compr_buf *buf, *tmp; 3977 3978 list_for_each_entry_safe(buf, tmp, &dsp->buffer_list, list) { 3979 wm_adsp_compr_detach(buf->compr); 3980 3981 kfree(buf->name); 3982 kfree(buf->regions); 3983 list_del(&buf->list); 3984 kfree(buf); 3985 } 3986 3987 return 0; 3988 } 3989 3990 static int wm_adsp_buffer_get_error(struct wm_adsp_compr_buf *buf) 3991 { 3992 int ret; 3993 3994 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error); 3995 if (ret < 0) { 3996 compr_err(buf, "Failed to check buffer error: %d\n", ret); 3997 return ret; 3998 } 3999 if (buf->error != 0) { 4000 compr_err(buf, "Buffer error occurred: %d\n", buf->error); 4001 return -EIO; 4002 } 4003 4004 return 0; 4005 } 4006 4007 int wm_adsp_compr_trigger(struct snd_soc_component *component, 4008 struct snd_compr_stream *stream, int cmd) 4009 { 4010 struct wm_adsp_compr *compr = stream->runtime->private_data; 4011 struct wm_adsp *dsp = compr->dsp; 4012 int ret = 0; 4013 4014 compr_dbg(compr, "Trigger: %d\n", cmd); 4015 4016 mutex_lock(&dsp->pwr_lock); 4017 4018 switch (cmd) { 4019 case SNDRV_PCM_TRIGGER_START: 4020 if (!wm_adsp_compr_attached(compr)) { 4021 ret = wm_adsp_compr_attach(compr); 4022 if (ret < 0) { 4023 compr_err(compr, "Failed to link buffer and stream: %d\n", 4024 ret); 4025 break; 4026 } 4027 } 4028 4029 ret = wm_adsp_buffer_get_error(compr->buf); 4030 if (ret < 0) 4031 break; 4032 4033 /* Trigger the IRQ at one fragment of data */ 4034 ret = wm_adsp_buffer_write(compr->buf, 4035 HOST_BUFFER_FIELD(high_water_mark), 4036 wm_adsp_compr_frag_words(compr)); 4037 if (ret < 0) { 4038 compr_err(compr, "Failed to set high water mark: %d\n", 4039 ret); 4040 break; 4041 } 4042 break; 4043 case SNDRV_PCM_TRIGGER_STOP: 4044 if (wm_adsp_compr_attached(compr)) 4045 wm_adsp_buffer_clear(compr->buf); 4046 break; 4047 default: 4048 ret = -EINVAL; 4049 break; 4050 } 4051 4052 mutex_unlock(&dsp->pwr_lock); 4053 4054 return ret; 4055 } 4056 EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger); 4057 4058 static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf) 4059 { 4060 int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1; 4061 4062 return buf->regions[last_region].cumulative_size; 4063 } 4064 4065 static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf) 4066 { 4067 u32 next_read_index, next_write_index; 4068 int write_index, read_index, avail; 4069 int ret; 4070 4071 /* Only sync read index if we haven't already read a valid index */ 4072 if (buf->read_index < 0) { 4073 ret = wm_adsp_buffer_read(buf, 4074 HOST_BUFFER_FIELD(next_read_index), 4075 &next_read_index); 4076 if (ret < 0) 4077 return ret; 4078 4079 read_index = sign_extend32(next_read_index, 23); 4080 4081 if (read_index < 0) { 4082 compr_dbg(buf, "Avail check on unstarted stream\n"); 4083 return 0; 4084 } 4085 4086 buf->read_index = read_index; 4087 } 4088 4089 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index), 4090 &next_write_index); 4091 if (ret < 0) 4092 return ret; 4093 4094 write_index = sign_extend32(next_write_index, 23); 4095 4096 avail = write_index - buf->read_index; 4097 if (avail < 0) 4098 avail += wm_adsp_buffer_size(buf); 4099 4100 compr_dbg(buf, "readindex=0x%x, writeindex=0x%x, avail=%d\n", 4101 buf->read_index, write_index, avail * WM_ADSP_DATA_WORD_SIZE); 4102 4103 buf->avail = avail; 4104 4105 return 0; 4106 } 4107 4108 int wm_adsp_compr_handle_irq(struct wm_adsp *dsp) 4109 { 4110 struct wm_adsp_compr_buf *buf; 4111 struct wm_adsp_compr *compr; 4112 int ret = 0; 4113 4114 mutex_lock(&dsp->pwr_lock); 4115 4116 if (list_empty(&dsp->buffer_list)) { 4117 ret = -ENODEV; 4118 goto out; 4119 } 4120 4121 adsp_dbg(dsp, "Handling buffer IRQ\n"); 4122 4123 list_for_each_entry(buf, &dsp->buffer_list, list) { 4124 compr = buf->compr; 4125 4126 ret = wm_adsp_buffer_get_error(buf); 4127 if (ret < 0) 4128 goto out_notify; /* Wake poll to report error */ 4129 4130 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count), 4131 &buf->irq_count); 4132 if (ret < 0) { 4133 compr_err(buf, "Failed to get irq_count: %d\n", ret); 4134 goto out; 4135 } 4136 4137 ret = wm_adsp_buffer_update_avail(buf); 4138 if (ret < 0) { 4139 compr_err(buf, "Error reading avail: %d\n", ret); 4140 goto out; 4141 } 4142 4143 if (wm_adsp_fw[dsp->fw].voice_trigger && buf->irq_count == 2) 4144 ret = WM_ADSP_COMPR_VOICE_TRIGGER; 4145 4146 out_notify: 4147 if (compr && compr->stream) 4148 snd_compr_fragment_elapsed(compr->stream); 4149 } 4150 4151 out: 4152 mutex_unlock(&dsp->pwr_lock); 4153 4154 return ret; 4155 } 4156 EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq); 4157 4158 static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf) 4159 { 4160 if (buf->irq_count & 0x01) 4161 return 0; 4162 4163 compr_dbg(buf, "Enable IRQ(0x%x) for next fragment\n", buf->irq_count); 4164 4165 buf->irq_count |= 0x01; 4166 4167 return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack), 4168 buf->irq_count); 4169 } 4170 4171 int wm_adsp_compr_pointer(struct snd_soc_component *component, 4172 struct snd_compr_stream *stream, 4173 struct snd_compr_tstamp *tstamp) 4174 { 4175 struct wm_adsp_compr *compr = stream->runtime->private_data; 4176 struct wm_adsp *dsp = compr->dsp; 4177 struct wm_adsp_compr_buf *buf; 4178 int ret = 0; 4179 4180 compr_dbg(compr, "Pointer request\n"); 4181 4182 mutex_lock(&dsp->pwr_lock); 4183 4184 buf = compr->buf; 4185 4186 if (dsp->fatal_error || !buf || buf->error) { 4187 snd_compr_stop_error(stream, SNDRV_PCM_STATE_XRUN); 4188 ret = -EIO; 4189 goto out; 4190 } 4191 4192 if (buf->avail < wm_adsp_compr_frag_words(compr)) { 4193 ret = wm_adsp_buffer_update_avail(buf); 4194 if (ret < 0) { 4195 compr_err(compr, "Error reading avail: %d\n", ret); 4196 goto out; 4197 } 4198 4199 /* 4200 * If we really have less than 1 fragment available tell the 4201 * DSP to inform us once a whole fragment is available. 4202 */ 4203 if (buf->avail < wm_adsp_compr_frag_words(compr)) { 4204 ret = wm_adsp_buffer_get_error(buf); 4205 if (ret < 0) { 4206 if (buf->error) 4207 snd_compr_stop_error(stream, 4208 SNDRV_PCM_STATE_XRUN); 4209 goto out; 4210 } 4211 4212 ret = wm_adsp_buffer_reenable_irq(buf); 4213 if (ret < 0) { 4214 compr_err(compr, "Failed to re-enable buffer IRQ: %d\n", 4215 ret); 4216 goto out; 4217 } 4218 } 4219 } 4220 4221 tstamp->copied_total = compr->copied_total; 4222 tstamp->copied_total += buf->avail * WM_ADSP_DATA_WORD_SIZE; 4223 tstamp->sampling_rate = compr->sample_rate; 4224 4225 out: 4226 mutex_unlock(&dsp->pwr_lock); 4227 4228 return ret; 4229 } 4230 EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer); 4231 4232 static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target) 4233 { 4234 struct wm_adsp_compr_buf *buf = compr->buf; 4235 unsigned int adsp_addr; 4236 int mem_type, nwords, max_read; 4237 int i, ret; 4238 4239 /* Calculate read parameters */ 4240 for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i) 4241 if (buf->read_index < buf->regions[i].cumulative_size) 4242 break; 4243 4244 if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions) 4245 return -EINVAL; 4246 4247 mem_type = buf->regions[i].mem_type; 4248 adsp_addr = buf->regions[i].base_addr + 4249 (buf->read_index - buf->regions[i].offset); 4250 4251 max_read = wm_adsp_compr_frag_words(compr); 4252 nwords = buf->regions[i].cumulative_size - buf->read_index; 4253 4254 if (nwords > target) 4255 nwords = target; 4256 if (nwords > buf->avail) 4257 nwords = buf->avail; 4258 if (nwords > max_read) 4259 nwords = max_read; 4260 if (!nwords) 4261 return 0; 4262 4263 /* Read data from DSP */ 4264 ret = wm_adsp_read_data_block(buf->dsp, mem_type, adsp_addr, 4265 nwords, compr->raw_buf); 4266 if (ret < 0) 4267 return ret; 4268 4269 wm_adsp_remove_padding(compr->raw_buf, nwords, WM_ADSP_DATA_WORD_SIZE); 4270 4271 /* update read index to account for words read */ 4272 buf->read_index += nwords; 4273 if (buf->read_index == wm_adsp_buffer_size(buf)) 4274 buf->read_index = 0; 4275 4276 ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index), 4277 buf->read_index); 4278 if (ret < 0) 4279 return ret; 4280 4281 /* update avail to account for words read */ 4282 buf->avail -= nwords; 4283 4284 return nwords; 4285 } 4286 4287 static int wm_adsp_compr_read(struct wm_adsp_compr *compr, 4288 char __user *buf, size_t count) 4289 { 4290 struct wm_adsp *dsp = compr->dsp; 4291 int ntotal = 0; 4292 int nwords, nbytes; 4293 4294 compr_dbg(compr, "Requested read of %zu bytes\n", count); 4295 4296 if (dsp->fatal_error || !compr->buf || compr->buf->error) { 4297 snd_compr_stop_error(compr->stream, SNDRV_PCM_STATE_XRUN); 4298 return -EIO; 4299 } 4300 4301 count /= WM_ADSP_DATA_WORD_SIZE; 4302 4303 do { 4304 nwords = wm_adsp_buffer_capture_block(compr, count); 4305 if (nwords < 0) { 4306 compr_err(compr, "Failed to capture block: %d\n", 4307 nwords); 4308 return nwords; 4309 } 4310 4311 nbytes = nwords * WM_ADSP_DATA_WORD_SIZE; 4312 4313 compr_dbg(compr, "Read %d bytes\n", nbytes); 4314 4315 if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) { 4316 compr_err(compr, "Failed to copy data to user: %d, %d\n", 4317 ntotal, nbytes); 4318 return -EFAULT; 4319 } 4320 4321 count -= nwords; 4322 ntotal += nbytes; 4323 } while (nwords > 0 && count > 0); 4324 4325 compr->copied_total += ntotal; 4326 4327 return ntotal; 4328 } 4329 4330 int wm_adsp_compr_copy(struct snd_soc_component *component, 4331 struct snd_compr_stream *stream, char __user *buf, 4332 size_t count) 4333 { 4334 struct wm_adsp_compr *compr = stream->runtime->private_data; 4335 struct wm_adsp *dsp = compr->dsp; 4336 int ret; 4337 4338 mutex_lock(&dsp->pwr_lock); 4339 4340 if (stream->direction == SND_COMPRESS_CAPTURE) 4341 ret = wm_adsp_compr_read(compr, buf, count); 4342 else 4343 ret = -ENOTSUPP; 4344 4345 mutex_unlock(&dsp->pwr_lock); 4346 4347 return ret; 4348 } 4349 EXPORT_SYMBOL_GPL(wm_adsp_compr_copy); 4350 4351 static void wm_adsp_fatal_error(struct wm_adsp *dsp) 4352 { 4353 struct wm_adsp_compr *compr; 4354 4355 dsp->fatal_error = true; 4356 4357 list_for_each_entry(compr, &dsp->compr_list, list) { 4358 if (compr->stream) 4359 snd_compr_fragment_elapsed(compr->stream); 4360 } 4361 } 4362 4363 irqreturn_t wm_adsp2_bus_error(int irq, void *data) 4364 { 4365 struct wm_adsp *dsp = (struct wm_adsp *)data; 4366 unsigned int val; 4367 struct regmap *regmap = dsp->regmap; 4368 int ret = 0; 4369 4370 mutex_lock(&dsp->pwr_lock); 4371 4372 ret = regmap_read(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL, &val); 4373 if (ret) { 4374 adsp_err(dsp, 4375 "Failed to read Region Lock Ctrl register: %d\n", ret); 4376 goto error; 4377 } 4378 4379 if (val & ADSP2_WDT_TIMEOUT_STS_MASK) { 4380 adsp_err(dsp, "watchdog timeout error\n"); 4381 dsp->ops->stop_watchdog(dsp); 4382 wm_adsp_fatal_error(dsp); 4383 } 4384 4385 if (val & (ADSP2_ADDR_ERR_MASK | ADSP2_REGION_LOCK_ERR_MASK)) { 4386 if (val & ADSP2_ADDR_ERR_MASK) 4387 adsp_err(dsp, "bus error: address error\n"); 4388 else 4389 adsp_err(dsp, "bus error: region lock error\n"); 4390 4391 ret = regmap_read(regmap, dsp->base + ADSP2_BUS_ERR_ADDR, &val); 4392 if (ret) { 4393 adsp_err(dsp, 4394 "Failed to read Bus Err Addr register: %d\n", 4395 ret); 4396 goto error; 4397 } 4398 4399 adsp_err(dsp, "bus error address = 0x%x\n", 4400 val & ADSP2_BUS_ERR_ADDR_MASK); 4401 4402 ret = regmap_read(regmap, 4403 dsp->base + ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR, 4404 &val); 4405 if (ret) { 4406 adsp_err(dsp, 4407 "Failed to read Pmem Xmem Err Addr register: %d\n", 4408 ret); 4409 goto error; 4410 } 4411 4412 adsp_err(dsp, "xmem error address = 0x%x\n", 4413 val & ADSP2_XMEM_ERR_ADDR_MASK); 4414 adsp_err(dsp, "pmem error address = 0x%x\n", 4415 (val & ADSP2_PMEM_ERR_ADDR_MASK) >> 4416 ADSP2_PMEM_ERR_ADDR_SHIFT); 4417 } 4418 4419 regmap_update_bits(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL, 4420 ADSP2_CTRL_ERR_EINT, ADSP2_CTRL_ERR_EINT); 4421 4422 error: 4423 mutex_unlock(&dsp->pwr_lock); 4424 4425 return IRQ_HANDLED; 4426 } 4427 EXPORT_SYMBOL_GPL(wm_adsp2_bus_error); 4428 4429 irqreturn_t wm_halo_bus_error(int irq, void *data) 4430 { 4431 struct wm_adsp *dsp = (struct wm_adsp *)data; 4432 struct regmap *regmap = dsp->regmap; 4433 unsigned int fault[6]; 4434 struct reg_sequence clear[] = { 4435 { dsp->base + HALO_MPU_XM_VIO_STATUS, 0x0 }, 4436 { dsp->base + HALO_MPU_YM_VIO_STATUS, 0x0 }, 4437 { dsp->base + HALO_MPU_PM_VIO_STATUS, 0x0 }, 4438 }; 4439 int ret; 4440 4441 mutex_lock(&dsp->pwr_lock); 4442 4443 ret = regmap_read(regmap, dsp->base_sysinfo + HALO_AHBM_WINDOW_DEBUG_1, 4444 fault); 4445 if (ret) { 4446 adsp_warn(dsp, "Failed to read AHB DEBUG_1: %d\n", ret); 4447 goto exit_unlock; 4448 } 4449 4450 adsp_warn(dsp, "AHB: STATUS: 0x%x ADDR: 0x%x\n", 4451 *fault & HALO_AHBM_FLAGS_ERR_MASK, 4452 (*fault & HALO_AHBM_CORE_ERR_ADDR_MASK) >> 4453 HALO_AHBM_CORE_ERR_ADDR_SHIFT); 4454 4455 ret = regmap_read(regmap, dsp->base_sysinfo + HALO_AHBM_WINDOW_DEBUG_0, 4456 fault); 4457 if (ret) { 4458 adsp_warn(dsp, "Failed to read AHB DEBUG_0: %d\n", ret); 4459 goto exit_unlock; 4460 } 4461 4462 adsp_warn(dsp, "AHB: SYS_ADDR: 0x%x\n", *fault); 4463 4464 ret = regmap_bulk_read(regmap, dsp->base + HALO_MPU_XM_VIO_ADDR, 4465 fault, ARRAY_SIZE(fault)); 4466 if (ret) { 4467 adsp_warn(dsp, "Failed to read MPU fault info: %d\n", ret); 4468 goto exit_unlock; 4469 } 4470 4471 adsp_warn(dsp, "XM: STATUS:0x%x ADDR:0x%x\n", fault[1], fault[0]); 4472 adsp_warn(dsp, "YM: STATUS:0x%x ADDR:0x%x\n", fault[3], fault[2]); 4473 adsp_warn(dsp, "PM: STATUS:0x%x ADDR:0x%x\n", fault[5], fault[4]); 4474 4475 ret = regmap_multi_reg_write(dsp->regmap, clear, ARRAY_SIZE(clear)); 4476 if (ret) 4477 adsp_warn(dsp, "Failed to clear MPU status: %d\n", ret); 4478 4479 exit_unlock: 4480 mutex_unlock(&dsp->pwr_lock); 4481 4482 return IRQ_HANDLED; 4483 } 4484 EXPORT_SYMBOL_GPL(wm_halo_bus_error); 4485 4486 irqreturn_t wm_halo_wdt_expire(int irq, void *data) 4487 { 4488 struct wm_adsp *dsp = data; 4489 4490 mutex_lock(&dsp->pwr_lock); 4491 4492 adsp_warn(dsp, "WDT Expiry Fault\n"); 4493 dsp->ops->stop_watchdog(dsp); 4494 wm_adsp_fatal_error(dsp); 4495 4496 mutex_unlock(&dsp->pwr_lock); 4497 4498 return IRQ_HANDLED; 4499 } 4500 EXPORT_SYMBOL_GPL(wm_halo_wdt_expire); 4501 4502 static struct wm_adsp_ops wm_adsp1_ops = { 4503 .validate_version = wm_adsp_validate_version, 4504 .parse_sizes = wm_adsp1_parse_sizes, 4505 .region_to_reg = wm_adsp_region_to_reg, 4506 }; 4507 4508 static struct wm_adsp_ops wm_adsp2_ops[] = { 4509 { 4510 .sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr), 4511 .parse_sizes = wm_adsp2_parse_sizes, 4512 .validate_version = wm_adsp_validate_version, 4513 .setup_algs = wm_adsp2_setup_algs, 4514 .region_to_reg = wm_adsp_region_to_reg, 4515 4516 .show_fw_status = wm_adsp2_show_fw_status, 4517 4518 .enable_memory = wm_adsp2_enable_memory, 4519 .disable_memory = wm_adsp2_disable_memory, 4520 4521 .enable_core = wm_adsp2_enable_core, 4522 .disable_core = wm_adsp2_disable_core, 4523 4524 .start_core = wm_adsp2_start_core, 4525 .stop_core = wm_adsp2_stop_core, 4526 4527 }, 4528 { 4529 .sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr), 4530 .parse_sizes = wm_adsp2_parse_sizes, 4531 .validate_version = wm_adsp_validate_version, 4532 .setup_algs = wm_adsp2_setup_algs, 4533 .region_to_reg = wm_adsp_region_to_reg, 4534 4535 .show_fw_status = wm_adsp2v2_show_fw_status, 4536 4537 .enable_memory = wm_adsp2_enable_memory, 4538 .disable_memory = wm_adsp2_disable_memory, 4539 .lock_memory = wm_adsp2_lock, 4540 4541 .enable_core = wm_adsp2v2_enable_core, 4542 .disable_core = wm_adsp2v2_disable_core, 4543 4544 .start_core = wm_adsp2_start_core, 4545 .stop_core = wm_adsp2_stop_core, 4546 }, 4547 { 4548 .sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr), 4549 .parse_sizes = wm_adsp2_parse_sizes, 4550 .validate_version = wm_adsp_validate_version, 4551 .setup_algs = wm_adsp2_setup_algs, 4552 .region_to_reg = wm_adsp_region_to_reg, 4553 4554 .show_fw_status = wm_adsp2v2_show_fw_status, 4555 .stop_watchdog = wm_adsp_stop_watchdog, 4556 4557 .enable_memory = wm_adsp2_enable_memory, 4558 .disable_memory = wm_adsp2_disable_memory, 4559 .lock_memory = wm_adsp2_lock, 4560 4561 .enable_core = wm_adsp2v2_enable_core, 4562 .disable_core = wm_adsp2v2_disable_core, 4563 4564 .start_core = wm_adsp2_start_core, 4565 .stop_core = wm_adsp2_stop_core, 4566 }, 4567 }; 4568 4569 static struct wm_adsp_ops wm_halo_ops = { 4570 .sys_config_size = sizeof(struct wm_halo_system_config_xm_hdr), 4571 .parse_sizes = wm_adsp2_parse_sizes, 4572 .validate_version = wm_halo_validate_version, 4573 .setup_algs = wm_halo_setup_algs, 4574 .region_to_reg = wm_halo_region_to_reg, 4575 4576 .show_fw_status = wm_halo_show_fw_status, 4577 .stop_watchdog = wm_halo_stop_watchdog, 4578 4579 .lock_memory = wm_halo_configure_mpu, 4580 4581 .start_core = wm_halo_start_core, 4582 .stop_core = wm_halo_stop_core, 4583 }; 4584 4585 MODULE_LICENSE("GPL v2"); 4586