cros_ec_codec.c 27.1 KB
Newer Older
1 2
// SPDX-License-Identifier: GPL-2.0
/*
3 4 5
 * Copyright 2019 Google, Inc.
 *
 * ChromeOS Embedded Controller codec driver.
6 7 8 9 10
 *
 * This driver uses the cros-ec interface to communicate with the ChromeOS
 * EC for audio function.
 */

T
Tzung-Bi Shih 已提交
11 12
#include <crypto/hash.h>
#include <crypto/sha.h>
13
#include <linux/acpi.h>
14 15
#include <linux/delay.h>
#include <linux/device.h>
T
Tzung-Bi Shih 已提交
16 17
#include <linux/io.h>
#include <linux/jiffies.h>
18 19
#include <linux/kernel.h>
#include <linux/module.h>
T
Tzung-Bi Shih 已提交
20 21
#include <linux/of.h>
#include <linux/of_address.h>
22 23
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
24 25 26 27 28 29
#include <linux/platform_device.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/tlv.h>

30
struct cros_ec_codec_priv {
31 32
	struct device *dev;
	struct cros_ec_device *ec_device;
T
Tzung-Bi Shih 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

	/* common */
	uint32_t ec_capabilities;

	uint64_t ec_shm_addr;
	uint32_t ec_shm_len;

	uint64_t ap_shm_phys_addr;
	uint32_t ap_shm_len;
	uint64_t ap_shm_addr;
	uint64_t ap_shm_last_alloc;

	/* DMIC */
	atomic_t dmic_probed;

	/* WoV */
	bool wov_enabled;
	uint8_t *wov_audio_shm_p;
	uint32_t wov_audio_shm_len;
	uint8_t wov_audio_shm_type;
	uint8_t *wov_lang_shm_p;
	uint32_t wov_lang_shm_len;
	uint8_t wov_lang_shm_type;

	struct mutex wov_dma_lock;
	uint8_t wov_buf[64000];
	uint32_t wov_rp, wov_wp;
	size_t wov_dma_offset;
	bool wov_burst_read;
	struct snd_pcm_substream *wov_substream;
	struct delayed_work wov_copy_work;
	struct notifier_block wov_notifier;
65 66
};

T
Tzung-Bi Shih 已提交
67 68 69 70 71
static int ec_codec_capable(struct cros_ec_codec_priv *priv, uint8_t cap)
{
	return priv->ec_capabilities & BIT(cap);
}

72 73 74
static int send_ec_host_command(struct cros_ec_device *ec_dev, uint32_t cmd,
				uint8_t *out, size_t outsize,
				uint8_t *in, size_t insize)
75 76
{
	int ret;
77 78 79 80 81
	struct cros_ec_command *msg;

	msg = kmalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
	if (!msg)
		return -ENOMEM;
82 83

	msg->version = 0;
84 85 86 87 88 89
	msg->command = cmd;
	msg->outsize = outsize;
	msg->insize = insize;

	if (outsize)
		memcpy(msg->data, out, outsize);
90

91 92 93
	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
	if (ret < 0)
		goto error;
94

95 96
	if (insize)
		memcpy(in, msg->data, insize);
97

98 99 100
	ret = 0;
error:
	kfree(msg);
101 102 103
	return ret;
}

T
Tzung-Bi Shih 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
static int calculate_sha256(struct cros_ec_codec_priv *priv,
			    uint8_t *buf, uint32_t size, uint8_t *digest)
{
	struct crypto_shash *tfm;

	tfm = crypto_alloc_shash("sha256", CRYPTO_ALG_TYPE_SHASH, 0);
	if (IS_ERR(tfm)) {
		dev_err(priv->dev, "can't alloc shash\n");
		return PTR_ERR(tfm);
	}

	{
		SHASH_DESC_ON_STACK(desc, tfm);

		desc->tfm = tfm;

		crypto_shash_digest(desc, buf, size, digest);
		shash_desc_zero(desc);
	}

	crypto_free_shash(tfm);

#ifdef DEBUG
	{
		char digest_str[65];

		bin2hex(digest_str, digest, 32);
		digest_str[64] = 0;
		dev_dbg(priv->dev, "hash=%s\n", digest_str);
	}
#endif

	return 0;
}

139 140
static int dmic_get_gain(struct snd_kcontrol *kcontrol,
			 struct snd_ctl_elem_value *ucontrol)
