1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2019 Google, Inc.
5 * ChromeOS Embedded Controller codec driver.
7 * This driver uses the cros-ec interface to communicate with the ChromeOS
8 * EC for audio function.
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>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/module.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>
30 struct cros_ec_codec_priv {
32 struct cros_ec_device *ec_device;
35 uint32_t ec_capabilities;
40 uint64_t ap_shm_phys_addr;
43 uint64_t ap_shm_last_alloc;
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;
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;
62 struct snd_pcm_substream *wov_substream;
63 struct delayed_work wov_copy_work;
64 struct notifier_block wov_notifier;
67 static int ec_codec_capable(struct cros_ec_codec_priv *priv, uint8_t cap)
69 return priv->ec_capabilities & BIT(cap);
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)
77 struct cros_ec_command *msg;
79 msg = kmalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
85 msg->outsize = outsize;
89 memcpy(msg->data, out, outsize);
91 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
96 memcpy(in, msg->data, insize);
104 static int calculate_sha256(struct cros_ec_codec_priv *priv,
105 uint8_t *buf, uint32_t size, uint8_t *digest)
107 struct crypto_shash *tfm;
109 tfm = crypto_alloc_shash("sha256", CRYPTO_ALG_TYPE_SHASH, 0);
111 dev_err(priv->dev, "can't alloc shash\n");
116 SHASH_DESC_ON_STACK(desc, tfm);
120 crypto_shash_digest(desc, buf, size, digest);
121 shash_desc_zero(desc);
124 crypto_free_shash(tfm);
130 bin2hex(digest_str, digest, 32);
132 dev_dbg(priv->dev, "hash=%s\n", digest_str);
139 static int dmic_get_gain(struct snd_kcontrol *kcontrol,
140 struct snd_ctl_elem_value *ucontrol)
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;
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));
157 ucontrol->value.integer.value[0] = r.gain;
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));
166 ucontrol->value.integer.value[1] = r.gain;
171 static int dmic_put_gain(struct snd_kcontrol *kcontrol,
172 struct snd_ctl_elem_value *ucontrol)
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;
186 if (left > max_dmic_gain || right > max_dmic_gain)
189 dev_dbg(component->dev, "set mic gain to %u, %u\n", left, right);
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);
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);
206 static const DECLARE_TLV_DB_SCALE(dmic_gain_tlv, 0, 100, 0);
212 static struct snd_kcontrol_new dmic_controls[] = {
214 SOC_DOUBLE_EXT_TLV("EC Mic Gain", SND_SOC_NOPM, SND_SOC_NOPM,
215 0, 0, 0, dmic_get_gain, dmic_put_gain,
219 static int dmic_probe(struct snd_soc_component *component)
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;
229 if (!atomic_add_unless(&priv->dmic_probed, 1, 1))
232 p.cmd = EC_CODEC_DMIC_GET_MAX_GAIN;
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));
238 dev_warn(dev, "get_max_gain() unsupported\n");
242 dev_dbg(dev, "max gain = %d\n", r.max_gain);
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;
249 return snd_soc_add_component_controls(component,
250 &dmic_controls[DMIC_CTL_GAIN], 1);
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)
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;
264 if (params_rate(params) != 48000)
267 switch (params_format(params)) {
268 case SNDRV_PCM_FORMAT_S16_LE:
269 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_16;
271 case SNDRV_PCM_FORMAT_S24_LE:
272 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_24;
278 dev_dbg(component->dev, "set depth to %u\n", depth);
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);
287 dev_dbg(component->dev, "set bclk to %u\n",
288 snd_soc_params_to_bclk(params));
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);
296 static int i2s_rx_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
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;
304 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
305 case SND_SOC_DAIFMT_CBS_CFS:
311 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
312 case SND_SOC_DAIFMT_NB_NF:
318 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
319 case SND_SOC_DAIFMT_I2S:
320 daifmt = EC_CODEC_I2S_RX_DAIFMT_I2S;
322 case SND_SOC_DAIFMT_RIGHT_J:
323 daifmt = EC_CODEC_I2S_RX_DAIFMT_RIGHT_J;
325 case SND_SOC_DAIFMT_LEFT_J:
326 daifmt = EC_CODEC_I2S_RX_DAIFMT_LEFT_J;
332 dev_dbg(component->dev, "set format to %u\n", daifmt);
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);
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,
345 static int i2s_rx_event(struct snd_soc_dapm_widget *w,
346 struct snd_kcontrol *kcontrol, int event)
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;
355 case SND_SOC_DAPM_PRE_PMU:
356 dev_dbg(component->dev, "enable I2S RX\n");
357 p.cmd = EC_CODEC_I2S_RX_ENABLE;
359 case SND_SOC_DAPM_PRE_PMD:
360 dev_dbg(component->dev, "disable I2S RX\n");
361 p.cmd = EC_CODEC_I2S_RX_DISABLE;
367 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
368 (uint8_t *)&p, sizeof(p), NULL, 0);
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),
378 static struct snd_soc_dapm_route i2s_rx_dapm_routes[] = {
379 {"I2S RX", NULL, "DMIC"},
380 {"I2S RX", NULL, "I2S RX Enable"},
383 static struct snd_soc_dai_driver i2s_rx_dai_driver = {
384 .name = "EC Codec I2S RX",
386 .stream_name = "I2S Capture",
389 .rates = SNDRV_PCM_RATE_48000,
390 .formats = SNDRV_PCM_FMTBIT_S16_LE |
391 SNDRV_PCM_FMTBIT_S24_LE,
393 .ops = &i2s_rx_dai_ops,
396 static int i2s_rx_probe(struct snd_soc_component *component)
398 return dmic_probe(component);
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),
409 static void *wov_map_shm(struct cros_ec_codec_priv *priv,
410 uint8_t shm_id, uint32_t *len, uint8_t *type)
412 struct ec_param_ec_codec p;
413 struct ec_response_ec_codec_get_shm_addr r;
414 uint32_t req, offset;
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");
425 dev_dbg(priv->dev, "phys_addr=%#llx, len=%#x\n", r.phys_addr, r.len);
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:
436 dev_err(priv->dev, "unknown status\n");
440 req = round_up(r.len, PAGE_SIZE);
441 dev_dbg(priv->dev, "round up from %u to %u\n", r.len, req);
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");
449 dev_dbg(priv->dev, "alloc AP SHM addr=%#llx, len=%#x\n",
450 priv->ap_shm_last_alloc, req);
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),
459 dev_err(priv->dev, "failed to EC_CODEC_SET_SHM_ADDR\n");
464 * Note: EC codec only requests for `r.len' but we allocate
465 * round up PAGE_SIZE `req'.
467 offset = priv->ap_shm_last_alloc - priv->ap_shm_phys_addr;
468 priv->ap_shm_last_alloc += req;
470 return (void *)(uintptr_t)(priv->ap_shm_addr + offset);
476 static bool wov_queue_full(struct cros_ec_codec_priv *priv)
478 return ((priv->wov_wp + 1) % sizeof(priv->wov_buf)) == priv->wov_rp;
481 static size_t wov_queue_size(struct cros_ec_codec_priv *priv)
483 if (priv->wov_wp >= priv->wov_rp)
484 return priv->wov_wp - priv->wov_rp;
486 return sizeof(priv->wov_buf) - priv->wov_rp + priv->wov_wp;
489 static void wov_queue_dequeue(struct cros_ec_codec_priv *priv, size_t len)
491 struct snd_pcm_runtime *runtime = priv->wov_substream->runtime;
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);
499 req = min(req, sizeof(priv->wov_buf) - priv->wov_rp);
501 memcpy(runtime->dma_area + priv->wov_dma_offset,
502 priv->wov_buf + priv->wov_rp, req);
504 priv->wov_dma_offset += req;
505 if (priv->wov_dma_offset == runtime->dma_bytes)
506 priv->wov_dma_offset = 0;
509 if (priv->wov_rp == sizeof(priv->wov_buf))
515 snd_pcm_period_elapsed(priv->wov_substream);
518 static void wov_queue_try_dequeue(struct cros_ec_codec_priv *priv)
520 size_t period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream);
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);
528 static void wov_queue_enqueue(struct cros_ec_codec_priv *priv,
529 uint8_t *addr, size_t len, bool iomem)
534 if (wov_queue_full(priv)) {
535 wov_queue_try_dequeue(priv);
537 if (wov_queue_full(priv)) {
538 dev_err(priv->dev, "overrun detected\n");
543 if (priv->wov_wp >= priv->wov_rp)
544 req = sizeof(priv->wov_buf) - priv->wov_wp;
546 /* Note: waste 1-byte to differentiate full and empty */
547 req = priv->wov_rp - priv->wov_wp - 1;
551 memcpy_fromio(priv->wov_buf + priv->wov_wp,
552 (void __force __iomem *)addr, req);
554 memcpy(priv->wov_buf + priv->wov_wp, addr, req);
557 if (priv->wov_wp == sizeof(priv->wov_buf))
564 wov_queue_try_dequeue(priv);
567 static int wov_read_audio_shm(struct cros_ec_codec_priv *priv)
569 struct ec_param_ec_codec_wov p;
570 struct ec_response_ec_codec_wov_read_audio_shm r;
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));
578 dev_err(priv->dev, "failed to EC_CODEC_WOV_READ_AUDIO_SHM\n");
583 dev_dbg(priv->dev, "no data, sleep\n");
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);
590 static int wov_read_audio(struct cros_ec_codec_priv *priv)
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;
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));
604 "failed to EC_CODEC_WOV_READ_AUDIO\n");
609 dev_dbg(priv->dev, "no data, sleep\n");
610 priv->wov_burst_read = false;
614 wov_queue_enqueue(priv, r.buf, r.len, false);
621 static void wov_copy_work(struct work_struct *w)
623 struct cros_ec_codec_priv *priv =
624 container_of(w, struct cros_ec_codec_priv, wov_copy_work.work);
627 mutex_lock(&priv->wov_dma_lock);
628 if (!priv->wov_substream) {
629 dev_warn(priv->dev, "no pcm substream\n");
633 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM))
634 ret = wov_read_audio_shm(priv);
636 ret = wov_read_audio(priv);
639 schedule_delayed_work(&priv->wov_copy_work,
640 msecs_to_jiffies(10));
642 dev_err(priv->dev, "failed to read audio data\n");
644 mutex_unlock(&priv->wov_dma_lock);
647 static int wov_enable_get(struct snd_kcontrol *kcontrol,
648 struct snd_ctl_elem_value *ucontrol)
650 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
651 struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c);
653 ucontrol->value.integer.value[0] = priv->wov_enabled;
657 static int wov_enable_put(struct snd_kcontrol *kcontrol,
658 struct snd_ctl_elem_value *ucontrol)
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;
666 if (priv->wov_enabled != enabled) {
668 p.cmd = EC_CODEC_WOV_ENABLE;
670 p.cmd = EC_CODEC_WOV_DISABLE;
672 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
673 (uint8_t *)&p, sizeof(p), NULL, 0);
675 dev_err(priv->dev, "failed to %s wov\n",
676 enabled ? "enable" : "disable");
680 priv->wov_enabled = enabled;
686 static int wov_set_lang_shm(struct cros_ec_codec_priv *priv,
687 uint8_t *buf, size_t size, uint8_t *digest)
689 struct ec_param_ec_codec_wov p;
690 struct ec_param_ec_codec_wov_set_lang_shm *pp = &p.set_lang_shm_param;
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);
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,
703 memset_io((void __force __iomem *)priv->wov_lang_shm_p + size,
704 0, priv->wov_lang_shm_len - size);
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);
711 /* make sure write to memory before calling host command */
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);
722 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG_SHM\n");
729 static int wov_set_lang(struct cros_ec_codec_priv *priv,
730 uint8_t *buf, size_t size, uint8_t *digest)
732 struct ec_param_ec_codec_wov p;
733 struct ec_param_ec_codec_wov_set_lang *pp = &p.set_lang_param;
737 for (i = 0; i < size; i += req) {
738 req = min(size - i, ARRAY_SIZE(pp->buf));
740 p.cmd = EC_CODEC_WOV_SET_LANG;
741 memcpy(pp->hash, digest, SHA256_DIGEST_SIZE);
742 pp->total_len = size;
744 memcpy(pp->buf, buf + i, req);
746 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
747 (uint8_t *)&p, sizeof(p), NULL, 0);
749 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG\n");
757 static int wov_hotword_model_put(struct snd_kcontrol *kcontrol,
758 const unsigned int __user *bytes,
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];
770 /* Skips the TLV header. */
774 dev_dbg(priv->dev, "%s: size=%d\n", __func__, size);
776 buf = memdup_user(bytes, size);
780 ret = calculate_sha256(priv, buf, size, digest);
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));
791 if (memcmp(digest, r.hash, SHA256_DIGEST_SIZE) == 0) {
792 dev_dbg(priv->dev, "not updated");
796 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM))
797 ret = wov_set_lang_shm(priv, buf, size, digest);
799 ret = wov_set_lang(priv, buf, size, digest);
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),
813 static struct snd_soc_dai_driver wov_dai_driver = {
814 .name = "Wake on Voice",
816 .stream_name = "WoV Capture",
819 .rates = SNDRV_PCM_RATE_16000,
820 .formats = SNDRV_PCM_FMTBIT_S16_LE,
824 static int wov_host_event(struct notifier_block *nb,
825 unsigned long queued_during_suspend, void *notify)
827 struct cros_ec_codec_priv *priv =
828 container_of(nb, struct cros_ec_codec_priv, wov_notifier);
831 dev_dbg(priv->dev, "%s\n", __func__);
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);
842 static int wov_probe(struct snd_soc_component *component)
844 struct cros_ec_codec_priv *priv =
845 snd_soc_component_get_drvdata(component);
848 mutex_init(&priv->wov_dma_lock);
849 INIT_DELAYED_WORK(&priv->wov_copy_work, wov_copy_work);
851 priv->wov_notifier.notifier_call = wov_host_event;
852 ret = blocking_notifier_chain_register(
853 &priv->ec_device->event_notifier, &priv->wov_notifier);
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)
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)
875 return dmic_probe(component);
878 static void wov_remove(struct snd_soc_component *component)
880 struct cros_ec_codec_priv *priv =
881 snd_soc_component_get_drvdata(component);
883 blocking_notifier_chain_unregister(
884 &priv->ec_device->event_notifier, &priv->wov_notifier);
887 static int wov_pcm_open(struct snd_soc_component *component,
888 struct snd_pcm_substream *substream)
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,
898 .period_bytes_min = PAGE_SIZE,
899 .period_bytes_max = 0x20000 / 8,
902 .buffer_bytes_max = 0x20000,
905 return snd_soc_set_runtime_hwparams(substream, &hw_param);
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)
912 struct cros_ec_codec_priv *priv =
913 snd_soc_component_get_drvdata(component);
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);
925 static int wov_pcm_hw_free(struct snd_soc_component *component,
926 struct snd_pcm_substream *substream)
928 struct cros_ec_codec_priv *priv =
929 snd_soc_component_get_drvdata(component);
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);
936 cancel_delayed_work_sync(&priv->wov_copy_work);
941 static snd_pcm_uframes_t wov_pcm_pointer(struct snd_soc_component *component,
942 struct snd_pcm_substream *substream)
944 struct snd_pcm_runtime *runtime = substream->runtime;
945 struct cros_ec_codec_priv *priv =
946 snd_soc_component_get_drvdata(component);
948 return bytes_to_frames(runtime, priv->wov_dma_offset);
951 static int wov_pcm_new(struct snd_soc_component *component,
952 struct snd_soc_pcm_runtime *rtd)
954 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_VMALLOC,
959 static const struct snd_soc_component_driver wov_component_driver = {
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,
971 static int cros_ec_codec_platform_probe(struct platform_device *pdev)
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;
980 struct device_node *node;
983 const __be32 *regaddr_p;
986 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
991 regaddr_p = of_get_address(dev->of_node, 0, &ec_shm_size, NULL);
993 priv->ec_shm_addr = of_read_number(regaddr_p, 2);
994 priv->ec_shm_len = ec_shm_size;
996 dev_dbg(dev, "ec_shm_addr=%#llx len=%#x\n",
997 priv->ec_shm_addr, priv->ec_shm_len);
1000 node = of_parse_phandle(dev->of_node, "memory-region", 0);
1002 ret = of_address_to_resource(node, 0, &res);
1004 priv->ap_shm_phys_addr = res.start;
1005 priv->ap_shm_len = resource_size(&res);
1007 (uint64_t)(uintptr_t)devm_ioremap_wc(
1008 dev, priv->ap_shm_phys_addr,
1010 priv->ap_shm_last_alloc = priv->ap_shm_phys_addr;
1012 dev_dbg(dev, "ap_shm_phys_addr=%#llx len=%#x\n",
1013 priv->ap_shm_phys_addr, priv->ap_shm_len);
1019 priv->ec_device = ec_device;
1020 atomic_set(&priv->dmic_probed, 0);
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));
1027 dev_err(dev, "failed to EC_CODEC_GET_CAPABILITIES\n");
1030 priv->ec_capabilities = r.capabilities;
1032 platform_set_drvdata(pdev, priv);
1034 ret = devm_snd_soc_register_component(dev, &i2s_rx_component_driver,
1035 &i2s_rx_dai_driver, 1);
1039 return devm_snd_soc_register_component(dev, &wov_component_driver,
1040 &wov_dai_driver, 1);
1044 static const struct of_device_id cros_ec_codec_of_match[] = {
1045 { .compatible = "google,cros-ec-codec" },
1048 MODULE_DEVICE_TABLE(of, cros_ec_codec_of_match);
1051 static const struct acpi_device_id cros_ec_codec_acpi_id[] = {
1055 MODULE_DEVICE_TABLE(acpi, cros_ec_codec_acpi_id);
1057 static struct platform_driver cros_ec_codec_platform_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),
1063 .probe = cros_ec_codec_platform_probe,
1066 module_platform_driver(cros_ec_codec_platform_driver);
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");