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/regmap.h> 19 #include <linux/regulator/consumer.h> 20 #include <linux/slab.h> 21 #include <linux/vmalloc.h> 22 #include <linux/workqueue.h> 23 #include <linux/debugfs.h> 24 #include <sound/core.h> 25 #include <sound/pcm.h> 26 #include <sound/pcm_params.h> 27 #include <sound/soc.h> 28 #include <sound/jack.h> 29 #include <sound/initval.h> 30 #include <sound/tlv.h> 31 32 #include "wm_adsp.h" 33 34 #define adsp_crit(_dsp, fmt, ...) \ 35 dev_crit(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) 36 #define adsp_err(_dsp, fmt, ...) \ 37 dev_err(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) 38 #define adsp_warn(_dsp, fmt, ...) \ 39 dev_warn(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) 40 #define adsp_info(_dsp, fmt, ...) \ 41 dev_info(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) 42 #define adsp_dbg(_dsp, fmt, ...) \ 43 dev_dbg(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) 44 45 #define compr_err(_obj, fmt, ...) \ 46 adsp_err(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \ 47 ##__VA_ARGS__) 48 #define compr_dbg(_obj, fmt, ...) \ 49 adsp_dbg(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \ 50 ##__VA_ARGS__) 51 52 #define ADSP_MAX_STD_CTRL_SIZE 512 53 54 static const struct cs_dsp_client_ops wm_adsp1_client_ops; 55 static const struct cs_dsp_client_ops wm_adsp2_client_ops; 56 57 #define WM_ADSP_FW_MBC_VSS 0 58 #define WM_ADSP_FW_HIFI 1 59 #define WM_ADSP_FW_TX 2 60 #define WM_ADSP_FW_TX_SPK 3 61 #define WM_ADSP_FW_RX 4 62 #define WM_ADSP_FW_RX_ANC 5 63 #define WM_ADSP_FW_CTRL 6 64 #define WM_ADSP_FW_ASR 7 65 #define WM_ADSP_FW_TRACE 8 66 #define WM_ADSP_FW_SPK_PROT 9 67 #define WM_ADSP_FW_SPK_CALI 10 68 #define WM_ADSP_FW_SPK_DIAG 11 69 #define WM_ADSP_FW_MISC 12 70 71 #define WM_ADSP_NUM_FW 13 72 73 static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = { 74 [WM_ADSP_FW_MBC_VSS] = "MBC/VSS", 75 [WM_ADSP_FW_HIFI] = "MasterHiFi", 76 [WM_ADSP_FW_TX] = "Tx", 77 [WM_ADSP_FW_TX_SPK] = "Tx Speaker", 78 [WM_ADSP_FW_RX] = "Rx", 79 [WM_ADSP_FW_RX_ANC] = "Rx ANC", 80 [WM_ADSP_FW_CTRL] = "Voice Ctrl", 81 [WM_ADSP_FW_ASR] = "ASR Assist", 82 [WM_ADSP_FW_TRACE] = "Dbg Trace", 83 [WM_ADSP_FW_SPK_PROT] = "Protection", 84 [WM_ADSP_FW_SPK_CALI] = "Calibration", 85 [WM_ADSP_FW_SPK_DIAG] = "Diagnostic", 86 [WM_ADSP_FW_MISC] = "Misc", 87 }; 88 89 struct wm_adsp_system_config_xm_hdr { 90 __be32 sys_enable; 91 __be32 fw_id; 92 __be32 fw_rev; 93 __be32 boot_status; 94 __be32 watchdog; 95 __be32 dma_buffer_size; 96 __be32 rdma[6]; 97 __be32 wdma[8]; 98 __be32 build_job_name[3]; 99 __be32 build_job_number; 100 } __packed; 101 102 struct wm_halo_system_config_xm_hdr { 103 __be32 halo_heartbeat; 104 __be32 build_job_name[3]; 105 __be32 build_job_number; 106 } __packed; 107 108 struct wm_adsp_alg_xm_struct { 109 __be32 magic; 110 __be32 smoothing; 111 __be32 threshold; 112 __be32 host_buf_ptr; 113 __be32 start_seq; 114 __be32 high_water_mark; 115 __be32 low_water_mark; 116 __be64 smoothed_power; 117 } __packed; 118 119 struct wm_adsp_host_buf_coeff_v1 { 120 __be32 host_buf_ptr; /* Host buffer pointer */ 121 __be32 versions; /* Version numbers */ 122 __be32 name[4]; /* The buffer name */ 123 } __packed; 124 125 struct wm_adsp_buffer { 126 __be32 buf1_base; /* Base addr of first buffer area */ 127 __be32 buf1_size; /* Size of buf1 area in DSP words */ 128 __be32 buf2_base; /* Base addr of 2nd buffer area */ 129 __be32 buf1_buf2_size; /* Size of buf1+buf2 in DSP words */ 130 __be32 buf3_base; /* Base addr of buf3 area */ 131 __be32 buf_total_size; /* Size of buf1+buf2+buf3 in DSP words */ 132 __be32 high_water_mark; /* Point at which IRQ is asserted */ 133 __be32 irq_count; /* bits 1-31 count IRQ assertions */ 134 __be32 irq_ack; /* acked IRQ count, bit 0 enables IRQ */ 135 __be32 next_write_index; /* word index of next write */ 136 __be32 next_read_index; /* word index of next read */ 137 __be32 error; /* error if any */ 138 __be32 oldest_block_index; /* word index of oldest surviving */ 139 __be32 requested_rewind; /* how many blocks rewind was done */ 140 __be32 reserved_space; /* internal */ 141 __be32 min_free; /* min free space since stream start */ 142 __be32 blocks_written[2]; /* total blocks written (64 bit) */ 143 __be32 words_written[2]; /* total words written (64 bit) */ 144 } __packed; 145 146 struct wm_adsp_compr; 147 148 struct wm_adsp_compr_buf { 149 struct list_head list; 150 struct wm_adsp *dsp; 151 struct wm_adsp_compr *compr; 152 153 struct wm_adsp_buffer_region *regions; 154 u32 host_buf_ptr; 155 156 u32 error; 157 u32 irq_count; 158 int read_index; 159 int avail; 160 int host_buf_mem_type; 161 162 char *name; 163 }; 164 165 struct wm_adsp_compr { 166 struct list_head list; 167 struct wm_adsp *dsp; 168 struct wm_adsp_compr_buf *buf; 169 170 struct snd_compr_stream *stream; 171 struct snd_compressed_buffer size; 172 173 u32 *raw_buf; 174 unsigned int copied_total; 175 176 unsigned int sample_rate; 177 178 const char *name; 179 }; 180 181 #define WM_ADSP_MIN_FRAGMENTS 1 182 #define WM_ADSP_MAX_FRAGMENTS 256 183 #define WM_ADSP_MIN_FRAGMENT_SIZE (16 * CS_DSP_DATA_WORD_SIZE) 184 #define WM_ADSP_MAX_FRAGMENT_SIZE (4096 * CS_DSP_DATA_WORD_SIZE) 185 186 #define WM_ADSP_ALG_XM_STRUCT_MAGIC 0x49aec7 187 188 #define HOST_BUFFER_FIELD(field) \ 189 (offsetof(struct wm_adsp_buffer, field) / sizeof(__be32)) 190 191 #define ALG_XM_FIELD(field) \ 192 (offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32)) 193 194 #define HOST_BUF_COEFF_SUPPORTED_COMPAT_VER 1 195 196 #define HOST_BUF_COEFF_COMPAT_VER_MASK 0xFF00 197 #define HOST_BUF_COEFF_COMPAT_VER_SHIFT 8 198 199 static int wm_adsp_buffer_init(struct wm_adsp *dsp); 200 static int wm_adsp_buffer_free(struct wm_adsp *dsp); 201 202 struct wm_adsp_buffer_region { 203 unsigned int offset; 204 unsigned int cumulative_size; 205 unsigned int mem_type; 206 unsigned int base_addr; 207 }; 208 209 struct wm_adsp_buffer_region_def { 210 unsigned int mem_type; 211 unsigned int base_offset; 212 unsigned int size_offset; 213 }; 214 215 static const struct wm_adsp_buffer_region_def default_regions[] = { 216 { 217 .mem_type = WMFW_ADSP2_XM, 218 .base_offset = HOST_BUFFER_FIELD(buf1_base), 219 .size_offset = HOST_BUFFER_FIELD(buf1_size), 220 }, 221 { 222 .mem_type = WMFW_ADSP2_XM, 223 .base_offset = HOST_BUFFER_FIELD(buf2_base), 224 .size_offset = HOST_BUFFER_FIELD(buf1_buf2_size), 225 }, 226 { 227 .mem_type = WMFW_ADSP2_YM, 228 .base_offset = HOST_BUFFER_FIELD(buf3_base), 229 .size_offset = HOST_BUFFER_FIELD(buf_total_size), 230 }, 231 }; 232 233 struct wm_adsp_fw_caps { 234 u32 id; 235 struct snd_codec_desc desc; 236 int num_regions; 237 const struct wm_adsp_buffer_region_def *region_defs; 238 }; 239 240 static const struct wm_adsp_fw_caps ctrl_caps[] = { 241 { 242 .id = SND_AUDIOCODEC_BESPOKE, 243 .desc = { 244 .max_ch = 8, 245 .sample_rates = { 16000 }, 246 .num_sample_rates = 1, 247 .formats = SNDRV_PCM_FMTBIT_S16_LE, 248 }, 249 .num_regions = ARRAY_SIZE(default_regions), 250 .region_defs = default_regions, 251 }, 252 }; 253 254 static const struct wm_adsp_fw_caps trace_caps[] = { 255 { 256 .id = SND_AUDIOCODEC_BESPOKE, 257 .desc = { 258 .max_ch = 8, 259 .sample_rates = { 260 4000, 8000, 11025, 12000, 16000, 22050, 261 24000, 32000, 44100, 48000, 64000, 88200, 262 96000, 176400, 192000 263 }, 264 .num_sample_rates = 15, 265 .formats = SNDRV_PCM_FMTBIT_S16_LE, 266 }, 267 .num_regions = ARRAY_SIZE(default_regions), 268 .region_defs = default_regions, 269 }, 270 }; 271 272 static const struct { 273 const char *file; 274 int compr_direction; 275 int num_caps; 276 const struct wm_adsp_fw_caps *caps; 277 bool voice_trigger; 278 } wm_adsp_fw[WM_ADSP_NUM_FW] = { 279 [WM_ADSP_FW_MBC_VSS] = { .file = "mbc-vss" }, 280 [WM_ADSP_FW_HIFI] = { .file = "hifi" }, 281 [WM_ADSP_FW_TX] = { .file = "tx" }, 282 [WM_ADSP_FW_TX_SPK] = { .file = "tx-spk" }, 283 [WM_ADSP_FW_RX] = { .file = "rx" }, 284 [WM_ADSP_FW_RX_ANC] = { .file = "rx-anc" }, 285 [WM_ADSP_FW_CTRL] = { 286 .file = "ctrl", 287 .compr_direction = SND_COMPRESS_CAPTURE, 288 .num_caps = ARRAY_SIZE(ctrl_caps), 289 .caps = ctrl_caps, 290 .voice_trigger = true, 291 }, 292 [WM_ADSP_FW_ASR] = { .file = "asr" }, 293 [WM_ADSP_FW_TRACE] = { 294 .file = "trace", 295 .compr_direction = SND_COMPRESS_CAPTURE, 296 .num_caps = ARRAY_SIZE(trace_caps), 297 .caps = trace_caps, 298 }, 299 [WM_ADSP_FW_SPK_PROT] = { 300 .file = "spk-prot", 301 .compr_direction = SND_COMPRESS_CAPTURE, 302 .num_caps = ARRAY_SIZE(trace_caps), 303 .caps = trace_caps, 304 }, 305 [WM_ADSP_FW_SPK_CALI] = { .file = "spk-cali" }, 306 [WM_ADSP_FW_SPK_DIAG] = { .file = "spk-diag" }, 307 [WM_ADSP_FW_MISC] = { .file = "misc" }, 308 }; 309 310 struct wm_coeff_ctl { 311 const char *name; 312 struct cs_dsp_coeff_ctl *cs_ctl; 313 struct soc_bytes_ext bytes_ext; 314 struct work_struct work; 315 }; 316 317 int wm_adsp_fw_get(struct snd_kcontrol *kcontrol, 318 struct snd_ctl_elem_value *ucontrol) 319 { 320 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 321 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 322 struct wm_adsp *dsp = snd_soc_component_get_drvdata(component); 323 324 ucontrol->value.enumerated.item[0] = dsp[e->shift_l].fw; 325 326 return 0; 327 } 328 EXPORT_SYMBOL_GPL(wm_adsp_fw_get); 329 330 int wm_adsp_fw_put(struct snd_kcontrol *kcontrol, 331 struct snd_ctl_elem_value *ucontrol) 332 { 333 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 334 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 335 struct wm_adsp *dsp = snd_soc_component_get_drvdata(component); 336 int ret = 1; 337 338 if (ucontrol->value.enumerated.item[0] == dsp[e->shift_l].fw) 339 return 0; 340 341 if (ucontrol->value.enumerated.item[0] >= WM_ADSP_NUM_FW) 342 return -EINVAL; 343 344 mutex_lock(&dsp[e->shift_l].cs_dsp.pwr_lock); 345 346 if (dsp[e->shift_l].cs_dsp.booted || !list_empty(&dsp[e->shift_l].compr_list)) 347 ret = -EBUSY; 348 else 349 dsp[e->shift_l].fw = ucontrol->value.enumerated.item[0]; 350 351 mutex_unlock(&dsp[e->shift_l].cs_dsp.pwr_lock); 352 353 return ret; 354 } 355 EXPORT_SYMBOL_GPL(wm_adsp_fw_put); 356 357 const struct soc_enum wm_adsp_fw_enum[] = { 358 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 359 SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 360 SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 361 SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 362 SOC_ENUM_SINGLE(0, 4, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 363 SOC_ENUM_SINGLE(0, 5, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 364 SOC_ENUM_SINGLE(0, 6, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), 365 }; 366 EXPORT_SYMBOL_GPL(wm_adsp_fw_enum); 367 368 static inline struct wm_coeff_ctl *bytes_ext_to_ctl(struct soc_bytes_ext *ext) 369 { 370 return container_of(ext, struct wm_coeff_ctl, bytes_ext); 371 } 372 373 static int wm_coeff_info(struct snd_kcontrol *kctl, 374 struct snd_ctl_elem_info *uinfo) 375 { 376 struct soc_bytes_ext *bytes_ext = 377 (struct soc_bytes_ext *)kctl->private_value; 378 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 379 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; 380 381 switch (cs_ctl->type) { 382 case WMFW_CTL_TYPE_ACKED: 383 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 384 uinfo->value.integer.min = CS_DSP_ACKED_CTL_MIN_VALUE; 385 uinfo->value.integer.max = CS_DSP_ACKED_CTL_MAX_VALUE; 386 uinfo->value.integer.step = 1; 387 uinfo->count = 1; 388 break; 389 default: 390 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; 391 uinfo->count = cs_ctl->len; 392 break; 393 } 394 395 return 0; 396 } 397 398 static int wm_coeff_put(struct snd_kcontrol *kctl, 399 struct snd_ctl_elem_value *ucontrol) 400 { 401 struct soc_bytes_ext *bytes_ext = 402 (struct soc_bytes_ext *)kctl->private_value; 403 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 404 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; 405 char *p = ucontrol->value.bytes.data; 406 int ret = 0; 407 408 mutex_lock(&cs_ctl->dsp->pwr_lock); 409 ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, p, cs_ctl->len); 410 mutex_unlock(&cs_ctl->dsp->pwr_lock); 411 412 return ret; 413 } 414 415 static int wm_coeff_tlv_put(struct snd_kcontrol *kctl, 416 const unsigned int __user *bytes, unsigned int size) 417 { 418 struct soc_bytes_ext *bytes_ext = 419 (struct soc_bytes_ext *)kctl->private_value; 420 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 421 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; 422 void *scratch; 423 int ret = 0; 424 425 scratch = vmalloc(size); 426 if (!scratch) 427 return -ENOMEM; 428 429 if (copy_from_user(scratch, bytes, size)) { 430 ret = -EFAULT; 431 } else { 432 mutex_lock(&cs_ctl->dsp->pwr_lock); 433 ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, scratch, size); 434 mutex_unlock(&cs_ctl->dsp->pwr_lock); 435 } 436 vfree(scratch); 437 438 return ret; 439 } 440 441 static int wm_coeff_put_acked(struct snd_kcontrol *kctl, 442 struct snd_ctl_elem_value *ucontrol) 443 { 444 struct soc_bytes_ext *bytes_ext = 445 (struct soc_bytes_ext *)kctl->private_value; 446 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 447 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; 448 unsigned int val = ucontrol->value.integer.value[0]; 449 int ret; 450 451 if (val == 0) 452 return 0; /* 0 means no event */ 453 454 mutex_lock(&cs_ctl->dsp->pwr_lock); 455 456 if (cs_ctl->enabled) 457 ret = cs_dsp_coeff_write_acked_control(cs_ctl, val); 458 else 459 ret = -EPERM; 460 461 mutex_unlock(&cs_ctl->dsp->pwr_lock); 462 463 if (ret < 0) 464 return ret; 465 466 return 1; 467 } 468 469 static int wm_coeff_get(struct snd_kcontrol *kctl, 470 struct snd_ctl_elem_value *ucontrol) 471 { 472 struct soc_bytes_ext *bytes_ext = 473 (struct soc_bytes_ext *)kctl->private_value; 474 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 475 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; 476 char *p = ucontrol->value.bytes.data; 477 int ret; 478 479 mutex_lock(&cs_ctl->dsp->pwr_lock); 480 ret = cs_dsp_coeff_read_ctrl(cs_ctl, 0, p, cs_ctl->len); 481 mutex_unlock(&cs_ctl->dsp->pwr_lock); 482 483 return ret; 484 } 485 486 static int wm_coeff_tlv_get(struct snd_kcontrol *kctl, 487 unsigned int __user *bytes, unsigned int size) 488 { 489 struct soc_bytes_ext *bytes_ext = 490 (struct soc_bytes_ext *)kctl->private_value; 491 struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); 492 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; 493 int ret = 0; 494 495 mutex_lock(&cs_ctl->dsp->pwr_lock); 496 497 ret = cs_dsp_coeff_read_ctrl(cs_ctl, 0, cs_ctl->cache, size); 498 499 if (!ret && copy_to_user(bytes, cs_ctl->cache, size)) 500 ret = -EFAULT; 501 502 mutex_unlock(&cs_ctl->dsp->pwr_lock); 503 504 return ret; 505 } 506 507 static int wm_coeff_get_acked(struct snd_kcontrol *kcontrol, 508 struct snd_ctl_elem_value *ucontrol) 509 { 510 /* 511 * Although it's not useful to read an acked control, we must satisfy 512 * user-side assumptions that all controls are readable and that a 513 * write of the same value should be filtered out (it's valid to send 514 * the same event number again to the firmware). We therefore return 0, 515 * meaning "no event" so valid event numbers will always be a change 516 */ 517 ucontrol->value.integer.value[0] = 0; 518 519 return 0; 520 } 521 522 static unsigned int wmfw_convert_flags(unsigned int in, unsigned int len) 523 { 524 unsigned int out, rd, wr, vol; 525 526 if (len > ADSP_MAX_STD_CTRL_SIZE) { 527 rd = SNDRV_CTL_ELEM_ACCESS_TLV_READ; 528 wr = SNDRV_CTL_ELEM_ACCESS_TLV_WRITE; 529 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE; 530 531 out = SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; 532 } else { 533 rd = SNDRV_CTL_ELEM_ACCESS_READ; 534 wr = SNDRV_CTL_ELEM_ACCESS_WRITE; 535 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE; 536 537 out = 0; 538 } 539 540 if (in) { 541 out |= rd; 542 if (in & WMFW_CTL_FLAG_WRITEABLE) 543 out |= wr; 544 if (in & WMFW_CTL_FLAG_VOLATILE) 545 out |= vol; 546 } else { 547 out |= rd | wr | vol; 548 } 549 550 return out; 551 } 552 553 static void wm_adsp_ctl_work(struct work_struct *work) 554 { 555 struct wm_coeff_ctl *ctl = container_of(work, 556 struct wm_coeff_ctl, 557 work); 558 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; 559 struct wm_adsp *dsp = container_of(cs_ctl->dsp, 560 struct wm_adsp, 561 cs_dsp); 562 struct snd_kcontrol_new *kcontrol; 563 564 kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL); 565 if (!kcontrol) 566 return; 567 568 kcontrol->name = ctl->name; 569 kcontrol->info = wm_coeff_info; 570 kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 571 kcontrol->tlv.c = snd_soc_bytes_tlv_callback; 572 kcontrol->private_value = (unsigned long)&ctl->bytes_ext; 573 kcontrol->access = wmfw_convert_flags(cs_ctl->flags, cs_ctl->len); 574 575 switch (cs_ctl->type) { 576 case WMFW_CTL_TYPE_ACKED: 577 kcontrol->get = wm_coeff_get_acked; 578 kcontrol->put = wm_coeff_put_acked; 579 break; 580 default: 581 if (kcontrol->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 582 ctl->bytes_ext.max = cs_ctl->len; 583 ctl->bytes_ext.get = wm_coeff_tlv_get; 584 ctl->bytes_ext.put = wm_coeff_tlv_put; 585 } else { 586 kcontrol->get = wm_coeff_get; 587 kcontrol->put = wm_coeff_put; 588 } 589 break; 590 } 591 592 snd_soc_add_component_controls(dsp->component, kcontrol, 1); 593 594 kfree(kcontrol); 595 } 596 597 static int wm_adsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl) 598 { 599 struct wm_adsp *dsp = container_of(cs_ctl->dsp, struct wm_adsp, cs_dsp); 600 struct cs_dsp *cs_dsp = &dsp->cs_dsp; 601 struct wm_coeff_ctl *ctl; 602 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 603 const char *region_name; 604 int ret; 605 606 if (cs_ctl->flags & WMFW_CTL_FLAG_SYS) 607 return 0; 608 609 region_name = cs_dsp_mem_region_name(cs_ctl->alg_region.type); 610 if (!region_name) { 611 adsp_err(dsp, "Unknown region type: %d\n", cs_ctl->alg_region.type); 612 return -EINVAL; 613 } 614 615 switch (cs_dsp->fw_ver) { 616 case 0: 617 case 1: 618 ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, 619 "%s %s %x", cs_dsp->name, region_name, 620 cs_ctl->alg_region.alg); 621 break; 622 case 2: 623 ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, 624 "%s%c %.12s %x", cs_dsp->name, *region_name, 625 wm_adsp_fw_text[dsp->fw], cs_ctl->alg_region.alg); 626 break; 627 default: 628 ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, 629 "%s %.12s %x", cs_dsp->name, 630 wm_adsp_fw_text[dsp->fw], cs_ctl->alg_region.alg); 631 break; 632 } 633 634 if (cs_ctl->subname) { 635 int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2; 636 int skip = 0; 637 638 if (dsp->component->name_prefix) 639 avail -= strlen(dsp->component->name_prefix) + 1; 640 641 /* Truncate the subname from the start if it is too long */ 642 if (cs_ctl->subname_len > avail) 643 skip = cs_ctl->subname_len - avail; 644 645 snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret, 646 " %.*s", cs_ctl->subname_len - skip, cs_ctl->subname + skip); 647 } 648 649 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); 650 if (!ctl) 651 return -ENOMEM; 652 ctl->cs_ctl = cs_ctl; 653 654 ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL); 655 if (!ctl->name) { 656 ret = -ENOMEM; 657 goto err_ctl; 658 } 659 660 cs_ctl->priv = ctl; 661 662 INIT_WORK(&ctl->work, wm_adsp_ctl_work); 663 schedule_work(&ctl->work); 664 665 return 0; 666 667 err_ctl: 668 kfree(ctl); 669 670 return ret; 671 } 672 673 static void wm_adsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl) 674 { 675 struct wm_coeff_ctl *ctl = cs_ctl->priv; 676 677 cancel_work_sync(&ctl->work); 678 679 kfree(ctl->name); 680 kfree(ctl); 681 } 682 683 int wm_adsp_write_ctl(struct wm_adsp *dsp, const char *name, int type, 684 unsigned int alg, void *buf, size_t len) 685 { 686 struct cs_dsp_coeff_ctl *cs_ctl = cs_dsp_get_ctl(&dsp->cs_dsp, name, type, alg); 687 struct wm_coeff_ctl *ctl; 688 int ret; 689 690 mutex_lock(&dsp->cs_dsp.pwr_lock); 691 ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, buf, len); 692 mutex_unlock(&dsp->cs_dsp.pwr_lock); 693 694 if (ret < 0) 695 return ret; 696 697 if (ret == 0 || (cs_ctl->flags & WMFW_CTL_FLAG_SYS)) 698 return 0; 699 700 ctl = cs_ctl->priv; 701 702 return snd_soc_component_notify_control(dsp->component, ctl->name); 703 } 704 EXPORT_SYMBOL_GPL(wm_adsp_write_ctl); 705 706 int wm_adsp_read_ctl(struct wm_adsp *dsp, const char *name, int type, 707 unsigned int alg, void *buf, size_t len) 708 { 709 int ret; 710 711 mutex_lock(&dsp->cs_dsp.pwr_lock); 712 ret = cs_dsp_coeff_read_ctrl(cs_dsp_get_ctl(&dsp->cs_dsp, name, type, alg), 713 0, buf, len); 714 mutex_unlock(&dsp->cs_dsp.pwr_lock); 715 716 return ret; 717 } 718 EXPORT_SYMBOL_GPL(wm_adsp_read_ctl); 719 720 static void wm_adsp_release_firmware_files(struct wm_adsp *dsp, 721 const struct firmware *wmfw_firmware, 722 char *wmfw_filename, 723 const struct firmware *coeff_firmware, 724 char *coeff_filename) 725 { 726 if (wmfw_firmware) 727 release_firmware(wmfw_firmware); 728 kfree(wmfw_filename); 729 730 if (coeff_firmware) 731 release_firmware(coeff_firmware); 732 kfree(coeff_filename); 733 } 734 735 static int wm_adsp_request_firmware_file(struct wm_adsp *dsp, 736 const struct firmware **firmware, char **filename, 737 const char *dir, const char *system_name, 738 const char *asoc_component_prefix, 739 const char *filetype) 740 { 741 struct cs_dsp *cs_dsp = &dsp->cs_dsp; 742 char *s, c; 743 int ret = 0; 744 745 if (system_name && asoc_component_prefix) 746 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s-%s.%s", dir, dsp->part, 747 dsp->fwf_name, wm_adsp_fw[dsp->fw].file, system_name, 748 asoc_component_prefix, filetype); 749 else if (system_name) 750 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s.%s", dir, dsp->part, 751 dsp->fwf_name, wm_adsp_fw[dsp->fw].file, system_name, 752 filetype); 753 else 754 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s.%s", dir, dsp->part, dsp->fwf_name, 755 wm_adsp_fw[dsp->fw].file, filetype); 756 757 if (*filename == NULL) 758 return -ENOMEM; 759 760 /* 761 * Make sure that filename is lower-case and any non alpha-numeric 762 * characters except full stop and forward slash are replaced with 763 * hyphens. 764 */ 765 s = *filename; 766 while (*s) { 767 c = *s; 768 if (isalnum(c)) 769 *s = tolower(c); 770 else if ((c != '.') && (c != '/')) 771 *s = '-'; 772 s++; 773 } 774 775 ret = firmware_request_nowarn(firmware, *filename, cs_dsp->dev); 776 if (ret != 0) { 777 adsp_dbg(dsp, "Failed to request '%s'\n", *filename); 778 kfree(*filename); 779 *filename = NULL; 780 } else { 781 adsp_dbg(dsp, "Found '%s'\n", *filename); 782 } 783 784 return ret; 785 } 786 787 static const char *cirrus_dir = "cirrus/"; 788 static int wm_adsp_request_firmware_files(struct wm_adsp *dsp, 789 const struct firmware **wmfw_firmware, 790 char **wmfw_filename, 791 const struct firmware **coeff_firmware, 792 char **coeff_filename) 793 { 794 const char *system_name = dsp->system_name; 795 const char *asoc_component_prefix = dsp->component->name_prefix; 796 int ret = 0; 797 798 if (system_name && asoc_component_prefix) { 799 if (!wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, 800 cirrus_dir, system_name, 801 asoc_component_prefix, "wmfw")) { 802 wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, 803 cirrus_dir, system_name, 804 asoc_component_prefix, "bin"); 805 return 0; 806 } 807 } 808 809 if (system_name) { 810 if (!wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, 811 cirrus_dir, system_name, 812 NULL, "wmfw")) { 813 if (asoc_component_prefix) 814 wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, 815 cirrus_dir, system_name, 816 asoc_component_prefix, "bin"); 817 818 if (!*coeff_firmware) 819 wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, 820 cirrus_dir, system_name, 821 NULL, "bin"); 822 return 0; 823 } 824 } 825 826 if (!wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, 827 "", NULL, NULL, "wmfw")) { 828 wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, 829 "", NULL, NULL, "bin"); 830 return 0; 831 } 832 833 ret = wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, 834 cirrus_dir, NULL, NULL, "wmfw"); 835 if (!ret) { 836 wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, 837 cirrus_dir, NULL, NULL, "bin"); 838 return 0; 839 } 840 841 if (dsp->wmfw_optional) { 842 if (system_name) { 843 if (asoc_component_prefix) 844 wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, 845 cirrus_dir, system_name, 846 asoc_component_prefix, "bin"); 847 848 if (!*coeff_firmware) 849 wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, 850 cirrus_dir, system_name, 851 NULL, "bin"); 852 } 853 854 if (!*coeff_firmware) 855 wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, 856 "", NULL, NULL, "bin"); 857 858 if (!*coeff_firmware) 859 wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, 860 cirrus_dir, NULL, NULL, "bin"); 861 862 return 0; 863 } 864 865 adsp_err(dsp, "Failed to request firmware <%s>%s-%s-%s<-%s<%s>>.wmfw\n", 866 cirrus_dir, dsp->part, dsp->fwf_name, wm_adsp_fw[dsp->fw].file, 867 system_name, asoc_component_prefix); 868 869 return -ENOENT; 870 } 871 872 static int wm_adsp_common_init(struct wm_adsp *dsp) 873 { 874 char *p; 875 876 INIT_LIST_HEAD(&dsp->compr_list); 877 INIT_LIST_HEAD(&dsp->buffer_list); 878 879 if (!dsp->fwf_name) { 880 p = devm_kstrdup(dsp->cs_dsp.dev, dsp->cs_dsp.name, GFP_KERNEL); 881 if (!p) 882 return -ENOMEM; 883 884 dsp->fwf_name = p; 885 for (; *p != 0; ++p) 886 *p = tolower(*p); 887 } 888 889 return 0; 890 } 891 892 int wm_adsp1_init(struct wm_adsp *dsp) 893 { 894 int ret; 895 896 dsp->cs_dsp.client_ops = &wm_adsp1_client_ops; 897 898 ret = cs_dsp_adsp1_init(&dsp->cs_dsp); 899 if (ret) 900 return ret; 901 902 return wm_adsp_common_init(dsp); 903 } 904 EXPORT_SYMBOL_GPL(wm_adsp1_init); 905 906 int wm_adsp1_event(struct snd_soc_dapm_widget *w, 907 struct snd_kcontrol *kcontrol, 908 int event) 909 { 910 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 911 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 912 struct wm_adsp *dsp = &dsps[w->shift]; 913 int ret = 0; 914 char *wmfw_filename = NULL; 915 const struct firmware *wmfw_firmware = NULL; 916 char *coeff_filename = NULL; 917 const struct firmware *coeff_firmware = NULL; 918 919 dsp->component = component; 920 921 switch (event) { 922 case SND_SOC_DAPM_POST_PMU: 923 ret = wm_adsp_request_firmware_files(dsp, 924 &wmfw_firmware, &wmfw_filename, 925 &coeff_firmware, &coeff_filename); 926 if (ret) 927 break; 928 929 ret = cs_dsp_adsp1_power_up(&dsp->cs_dsp, 930 wmfw_firmware, wmfw_filename, 931 coeff_firmware, coeff_filename, 932 wm_adsp_fw_text[dsp->fw]); 933 934 wm_adsp_release_firmware_files(dsp, 935 wmfw_firmware, wmfw_filename, 936 coeff_firmware, coeff_filename); 937 break; 938 case SND_SOC_DAPM_PRE_PMD: 939 cs_dsp_adsp1_power_down(&dsp->cs_dsp); 940 break; 941 default: 942 break; 943 } 944 945 return ret; 946 } 947 EXPORT_SYMBOL_GPL(wm_adsp1_event); 948 949 int wm_adsp2_set_dspclk(struct snd_soc_dapm_widget *w, unsigned int freq) 950 { 951 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 952 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 953 struct wm_adsp *dsp = &dsps[w->shift]; 954 955 return cs_dsp_set_dspclk(&dsp->cs_dsp, freq); 956 } 957 EXPORT_SYMBOL_GPL(wm_adsp2_set_dspclk); 958 959 int wm_adsp2_preloader_get(struct snd_kcontrol *kcontrol, 960 struct snd_ctl_elem_value *ucontrol) 961 { 962 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 963 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 964 struct soc_mixer_control *mc = 965 (struct soc_mixer_control *)kcontrol->private_value; 966 struct wm_adsp *dsp = &dsps[mc->shift - 1]; 967 968 ucontrol->value.integer.value[0] = dsp->preloaded; 969 970 return 0; 971 } 972 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_get); 973 974 int wm_adsp2_preloader_put(struct snd_kcontrol *kcontrol, 975 struct snd_ctl_elem_value *ucontrol) 976 { 977 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 978 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 979 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 980 struct soc_mixer_control *mc = 981 (struct soc_mixer_control *)kcontrol->private_value; 982 struct wm_adsp *dsp = &dsps[mc->shift - 1]; 983 char preload[32]; 984 985 if (dsp->preloaded == ucontrol->value.integer.value[0]) 986 return 0; 987 988 snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->cs_dsp.name); 989 990 if (ucontrol->value.integer.value[0] || dsp->toggle_preload) 991 snd_soc_component_force_enable_pin(component, preload); 992 else 993 snd_soc_component_disable_pin(component, preload); 994 995 snd_soc_dapm_sync(dapm); 996 997 flush_work(&dsp->boot_work); 998 999 dsp->preloaded = ucontrol->value.integer.value[0]; 1000 1001 if (dsp->toggle_preload) { 1002 snd_soc_component_disable_pin(component, preload); 1003 snd_soc_dapm_sync(dapm); 1004 } 1005 1006 return 1; 1007 } 1008 EXPORT_SYMBOL_GPL(wm_adsp2_preloader_put); 1009 1010 int wm_adsp_power_up(struct wm_adsp *dsp, bool load_firmware) 1011 { 1012 int ret = 0; 1013 char *wmfw_filename = NULL; 1014 const struct firmware *wmfw_firmware = NULL; 1015 char *coeff_filename = NULL; 1016 const struct firmware *coeff_firmware = NULL; 1017 1018 if (load_firmware) { 1019 ret = wm_adsp_request_firmware_files(dsp, 1020 &wmfw_firmware, &wmfw_filename, 1021 &coeff_firmware, &coeff_filename); 1022 if (ret) 1023 return ret; 1024 } 1025 1026 ret = cs_dsp_power_up(&dsp->cs_dsp, 1027 wmfw_firmware, wmfw_filename, 1028 coeff_firmware, coeff_filename, 1029 wm_adsp_fw_text[dsp->fw]); 1030 1031 wm_adsp_release_firmware_files(dsp, 1032 wmfw_firmware, wmfw_filename, 1033 coeff_firmware, coeff_filename); 1034 1035 return ret; 1036 } 1037 EXPORT_SYMBOL_GPL(wm_adsp_power_up); 1038 1039 void wm_adsp_power_down(struct wm_adsp *dsp) 1040 { 1041 cs_dsp_power_down(&dsp->cs_dsp); 1042 } 1043 EXPORT_SYMBOL_GPL(wm_adsp_power_down); 1044 1045 static void wm_adsp_boot_work(struct work_struct *work) 1046 { 1047 struct wm_adsp *dsp = container_of(work, 1048 struct wm_adsp, 1049 boot_work); 1050 1051 wm_adsp_power_up(dsp, true); 1052 } 1053 1054 int wm_adsp_early_event(struct snd_soc_dapm_widget *w, 1055 struct snd_kcontrol *kcontrol, int event) 1056 { 1057 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 1058 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 1059 struct wm_adsp *dsp = &dsps[w->shift]; 1060 1061 switch (event) { 1062 case SND_SOC_DAPM_PRE_PMU: 1063 queue_work(system_unbound_wq, &dsp->boot_work); 1064 break; 1065 case SND_SOC_DAPM_PRE_PMD: 1066 wm_adsp_power_down(dsp); 1067 break; 1068 default: 1069 break; 1070 } 1071 1072 return 0; 1073 } 1074 EXPORT_SYMBOL_GPL(wm_adsp_early_event); 1075 1076 static int wm_adsp_pre_run(struct cs_dsp *cs_dsp) 1077 { 1078 struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp); 1079 1080 if (!dsp->pre_run) 1081 return 0; 1082 1083 return (*dsp->pre_run)(dsp); 1084 } 1085 1086 static int wm_adsp_event_post_run(struct cs_dsp *cs_dsp) 1087 { 1088 struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp); 1089 1090 if (wm_adsp_fw[dsp->fw].num_caps != 0) 1091 return wm_adsp_buffer_init(dsp); 1092 1093 return 0; 1094 } 1095 1096 static void wm_adsp_event_post_stop(struct cs_dsp *cs_dsp) 1097 { 1098 struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp); 1099 1100 if (wm_adsp_fw[dsp->fw].num_caps != 0) 1101 wm_adsp_buffer_free(dsp); 1102 1103 dsp->fatal_error = false; 1104 } 1105 1106 int wm_adsp_event(struct snd_soc_dapm_widget *w, 1107 struct snd_kcontrol *kcontrol, int event) 1108 { 1109 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 1110 struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); 1111 struct wm_adsp *dsp = &dsps[w->shift]; 1112 int ret = 0; 1113 1114 switch (event) { 1115 case SND_SOC_DAPM_POST_PMU: 1116 flush_work(&dsp->boot_work); 1117 ret = cs_dsp_run(&dsp->cs_dsp); 1118 break; 1119 case SND_SOC_DAPM_PRE_PMD: 1120 cs_dsp_stop(&dsp->cs_dsp); 1121 break; 1122 default: 1123 break; 1124 } 1125 1126 return ret; 1127 } 1128 EXPORT_SYMBOL_GPL(wm_adsp_event); 1129 1130 int wm_adsp2_component_probe(struct wm_adsp *dsp, struct snd_soc_component *component) 1131 { 1132 char preload[32]; 1133 1134 if (!dsp->cs_dsp.no_core_startstop) { 1135 snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->cs_dsp.name); 1136 snd_soc_component_disable_pin(component, preload); 1137 } 1138 1139 cs_dsp_init_debugfs(&dsp->cs_dsp, component->debugfs_root); 1140 1141 dsp->component = component; 1142 1143 return 0; 1144 } 1145 EXPORT_SYMBOL_GPL(wm_adsp2_component_probe); 1146 1147 int wm_adsp2_component_remove(struct wm_adsp *dsp, struct snd_soc_component *component) 1148 { 1149 cs_dsp_cleanup_debugfs(&dsp->cs_dsp); 1150 1151 return 0; 1152 } 1153 EXPORT_SYMBOL_GPL(wm_adsp2_component_remove); 1154 1155 int wm_adsp2_init(struct wm_adsp *dsp) 1156 { 1157 int ret; 1158 1159 INIT_WORK(&dsp->boot_work, wm_adsp_boot_work); 1160 1161 dsp->sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr); 1162 dsp->cs_dsp.client_ops = &wm_adsp2_client_ops; 1163 1164 ret = cs_dsp_adsp2_init(&dsp->cs_dsp); 1165 if (ret) 1166 return ret; 1167 1168 return wm_adsp_common_init(dsp); 1169 } 1170 EXPORT_SYMBOL_GPL(wm_adsp2_init); 1171 1172 int wm_halo_init(struct wm_adsp *dsp) 1173 { 1174 int ret; 1175 1176 INIT_WORK(&dsp->boot_work, wm_adsp_boot_work); 1177 1178 dsp->sys_config_size = sizeof(struct wm_halo_system_config_xm_hdr); 1179 dsp->cs_dsp.client_ops = &wm_adsp2_client_ops; 1180 1181 ret = cs_dsp_halo_init(&dsp->cs_dsp); 1182 if (ret) 1183 return ret; 1184 1185 return wm_adsp_common_init(dsp); 1186 } 1187 EXPORT_SYMBOL_GPL(wm_halo_init); 1188 1189 void wm_adsp2_remove(struct wm_adsp *dsp) 1190 { 1191 cs_dsp_remove(&dsp->cs_dsp); 1192 } 1193 EXPORT_SYMBOL_GPL(wm_adsp2_remove); 1194 1195 static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr) 1196 { 1197 return compr->buf != NULL; 1198 } 1199 1200 static int wm_adsp_compr_attach(struct wm_adsp_compr *compr) 1201 { 1202 struct wm_adsp_compr_buf *buf = NULL, *tmp; 1203 1204 if (compr->dsp->fatal_error) 1205 return -EINVAL; 1206 1207 list_for_each_entry(tmp, &compr->dsp->buffer_list, list) { 1208 if (!tmp->name || !strcmp(compr->name, tmp->name)) { 1209 buf = tmp; 1210 break; 1211 } 1212 } 1213 1214 if (!buf) 1215 return -EINVAL; 1216 1217 compr->buf = buf; 1218 buf->compr = compr; 1219 1220 return 0; 1221 } 1222 1223 static void wm_adsp_compr_detach(struct wm_adsp_compr *compr) 1224 { 1225 if (!compr) 1226 return; 1227 1228 /* Wake the poll so it can see buffer is no longer attached */ 1229 if (compr->stream) 1230 snd_compr_fragment_elapsed(compr->stream); 1231 1232 if (wm_adsp_compr_attached(compr)) { 1233 compr->buf->compr = NULL; 1234 compr->buf = NULL; 1235 } 1236 } 1237 1238 int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream) 1239 { 1240 struct wm_adsp_compr *compr, *tmp; 1241 struct snd_soc_pcm_runtime *rtd = stream->private_data; 1242 int ret = 0; 1243 1244 mutex_lock(&dsp->cs_dsp.pwr_lock); 1245 1246 if (wm_adsp_fw[dsp->fw].num_caps == 0) { 1247 adsp_err(dsp, "%s: Firmware does not support compressed API\n", 1248 snd_soc_rtd_to_codec(rtd, 0)->name); 1249 ret = -ENXIO; 1250 goto out; 1251 } 1252 1253 if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) { 1254 adsp_err(dsp, "%s: Firmware does not support stream direction\n", 1255 snd_soc_rtd_to_codec(rtd, 0)->name); 1256 ret = -EINVAL; 1257 goto out; 1258 } 1259 1260 list_for_each_entry(tmp, &dsp->compr_list, list) { 1261 if (!strcmp(tmp->name, snd_soc_rtd_to_codec(rtd, 0)->name)) { 1262 adsp_err(dsp, "%s: Only a single stream supported per dai\n", 1263 snd_soc_rtd_to_codec(rtd, 0)->name); 1264 ret = -EBUSY; 1265 goto out; 1266 } 1267 } 1268 1269 compr = kzalloc(sizeof(*compr), GFP_KERNEL); 1270 if (!compr) { 1271 ret = -ENOMEM; 1272 goto out; 1273 } 1274 1275 compr->dsp = dsp; 1276 compr->stream = stream; 1277 compr->name = snd_soc_rtd_to_codec(rtd, 0)->name; 1278 1279 list_add_tail(&compr->list, &dsp->compr_list); 1280 1281 stream->runtime->private_data = compr; 1282 1283 out: 1284 mutex_unlock(&dsp->cs_dsp.pwr_lock); 1285 1286 return ret; 1287 } 1288 EXPORT_SYMBOL_GPL(wm_adsp_compr_open); 1289 1290 int wm_adsp_compr_free(struct snd_soc_component *component, 1291 struct snd_compr_stream *stream) 1292 { 1293 struct wm_adsp_compr *compr = stream->runtime->private_data; 1294 struct wm_adsp *dsp = compr->dsp; 1295 1296 mutex_lock(&dsp->cs_dsp.pwr_lock); 1297 1298 wm_adsp_compr_detach(compr); 1299 list_del(&compr->list); 1300 1301 kfree(compr->raw_buf); 1302 kfree(compr); 1303 1304 mutex_unlock(&dsp->cs_dsp.pwr_lock); 1305 1306 return 0; 1307 } 1308 EXPORT_SYMBOL_GPL(wm_adsp_compr_free); 1309 1310 static int wm_adsp_compr_check_params(struct snd_compr_stream *stream, 1311 struct snd_compr_params *params) 1312 { 1313 struct wm_adsp_compr *compr = stream->runtime->private_data; 1314 struct wm_adsp *dsp = compr->dsp; 1315 const struct wm_adsp_fw_caps *caps; 1316 const struct snd_codec_desc *desc; 1317 int i, j; 1318 1319 if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE || 1320 params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE || 1321 params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS || 1322 params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS || 1323 params->buffer.fragment_size % CS_DSP_DATA_WORD_SIZE) { 1324 compr_err(compr, "Invalid buffer fragsize=%d fragments=%d\n", 1325 params->buffer.fragment_size, 1326 params->buffer.fragments); 1327 1328 return -EINVAL; 1329 } 1330 1331 for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) { 1332 caps = &wm_adsp_fw[dsp->fw].caps[i]; 1333 desc = &caps->desc; 1334 1335 if (caps->id != params->codec.id) 1336 continue; 1337 1338 if (stream->direction == SND_COMPRESS_PLAYBACK) { 1339 if (desc->max_ch < params->codec.ch_out) 1340 continue; 1341 } else { 1342 if (desc->max_ch < params->codec.ch_in) 1343 continue; 1344 } 1345 1346 if (!(desc->formats & (1 << params->codec.format))) 1347 continue; 1348 1349 for (j = 0; j < desc->num_sample_rates; ++j) 1350 if (desc->sample_rates[j] == params->codec.sample_rate) 1351 return 0; 1352 } 1353 1354 compr_err(compr, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n", 1355 params->codec.id, params->codec.ch_in, params->codec.ch_out, 1356 params->codec.sample_rate, params->codec.format); 1357 return -EINVAL; 1358 } 1359 1360 static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr) 1361 { 1362 return compr->size.fragment_size / CS_DSP_DATA_WORD_SIZE; 1363 } 1364 1365 int wm_adsp_compr_set_params(struct snd_soc_component *component, 1366 struct snd_compr_stream *stream, 1367 struct snd_compr_params *params) 1368 { 1369 struct wm_adsp_compr *compr = stream->runtime->private_data; 1370 unsigned int size; 1371 int ret; 1372 1373 ret = wm_adsp_compr_check_params(stream, params); 1374 if (ret) 1375 return ret; 1376 1377 compr->size = params->buffer; 1378 1379 compr_dbg(compr, "fragment_size=%d fragments=%d\n", 1380 compr->size.fragment_size, compr->size.fragments); 1381 1382 size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf); 1383 compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL); 1384 if (!compr->raw_buf) 1385 return -ENOMEM; 1386 1387 compr->sample_rate = params->codec.sample_rate; 1388 1389 return 0; 1390 } 1391 EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params); 1392 1393 int wm_adsp_compr_get_caps(struct snd_soc_component *component, 1394 struct snd_compr_stream *stream, 1395 struct snd_compr_caps *caps) 1396 { 1397 struct wm_adsp_compr *compr = stream->runtime->private_data; 1398 int fw = compr->dsp->fw; 1399 int i; 1400 1401 if (wm_adsp_fw[fw].caps) { 1402 for (i = 0; i < wm_adsp_fw[fw].num_caps; i++) 1403 caps->codecs[i] = wm_adsp_fw[fw].caps[i].id; 1404 1405 caps->num_codecs = i; 1406 caps->direction = wm_adsp_fw[fw].compr_direction; 1407 1408 caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE; 1409 caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE; 1410 caps->min_fragments = WM_ADSP_MIN_FRAGMENTS; 1411 caps->max_fragments = WM_ADSP_MAX_FRAGMENTS; 1412 } 1413 1414 return 0; 1415 } 1416 EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps); 1417 1418 static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf, 1419 unsigned int field_offset, u32 *data) 1420 { 1421 return cs_dsp_read_data_word(&buf->dsp->cs_dsp, buf->host_buf_mem_type, 1422 buf->host_buf_ptr + field_offset, data); 1423 } 1424 1425 static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf, 1426 unsigned int field_offset, u32 data) 1427 { 1428 return cs_dsp_write_data_word(&buf->dsp->cs_dsp, buf->host_buf_mem_type, 1429 buf->host_buf_ptr + field_offset, 1430 data); 1431 } 1432 1433 static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf) 1434 { 1435 const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps; 1436 struct wm_adsp_buffer_region *region; 1437 u32 offset = 0; 1438 int i, ret; 1439 1440 buf->regions = kcalloc(caps->num_regions, sizeof(*buf->regions), 1441 GFP_KERNEL); 1442 if (!buf->regions) 1443 return -ENOMEM; 1444 1445 for (i = 0; i < caps->num_regions; ++i) { 1446 region = &buf->regions[i]; 1447 1448 region->offset = offset; 1449 region->mem_type = caps->region_defs[i].mem_type; 1450 1451 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset, 1452 ®ion->base_addr); 1453 if (ret < 0) 1454 return ret; 1455 1456 ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset, 1457 &offset); 1458 if (ret < 0) 1459 return ret; 1460 1461 region->cumulative_size = offset; 1462 1463 compr_dbg(buf, 1464 "region=%d type=%d base=%08x off=%08x size=%08x\n", 1465 i, region->mem_type, region->base_addr, 1466 region->offset, region->cumulative_size); 1467 } 1468 1469 return 0; 1470 } 1471 1472 static void wm_adsp_buffer_clear(struct wm_adsp_compr_buf *buf) 1473 { 1474 buf->irq_count = 0xFFFFFFFF; 1475 buf->read_index = -1; 1476 buf->avail = 0; 1477 } 1478 1479 static struct wm_adsp_compr_buf *wm_adsp_buffer_alloc(struct wm_adsp *dsp) 1480 { 1481 struct wm_adsp_compr_buf *buf; 1482 1483 buf = kzalloc(sizeof(*buf), GFP_KERNEL); 1484 if (!buf) 1485 return NULL; 1486 1487 buf->dsp = dsp; 1488 1489 wm_adsp_buffer_clear(buf); 1490 1491 return buf; 1492 } 1493 1494 static int wm_adsp_buffer_parse_legacy(struct wm_adsp *dsp) 1495 { 1496 struct cs_dsp_alg_region *alg_region; 1497 struct wm_adsp_compr_buf *buf; 1498 u32 xmalg, addr, magic; 1499 int i, ret; 1500 1501 alg_region = cs_dsp_find_alg_region(&dsp->cs_dsp, WMFW_ADSP2_XM, dsp->cs_dsp.fw_id); 1502 if (!alg_region) { 1503 adsp_err(dsp, "No algorithm region found\n"); 1504 return -EINVAL; 1505 } 1506 1507 xmalg = dsp->sys_config_size / sizeof(__be32); 1508 1509 addr = alg_region->base + xmalg + ALG_XM_FIELD(magic); 1510 ret = cs_dsp_read_data_word(&dsp->cs_dsp, WMFW_ADSP2_XM, addr, &magic); 1511 if (ret < 0) 1512 return ret; 1513 1514 if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC) 1515 return -ENODEV; 1516 1517 buf = wm_adsp_buffer_alloc(dsp); 1518 if (!buf) 1519 return -ENOMEM; 1520 1521 addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr); 1522 for (i = 0; i < 5; ++i) { 1523 ret = cs_dsp_read_data_word(&dsp->cs_dsp, WMFW_ADSP2_XM, addr, 1524 &buf->host_buf_ptr); 1525 if (ret < 0) 1526 goto err; 1527 1528 if (buf->host_buf_ptr) 1529 break; 1530 1531 usleep_range(1000, 2000); 1532 } 1533 1534 if (!buf->host_buf_ptr) { 1535 ret = -EIO; 1536 goto err; 1537 } 1538 1539 buf->host_buf_mem_type = WMFW_ADSP2_XM; 1540 1541 ret = wm_adsp_buffer_populate(buf); 1542 if (ret < 0) 1543 goto err; 1544 1545 list_add_tail(&buf->list, &dsp->buffer_list); 1546 1547 compr_dbg(buf, "legacy host_buf_ptr=%x\n", buf->host_buf_ptr); 1548 1549 return 0; 1550 1551 err: 1552 kfree(buf); 1553 1554 return ret; 1555 } 1556 1557 static int wm_adsp_buffer_parse_coeff(struct cs_dsp_coeff_ctl *cs_ctl) 1558 { 1559 struct wm_adsp_host_buf_coeff_v1 coeff_v1; 1560 struct wm_adsp_compr_buf *buf; 1561 struct wm_adsp *dsp = container_of(cs_ctl->dsp, struct wm_adsp, cs_dsp); 1562 unsigned int version = 0; 1563 int ret, i; 1564 1565 for (i = 0; i < 5; ++i) { 1566 ret = cs_dsp_coeff_read_ctrl(cs_ctl, 0, &coeff_v1, 1567 min(cs_ctl->len, sizeof(coeff_v1))); 1568 if (ret < 0) 1569 return ret; 1570 1571 if (coeff_v1.host_buf_ptr) 1572 break; 1573 1574 usleep_range(1000, 2000); 1575 } 1576 1577 if (!coeff_v1.host_buf_ptr) { 1578 adsp_err(dsp, "Failed to acquire host buffer\n"); 1579 return -EIO; 1580 } 1581 1582 buf = wm_adsp_buffer_alloc(dsp); 1583 if (!buf) 1584 return -ENOMEM; 1585 1586 buf->host_buf_mem_type = cs_ctl->alg_region.type; 1587 buf->host_buf_ptr = be32_to_cpu(coeff_v1.host_buf_ptr); 1588 1589 ret = wm_adsp_buffer_populate(buf); 1590 if (ret < 0) 1591 goto err; 1592 1593 /* 1594 * v0 host_buffer coefficients didn't have versioning, so if the 1595 * control is one word, assume version 0. 1596 */ 1597 if (cs_ctl->len == 4) 1598 goto done; 1599 1600 version = be32_to_cpu(coeff_v1.versions) & HOST_BUF_COEFF_COMPAT_VER_MASK; 1601 version >>= HOST_BUF_COEFF_COMPAT_VER_SHIFT; 1602 1603 if (version > HOST_BUF_COEFF_SUPPORTED_COMPAT_VER) { 1604 adsp_err(dsp, 1605 "Host buffer coeff ver %u > supported version %u\n", 1606 version, HOST_BUF_COEFF_SUPPORTED_COMPAT_VER); 1607 ret = -EINVAL; 1608 goto err; 1609 } 1610 1611 cs_dsp_remove_padding((u32 *)&coeff_v1.name, ARRAY_SIZE(coeff_v1.name)); 1612 1613 buf->name = kasprintf(GFP_KERNEL, "%s-dsp-%s", dsp->part, 1614 (char *)&coeff_v1.name); 1615 1616 done: 1617 list_add_tail(&buf->list, &dsp->buffer_list); 1618 1619 compr_dbg(buf, "host_buf_ptr=%x coeff version %u\n", 1620 buf->host_buf_ptr, version); 1621 1622 return version; 1623 1624 err: 1625 kfree(buf); 1626 1627 return ret; 1628 } 1629 1630 static int wm_adsp_buffer_init(struct wm_adsp *dsp) 1631 { 1632 struct cs_dsp_coeff_ctl *cs_ctl; 1633 int ret; 1634 1635 list_for_each_entry(cs_ctl, &dsp->cs_dsp.ctl_list, list) { 1636 if (cs_ctl->type != WMFW_CTL_TYPE_HOST_BUFFER) 1637 continue; 1638 1639 if (!cs_ctl->enabled) 1640 continue; 1641 1642 ret = wm_adsp_buffer_parse_coeff(cs_ctl); 1643 if (ret < 0) { 1644 adsp_err(dsp, "Failed to parse coeff: %d\n", ret); 1645 goto error; 1646 } else if (ret == 0) { 1647 /* Only one buffer supported for version 0 */ 1648 return 0; 1649 } 1650 } 1651 1652 if (list_empty(&dsp->buffer_list)) { 1653 /* Fall back to legacy support */ 1654 ret = wm_adsp_buffer_parse_legacy(dsp); 1655 if (ret == -ENODEV) 1656 adsp_info(dsp, "Legacy support not available\n"); 1657 else if (ret) 1658 adsp_warn(dsp, "Failed to parse legacy: %d\n", ret); 1659 } 1660 1661 return 0; 1662 1663 error: 1664 wm_adsp_buffer_free(dsp); 1665 return ret; 1666 } 1667 1668 static int wm_adsp_buffer_free(struct wm_adsp *dsp) 1669 { 1670 struct wm_adsp_compr_buf *buf, *tmp; 1671 1672 list_for_each_entry_safe(buf, tmp, &dsp->buffer_list, list) { 1673 wm_adsp_compr_detach(buf->compr); 1674 1675 kfree(buf->name); 1676 kfree(buf->regions); 1677 list_del(&buf->list); 1678 kfree(buf); 1679 } 1680 1681 return 0; 1682 } 1683 1684 static int wm_adsp_buffer_get_error(struct wm_adsp_compr_buf *buf) 1685 { 1686 int ret; 1687 1688 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error); 1689 if (ret < 0) { 1690 compr_err(buf, "Failed to check buffer error: %d\n", ret); 1691 return ret; 1692 } 1693 if (buf->error != 0) { 1694 compr_err(buf, "Buffer error occurred: %d\n", buf->error); 1695 return -EIO; 1696 } 1697 1698 return 0; 1699 } 1700 1701 int wm_adsp_compr_trigger(struct snd_soc_component *component, 1702 struct snd_compr_stream *stream, int cmd) 1703 { 1704 struct wm_adsp_compr *compr = stream->runtime->private_data; 1705 struct wm_adsp *dsp = compr->dsp; 1706 int ret = 0; 1707 1708 compr_dbg(compr, "Trigger: %d\n", cmd); 1709 1710 mutex_lock(&dsp->cs_dsp.pwr_lock); 1711 1712 switch (cmd) { 1713 case SNDRV_PCM_TRIGGER_START: 1714 if (!wm_adsp_compr_attached(compr)) { 1715 ret = wm_adsp_compr_attach(compr); 1716 if (ret < 0) { 1717 compr_err(compr, "Failed to link buffer and stream: %d\n", 1718 ret); 1719 break; 1720 } 1721 } 1722 1723 ret = wm_adsp_buffer_get_error(compr->buf); 1724 if (ret < 0) 1725 break; 1726 1727 /* Trigger the IRQ at one fragment of data */ 1728 ret = wm_adsp_buffer_write(compr->buf, 1729 HOST_BUFFER_FIELD(high_water_mark), 1730 wm_adsp_compr_frag_words(compr)); 1731 if (ret < 0) { 1732 compr_err(compr, "Failed to set high water mark: %d\n", 1733 ret); 1734 break; 1735 } 1736 break; 1737 case SNDRV_PCM_TRIGGER_STOP: 1738 if (wm_adsp_compr_attached(compr)) 1739 wm_adsp_buffer_clear(compr->buf); 1740 break; 1741 default: 1742 ret = -EINVAL; 1743 break; 1744 } 1745 1746 mutex_unlock(&dsp->cs_dsp.pwr_lock); 1747 1748 return ret; 1749 } 1750 EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger); 1751 1752 static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf) 1753 { 1754 int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1; 1755 1756 return buf->regions[last_region].cumulative_size; 1757 } 1758 1759 static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf) 1760 { 1761 u32 next_read_index, next_write_index; 1762 int write_index, read_index, avail; 1763 int ret; 1764 1765 /* Only sync read index if we haven't already read a valid index */ 1766 if (buf->read_index < 0) { 1767 ret = wm_adsp_buffer_read(buf, 1768 HOST_BUFFER_FIELD(next_read_index), 1769 &next_read_index); 1770 if (ret < 0) 1771 return ret; 1772 1773 read_index = sign_extend32(next_read_index, 23); 1774 1775 if (read_index < 0) { 1776 compr_dbg(buf, "Avail check on unstarted stream\n"); 1777 return 0; 1778 } 1779 1780 buf->read_index = read_index; 1781 } 1782 1783 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index), 1784 &next_write_index); 1785 if (ret < 0) 1786 return ret; 1787 1788 write_index = sign_extend32(next_write_index, 23); 1789 1790 avail = write_index - buf->read_index; 1791 if (avail < 0) 1792 avail += wm_adsp_buffer_size(buf); 1793 1794 compr_dbg(buf, "readindex=0x%x, writeindex=0x%x, avail=%d\n", 1795 buf->read_index, write_index, avail * CS_DSP_DATA_WORD_SIZE); 1796 1797 buf->avail = avail; 1798 1799 return 0; 1800 } 1801 1802 int wm_adsp_compr_handle_irq(struct wm_adsp *dsp) 1803 { 1804 struct wm_adsp_compr_buf *buf; 1805 struct wm_adsp_compr *compr; 1806 int ret = 0; 1807 1808 mutex_lock(&dsp->cs_dsp.pwr_lock); 1809 1810 if (list_empty(&dsp->buffer_list)) { 1811 ret = -ENODEV; 1812 goto out; 1813 } 1814 1815 adsp_dbg(dsp, "Handling buffer IRQ\n"); 1816 1817 list_for_each_entry(buf, &dsp->buffer_list, list) { 1818 compr = buf->compr; 1819 1820 ret = wm_adsp_buffer_get_error(buf); 1821 if (ret < 0) 1822 goto out_notify; /* Wake poll to report error */ 1823 1824 ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count), 1825 &buf->irq_count); 1826 if (ret < 0) { 1827 compr_err(buf, "Failed to get irq_count: %d\n", ret); 1828 goto out; 1829 } 1830 1831 ret = wm_adsp_buffer_update_avail(buf); 1832 if (ret < 0) { 1833 compr_err(buf, "Error reading avail: %d\n", ret); 1834 goto out; 1835 } 1836 1837 if (wm_adsp_fw[dsp->fw].voice_trigger && buf->irq_count == 2) 1838 ret = WM_ADSP_COMPR_VOICE_TRIGGER; 1839 1840 out_notify: 1841 if (compr && compr->stream) 1842 snd_compr_fragment_elapsed(compr->stream); 1843 } 1844 1845 out: 1846 mutex_unlock(&dsp->cs_dsp.pwr_lock); 1847 1848 return ret; 1849 } 1850 EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq); 1851 1852 static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf) 1853 { 1854 if (buf->irq_count & 0x01) 1855 return 0; 1856 1857 compr_dbg(buf, "Enable IRQ(0x%x) for next fragment\n", buf->irq_count); 1858 1859 buf->irq_count |= 0x01; 1860 1861 return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack), 1862 buf->irq_count); 1863 } 1864 1865 int wm_adsp_compr_pointer(struct snd_soc_component *component, 1866 struct snd_compr_stream *stream, 1867 struct snd_compr_tstamp *tstamp) 1868 { 1869 struct wm_adsp_compr *compr = stream->runtime->private_data; 1870 struct wm_adsp *dsp = compr->dsp; 1871 struct wm_adsp_compr_buf *buf; 1872 int ret = 0; 1873 1874 compr_dbg(compr, "Pointer request\n"); 1875 1876 mutex_lock(&dsp->cs_dsp.pwr_lock); 1877 1878 buf = compr->buf; 1879 1880 if (dsp->fatal_error || !buf || buf->error) { 1881 snd_compr_stop_error(stream, SNDRV_PCM_STATE_XRUN); 1882 ret = -EIO; 1883 goto out; 1884 } 1885 1886 if (buf->avail < wm_adsp_compr_frag_words(compr)) { 1887 ret = wm_adsp_buffer_update_avail(buf); 1888 if (ret < 0) { 1889 compr_err(compr, "Error reading avail: %d\n", ret); 1890 goto out; 1891 } 1892 1893 /* 1894 * If we really have less than 1 fragment available tell the 1895 * DSP to inform us once a whole fragment is available. 1896 */ 1897 if (buf->avail < wm_adsp_compr_frag_words(compr)) { 1898 ret = wm_adsp_buffer_get_error(buf); 1899 if (ret < 0) { 1900 if (buf->error) 1901 snd_compr_stop_error(stream, 1902 SNDRV_PCM_STATE_XRUN); 1903 goto out; 1904 } 1905 1906 ret = wm_adsp_buffer_reenable_irq(buf); 1907 if (ret < 0) { 1908 compr_err(compr, "Failed to re-enable buffer IRQ: %d\n", 1909 ret); 1910 goto out; 1911 } 1912 } 1913 } 1914 1915 tstamp->copied_total = compr->copied_total; 1916 tstamp->copied_total += buf->avail * CS_DSP_DATA_WORD_SIZE; 1917 tstamp->sampling_rate = compr->sample_rate; 1918 1919 out: 1920 mutex_unlock(&dsp->cs_dsp.pwr_lock); 1921 1922 return ret; 1923 } 1924 EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer); 1925 1926 static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target) 1927 { 1928 struct wm_adsp_compr_buf *buf = compr->buf; 1929 unsigned int adsp_addr; 1930 int mem_type, nwords, max_read; 1931 int i, ret; 1932 1933 /* Calculate read parameters */ 1934 for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i) 1935 if (buf->read_index < buf->regions[i].cumulative_size) 1936 break; 1937 1938 if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions) 1939 return -EINVAL; 1940 1941 mem_type = buf->regions[i].mem_type; 1942 adsp_addr = buf->regions[i].base_addr + 1943 (buf->read_index - buf->regions[i].offset); 1944 1945 max_read = wm_adsp_compr_frag_words(compr); 1946 nwords = buf->regions[i].cumulative_size - buf->read_index; 1947 1948 if (nwords > target) 1949 nwords = target; 1950 if (nwords > buf->avail) 1951 nwords = buf->avail; 1952 if (nwords > max_read) 1953 nwords = max_read; 1954 if (!nwords) 1955 return 0; 1956 1957 /* Read data from DSP */ 1958 ret = cs_dsp_read_raw_data_block(&buf->dsp->cs_dsp, mem_type, adsp_addr, 1959 nwords, (__be32 *)compr->raw_buf); 1960 if (ret < 0) 1961 return ret; 1962 1963 cs_dsp_remove_padding(compr->raw_buf, nwords); 1964 1965 /* update read index to account for words read */ 1966 buf->read_index += nwords; 1967 if (buf->read_index == wm_adsp_buffer_size(buf)) 1968 buf->read_index = 0; 1969 1970 ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index), 1971 buf->read_index); 1972 if (ret < 0) 1973 return ret; 1974 1975 /* update avail to account for words read */ 1976 buf->avail -= nwords; 1977 1978 return nwords; 1979 } 1980 1981 static int wm_adsp_compr_read(struct wm_adsp_compr *compr, 1982 char __user *buf, size_t count) 1983 { 1984 struct wm_adsp *dsp = compr->dsp; 1985 int ntotal = 0; 1986 int nwords, nbytes; 1987 1988 compr_dbg(compr, "Requested read of %zu bytes\n", count); 1989 1990 if (dsp->fatal_error || !compr->buf || compr->buf->error) { 1991 snd_compr_stop_error(compr->stream, SNDRV_PCM_STATE_XRUN); 1992 return -EIO; 1993 } 1994 1995 count /= CS_DSP_DATA_WORD_SIZE; 1996 1997 do { 1998 nwords = wm_adsp_buffer_capture_block(compr, count); 1999 if (nwords < 0) { 2000 compr_err(compr, "Failed to capture block: %d\n", 2001 nwords); 2002 return nwords; 2003 } 2004 2005 nbytes = nwords * CS_DSP_DATA_WORD_SIZE; 2006 2007 compr_dbg(compr, "Read %d bytes\n", nbytes); 2008 2009 if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) { 2010 compr_err(compr, "Failed to copy data to user: %d, %d\n", 2011 ntotal, nbytes); 2012 return -EFAULT; 2013 } 2014 2015 count -= nwords; 2016 ntotal += nbytes; 2017 } while (nwords > 0 && count > 0); 2018 2019 compr->copied_total += ntotal; 2020 2021 return ntotal; 2022 } 2023 2024 int wm_adsp_compr_copy(struct snd_soc_component *component, 2025 struct snd_compr_stream *stream, char __user *buf, 2026 size_t count) 2027 { 2028 struct wm_adsp_compr *compr = stream->runtime->private_data; 2029 struct wm_adsp *dsp = compr->dsp; 2030 int ret; 2031 2032 mutex_lock(&dsp->cs_dsp.pwr_lock); 2033 2034 if (stream->direction == SND_COMPRESS_CAPTURE) 2035 ret = wm_adsp_compr_read(compr, buf, count); 2036 else 2037 ret = -ENOTSUPP; 2038 2039 mutex_unlock(&dsp->cs_dsp.pwr_lock); 2040 2041 return ret; 2042 } 2043 EXPORT_SYMBOL_GPL(wm_adsp_compr_copy); 2044 2045 static void wm_adsp_fatal_error(struct cs_dsp *cs_dsp) 2046 { 2047 struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp); 2048 struct wm_adsp_compr *compr; 2049 2050 dsp->fatal_error = true; 2051 2052 list_for_each_entry(compr, &dsp->compr_list, list) { 2053 if (compr->stream) 2054 snd_compr_fragment_elapsed(compr->stream); 2055 } 2056 } 2057 2058 irqreturn_t wm_adsp2_bus_error(int irq, void *data) 2059 { 2060 struct wm_adsp *dsp = (struct wm_adsp *)data; 2061 2062 cs_dsp_adsp2_bus_error(&dsp->cs_dsp); 2063 2064 return IRQ_HANDLED; 2065 } 2066 EXPORT_SYMBOL_GPL(wm_adsp2_bus_error); 2067 2068 irqreturn_t wm_halo_bus_error(int irq, void *data) 2069 { 2070 struct wm_adsp *dsp = (struct wm_adsp *)data; 2071 2072 cs_dsp_halo_bus_error(&dsp->cs_dsp); 2073 2074 return IRQ_HANDLED; 2075 } 2076 EXPORT_SYMBOL_GPL(wm_halo_bus_error); 2077 2078 irqreturn_t wm_halo_wdt_expire(int irq, void *data) 2079 { 2080 struct wm_adsp *dsp = data; 2081 2082 cs_dsp_halo_wdt_expire(&dsp->cs_dsp); 2083 2084 return IRQ_HANDLED; 2085 } 2086 EXPORT_SYMBOL_GPL(wm_halo_wdt_expire); 2087 2088 static const struct cs_dsp_client_ops wm_adsp1_client_ops = { 2089 .control_add = wm_adsp_control_add, 2090 .control_remove = wm_adsp_control_remove, 2091 }; 2092 2093 static const struct cs_dsp_client_ops wm_adsp2_client_ops = { 2094 .control_add = wm_adsp_control_add, 2095 .control_remove = wm_adsp_control_remove, 2096 .pre_run = wm_adsp_pre_run, 2097 .post_run = wm_adsp_event_post_run, 2098 .post_stop = wm_adsp_event_post_stop, 2099 .watchdog_expired = wm_adsp_fatal_error, 2100 }; 2101 2102 MODULE_LICENSE("GPL v2"); 2103 MODULE_IMPORT_NS(FW_CS_DSP); 2104