141
{
142 143 144
	struct snd_soc_component *component =
		snd_soc_kcontrol_component(kcontrol);
	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 151
	p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX;
	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 154 155 156
				   (uint8_t *)&p, sizeof(p),
				   (uint8_t *)&r, sizeof(r));
	if (ret < 0)
		return ret;
157
	ucontrol->value.integer.value[0] = r.gain;
158

159 160 161 162 163 164 165 166
	p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX;
	p.get_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1;
	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
				   (uint8_t *)&p, sizeof(p),
				   (uint8_t *)&r, sizeof(r));
	if (ret < 0)
		return ret;
	ucontrol->value.integer.value[1] = r.gain;
167

168
	return 0;
169 170
}

171 172
static int dmic_put_gain(struct snd_kcontrol *kcontrol,
			 struct snd_ctl_elem_value *ucontrol)
173
{
174 175 176 177 178 179 180 181 182
	struct snd_soc_component *component =
		snd_soc_kcontrol_component(kcontrol);
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);
	struct soc_mixer_control *control =
		(struct soc_mixer_control *)kcontrol->private_value;
	int max_dmic_gain = control->max;
	int left = ucontrol->value.integer.value[0];
	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 188
		return -EINVAL;

189
	dev_dbg(component->dev, "set mic gain to %u, %u\n", left, right);
190

191 192 193 194 195 196 197 198 199 200 201
	p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX;
	p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_0;
	p.set_gain_idx_param.gain = left;
	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
				   (uint8_t *)&p, sizeof(p), NULL, 0);
	if (ret < 0)
		return ret;

	p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX;
	p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1;
	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 209 210
enum {
	DMIC_CTL_GAIN = 0,
};
211

212 213 214 215 216 217
static struct snd_kcontrol_new dmic_controls[] = {
	[DMIC_CTL_GAIN] =
		SOC_DOUBLE_EXT_TLV("EC Mic Gain", SND_SOC_NOPM, SND_SOC_NOPM,
				   0, 0, 0, dmic_get_gain, dmic_put_gain,
				   dmic_gain_tlv),
};
218

219 220 221 222 223 224
static int dmic_probe(struct snd_soc_component *component)
{
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);
	struct device *dev = priv->dev;
	struct soc_mixer_control *control;
225 226 227
	struct ec_param_ec_codec_dmic p;
	struct ec_response_ec_codec_dmic_get_max_gain r;
	int ret;
228

T
Tzung-Bi Shih 已提交
229 230 231
	if (!atomic_add_unless(&priv->dmic_probed, 1, 1))
		return 0;

232 233 234 235 236 237 238 239
	p.cmd = EC_CODEC_DMIC_GET_MAX_GAIN;

	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
				   (uint8_t *)&p, sizeof(p),
				   (uint8_t *)&r, sizeof(r));
	if (ret < 0) {
		dev_warn(dev, "get_max_gain() unsupported\n");
		return 0;
240 241
	}

242 243
	dev_dbg(dev, "max gain = %d\n", r.max_gain);

244 245
	control = (struct soc_mixer_control *)
		dmic_controls[DMIC_CTL_GAIN].private_value;
246 247
	control->max = r.max_gain;
	control->platform_max = r.max_gain;
248 249 250 251 252

	return snd_soc_add_component_controls(component,
			&dmic_controls[DMIC_CTL_GAIN], 1);
}

253 254 255
static int i2s_rx_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params,
			    struct snd_soc_dai *dai)
256 257
{
	struct snd_soc_component *component = dai->component;
258 259 260 261
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);
	struct ec_param_ec_codec_i2s_rx p;
	enum ec_codec_i2s_rx_sample_depth depth;
262 263
	int ret;

264
	if (params_rate(params) != 48000)
265 266 267 268
		return -EINVAL;

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
269
		depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_16;
270 271
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
272
		depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_24;
273 274 275 276 277
		break;
	default:
		return -EINVAL;
	}

278
	dev_dbg(component->dev, "set depth to %u\n", depth);
279

280 281 282 283
	p.cmd = EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH;
	p.set_sample_depth_param.depth = depth;
	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
				   (uint8_t *)&p, sizeof(p), NULL, 0);
284 285 286
	if (ret < 0)
		return ret;

287 288
	dev_dbg(component->dev, "set bclk to %u\n",
		snd_soc_params_to_bclk(params));
