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/delay.h>
14 #include <linux/device.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/module.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>
29 struct cros_ec_codec_priv {
31 struct cros_ec_device *ec_device;
34 uint32_t ec_capabilities;
39 uint64_t ap_shm_phys_addr;
42 uint64_t ap_shm_last_alloc;
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;
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;
61 struct snd_pcm_substream *wov_substream;
62 struct delayed_work wov_copy_work;
63 struct notifier_block wov_notifier;
66 static int ec_codec_capable(struct cros_ec_codec_priv *priv, uint8_t cap)
68 return priv->ec_capabilities & BIT(cap);
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)
76 struct cros_ec_command *msg;
78 msg = kmalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
84 msg->outsize = outsize;
88 memcpy(msg->data, out, outsize);
90 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
95 memcpy(in, msg->data, insize);
103 static int calculate_sha256(struct cros_ec_codec_priv *priv,
104 uint8_t *buf, uint32_t size, uint8_t *digest)
106 struct crypto_shash *tfm;
108 tfm = crypto_alloc_shash("sha256", CRYPTO_ALG_TYPE_SHASH, 0);
110 dev_err(priv->dev, "can't alloc shash\n");
115 SHASH_DESC_ON_STACK(desc, tfm);
119 crypto_shash_digest(desc, buf, size, digest);
120 shash_desc_zero(desc);
123 crypto_free_shash(tfm);
129 bin2hex(digest_str, digest, 32);
131 dev_dbg(priv->dev, "hash=%s\n", digest_str);
138 static int dmic_get_gain(struct snd_kcontrol *kcontrol,
139 struct snd_ctl_elem_value *ucontrol)
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;
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));
156 ucontrol->value.integer.value[0] = r.gain;
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));
165 ucontrol->value.integer.value[1] = r.gain;
170 static int dmic_put_gain(struct snd_kcontrol *kcontrol,
171 struct snd_ctl_elem_value *ucontrol)
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;
185 if (left > max_dmic_gain || right > max_dmic_gain)
188 dev_dbg(component->dev, "set mic gain to %u, %u\n", left, right);
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);
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);
205 static const DECLARE_TLV_DB_SCALE(dmic_gain_tlv, 0, 100, 0);
211 static struct snd_kcontrol_new dmic_controls[] = {
213 SOC_DOUBLE_EXT_TLV("EC Mic Gain", SND_SOC_NOPM, SND_SOC_NOPM,
214 0, 0, 0, dmic_get_gain, dmic_put_gain,
218 static int dmic_probe(struct snd_soc_component *component)
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;
228 if (!atomic_add_unless(&priv->dmic_probed, 1, 1))
231 p.cmd = EC_CODEC_DMIC_GET_MAX_GAIN;
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));
237 dev_warn(dev, "get_max_gain() unsupported\n");
241 dev_dbg(dev, "max gain = %d\n", r.max_gain);
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;
248 return snd_soc_add_component_controls(component,
249 &dmic_controls[DMIC_CTL_GAIN], 1);
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)
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;
263 if (params_rate(params) != 48000)
266 switch (params_format(params)) {
267 case SNDRV_PCM_FORMAT_S16_LE:
268 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_16;
270 case SNDRV_PCM_FORMAT_S24_LE:
271 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_24;
277 dev_dbg(component->dev, "set depth to %u\n", depth);
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);
286 dev_dbg(component->dev, "set bclk to %u\n",
287 snd_soc_params_to_bclk(params));
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);
295 static int i2s_rx_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
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;
303 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
304 case SND_SOC_DAIFMT_CBS_CFS:
310 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
311 case SND_SOC_DAIFMT_NB_NF:
317 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
318 case SND_SOC_DAIFMT_I2S:
319 daifmt = EC_CODEC_I2S_RX_DAIFMT_I2S;
321 case SND_SOC_DAIFMT_RIGHT_J:
322 daifmt = EC_CODEC_I2S_RX_DAIFMT_RIGHT_J;
324 case SND_SOC_DAIFMT_LEFT_J:
325 daifmt = EC_CODEC_I2S_RX_DAIFMT_LEFT_J;
331 dev_dbg(component->dev, "set format to %u\n", daifmt);
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);
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,
344 static int i2s_rx_event(struct snd_soc_dapm_widget *w,
345 struct snd_kcontrol *kcontrol, int event)
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;
354 case SND_SOC_DAPM_PRE_PMU:
355 dev_dbg(component->dev, "enable I2S RX\n");
356 p.cmd = EC_CODEC_I2S_RX_ENABLE;
358 case SND_SOC_DAPM_PRE_PMD:
359 dev_dbg(component->dev, "disable I2S RX\n");
360 p.cmd = EC_CODEC_I2S_RX_DISABLE;
366 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
367 (uint8_t *)&p, sizeof(p), NULL, 0);
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),
377 static struct snd_soc_dapm_route i2s_rx_dapm_routes[] = {
378 {"I2S RX", NULL, "DMIC"},
379 {"I2S RX", NULL, "I2S RX Enable"},
382 static struct snd_soc_dai_driver i2s_rx_dai_driver = {
383 .name = "EC Codec I2S RX",
385 .stream_name = "I2S Capture",
388 .rates = SNDRV_PCM_RATE_48000,
389 .formats = SNDRV_PCM_FMTBIT_S16_LE |
390 SNDRV_PCM_FMTBIT_S24_LE,
392 .ops = &i2s_rx_dai_ops,
395 static int i2s_rx_probe(struct snd_soc_component *component)
397 return dmic_probe(component);
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),
408 static void *wov_map_shm(struct cros_ec_codec_priv *priv,
409 uint8_t shm_id, uint32_t *len, uint8_t *type)
411 struct ec_param_ec_codec p;
412 struct ec_response_ec_codec_get_shm_addr r;
413 uint32_t req, offset;
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");
424 dev_dbg(priv->dev, "phys_addr=%#llx, len=%#x\n", r.phys_addr, r.len);
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:
435 dev_err(priv->dev, "unknown status\n");
439 req = round_up(r.len, PAGE_SIZE);
440 dev_dbg(priv->dev, "round up from %u to %u\n", r.len, req);
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");
448 dev_dbg(priv->dev, "alloc AP SHM addr=%#llx, len=%#x\n",
449 priv->ap_shm_last_alloc, req);
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),
458 dev_err(priv->dev, "failed to EC_CODEC_SET_SHM_ADDR\n");
463 * Note: EC codec only requests for `r.len' but we allocate
464 * round up PAGE_SIZE `req'.
466 offset = priv->ap_shm_last_alloc - priv->ap_shm_phys_addr;
467 priv->ap_shm_last_alloc += req;
469 return (void *)(uintptr_t)(priv->ap_shm_addr + offset);
475 static bool wov_queue_full(struct cros_ec_codec_priv *priv)
477 return ((priv->wov_wp + 1) % sizeof(priv->wov_buf)) == priv->wov_rp;
480 static size_t wov_queue_size(struct cros_ec_codec_priv *priv)
482 if (priv->wov_wp >= priv->wov_rp)
483 return priv->wov_wp - priv->wov_rp;
485 return sizeof(priv->wov_buf) - priv->wov_rp + priv->wov_wp;
488 static void wov_queue_dequeue(struct cros_ec_codec_priv *priv, size_t len)
490 struct snd_pcm_runtime *runtime = priv->wov_substream->runtime;
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);
498 req = min(req, sizeof(priv->wov_buf) - priv->wov_rp);
500 memcpy(runtime->dma_area + priv->wov_dma_offset,
501 priv->wov_buf + priv->wov_rp, req);
503 priv->wov_dma_offset += req;
504 if (priv->wov_dma_offset == runtime->dma_bytes)
505 priv->wov_dma_offset = 0;
508 if (priv->wov_rp == sizeof(priv->wov_buf))
514 snd_pcm_period_elapsed(priv->wov_substream);
517 static void wov_queue_try_dequeue(struct cros_ec_codec_priv *priv)
519 size_t period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream);
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);
527 static void wov_queue_enqueue(struct cros_ec_codec_priv *priv,
528 uint8_t *addr, size_t len, bool iomem)
533 if (wov_queue_full(priv)) {
534 wov_queue_try_dequeue(priv);
536 if (wov_queue_full(priv)) {
537 dev_err(priv->dev, "overrun detected\n");
542 if (priv->wov_wp >= priv->wov_rp)
543 req = sizeof(priv->wov_buf) - priv->wov_wp;
545 /* Note: waste 1-byte to differentiate full and empty */
546 req = priv->wov_rp - priv->wov_wp - 1;
550 memcpy_fromio(priv->wov_buf + priv->wov_wp,
551 (void __force __iomem *)addr, req);
553 memcpy(priv->wov_buf + priv->wov_wp, addr, req);
556 if (priv->wov_wp == sizeof(priv->wov_buf))
563 wov_queue_try_dequeue(priv);
566 static int wov_read_audio_shm(struct cros_ec_codec_priv *priv)
568 struct ec_param_ec_codec_wov p;
569 struct ec_response_ec_codec_wov_read_audio_shm r;
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));
577 dev_err(priv->dev, "failed to EC_CODEC_WOV_READ_AUDIO_SHM\n");
582 dev_dbg(priv->dev, "no data, sleep\n");
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);
589 static int wov_read_audio(struct cros_ec_codec_priv *priv)
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;
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));
603 "failed to EC_CODEC_WOV_READ_AUDIO\n");
608 dev_dbg(priv->dev, "no data, sleep\n");
609 priv->wov_burst_read = false;
613 wov_queue_enqueue(priv, r.buf, r.len, false);
620 static void wov_copy_work(struct work_struct *w)
622 struct cros_ec_codec_priv *priv =
623 container_of(w, struct cros_ec_codec_priv, wov_copy_work.work);
626 mutex_lock(&priv->wov_dma_lock);
627 if (!priv->wov_substream) {
628 dev_warn(priv->dev, "no pcm substream\n");
632 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM))
633 ret = wov_read_audio_shm(priv);
635 ret = wov_read_audio(priv);
638 schedule_delayed_work(&priv->wov_copy_work,
639 msecs_to_jiffies(10));
641 dev_err(priv->dev, "failed to read audio data\n");
643 mutex_unlock(&priv->wov_dma_lock);
646 static int wov_enable_get(struct snd_kcontrol *kcontrol,
647 struct snd_ctl_elem_value *ucontrol)
649 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
650 struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c);
652 ucontrol->value.integer.value[0] = priv->wov_enabled;
656 static int wov_enable_put(struct snd_kcontrol *kcontrol,
657 struct snd_ctl_elem_value *ucontrol)
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;
665 if (priv->wov_enabled != enabled) {
667 p.cmd = EC_CODEC_WOV_ENABLE;
669 p.cmd = EC_CODEC_WOV_DISABLE;
671 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
672 (uint8_t *)&p, sizeof(p), NULL, 0);
674 dev_err(priv->dev, "failed to %s wov\n",
675 enabled ? "enable" : "disable");
679 priv->wov_enabled = enabled;
685 static int wov_set_lang_shm(struct cros_ec_codec_priv *priv,
686 uint8_t *buf, size_t size, uint8_t *digest)
688 struct ec_param_ec_codec_wov p;
689 struct ec_param_ec_codec_wov_set_lang_shm *pp = &p.set_lang_shm_param;
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);
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,
702 memset_io((void __force __iomem *)priv->wov_lang_shm_p + size,
703 0, priv->wov_lang_shm_len - size);
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);
710 /* make sure write to memory before calling host command */
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);
721 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG_SHM\n");
728 static int wov_set_lang(struct cros_ec_codec_priv *priv,
729 uint8_t *buf, size_t size, uint8_t *digest)
731 struct ec_param_ec_codec_wov p;
732 struct ec_param_ec_codec_wov_set_lang *pp = &p.set_lang_param;
736 for (i = 0; i < size; i += req) {
737 req = min(size - i, ARRAY_SIZE(pp->buf));
739 p.cmd = EC_CODEC_WOV_SET_LANG;
740 memcpy(pp->hash, digest, SHA256_DIGEST_SIZE);
741 pp->total_len = size;
743 memcpy(pp->buf, buf + i, req);
745 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
746 (uint8_t *)&p, sizeof(p), NULL, 0);
748 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG\n");
756 static int wov_hotword_model_put(struct snd_kcontrol *kcontrol,
757 const unsigned int __user *bytes,
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];
769 /* Skips the TLV header. */
773 dev_dbg(priv->dev, "%s: size=%d\n", __func__, size);
775 buf = memdup_user(bytes, size);
779 ret = calculate_sha256(priv, buf, size, digest);
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));
790 if (memcmp(digest, r.hash, SHA256_DIGEST_SIZE) == 0) {
791 dev_dbg(priv->dev, "not updated");
795 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM))
796 ret = wov_set_lang_shm(priv, buf, size, digest);
798 ret = wov_set_lang(priv, buf, size, digest);
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),
812 static struct snd_soc_dai_driver wov_dai_driver = {
813 .name = "Wake on Voice",
815 .stream_name = "WoV Capture",
818 .rates = SNDRV_PCM_RATE_16000,
819 .formats = SNDRV_PCM_FMTBIT_S16_LE,
823 static int wov_host_event(struct notifier_block *nb,
824 unsigned long queued_during_suspend, void *notify)
826 struct cros_ec_codec_priv *priv =
827 container_of(nb, struct cros_ec_codec_priv, wov_notifier);
830 dev_dbg(priv->dev, "%s\n", __func__);
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);
841 static int wov_probe(struct snd_soc_component *component)
843 struct cros_ec_codec_priv *priv =
844 snd_soc_component_get_drvdata(component);
847 mutex_init(&priv->wov_dma_lock);
848 INIT_DELAYED_WORK(&priv->wov_copy_work, wov_copy_work);
850 priv->wov_notifier.notifier_call = wov_host_event;
851 ret = blocking_notifier_chain_register(
852 &priv->ec_device->event_notifier, &priv->wov_notifier);
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)
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)
874 return dmic_probe(component);
877 static void wov_remove(struct snd_soc_component *component)
879 struct cros_ec_codec_priv *priv =
880 snd_soc_component_get_drvdata(component);
882 blocking_notifier_chain_unregister(
883 &priv->ec_device->event_notifier, &priv->wov_notifier);
886 static int wov_pcm_open(struct snd_soc_component *component,
887 struct snd_pcm_substream *substream)
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,
897 .period_bytes_min = PAGE_SIZE,
898 .period_bytes_max = 0x20000 / 8,
901 .buffer_bytes_max = 0x20000,
904 return snd_soc_set_runtime_hwparams(substream, &hw_param);
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)
911 struct cros_ec_codec_priv *priv =
912 snd_soc_component_get_drvdata(component);
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);
924 static int wov_pcm_hw_free(struct snd_soc_component *component,
925 struct snd_pcm_substream *substream)
927 struct cros_ec_codec_priv *priv =
928 snd_soc_component_get_drvdata(component);
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);
935 cancel_delayed_work_sync(&priv->wov_copy_work);
940 static snd_pcm_uframes_t wov_pcm_pointer(struct snd_soc_component *component,
941 struct snd_pcm_substream *substream)
943 struct snd_pcm_runtime *runtime = substream->runtime;
944 struct cros_ec_codec_priv *priv =
945 snd_soc_component_get_drvdata(component);
947 return bytes_to_frames(runtime, priv->wov_dma_offset);
950 static int wov_pcm_new(struct snd_soc_component *component,
951 struct snd_soc_pcm_runtime *rtd)
953 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_VMALLOC,
958 static const struct snd_soc_component_driver wov_component_driver = {
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,
970 static int cros_ec_codec_platform_probe(struct platform_device *pdev)
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;
979 struct device_node *node;
982 const __be32 *regaddr_p;
985 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
990 regaddr_p = of_get_address(dev->of_node, 0, &ec_shm_size, NULL);
992 priv->ec_shm_addr = of_read_number(regaddr_p, 2);
993 priv->ec_shm_len = ec_shm_size;
995 dev_dbg(dev, "ec_shm_addr=%#llx len=%#x\n",
996 priv->ec_shm_addr, priv->ec_shm_len);
999 node = of_parse_phandle(dev->of_node, "memory-region", 0);
1001 ret = of_address_to_resource(node, 0, &res);
1003 priv->ap_shm_phys_addr = res.start;
1004 priv->ap_shm_len = resource_size(&res);
1006 (uint64_t)(uintptr_t)devm_ioremap_wc(
1007 dev, priv->ap_shm_phys_addr,
1009 priv->ap_shm_last_alloc = priv->ap_shm_phys_addr;
1011 dev_dbg(dev, "ap_shm_phys_addr=%#llx len=%#x\n",
1012 priv->ap_shm_phys_addr, priv->ap_shm_len);
1018 priv->ec_device = ec_device;
1019 atomic_set(&priv->dmic_probed, 0);
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));
1026 dev_err(dev, "failed to EC_CODEC_GET_CAPABILITIES\n");
1029 priv->ec_capabilities = r.capabilities;
1031 platform_set_drvdata(pdev, priv);
1033 ret = devm_snd_soc_register_component(dev, &i2s_rx_component_driver,
1034 &i2s_rx_dai_driver, 1);
1038 return devm_snd_soc_register_component(dev, &wov_component_driver,
1039 &wov_dai_driver, 1);
1043 static const struct of_device_id cros_ec_codec_of_match[] = {
1044 { .compatible = "google,cros-ec-codec" },
1047 MODULE_DEVICE_TABLE(of, cros_ec_codec_of_match);
1050 static struct platform_driver cros_ec_codec_platform_driver = {
1052 .name = "cros-ec-codec",
1053 .of_match_table = of_match_ptr(cros_ec_codec_of_match),
1055 .probe = cros_ec_codec_platform_probe,
1058 module_platform_driver(cros_ec_codec_platform_driver);
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");