1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright 2019 Google, Inc. 4 * 5 * ChromeOS Embedded Controller codec driver. 6 * 7 * This driver uses the cros-ec interface to communicate with the ChromeOS 8 * EC for audio function. 9 */ 10 11 #include <crypto/hash.h> 12 #include <crypto/sha.h> 13 #include <linux/delay.h> 14 #include <linux/device.h> 15 #include <linux/io.h> 16 #include <linux/jiffies.h> 17 #include <linux/kernel.h> 18 #include <linux/module.h> 19 #include <linux/of.h> 20 #include <linux/of_address.h> 21 #include <linux/platform_data/cros_ec_commands.h> 22 #include <linux/platform_data/cros_ec_proto.h> 23 #include <linux/platform_device.h> 24 #include <sound/pcm.h> 25 #include <sound/pcm_params.h> 26 #include <sound/soc.h> 27 #include <sound/tlv.h> 28 29 struct cros_ec_codec_priv { 30 struct device *dev; 31 struct cros_ec_device *ec_device; 32 33 /* common */ 34 uint32_t ec_capabilities; 35 36 uint64_t ec_shm_addr; 37 uint32_t ec_shm_len; 38 39 uint64_t ap_shm_phys_addr; 40 uint32_t ap_shm_len; 41 uint64_t ap_shm_addr; 42 uint64_t ap_shm_last_alloc; 43 44 /* DMIC */ 45 atomic_t dmic_probed; 46 47 /* WoV */ 48 bool wov_enabled; 49 uint8_t *wov_audio_shm_p; 50 uint32_t wov_audio_shm_len; 51 uint8_t wov_audio_shm_type; 52 uint8_t *wov_lang_shm_p; 53 uint32_t wov_lang_shm_len; 54 uint8_t wov_lang_shm_type; 55 56 struct mutex wov_dma_lock; 57 uint8_t wov_buf[64000]; 58 uint32_t wov_rp, wov_wp; 59 size_t wov_dma_offset; 60 bool wov_burst_read; 61 struct snd_pcm_substream *wov_substream; 62 struct delayed_work wov_copy_work; 63 struct notifier_block wov_notifier; 64 }; 65 66 static int ec_codec_capable(struct cros_ec_codec_priv *priv, uint8_t cap) 67 { 68 return priv->ec_capabilities & BIT(cap); 69 } 70 71 static int send_ec_host_command(struct cros_ec_device *ec_dev, uint32_t cmd, 72 uint8_t *out, size_t outsize, 73 uint8_t *in, size_t insize) 74 { 75 int ret; 76 struct cros_ec_command *msg; 77 78 msg = kmalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL); 79 if (!msg) 80 return -ENOMEM; 81 82 msg->version = 0; 83 msg->command = cmd; 84 msg->outsize = outsize; 85 msg->insize = insize; 86 87 if (outsize) 88 memcpy(msg->data, out, outsize); 89 90 ret = cros_ec_cmd_xfer_status(ec_dev, msg); 91 if (ret < 0) 92 goto error; 93 94 if (insize) 95 memcpy(in, msg->data, insize); 96 97 ret = 0; 98 error: 99 kfree(msg); 100 return ret; 101 } 102 103 static int calculate_sha256(struct cros_ec_codec_priv *priv, 104 uint8_t *buf, uint32_t size, uint8_t *digest) 105 { 106 struct crypto_shash *tfm; 107 108 tfm = crypto_alloc_shash("sha256", CRYPTO_ALG_TYPE_SHASH, 0); 109 if (IS_ERR(tfm)) { 110 dev_err(priv->dev, "can't alloc shash\n"); 111 return PTR_ERR(tfm); 112 } 113 114 { 115 SHASH_DESC_ON_STACK(desc, tfm); 116 117 desc->tfm = tfm; 118 119 crypto_shash_digest(desc, buf, size, digest); 120 shash_desc_zero(desc); 121 } 122 123 crypto_free_shash(tfm); 124 125 #ifdef DEBUG 126 { 127 char digest_str[65]; 128 129 bin2hex(digest_str, digest, 32); 130 digest_str[64] = 0; 131 dev_dbg(priv->dev, "hash=%s\n", digest_str); 132 } 133 #endif 134 135 return 0; 136 } 137 138 static int dmic_get_gain(struct snd_kcontrol *kcontrol, 139 struct snd_ctl_elem_value *ucontrol) 140 { 141 struct snd_soc_component *component = 142 snd_soc_kcontrol_component(kcontrol); 143 struct cros_ec_codec_priv *priv = 144 snd_soc_component_get_drvdata(component); 145 struct ec_param_ec_codec_dmic p; 146 struct ec_response_ec_codec_dmic_get_gain_idx r; 147 int ret; 148 149 p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX; 150 p.get_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_0; 151 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, 152 (uint8_t *)&p, sizeof(p), 153 (uint8_t *)&r, sizeof(r)); 154 if (ret < 0) 155 return ret; 156 ucontrol->value.integer.value[0] = r.gain; 157 158 p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX; 159 p.get_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1; 160 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, 161 (uint8_t *)&p, sizeof(p), 162 (uint8_t *)&r, sizeof(r)); 163 if (ret < 0) 164 return ret; 165 ucontrol->value.integer.value[1] = r.gain; 166 167 return 0; 168 } 169 170 static int dmic_put_gain(struct snd_kcontrol *kcontrol, 171 struct snd_ctl_elem_value *ucontrol) 172 { 173 struct snd_soc_component *component = 174 snd_soc_kcontrol_component(kcontrol); 175 struct cros_ec_codec_priv *priv = 176 snd_soc_component_get_drvdata(component); 177 struct soc_mixer_control *control = 178 (struct soc_mixer_control *)kcontrol->private_value; 179 int max_dmic_gain = control->max; 180 int left = ucontrol->value.integer.value[0]; 181 int right = ucontrol->value.integer.value[1]; 182 struct ec_param_ec_codec_dmic p; 183 int ret; 184 185 if (left > max_dmic_gain || right > max_dmic_gain) 186 return -EINVAL; 187 188 dev_dbg(component->dev, "set mic gain to %u, %u\n", left, right); 189 190 p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX; 191 p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_0; 192 p.set_gain_idx_param.gain = left; 193 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, 194 (uint8_t *)&p, sizeof(p), NULL, 0); 195 if (ret < 0) 196 return ret; 197 198 p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX; 199 p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1; 200 p.set_gain_idx_param.gain = right; 201 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, 202 (uint8_t *)&p, sizeof(p), NULL, 0); 203 } 204 205 static const DECLARE_TLV_DB_SCALE(dmic_gain_tlv, 0, 100, 0); 206 207 enum { 208 DMIC_CTL_GAIN = 0, 209 }; 210 211 static struct snd_kcontrol_new dmic_controls[] = { 212 [DMIC_CTL_GAIN] = 213 SOC_DOUBLE_EXT_TLV("EC Mic Gain", SND_SOC_NOPM, SND_SOC_NOPM, 214 0, 0, 0, dmic_get_gain, dmic_put_gain, 215 dmic_gain_tlv), 216 }; 217 218 static int dmic_probe(struct snd_soc_component *component) 219 { 220 struct cros_ec_codec_priv *priv = 221 snd_soc_component_get_drvdata(component); 222 struct device *dev = priv->dev; 223 struct soc_mixer_control *control; 224 struct ec_param_ec_codec_dmic p; 225 struct ec_response_ec_codec_dmic_get_max_gain r; 226 int ret; 227 228 if (!atomic_add_unless(&priv->dmic_probed, 1, 1)) 229 return 0; 230 231 p.cmd = EC_CODEC_DMIC_GET_MAX_GAIN; 232 233 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC, 234 (uint8_t *)&p, sizeof(p), 235 (uint8_t *)&r, sizeof(r)); 236 if (ret < 0) { 237 dev_warn(dev, "get_max_gain() unsupported\n"); 238 return 0; 239 } 240 241 dev_dbg(dev, "max gain = %d\n", r.max_gain); 242 243 control = (struct soc_mixer_control *) 244 dmic_controls[DMIC_CTL_GAIN].private_value; 245 control->max = r.max_gain; 246 control->platform_max = r.max_gain; 247 248 return snd_soc_add_component_controls(component, 249 &dmic_controls[DMIC_CTL_GAIN], 1); 250 } 251 252 static int i2s_rx_hw_params(struct snd_pcm_substream *substream, 253 struct snd_pcm_hw_params *params, 254 struct snd_soc_dai *dai) 255 { 256 struct snd_soc_component *component = dai->component; 257 struct cros_ec_codec_priv *priv = 258 snd_soc_component_get_drvdata(component); 259 struct ec_param_ec_codec_i2s_rx p; 260 enum ec_codec_i2s_rx_sample_depth depth; 261 int ret; 262 263 if (params_rate(params) != 48000) 264 return -EINVAL; 265 266 switch (params_format(params)) { 267 case SNDRV_PCM_FORMAT_S16_LE: 268 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_16; 269 break; 270 case SNDRV_PCM_FORMAT_S24_LE: 271 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_24; 272 break; 273 default: 274 return -EINVAL; 275 } 276 277 dev_dbg(component->dev, "set depth to %u\n", depth); 278 279 p.cmd = EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH; 280 p.set_sample_depth_param.depth = depth; 281 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX, 282 (uint8_t *)&p, sizeof(p), NULL, 0); 283 if (ret < 0) 284 return ret; 285 286 dev_dbg(component->dev, "set bclk to %u\n", 287 snd_soc_params_to_bclk(params)); 288 289 p.cmd = EC_CODEC_I2S_RX_SET_BCLK; 290 p.set_bclk_param.bclk = snd_soc_params_to_bclk(params); 291 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX, 292 (uint8_t *)&p, sizeof(p), NULL, 0); 293 } 294 295 static int i2s_rx_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 296 { 297 struct snd_soc_component *component = dai->component; 298 struct cros_ec_codec_priv *priv = 299 snd_soc_component_get_drvdata(component); 300 struct ec_param_ec_codec_i2s_rx p; 301 enum ec_codec_i2s_rx_daifmt daifmt; 302 303 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 304 case SND_SOC_DAIFMT_CBS_CFS: 305 break; 306 default: 307 return -EINVAL; 308 } 309 310 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 311 case SND_SOC_DAIFMT_NB_NF: 312 break; 313 default: 314 return -EINVAL; 315 } 316 317 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 318 case SND_SOC_DAIFMT_I2S: 319 daifmt = EC_CODEC_I2S_RX_DAIFMT_I2S; 320 break; 321 case SND_SOC_DAIFMT_RIGHT_J: 322 daifmt = EC_CODEC_I2S_RX_DAIFMT_RIGHT_J; 323 break; 324 case SND_SOC_DAIFMT_LEFT_J: 325 daifmt = EC_CODEC_I2S_RX_DAIFMT_LEFT_J; 326 break; 327 default: 328 return -EINVAL; 329 } 330 331 dev_dbg(component->dev, "set format to %u\n", daifmt); 332 333 p.cmd = EC_CODEC_I2S_RX_SET_DAIFMT; 334 p.set_daifmt_param.daifmt = daifmt; 335 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX, 336 (uint8_t *)&p, sizeof(p), NULL, 0); 337 } 338 339 static const struct snd_soc_dai_ops i2s_rx_dai_ops = { 340 .hw_params = i2s_rx_hw_params, 341 .set_fmt = i2s_rx_set_fmt, 342 }; 343 344 static int i2s_rx_event(struct snd_soc_dapm_widget *w, 345 struct snd_kcontrol *kcontrol, int event) 346 { 347 struct snd_soc_component *component = 348 snd_soc_dapm_to_component(w->dapm); 349 struct cros_ec_codec_priv *priv = 350 snd_soc_component_get_drvdata(component); 351 struct ec_param_ec_codec_i2s_rx p; 352 353 switch (event) { 354 case SND_SOC_DAPM_PRE_PMU: 355 dev_dbg(component->dev, "enable I2S RX\n"); 356 p.cmd = EC_CODEC_I2S_RX_ENABLE; 357 break; 358 case SND_SOC_DAPM_PRE_PMD: 359 dev_dbg(component->dev, "disable I2S RX\n"); 360 p.cmd = EC_CODEC_I2S_RX_DISABLE; 361 break; 362 default: 363 return 0; 364 } 365 366 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX, 367 (uint8_t *)&p, sizeof(p), NULL, 0); 368 } 369 370 static struct snd_soc_dapm_widget i2s_rx_dapm_widgets[] = { 371 SND_SOC_DAPM_INPUT("DMIC"), 372 SND_SOC_DAPM_SUPPLY("I2S RX Enable", SND_SOC_NOPM, 0, 0, i2s_rx_event, 373 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD), 374 SND_SOC_DAPM_AIF_OUT("I2S RX", "I2S Capture", 0, SND_SOC_NOPM, 0, 0), 375 }; 376 377 static struct snd_soc_dapm_route i2s_rx_dapm_routes[] = { 378 {"I2S RX", NULL, "DMIC"}, 379 {"I2S RX", NULL, "I2S RX Enable"}, 380 }; 381 382 static struct snd_soc_dai_driver i2s_rx_dai_driver = { 383 .name = "EC Codec I2S RX", 384 .capture = { 385 .stream_name = "I2S Capture", 386 .channels_min = 2, 387 .channels_max = 2, 388 .rates = SNDRV_PCM_RATE_48000, 389 .formats = SNDRV_PCM_FMTBIT_S16_LE | 390 SNDRV_PCM_FMTBIT_S24_LE, 391 }, 392 .ops = &i2s_rx_dai_ops, 393 }; 394 395 static int i2s_rx_probe(struct snd_soc_component *component) 396 { 397 return dmic_probe(component); 398 } 399 400 static const struct snd_soc_component_driver i2s_rx_component_driver = { 401 .probe = i2s_rx_probe, 402 .dapm_widgets = i2s_rx_dapm_widgets, 403 .num_dapm_widgets = ARRAY_SIZE(i2s_rx_dapm_widgets), 404 .dapm_routes = i2s_rx_dapm_routes, 405 .num_dapm_routes = ARRAY_SIZE(i2s_rx_dapm_routes), 406 }; 407 408 static void *wov_map_shm(struct cros_ec_codec_priv *priv, 409 uint8_t shm_id, uint32_t *len, uint8_t *type) 410 { 411 struct ec_param_ec_codec p; 412 struct ec_response_ec_codec_get_shm_addr r; 413 uint32_t req, offset; 414 415 p.cmd = EC_CODEC_GET_SHM_ADDR; 416 p.get_shm_addr_param.shm_id = shm_id; 417 if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC, 418 (uint8_t *)&p, sizeof(p), 419 (uint8_t *)&r, sizeof(r)) < 0) { 420 dev_err(priv->dev, "failed to EC_CODEC_GET_SHM_ADDR\n"); 421 return NULL; 422 } 423 424 dev_dbg(priv->dev, "phys_addr=%#llx, len=%#x\n", r.phys_addr, r.len); 425 426 *len = r.len; 427 *type = r.type; 428 429 switch (r.type) { 430 case EC_CODEC_SHM_TYPE_EC_RAM: 431 return (void __force *)devm_ioremap_wc(priv->dev, 432 r.phys_addr + priv->ec_shm_addr, r.len); 433 case EC_CODEC_SHM_TYPE_SYSTEM_RAM: 434 if (r.phys_addr) { 435 dev_err(priv->dev, "unknown status\n"); 436 return NULL; 437 } 438 439 req = round_up(r.len, PAGE_SIZE); 440 dev_dbg(priv->dev, "round up from %u to %u\n", r.len, req); 441 442 if (priv->ap_shm_last_alloc + req > 443 priv->ap_shm_phys_addr + priv->ap_shm_len) { 444 dev_err(priv->dev, "insufficient space for AP SHM\n"); 445 return NULL; 446 } 447 448 dev_dbg(priv->dev, "alloc AP SHM addr=%#llx, len=%#x\n", 449 priv->ap_shm_last_alloc, req); 450 451 p.cmd = EC_CODEC_SET_SHM_ADDR; 452 p.set_shm_addr_param.phys_addr = priv->ap_shm_last_alloc; 453 p.set_shm_addr_param.len = req; 454 p.set_shm_addr_param.shm_id = shm_id; 455 if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC, 456 (uint8_t *)&p, sizeof(p), 457 NULL, 0) < 0) { 458 dev_err(priv->dev, "failed to EC_CODEC_SET_SHM_ADDR\n"); 459 return NULL; 460 } 461 462 /* 463 * Note: EC codec only requests for `r.len' but we allocate 464 * round up PAGE_SIZE `req'. 465 */ 466 offset = priv->ap_shm_last_alloc - priv->ap_shm_phys_addr; 467 priv->ap_shm_last_alloc += req; 468 469 return (void *)(uintptr_t)(priv->ap_shm_addr + offset); 470 default: 471 return NULL; 472 } 473 } 474 475 static bool wov_queue_full(struct cros_ec_codec_priv *priv) 476 { 477 return ((priv->wov_wp + 1) % sizeof(priv->wov_buf)) == priv->wov_rp; 478 } 479 480 static size_t wov_queue_size(struct cros_ec_codec_priv *priv) 481 { 482 if (priv->wov_wp >= priv->wov_rp) 483 return priv->wov_wp - priv->wov_rp; 484 else 485 return sizeof(priv->wov_buf) - priv->wov_rp + priv->wov_wp; 486 } 487 488 static void wov_queue_dequeue(struct cros_ec_codec_priv *priv, size_t len) 489 { 490 struct snd_pcm_runtime *runtime = priv->wov_substream->runtime; 491 size_t req; 492 493 while (len) { 494 req = min(len, runtime->dma_bytes - priv->wov_dma_offset); 495 if (priv->wov_wp >= priv->wov_rp) 496 req = min(req, (size_t)priv->wov_wp - priv->wov_rp); 497 else 498 req = min(req, sizeof(priv->wov_buf) - priv->wov_rp); 499 500 memcpy(runtime->dma_area + priv->wov_dma_offset, 501 priv->wov_buf + priv->wov_rp, req); 502 503 priv->wov_dma_offset += req; 504 if (priv->wov_dma_offset == runtime->dma_bytes) 505 priv->wov_dma_offset = 0; 506 507 priv->wov_rp += req; 508 if (priv->wov_rp == sizeof(priv->wov_buf)) 509 priv->wov_rp = 0; 510 511 len -= req; 512 } 513 514 snd_pcm_period_elapsed(priv->wov_substream); 515 } 516 517 static void wov_queue_try_dequeue(struct cros_ec_codec_priv *priv) 518 { 519 size_t period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream); 520 521 while (period_bytes && wov_queue_size(priv) >= period_bytes) { 522 wov_queue_dequeue(priv, period_bytes); 523 period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream); 524 } 525 } 526 527 static void wov_queue_enqueue(struct cros_ec_codec_priv *priv, 528 uint8_t *addr, size_t len, bool iomem) 529 { 530 size_t req; 531 532 while (len) { 533 if (wov_queue_full(priv)) { 534 wov_queue_try_dequeue(priv); 535 536 if (wov_queue_full(priv)) { 537 dev_err(priv->dev, "overrun detected\n"); 538 return; 539 } 540 } 541 542 if (priv->wov_wp >= priv->wov_rp) 543 req = sizeof(priv->wov_buf) - priv->wov_wp; 544 else 545 /* Note: waste 1-byte to differentiate full and empty */ 546 req = priv->wov_rp - priv->wov_wp - 1; 547 req = min(req, len); 548 549 if (iomem) 550 memcpy_fromio(priv->wov_buf + priv->wov_wp, 551 (void __force __iomem *)addr, req); 552 else 553 memcpy(priv->wov_buf + priv->wov_wp, addr, req); 554 555 priv->wov_wp += req; 556 if (priv->wov_wp == sizeof(priv->wov_buf)) 557 priv->wov_wp = 0; 558 559 addr += req; 560 len -= req; 561 } 562 563 wov_queue_try_dequeue(priv); 564 } 565 566 static int wov_read_audio_shm(struct cros_ec_codec_priv *priv) 567 { 568 struct ec_param_ec_codec_wov p; 569 struct ec_response_ec_codec_wov_read_audio_shm r; 570 int ret; 571 572 p.cmd = EC_CODEC_WOV_READ_AUDIO_SHM; 573 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, 574 (uint8_t *)&p, sizeof(p), 575 (uint8_t *)&r, sizeof(r)); 576 if (ret) { 577 dev_err(priv->dev, "failed to EC_CODEC_WOV_READ_AUDIO_SHM\n"); 578 return ret; 579 } 580 581 if (!r.len) 582 dev_dbg(priv->dev, "no data, sleep\n"); 583 else 584 wov_queue_enqueue(priv, priv->wov_audio_shm_p + r.offset, r.len, 585 priv->wov_audio_shm_type == EC_CODEC_SHM_TYPE_EC_RAM); 586 return -EAGAIN; 587 } 588 589 static int wov_read_audio(struct cros_ec_codec_priv *priv) 590 { 591 struct ec_param_ec_codec_wov p; 592 struct ec_response_ec_codec_wov_read_audio r; 593 int remain = priv->wov_burst_read ? 16000 : 320; 594 int ret; 595 596 while (remain >= 0) { 597 p.cmd = EC_CODEC_WOV_READ_AUDIO; 598 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, 599 (uint8_t *)&p, sizeof(p), 600 (uint8_t *)&r, sizeof(r)); 601 if (ret) { 602 dev_err(priv->dev, 603 "failed to EC_CODEC_WOV_READ_AUDIO\n"); 604 return ret; 605 } 606 607 if (!r.len) { 608 dev_dbg(priv->dev, "no data, sleep\n"); 609 priv->wov_burst_read = false; 610 break; 611 } 612 613 wov_queue_enqueue(priv, r.buf, r.len, false); 614 remain -= r.len; 615 } 616 617 return -EAGAIN; 618 } 619 620 static void wov_copy_work(struct work_struct *w) 621 { 622 struct cros_ec_codec_priv *priv = 623 container_of(w, struct cros_ec_codec_priv, wov_copy_work.work); 624 int ret; 625 626 mutex_lock(&priv->wov_dma_lock); 627 if (!priv->wov_substream) { 628 dev_warn(priv->dev, "no pcm substream\n"); 629 goto leave; 630 } 631 632 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM)) 633 ret = wov_read_audio_shm(priv); 634 else 635 ret = wov_read_audio(priv); 636 637 if (ret == -EAGAIN) 638 schedule_delayed_work(&priv->wov_copy_work, 639 msecs_to_jiffies(10)); 640 else if (ret) 641 dev_err(priv->dev, "failed to read audio data\n"); 642 leave: 643 mutex_unlock(&priv->wov_dma_lock); 644 } 645 646 static int wov_enable_get(struct snd_kcontrol *kcontrol, 647 struct snd_ctl_elem_value *ucontrol) 648 { 649 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol); 650 struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c); 651 652 ucontrol->value.integer.value[0] = priv->wov_enabled; 653 return 0; 654 } 655 656 static int wov_enable_put(struct snd_kcontrol *kcontrol, 657 struct snd_ctl_elem_value *ucontrol) 658 { 659 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol); 660 struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c); 661 int enabled = ucontrol->value.integer.value[0]; 662 struct ec_param_ec_codec_wov p; 663 int ret; 664 665 if (priv->wov_enabled != enabled) { 666 if (enabled) 667 p.cmd = EC_CODEC_WOV_ENABLE; 668 else 669 p.cmd = EC_CODEC_WOV_DISABLE; 670 671 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, 672 (uint8_t *)&p, sizeof(p), NULL, 0); 673 if (ret) { 674 dev_err(priv->dev, "failed to %s wov\n", 675 enabled ? "enable" : "disable"); 676 return ret; 677 } 678 679 priv->wov_enabled = enabled; 680 } 681 682 return 0; 683 } 684 685 static int wov_set_lang_shm(struct cros_ec_codec_priv *priv, 686 uint8_t *buf, size_t size, uint8_t *digest) 687 { 688 struct ec_param_ec_codec_wov p; 689 struct ec_param_ec_codec_wov_set_lang_shm *pp = &p.set_lang_shm_param; 690 int ret; 691 692 if (size > priv->wov_lang_shm_len) { 693 dev_err(priv->dev, "no enough SHM size: %d\n", 694 priv->wov_lang_shm_len); 695 return -EIO; 696 } 697 698 switch (priv->wov_lang_shm_type) { 699 case EC_CODEC_SHM_TYPE_EC_RAM: 700 memcpy_toio((void __force __iomem *)priv->wov_lang_shm_p, 701 buf, size); 702 memset_io((void __force __iomem *)priv->wov_lang_shm_p + size, 703 0, priv->wov_lang_shm_len - size); 704 break; 705 case EC_CODEC_SHM_TYPE_SYSTEM_RAM: 706 memcpy(priv->wov_lang_shm_p, buf, size); 707 memset(priv->wov_lang_shm_p + size, 0, 708 priv->wov_lang_shm_len - size); 709 710 /* make sure write to memory before calling host command */ 711 wmb(); 712 break; 713 } 714 715 p.cmd = EC_CODEC_WOV_SET_LANG_SHM; 716 memcpy(pp->hash, digest, SHA256_DIGEST_SIZE); 717 pp->total_len = size; 718 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, 719 (uint8_t *)&p, sizeof(p), NULL, 0); 720 if (ret) { 721 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG_SHM\n"); 722 return ret; 723 } 724 725 return 0; 726 } 727 728 static int wov_set_lang(struct cros_ec_codec_priv *priv, 729 uint8_t *buf, size_t size, uint8_t *digest) 730 { 731 struct ec_param_ec_codec_wov p; 732 struct ec_param_ec_codec_wov_set_lang *pp = &p.set_lang_param; 733 size_t i, req; 734 int ret; 735 736 for (i = 0; i < size; i += req) { 737 req = min(size - i, ARRAY_SIZE(pp->buf)); 738 739 p.cmd = EC_CODEC_WOV_SET_LANG; 740 memcpy(pp->hash, digest, SHA256_DIGEST_SIZE); 741 pp->total_len = size; 742 pp->offset = i; 743 memcpy(pp->buf, buf + i, req); 744 pp->len = req; 745 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, 746 (uint8_t *)&p, sizeof(p), NULL, 0); 747 if (ret) { 748 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG\n"); 749 return ret; 750 } 751 } 752 753 return 0; 754 } 755 756 static int wov_hotword_model_put(struct snd_kcontrol *kcontrol, 757 const unsigned int __user *bytes, 758 unsigned int size) 759 { 760 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 761 struct cros_ec_codec_priv *priv = 762 snd_soc_component_get_drvdata(component); 763 struct ec_param_ec_codec_wov p; 764 struct ec_response_ec_codec_wov_get_lang r; 765 uint8_t digest[SHA256_DIGEST_SIZE]; 766 uint8_t *buf; 767 int ret; 768 769 /* Skips the TLV header. */ 770 bytes += 2; 771 size -= 8; 772 773 dev_dbg(priv->dev, "%s: size=%d\n", __func__, size); 774 775 buf = memdup_user(bytes, size); 776 if (IS_ERR(buf)) 777 return PTR_ERR(buf); 778 779 ret = calculate_sha256(priv, buf, size, digest); 780 if (ret) 781 goto leave; 782 783 p.cmd = EC_CODEC_WOV_GET_LANG; 784 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV, 785 (uint8_t *)&p, sizeof(p), 786 (uint8_t *)&r, sizeof(r)); 787 if (ret) 788 goto leave; 789 790 if (memcmp(digest, r.hash, SHA256_DIGEST_SIZE) == 0) { 791 dev_dbg(priv->dev, "not updated"); 792 goto leave; 793 } 794 795 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM)) 796 ret = wov_set_lang_shm(priv, buf, size, digest); 797 else 798 ret = wov_set_lang(priv, buf, size, digest); 799 800 leave: 801 kfree(buf); 802 return ret; 803 } 804 805 static struct snd_kcontrol_new wov_controls[] = { 806 SOC_SINGLE_BOOL_EXT("Wake-on-Voice Switch", 0, 807 wov_enable_get, wov_enable_put), 808 SND_SOC_BYTES_TLV("Hotword Model", 0x11000, NULL, 809 wov_hotword_model_put), 810 }; 811 812 static struct snd_soc_dai_driver wov_dai_driver = { 813 .name = "Wake on Voice", 814 .capture = { 815 .stream_name = "WoV Capture", 816 .channels_min = 1, 817 .channels_max = 1, 818 .rates = SNDRV_PCM_RATE_16000, 819 .formats = SNDRV_PCM_FMTBIT_S16_LE, 820 }, 821 }; 822 823 static int wov_host_event(struct notifier_block *nb, 824 unsigned long queued_during_suspend, void *notify) 825 { 826 struct cros_ec_codec_priv *priv = 827 container_of(nb, struct cros_ec_codec_priv, wov_notifier); 828 u32 host_event; 829 830 dev_dbg(priv->dev, "%s\n", __func__); 831 832 host_event = cros_ec_get_host_event(priv->ec_device); 833 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_WOV)) { 834 schedule_delayed_work(&priv->wov_copy_work, 0); 835 return NOTIFY_OK; 836 } else { 837 return NOTIFY_DONE; 838 } 839 } 840 841 static int wov_probe(struct snd_soc_component *component) 842 { 843 struct cros_ec_codec_priv *priv = 844 snd_soc_component_get_drvdata(component); 845 int ret; 846 847 mutex_init(&priv->wov_dma_lock); 848 INIT_DELAYED_WORK(&priv->wov_copy_work, wov_copy_work); 849 850 priv->wov_notifier.notifier_call = wov_host_event; 851 ret = blocking_notifier_chain_register( 852 &priv->ec_device->event_notifier, &priv->wov_notifier); 853 if (ret) 854 return ret; 855 856 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM)) { 857 priv->wov_lang_shm_p = wov_map_shm(priv, 858 EC_CODEC_SHM_ID_WOV_LANG, 859 &priv->wov_lang_shm_len, 860 &priv->wov_lang_shm_type); 861 if (!priv->wov_lang_shm_p) 862 return -EFAULT; 863 } 864 865 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM)) { 866 priv->wov_audio_shm_p = wov_map_shm(priv, 867 EC_CODEC_SHM_ID_WOV_AUDIO, 868 &priv->wov_audio_shm_len, 869 &priv->wov_audio_shm_type); 870 if (!priv->wov_audio_shm_p) 871 return -EFAULT; 872 } 873 874 return dmic_probe(component); 875 } 876 877 static void wov_remove(struct snd_soc_component *component) 878 { 879 struct cros_ec_codec_priv *priv = 880 snd_soc_component_get_drvdata(component); 881 882 blocking_notifier_chain_unregister( 883 &priv->ec_device->event_notifier, &priv->wov_notifier); 884 } 885 886 static int wov_pcm_open(struct snd_soc_component *component, 887 struct snd_pcm_substream *substream) 888 { 889 static const struct snd_pcm_hardware hw_param = { 890 .info = SNDRV_PCM_INFO_MMAP | 891 SNDRV_PCM_INFO_INTERLEAVED | 892 SNDRV_PCM_INFO_MMAP_VALID, 893 .formats = SNDRV_PCM_FMTBIT_S16_LE, 894 .rates = SNDRV_PCM_RATE_16000, 895 .channels_min = 1, 896 .channels_max = 1, 897 .period_bytes_min = PAGE_SIZE, 898 .period_bytes_max = 0x20000 / 8, 899 .periods_min = 8, 900 .periods_max = 8, 901 .buffer_bytes_max = 0x20000, 902 }; 903 904 return snd_soc_set_runtime_hwparams(substream, &hw_param); 905 } 906 907 static int wov_pcm_hw_params(struct snd_soc_component *component, 908 struct snd_pcm_substream *substream, 909 struct snd_pcm_hw_params *hw_params) 910 { 911 struct cros_ec_codec_priv *priv = 912 snd_soc_component_get_drvdata(component); 913 914 mutex_lock(&priv->wov_dma_lock); 915 priv->wov_substream = substream; 916 priv->wov_rp = priv->wov_wp = 0; 917 priv->wov_dma_offset = 0; 918 priv->wov_burst_read = true; 919 mutex_unlock(&priv->wov_dma_lock); 920 921 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); 922 } 923 924 static int wov_pcm_hw_free(struct snd_soc_component *component, 925 struct snd_pcm_substream *substream) 926 { 927 struct cros_ec_codec_priv *priv = 928 snd_soc_component_get_drvdata(component); 929 930 mutex_lock(&priv->wov_dma_lock); 931 wov_queue_dequeue(priv, wov_queue_size(priv)); 932 priv->wov_substream = NULL; 933 mutex_unlock(&priv->wov_dma_lock); 934 935 cancel_delayed_work_sync(&priv->wov_copy_work); 936 937 return snd_pcm_lib_free_pages(substream); 938 } 939 940 static snd_pcm_uframes_t wov_pcm_pointer(struct snd_soc_component *component, 941 struct snd_pcm_substream *substream) 942 { 943 struct snd_pcm_runtime *runtime = substream->runtime; 944 struct cros_ec_codec_priv *priv = 945 snd_soc_component_get_drvdata(component); 946 947 return bytes_to_frames(runtime, priv->wov_dma_offset); 948 } 949 950 static int wov_pcm_new(struct snd_soc_component *component, 951 struct snd_soc_pcm_runtime *rtd) 952 { 953 snd_pcm_lib_preallocate_pages_for_all(rtd->pcm, SNDRV_DMA_TYPE_VMALLOC, 954 NULL, 0, 0); 955 return 0; 956 } 957 958 static const struct snd_soc_component_driver wov_component_driver = { 959 .probe = wov_probe, 960 .remove = wov_remove, 961 .controls = wov_controls, 962 .num_controls = ARRAY_SIZE(wov_controls), 963 .open = wov_pcm_open, 964 .hw_params = wov_pcm_hw_params, 965 .hw_free = wov_pcm_hw_free, 966 .pointer = wov_pcm_pointer, 967 .pcm_construct = wov_pcm_new, 968 }; 969 970 static int cros_ec_codec_platform_probe(struct platform_device *pdev) 971 { 972 struct device *dev = &pdev->dev; 973 struct cros_ec_device *ec_device = dev_get_drvdata(pdev->dev.parent); 974 struct cros_ec_codec_priv *priv; 975 struct ec_param_ec_codec p; 976 struct ec_response_ec_codec_get_capabilities r; 977 int ret; 978 #ifdef CONFIG_OF 979 struct device_node *node; 980 struct resource res; 981 u64 ec_shm_size; 982 const __be32 *regaddr_p; 983 #endif 984 985 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 986 if (!priv) 987 return -ENOMEM; 988 989 #ifdef CONFIG_OF 990 regaddr_p = of_get_address(dev->of_node, 0, &ec_shm_size, NULL); 991 if (regaddr_p) { 992 priv->ec_shm_addr = of_read_number(regaddr_p, 2); 993 priv->ec_shm_len = ec_shm_size; 994 995 dev_dbg(dev, "ec_shm_addr=%#llx len=%#x\n", 996 priv->ec_shm_addr, priv->ec_shm_len); 997 } 998 999 node = of_parse_phandle(dev->of_node, "memory-region", 0); 1000 if (node) { 1001 ret = of_address_to_resource(node, 0, &res); 1002 if (!ret) { 1003 priv->ap_shm_phys_addr = res.start; 1004 priv->ap_shm_len = resource_size(&res); 1005 priv->ap_shm_addr = 1006 (uint64_t)(uintptr_t)devm_ioremap_wc( 1007 dev, priv->ap_shm_phys_addr, 1008 priv->ap_shm_len); 1009 priv->ap_shm_last_alloc = priv->ap_shm_phys_addr; 1010 1011 dev_dbg(dev, "ap_shm_phys_addr=%#llx len=%#x\n", 1012 priv->ap_shm_phys_addr, priv->ap_shm_len); 1013 } 1014 } 1015 #endif 1016 1017 priv->dev = dev; 1018 priv->ec_device = ec_device; 1019 atomic_set(&priv->dmic_probed, 0); 1020 1021 p.cmd = EC_CODEC_GET_CAPABILITIES; 1022 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC, 1023 (uint8_t *)&p, sizeof(p), 1024 (uint8_t *)&r, sizeof(r)); 1025 if (ret) { 1026 dev_err(dev, "failed to EC_CODEC_GET_CAPABILITIES\n"); 1027 return ret; 1028 } 1029 priv->ec_capabilities = r.capabilities; 1030 1031 platform_set_drvdata(pdev, priv); 1032 1033 ret = devm_snd_soc_register_component(dev, &i2s_rx_component_driver, 1034 &i2s_rx_dai_driver, 1); 1035 if (ret) 1036 return ret; 1037 1038 return devm_snd_soc_register_component(dev, &wov_component_driver, 1039 &wov_dai_driver, 1); 1040 } 1041 1042 #ifdef CONFIG_OF 1043 static const struct of_device_id cros_ec_codec_of_match[] = { 1044 { .compatible = "google,cros-ec-codec" }, 1045 {}, 1046 }; 1047 MODULE_DEVICE_TABLE(of, cros_ec_codec_of_match); 1048 #endif 1049 1050 static struct platform_driver cros_ec_codec_platform_driver = { 1051 .driver = { 1052 .name = "cros-ec-codec", 1053 .of_match_table = of_match_ptr(cros_ec_codec_of_match), 1054 }, 1055 .probe = cros_ec_codec_platform_probe, 1056 }; 1057 1058 module_platform_driver(cros_ec_codec_platform_driver); 1059 1060 MODULE_LICENSE("GPL v2"); 1061 MODULE_DESCRIPTION("ChromeOS EC codec driver"); 1062 MODULE_AUTHOR("Cheng-Yi Chiang <cychiang@chromium.org>"); 1063 MODULE_ALIAS("platform:cros-ec-codec"); 1064