289

290 291 292 293
	p.cmd = EC_CODEC_I2S_RX_SET_BCLK;
	p.set_bclk_param.bclk = snd_soc_params_to_bclk(params);
	return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
				    (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 299
	struct snd_soc_component *component = dai->component;
	struct cros_ec_codec_priv *priv =
300
		snd_soc_component_get_drvdata(component);
301 302
	struct ec_param_ec_codec_i2s_rx p;
	enum ec_codec_i2s_rx_daifmt daifmt;
303

304 305 306 307
	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBS_CFS:
		break;
	default:
308
		return -EINVAL;
309
	}
310

311 312 313 314 315 316
	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_NB_NF:
		break;
	default:
		return -EINVAL;
	}
317

318 319 320 321 322 323 324 325 326 327 328 329 330
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		daifmt = EC_CODEC_I2S_RX_DAIFMT_I2S;
		break;
	case SND_SOC_DAIFMT_RIGHT_J:
		daifmt = EC_CODEC_I2S_RX_DAIFMT_RIGHT_J;
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		daifmt = EC_CODEC_I2S_RX_DAIFMT_LEFT_J;
		break;
	default:
		return -EINVAL;
	}
331

332
	dev_dbg(component->dev, "set format to %u\n", daifmt);
333

334 335 336 337
	p.cmd = EC_CODEC_I2S_RX_SET_DAIFMT;
	p.set_daifmt_param.daifmt = daifmt;
	return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
				    (uint8_t *)&p, sizeof(p), NULL, 0);
338 339
}

340 341 342 343 344 345 346
static const struct snd_soc_dai_ops i2s_rx_dai_ops = {
	.hw_params = i2s_rx_hw_params,
	.set_fmt = i2s_rx_set_fmt,
};

static int i2s_rx_event(struct snd_soc_dapm_widget *w,
			struct snd_kcontrol *kcontrol, int event)
347 348 349
{
	struct snd_soc_component *component =
		snd_soc_dapm_to_component(w->dapm);
350 351 352
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);
	struct ec_param_ec_codec_i2s_rx p;
353 354 355

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
356 357 358
		dev_dbg(component->dev, "enable I2S RX\n");
		p.cmd = EC_CODEC_I2S_RX_ENABLE;
		break;
359
	case SND_SOC_DAPM_PRE_PMD:
360 361 362 363 364
		dev_dbg(component->dev, "disable I2S RX\n");
		p.cmd = EC_CODEC_I2S_RX_DISABLE;
		break;
	default:
		return 0;
365 366
	}

367 368
	return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
				    (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 376
	SND_SOC_DAPM_AIF_OUT("I2S RX", "I2S Capture", 0, SND_SOC_NOPM, 0, 0),
};
377

378 379 380
static struct snd_soc_dapm_route i2s_rx_dapm_routes[] = {
	{"I2S RX", NULL, "DMIC"},
	{"I2S RX", NULL, "I2S RX Enable"},
381 382
};

