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