383 384 385 386 387 388 389 390 391 392 393
static struct snd_soc_dai_driver i2s_rx_dai_driver = {
	.name = "EC Codec I2S RX",
	.capture = {
		.stream_name = "I2S Capture",
		.channels_min = 2,
		.channels_max = 2,
		.rates = SNDRV_PCM_RATE_48000,
		.formats = SNDRV_PCM_FMTBIT_S16_LE |
			SNDRV_PCM_FMTBIT_S24_LE,
	},
	.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 402 403 404 405 406
static const struct snd_soc_component_driver i2s_rx_component_driver = {
	.probe			= i2s_rx_probe,
	.dapm_widgets		= i2s_rx_dapm_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(i2s_rx_dapm_widgets),
	.dapm_routes		= i2s_rx_dapm_routes,
	.num_dapm_routes	= ARRAY_SIZE(i2s_rx_dapm_routes),
407 408
};

T
Tzung-Bi Shih 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
static void *wov_map_shm(struct cros_ec_codec_priv *priv,
			 uint8_t shm_id, uint32_t *len, uint8_t *type)
{
	struct ec_param_ec_codec p;
	struct ec_response_ec_codec_get_shm_addr r;
	uint32_t req, offset;

	p.cmd = EC_CODEC_GET_SHM_ADDR;
	p.get_shm_addr_param.shm_id = shm_id;
	if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC,
				 (uint8_t *)&p, sizeof(p),
				 (uint8_t *)&r, sizeof(r)) < 0) {
		dev_err(priv->dev, "failed to EC_CODEC_GET_SHM_ADDR\n");
		return NULL;
	}

	dev_dbg(priv->dev, "phys_addr=%#llx, len=%#x\n", r.phys_addr, r.len);

	*len = r.len;
	*type = r.type;

	switch (r.type) {
	case EC_CODEC_SHM_TYPE_EC_RAM:
		return (void __force *)devm_ioremap_wc(priv->dev,
				r.phys_addr + priv->ec_shm_addr, r.len);
	case EC_CODEC_SHM_TYPE_SYSTEM_RAM:
		if (r.phys_addr) {
			dev_err(priv->dev, "unknown status\n");
			return NULL;
		}

		req = round_up(r.len, PAGE_SIZE);
		dev_dbg(priv->dev, "round up from %u to %u\n", r.len, req);

		if (priv->ap_shm_last_alloc + req >
		    priv->ap_shm_phys_addr + priv->ap_shm_len) {
			dev_err(priv->dev, "insufficient space for AP SHM\n");
			return NULL;
		}

		dev_dbg(priv->dev, "alloc AP SHM addr=%#llx, len=%#x\n",
			priv->ap_shm_last_alloc, req);

		p.cmd = EC_CODEC_SET_SHM_ADDR;
		p.set_shm_addr_param.phys_addr = priv->ap_shm_last_alloc;
		p.set_shm_addr_param.len = req;
		p.set_shm_addr_param.shm_id = shm_id;
		if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC,
					 (uint8_t *)&p, sizeof(p),
					 NULL, 0) < 0) {
			dev_err(priv->dev, "failed to EC_CODEC_SET_SHM_ADDR\n");
			return NULL;
		}

		/*
		 * Note: EC codec only requests for `r.len' but we allocate
		 * round up PAGE_SIZE `req'.
		 */
		offset = priv->ap_shm_last_alloc - priv->ap_shm_phys_addr;
		priv->ap_shm_last_alloc += req;

		return (void *)(uintptr_t)(priv->ap_shm_addr + offset);
	default:
		return NULL;
	}
}

static bool wov_queue_full(struct cros_ec_codec_priv *priv)
{
	return ((priv->wov_wp + 1) % sizeof(priv->wov_buf)) == priv->wov_rp;
}

static size_t wov_queue_size(struct cros_ec_codec_priv *priv)
{
	if (priv->wov_wp >= priv->wov_rp)
		return priv->wov_wp - priv->wov_rp;
	else
		return sizeof(priv->wov_buf) - priv->wov_rp + priv->wov_wp;
}

static void wov_queue_dequeue(struct cros_ec_codec_priv *priv, size_t len)
{
	struct snd_pcm_runtime *runtime = priv->wov_substream->runtime;
	size_t req;

	while (len) {
		req = min(len, runtime->dma_bytes - priv->wov_dma_offset);
		if (priv->wov_wp >= priv->wov_rp)
			req = min(req, (size_t)priv->wov_wp - priv->wov_rp);
		else
			req = min(req, sizeof(priv->wov_buf) - priv->wov_rp);

		memcpy(runtime->dma_area + priv->wov_dma_offset,
		       priv->wov_buf + priv->wov_rp, req);

		priv->wov_dma_offset += req;
		if (priv->wov_dma_offset == runtime->dma_bytes)
			priv->wov_dma_offset = 0;

		priv->wov_rp += req;
		if (priv->wov_rp == sizeof(priv->wov_buf))
			priv->wov_rp = 0;

		len -= req;
	}

	snd_pcm_period_elapsed(priv->wov_substream);
}

static void wov_queue_try_dequeue(struct cros_ec_codec_priv *priv)
{
	size_t period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream);

	while (period_bytes && wov_queue_size(priv) >= period_bytes) {
		wov_queue_dequeue(priv, period_bytes);
		period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream);
	}
}

static void wov_queue_enqueue(struct cros_ec_codec_priv *priv,
			      uint8_t *addr, size_t len, bool iomem)
{
	size_t req;

	while (len) {
		if (wov_queue_full(priv)) {
			wov_queue_try_dequeue(priv);

			if (wov_queue_full(priv)) {
				dev_err(priv->dev, "overrun detected\n");
				return;
			}
		}

		if (priv->wov_wp >= priv->wov_rp)
			req = sizeof(priv->wov_buf) - priv->wov_wp;
		else
			/* Note: waste 1-byte to differentiate full and empty */
			req = priv->wov_rp - priv->wov_wp - 1;
		req = min(req, len);

		if (iomem)
			memcpy_fromio(priv->wov_buf + priv->wov_wp,
				      (void __force __iomem *)addr, req);
		else
			memcpy(priv->wov_buf + priv->wov_wp, addr, req);

		priv->wov_wp += req;
		if (priv->wov_wp == sizeof(priv->wov_buf))
			priv->wov_wp = 0;

		addr += req;
		len -= req;
	}

	wov_queue_try_dequeue(priv);
}

static int wov_read_audio_shm(struct cros_ec_codec_priv *priv)
{
	struct ec_param_ec_codec_wov p;
	struct ec_response_ec_codec_wov_read_audio_shm r;
	int ret;

	p.cmd = EC_CODEC_WOV_READ_AUDIO_SHM;
	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
				   (uint8_t *)&p, sizeof(p),
				   (uint8_t *)&r, sizeof(r));
	if (ret) {
		dev_err(priv->dev, "failed to EC_CODEC_WOV_READ_AUDIO_SHM\n");
		return ret;
	}

	if (!r.len)
		dev_dbg(priv->dev, "no data, sleep\n");
	else
		wov_queue_enqueue(priv, priv->wov_audio_shm_p + r.offset, r.len,
			priv->wov_audio_shm_type == EC_CODEC_SHM_TYPE_EC_RAM);
	return -EAGAIN;
}

static int wov_read_audio(struct cros_ec_codec_priv *priv)
{
	struct ec_param_ec_codec_wov p;
	struct ec_response_ec_codec_wov_read_audio r;
	int remain = priv->wov_burst_read ? 16000 : 320;
	int ret;

	while (remain >= 0) {
		p.cmd = EC_CODEC_WOV_READ_AUDIO;
		ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
					   (uint8_t *)&p, sizeof(p),
					   (uint8_t *)&r, sizeof(r));
		if (ret) {
			dev_err(priv->dev,
				"failed to EC_CODEC_WOV_READ_AUDIO\n");
			return ret;
		}

		if (!r.len) {
			dev_dbg(priv->dev, "no data, sleep\n");
			priv->wov_burst_read = false;
			break;
		}

		wov_queue_enqueue(priv, r.buf, r.len, false);
		remain -= r.len;
	}

	return -EAGAIN;
}

static void wov_copy_work(struct work_struct *w)
{
	struct cros_ec_codec_priv *priv =
		container_of(w, struct cros_ec_codec_priv, wov_copy_work.work);
	int ret;

	mutex_lock(&priv->wov_dma_lock);
	if (!priv->wov_substream) {
		dev_warn(priv->dev, "no pcm substream\n");
		goto leave;
	}

	if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM))
		ret = wov_read_audio_shm(priv);
	else
		ret = wov_read_audio(priv);

	if (ret == -EAGAIN)
		schedule_delayed_work(&priv->wov_copy_work,
				      msecs_to_jiffies(10));
	else if (ret)
		dev_err(priv->dev, "failed to read audio data\n");
leave:
	mutex_unlock(&priv->wov_dma_lock);
}

static int wov_enable_get(struct snd_kcontrol *kcontrol,
			  struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
	struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c);

	ucontrol->value.integer.value[0] = priv->wov_enabled;
	return 0;
}

static int wov_enable_put(struct snd_kcontrol *kcontrol,
			  struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
	struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c);
	int enabled = ucontrol->value.integer.value[0];
	struct ec_param_ec_codec_wov p;
	int ret;

	if (priv->wov_enabled != enabled) {
		if (enabled)
			p.cmd = EC_CODEC_WOV_ENABLE;
		else
			p.cmd = EC_CODEC_WOV_DISABLE;

		ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
					   (uint8_t *)&p, sizeof(p), NULL, 0);
		if (ret) {
			dev_err(priv->dev, "failed to %s wov\n",
				enabled ? "enable" : "disable");
			return ret;
		}

		priv->wov_enabled = enabled;
	}

	return 0;
}

static int wov_set_lang_shm(struct cros_ec_codec_priv *priv,
			    uint8_t *buf, size_t size, uint8_t *digest)
{
	struct ec_param_ec_codec_wov p;
	struct ec_param_ec_codec_wov_set_lang_shm *pp = &p.set_lang_shm_param;
	int ret;

	if (size > priv->wov_lang_shm_len) {
		dev_err(priv->dev, "no enough SHM size: %d\n",
			priv->wov_lang_shm_len);
		return -EIO;
	}

	switch (priv->wov_lang_shm_type) {
	case EC_CODEC_SHM_TYPE_EC_RAM:
		memcpy_toio((void __force __iomem *)priv->wov_lang_shm_p,
			    buf, size);
		memset_io((void __force __iomem *)priv->wov_lang_shm_p + size,
			  0, priv->wov_lang_shm_len - size);
		break;
	case EC_CODEC_SHM_TYPE_SYSTEM_RAM:
		memcpy(priv->wov_lang_shm_p, buf, size);
		memset(priv->wov_lang_shm_p + size, 0,
		       priv->wov_lang_shm_len - size);

		/* make sure write to memory before calling host command */
		wmb();
		break;
	}

	p.cmd = EC_CODEC_WOV_SET_LANG_SHM;
	memcpy(pp->hash, digest, SHA256_DIGEST_SIZE);
	pp->total_len = size;
	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
				   (uint8_t *)&p, sizeof(p), NULL, 0);
	if (ret) {
		dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG_SHM\n");
		return ret;
	}

	return 0;
}

static int wov_set_lang(struct cros_ec_codec_priv *priv,
			uint8_t *buf, size_t size, uint8_t *digest)
{
	struct ec_param_ec_codec_wov p;
	struct ec_param_ec_codec_wov_set_lang *pp = &p.set_lang_param;
	size_t i, req;
	int ret;

	for (i = 0; i < size; i += req) {
		req = min(size - i, ARRAY_SIZE(pp->buf));

		p.cmd = EC_CODEC_WOV_SET_LANG;
		memcpy(pp->hash, digest, SHA256_DIGEST_SIZE);
		pp->total_len = size;
		pp->offset = i;
		memcpy(pp->buf, buf + i, req);
		pp->len = req;
		ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
					   (uint8_t *)&p, sizeof(p), NULL, 0);
		if (ret) {
			dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG\n");
			return ret;
		}
	}

	return 0;
}

static int wov_hotword_model_put(struct snd_kcontrol *kcontrol,
				 const unsigned int __user *bytes,
				 unsigned int size)
{
	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);
	struct ec_param_ec_codec_wov p;
	struct ec_response_ec_codec_wov_get_lang r;
	uint8_t digest[SHA256_DIGEST_SIZE];
	uint8_t *buf;
	int ret;

	/* Skips the TLV header. */
	bytes += 2;
	size -= 8;

	dev_dbg(priv->dev, "%s: size=%d\n", __func__, size);

	buf = memdup_user(bytes, size);
	if (IS_ERR(buf))
		return PTR_ERR(buf);

	ret = calculate_sha256(priv, buf, size, digest);
	if (ret)
		goto leave;

	p.cmd = EC_CODEC_WOV_GET_LANG;
	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
				   (uint8_t *)&p, sizeof(p),
				   (uint8_t *)&r, sizeof(r));
	if (ret)
		goto leave;

	if (memcmp(digest, r.hash, SHA256_DIGEST_SIZE) == 0) {
		dev_dbg(priv->dev, "not updated");
		goto leave;
	}

	if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM))
		ret = wov_set_lang_shm(priv, buf, size, digest);
	else
		ret = wov_set_lang(priv, buf, size, digest);

leave:
	kfree(buf);
	return ret;
}

static struct snd_kcontrol_new wov_controls[] = {
	SOC_SINGLE_BOOL_EXT("Wake-on-Voice Switch", 0,
			    wov_enable_get, wov_enable_put),
	SND_SOC_BYTES_TLV("Hotword Model", 0x11000, NULL,
			  wov_hotword_model_put),
};

static struct snd_soc_dai_driver wov_dai_driver = {
	.name = "Wake on Voice",
	.capture = {
		.stream_name = "WoV Capture",
		.channels_min = 1,
		.channels_max = 1,
		.rates = SNDRV_PCM_RATE_16000,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,
	},
};

static int wov_host_event(struct notifier_block *nb,
			  unsigned long queued_during_suspend, void *notify)
{
	struct cros_ec_codec_priv *priv =
		container_of(nb, struct cros_ec_codec_priv, wov_notifier);
	u32 host_event;

	dev_dbg(priv->dev, "%s\n", __func__);

	host_event = cros_ec_get_host_event(priv->ec_device);
	if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_WOV)) {
		schedule_delayed_work(&priv->wov_copy_work, 0);
		return NOTIFY_OK;
	} else {
		return NOTIFY_DONE;
	}
}

static int wov_probe(struct snd_soc_component *component)
{
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);
	int ret;

	mutex_init(&priv->wov_dma_lock);
	INIT_DELAYED_WORK(&priv->wov_copy_work, wov_copy_work);

	priv->wov_notifier.notifier_call = wov_host_event;
	ret = blocking_notifier_chain_register(
			&priv->ec_device->event_notifier, &priv->wov_notifier);
	if (ret)
		return ret;

	if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM)) {
		priv->wov_lang_shm_p = wov_map_shm(priv,
				EC_CODEC_SHM_ID_WOV_LANG,
				&priv->wov_lang_shm_len,
				&priv->wov_lang_shm_type);
		if (!priv->wov_lang_shm_p)
			return -EFAULT;
	}

	if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM)) {
		priv->wov_audio_shm_p = wov_map_shm(priv,
				EC_CODEC_SHM_ID_WOV_AUDIO,
				&priv->wov_audio_shm_len,
				&priv->wov_audio_shm_type);
		if (!priv->wov_audio_shm_p)
			return -EFAULT;
	}

	return dmic_probe(component);
}

static void wov_remove(struct snd_soc_component *component)
{
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);

	blocking_notifier_chain_unregister(
			&priv->ec_device->event_notifier, &priv->wov_notifier);
}

static int wov_pcm_open(struct snd_soc_component *component,
			struct snd_pcm_substream *substream)
{
	static const struct snd_pcm_hardware hw_param = {
		.info = SNDRV_PCM_INFO_MMAP |
			SNDRV_PCM_INFO_INTERLEAVED |
			SNDRV_PCM_INFO_MMAP_VALID,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,
		.rates = SNDRV_PCM_RATE_16000,
		.channels_min = 1,
		.channels_max = 1,
		.period_bytes_min = PAGE_SIZE,
		.period_bytes_max = 0x20000 / 8,
		.periods_min = 8,
		.periods_max = 8,
		.buffer_bytes_max = 0x20000,
	};

	return snd_soc_set_runtime_hwparams(substream, &hw_param);
}

static int wov_pcm_hw_params(struct snd_soc_component *component,
			     struct snd_pcm_substream *substream,
			     struct snd_pcm_hw_params *hw_params)
{
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);

	mutex_lock(&priv->wov_dma_lock);
	priv->wov_substream = substream;
	priv->wov_rp = priv->wov_wp = 0;
	priv->wov_dma_offset = 0;
	priv->wov_burst_read = true;
	mutex_unlock(&priv->wov_dma_lock);

922
	return 0;
T
Tzung-Bi Shih 已提交
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
}

static int wov_pcm_hw_free(struct snd_soc_component *component,
			   struct snd_pcm_substream *substream)
{
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);

	mutex_lock(&priv->wov_dma_lock);
	wov_queue_dequeue(priv, wov_queue_size(priv));
	priv->wov_substream = NULL;
	mutex_unlock(&priv->wov_dma_lock);

	cancel_delayed_work_sync(&priv->wov_copy_work);

938
	return 0;
T
Tzung-Bi Shih 已提交
939 940 941 942 943 944 945 946 947 948 949 950
}

static snd_pcm_uframes_t wov_pcm_pointer(struct snd_soc_component *component,
					 struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct cros_ec_codec_priv *priv =
		snd_soc_component_get_drvdata(component);

	return bytes_to_frames(runtime, priv->wov_dma_offset);
}

951 952
static int wov_pcm_new(struct snd_soc_component *component,
		       struct snd_soc_pcm_runtime *rtd)
T
Tzung-Bi Shih 已提交
953
{
954 955
	snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_VMALLOC,
				       NULL, 0, 0);
956
	return 0;
T
Tzung-Bi Shih 已提交
957 958 959 960 961 962 963 964 965 966 967
}

static const struct snd_soc_component_driver wov_component_driver = {
	.probe		= wov_probe,
	.remove		= wov_remove,
	.controls	= wov_controls,
	.num_controls	= ARRAY_SIZE(wov_controls),
	.open		= wov_pcm_open,
	.hw_params	= wov_pcm_hw_params,
	.hw_free	= wov_pcm_hw_free,
	.pointer	= wov_pcm_pointer,
968
	.pcm_construct	= wov_pcm_new,
T
Tzung-Bi Shih 已提交
969 970
};

971
static int cros_ec_codec_platform_probe(struct platform_device *pdev)
972
{
973 974 975
	struct device *dev = &pdev->dev;
	struct cros_ec_device *ec_device = dev_get_drvdata(pdev->dev.parent);
	struct cros_ec_codec_priv *priv;
T
Tzung-Bi Shih 已提交
976 977 978 979 980 981 982 983 984
	struct ec_param_ec_codec p;
	struct ec_response_ec_codec_get_capabilities r;
	int ret;
#ifdef CONFIG_OF
	struct device_node *node;
	struct resource res;
	u64 ec_shm_size;
	const __be32 *regaddr_p;
#endif
985

986 987
	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
988 989
		return -ENOMEM;

T
Tzung-Bi Shih 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
#ifdef CONFIG_OF
	regaddr_p = of_get_address(dev->of_node, 0, &ec_shm_size, NULL);
	if (regaddr_p) {
		priv->ec_shm_addr = of_read_number(regaddr_p, 2);
		priv->ec_shm_len = ec_shm_size;

		dev_dbg(dev, "ec_shm_addr=%#llx len=%#x\n",
			priv->ec_shm_addr, priv->ec_shm_len);
	}

	node = of_parse_phandle(dev->of_node, "memory-region", 0);
	if (node) {
		ret = of_address_to_resource(node, 0, &res);
		if (!ret) {
			priv->ap_shm_phys_addr = res.start;
			priv->ap_shm_len = resource_size(&res);
			priv->ap_shm_addr =
				(uint64_t)(uintptr_t)devm_ioremap_wc(
					dev, priv->ap_shm_phys_addr,
					priv->ap_shm_len);
			priv->ap_shm_last_alloc = priv->ap_shm_phys_addr;

			dev_dbg(dev, "ap_shm_phys_addr=%#llx len=%#x\n",
				priv->ap_shm_phys_addr, priv->ap_shm_len);
		}
	}
#endif

1018 1019
	priv->dev = dev;
	priv->ec_device = ec_device;
T
Tzung-Bi Shih 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
	atomic_set(&priv->dmic_probed, 0);

	p.cmd = EC_CODEC_GET_CAPABILITIES;
	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC,
				   (uint8_t *)&p, sizeof(p),
				   (uint8_t *)&r, sizeof(r));
	if (ret) {
		dev_err(dev, "failed to EC_CODEC_GET_CAPABILITIES\n");
		return ret;
	}
	priv->ec_capabilities = r.capabilities;
1031

1032
	platform_set_drvdata(pdev, priv);
1033

T
Tzung-Bi Shih 已提交
1034 1035 1036 1037 1038 1039 1040
	ret = devm_snd_soc_register_component(dev, &i2s_rx_component_driver,
					      &i2s_rx_dai_driver, 1);
	if (ret)
		return ret;

	return devm_snd_soc_register_component(dev, &wov_component_driver,
					       &wov_dai_driver, 1);
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
}

#ifdef CONFIG_OF
static const struct of_device_id cros_ec_codec_of_match[] = {
	{ .compatible = "google,cros-ec-codec" },
	{},
};
MODULE_DEVICE_TABLE(of, cros_ec_codec_of_match);
#endif

1051 1052 1053 1054 1055 1056
static const struct acpi_device_id cros_ec_codec_acpi_id[] = {
	{ "GOOG0013", 0 },
	{ }
};
MODULE_DEVICE_TABLE(acpi, cros_ec_codec_acpi_id);

1057 1058
static struct platform_driver cros_ec_codec_platform_driver = {
	.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 1064 1065 1066 1067 1068 1069 1070
	},
	.probe = cros_ec_codec_platform_probe,
};

module_platform_driver(cros_ec_codec_platform_driver);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("ChromeOS EC codec driver");
MODULE_AUTHOR("Cheng-Yi Chiang <cychiang@chromium.org>");
1071
MODULE_ALIAS("platform:cros-ec-codec");