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/sha2.h>
12 #include <linux/acpi.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;
48 uint32_t i2s_rx_bclk_ratio;
52 uint8_t *wov_audio_shm_p;
53 uint32_t wov_audio_shm_len;
54 uint8_t wov_audio_shm_type;
55 uint8_t *wov_lang_shm_p;
56 uint32_t wov_lang_shm_len;
57 uint8_t wov_lang_shm_type;
59 struct mutex wov_dma_lock;
60 uint8_t wov_buf[64000];
61 uint32_t wov_rp, wov_wp;
62 size_t wov_dma_offset;
64 struct snd_pcm_substream *wov_substream;
65 struct delayed_work wov_copy_work;
66 struct notifier_block wov_notifier;
69 static int ec_codec_capable(struct cros_ec_codec_priv *priv, uint8_t cap)
71 return priv->ec_capabilities & BIT(cap);
74 static int send_ec_host_command(struct cros_ec_device *ec_dev, uint32_t cmd,
75 uint8_t *out, size_t outsize,
76 uint8_t *in, size_t insize)
79 struct cros_ec_command *msg;
81 msg = kmalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
87 msg->outsize = outsize;
91 memcpy(msg->data, out, outsize);
93 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
98 memcpy(in, msg->data, insize);
106 static int dmic_get_gain(struct snd_kcontrol *kcontrol,
107 struct snd_ctl_elem_value *ucontrol)
109 struct snd_soc_component *component =
110 snd_soc_kcontrol_component(kcontrol);
111 struct cros_ec_codec_priv *priv =
112 snd_soc_component_get_drvdata(component);
113 struct ec_param_ec_codec_dmic p;
114 struct ec_response_ec_codec_dmic_get_gain_idx r;
117 p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX;
118 p.get_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_0;
119 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
120 (uint8_t *)&p, sizeof(p),
121 (uint8_t *)&r, sizeof(r));
124 ucontrol->value.integer.value[0] = r.gain;
126 p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX;
127 p.get_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1;
128 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
129 (uint8_t *)&p, sizeof(p),
130 (uint8_t *)&r, sizeof(r));
133 ucontrol->value.integer.value[1] = r.gain;
138 static int dmic_put_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 soc_mixer_control *control =
146 (struct soc_mixer_control *)kcontrol->private_value;
147 int max_dmic_gain = control->max;
148 int left = ucontrol->value.integer.value[0];
149 int right = ucontrol->value.integer.value[1];
150 struct ec_param_ec_codec_dmic p;
153 if (left > max_dmic_gain || right > max_dmic_gain)
156 dev_dbg(component->dev, "set mic gain to %u, %u\n", left, right);
158 p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX;
159 p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_0;
160 p.set_gain_idx_param.gain = left;
161 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
162 (uint8_t *)&p, sizeof(p), NULL, 0);
166 p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX;
167 p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1;
168 p.set_gain_idx_param.gain = right;
169 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
170 (uint8_t *)&p, sizeof(p), NULL, 0);
173 static const DECLARE_TLV_DB_SCALE(dmic_gain_tlv, 0, 100, 0);
179 static struct snd_kcontrol_new dmic_controls[] = {
181 SOC_DOUBLE_EXT_TLV("EC Mic Gain", SND_SOC_NOPM, SND_SOC_NOPM,
182 0, 0, 0, dmic_get_gain, dmic_put_gain,
186 static int dmic_probe(struct snd_soc_component *component)
188 struct cros_ec_codec_priv *priv =
189 snd_soc_component_get_drvdata(component);
190 struct device *dev = priv->dev;
191 struct soc_mixer_control *control;
192 struct ec_param_ec_codec_dmic p;
193 struct ec_response_ec_codec_dmic_get_max_gain r;
196 if (!atomic_add_unless(&priv->dmic_probed, 1, 1))
199 p.cmd = EC_CODEC_DMIC_GET_MAX_GAIN;
201 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
202 (uint8_t *)&p, sizeof(p),
203 (uint8_t *)&r, sizeof(r));
205 dev_warn(dev, "get_max_gain() unsupported\n");
209 dev_dbg(dev, "max gain = %d\n", r.max_gain);
211 control = (struct soc_mixer_control *)
212 dmic_controls[DMIC_CTL_GAIN].private_value;
213 control->max = r.max_gain;
214 control->platform_max = r.max_gain;
216 return snd_soc_add_component_controls(component,
217 &dmic_controls[DMIC_CTL_GAIN], 1);
220 static int i2s_rx_hw_params(struct snd_pcm_substream *substream,
221 struct snd_pcm_hw_params *params,
222 struct snd_soc_dai *dai)
224 struct snd_soc_component *component = dai->component;
225 struct cros_ec_codec_priv *priv =
226 snd_soc_component_get_drvdata(component);
227 struct ec_param_ec_codec_i2s_rx p;
228 enum ec_codec_i2s_rx_sample_depth depth;
232 if (params_rate(params) != 48000)
235 switch (params_format(params)) {
236 case SNDRV_PCM_FORMAT_S16_LE:
237 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_16;
239 case SNDRV_PCM_FORMAT_S24_LE:
240 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_24;
246 dev_dbg(component->dev, "set depth to %u\n", depth);
248 p.cmd = EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH;
249 p.set_sample_depth_param.depth = depth;
250 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
251 (uint8_t *)&p, sizeof(p), NULL, 0);
255 if (priv->i2s_rx_bclk_ratio)
256 bclk = params_rate(params) * priv->i2s_rx_bclk_ratio;
258 bclk = snd_soc_params_to_bclk(params);
260 dev_dbg(component->dev, "set bclk to %u\n", bclk);
262 p.cmd = EC_CODEC_I2S_RX_SET_BCLK;
263 p.set_bclk_param.bclk = bclk;
264 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
265 (uint8_t *)&p, sizeof(p), NULL, 0);
268 static int i2s_rx_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
270 struct snd_soc_component *component = dai->component;
271 struct cros_ec_codec_priv *priv =
272 snd_soc_component_get_drvdata(component);
274 priv->i2s_rx_bclk_ratio = ratio;
278 static int i2s_rx_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
280 struct snd_soc_component *component = dai->component;
281 struct cros_ec_codec_priv *priv =
282 snd_soc_component_get_drvdata(component);
283 struct ec_param_ec_codec_i2s_rx p;
284 enum ec_codec_i2s_rx_daifmt daifmt;
286 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
287 case SND_SOC_DAIFMT_CBS_CFS:
293 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
294 case SND_SOC_DAIFMT_NB_NF:
300 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
301 case SND_SOC_DAIFMT_I2S:
302 daifmt = EC_CODEC_I2S_RX_DAIFMT_I2S;
304 case SND_SOC_DAIFMT_RIGHT_J:
305 daifmt = EC_CODEC_I2S_RX_DAIFMT_RIGHT_J;
307 case SND_SOC_DAIFMT_LEFT_J:
308 daifmt = EC_CODEC_I2S_RX_DAIFMT_LEFT_J;
314 dev_dbg(component->dev, "set format to %u\n", daifmt);
316 p.cmd = EC_CODEC_I2S_RX_SET_DAIFMT;
317 p.set_daifmt_param.daifmt = daifmt;
318 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
319 (uint8_t *)&p, sizeof(p), NULL, 0);
322 static const struct snd_soc_dai_ops i2s_rx_dai_ops = {
323 .hw_params = i2s_rx_hw_params,
324 .set_fmt = i2s_rx_set_fmt,
325 .set_bclk_ratio = i2s_rx_set_bclk_ratio,
328 static int i2s_rx_event(struct snd_soc_dapm_widget *w,
329 struct snd_kcontrol *kcontrol, int event)
331 struct snd_soc_component *component =
332 snd_soc_dapm_to_component(w->dapm);
333 struct cros_ec_codec_priv *priv =
334 snd_soc_component_get_drvdata(component);
335 struct ec_param_ec_codec_i2s_rx p = {};
338 case SND_SOC_DAPM_PRE_PMU:
339 dev_dbg(component->dev, "enable I2S RX\n");
340 p.cmd = EC_CODEC_I2S_RX_ENABLE;
342 case SND_SOC_DAPM_PRE_PMD:
343 dev_dbg(component->dev, "disable I2S RX\n");
344 p.cmd = EC_CODEC_I2S_RX_DISABLE;
350 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
351 (uint8_t *)&p, sizeof(p), NULL, 0);
354 static struct snd_soc_dapm_widget i2s_rx_dapm_widgets[] = {
355 SND_SOC_DAPM_INPUT("DMIC"),
356 SND_SOC_DAPM_SUPPLY("I2S RX Enable", SND_SOC_NOPM, 0, 0, i2s_rx_event,
357 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
358 SND_SOC_DAPM_AIF_OUT("I2S RX", "I2S Capture", 0, SND_SOC_NOPM, 0, 0),
361 static struct snd_soc_dapm_route i2s_rx_dapm_routes[] = {
362 {"I2S RX", NULL, "DMIC"},
363 {"I2S RX", NULL, "I2S RX Enable"},
366 static struct snd_soc_dai_driver i2s_rx_dai_driver = {
367 .name = "EC Codec I2S RX",
369 .stream_name = "I2S Capture",
372 .rates = SNDRV_PCM_RATE_48000,
373 .formats = SNDRV_PCM_FMTBIT_S16_LE |
374 SNDRV_PCM_FMTBIT_S24_LE,
376 .ops = &i2s_rx_dai_ops,
379 static int i2s_rx_probe(struct snd_soc_component *component)
381 return dmic_probe(component);
384 static const struct snd_soc_component_driver i2s_rx_component_driver = {
385 .probe = i2s_rx_probe,
386 .dapm_widgets = i2s_rx_dapm_widgets,
387 .num_dapm_widgets = ARRAY_SIZE(i2s_rx_dapm_widgets),
388 .dapm_routes = i2s_rx_dapm_routes,
389 .num_dapm_routes = ARRAY_SIZE(i2s_rx_dapm_routes),
392 static void *wov_map_shm(struct cros_ec_codec_priv *priv,
393 uint8_t shm_id, uint32_t *len, uint8_t *type)
395 struct ec_param_ec_codec p;
396 struct ec_response_ec_codec_get_shm_addr r;
397 uint32_t req, offset;
399 p.cmd = EC_CODEC_GET_SHM_ADDR;
400 p.get_shm_addr_param.shm_id = shm_id;
401 if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC,
402 (uint8_t *)&p, sizeof(p),
403 (uint8_t *)&r, sizeof(r)) < 0) {
404 dev_err(priv->dev, "failed to EC_CODEC_GET_SHM_ADDR\n");
408 dev_dbg(priv->dev, "phys_addr=%#llx, len=%#x\n", r.phys_addr, r.len);
414 case EC_CODEC_SHM_TYPE_EC_RAM:
415 return (void __force *)devm_ioremap_wc(priv->dev,
416 r.phys_addr + priv->ec_shm_addr, r.len);
417 case EC_CODEC_SHM_TYPE_SYSTEM_RAM:
419 dev_err(priv->dev, "unknown status\n");
423 req = round_up(r.len, PAGE_SIZE);
424 dev_dbg(priv->dev, "round up from %u to %u\n", r.len, req);
426 if (priv->ap_shm_last_alloc + req >
427 priv->ap_shm_phys_addr + priv->ap_shm_len) {
428 dev_err(priv->dev, "insufficient space for AP SHM\n");
432 dev_dbg(priv->dev, "alloc AP SHM addr=%#llx, len=%#x\n",
433 priv->ap_shm_last_alloc, req);
435 p.cmd = EC_CODEC_SET_SHM_ADDR;
436 p.set_shm_addr_param.phys_addr = priv->ap_shm_last_alloc;
437 p.set_shm_addr_param.len = req;
438 p.set_shm_addr_param.shm_id = shm_id;
439 if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC,
440 (uint8_t *)&p, sizeof(p),
442 dev_err(priv->dev, "failed to EC_CODEC_SET_SHM_ADDR\n");
447 * Note: EC codec only requests for `r.len' but we allocate
448 * round up PAGE_SIZE `req'.
450 offset = priv->ap_shm_last_alloc - priv->ap_shm_phys_addr;
451 priv->ap_shm_last_alloc += req;
453 return (void *)(uintptr_t)(priv->ap_shm_addr + offset);
459 static bool wov_queue_full(struct cros_ec_codec_priv *priv)
461 return ((priv->wov_wp + 1) % sizeof(priv->wov_buf)) == priv->wov_rp;
464 static size_t wov_queue_size(struct cros_ec_codec_priv *priv)
466 if (priv->wov_wp >= priv->wov_rp)
467 return priv->wov_wp - priv->wov_rp;
469 return sizeof(priv->wov_buf) - priv->wov_rp + priv->wov_wp;
472 static void wov_queue_dequeue(struct cros_ec_codec_priv *priv, size_t len)
474 struct snd_pcm_runtime *runtime = priv->wov_substream->runtime;
478 req = min(len, runtime->dma_bytes - priv->wov_dma_offset);
479 if (priv->wov_wp >= priv->wov_rp)
480 req = min(req, (size_t)priv->wov_wp - priv->wov_rp);
482 req = min(req, sizeof(priv->wov_buf) - priv->wov_rp);
484 memcpy(runtime->dma_area + priv->wov_dma_offset,
485 priv->wov_buf + priv->wov_rp, req);
487 priv->wov_dma_offset += req;
488 if (priv->wov_dma_offset == runtime->dma_bytes)
489 priv->wov_dma_offset = 0;
492 if (priv->wov_rp == sizeof(priv->wov_buf))
498 snd_pcm_period_elapsed(priv->wov_substream);
501 static void wov_queue_try_dequeue(struct cros_ec_codec_priv *priv)
503 size_t period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream);
505 while (period_bytes && wov_queue_size(priv) >= period_bytes) {
506 wov_queue_dequeue(priv, period_bytes);
507 period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream);
511 static void wov_queue_enqueue(struct cros_ec_codec_priv *priv,
512 uint8_t *addr, size_t len, bool iomem)
517 if (wov_queue_full(priv)) {
518 wov_queue_try_dequeue(priv);
520 if (wov_queue_full(priv)) {
521 dev_err(priv->dev, "overrun detected\n");
526 if (priv->wov_wp >= priv->wov_rp)
527 req = sizeof(priv->wov_buf) - priv->wov_wp;
529 /* Note: waste 1-byte to differentiate full and empty */
530 req = priv->wov_rp - priv->wov_wp - 1;
534 memcpy_fromio(priv->wov_buf + priv->wov_wp,
535 (void __force __iomem *)addr, req);
537 memcpy(priv->wov_buf + priv->wov_wp, addr, req);
540 if (priv->wov_wp == sizeof(priv->wov_buf))
547 wov_queue_try_dequeue(priv);
550 static int wov_read_audio_shm(struct cros_ec_codec_priv *priv)
552 struct ec_param_ec_codec_wov p;
553 struct ec_response_ec_codec_wov_read_audio_shm r;
556 p.cmd = EC_CODEC_WOV_READ_AUDIO_SHM;
557 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
558 (uint8_t *)&p, sizeof(p),
559 (uint8_t *)&r, sizeof(r));
561 dev_err(priv->dev, "failed to EC_CODEC_WOV_READ_AUDIO_SHM\n");
566 dev_dbg(priv->dev, "no data, sleep\n");
568 wov_queue_enqueue(priv, priv->wov_audio_shm_p + r.offset, r.len,
569 priv->wov_audio_shm_type == EC_CODEC_SHM_TYPE_EC_RAM);
573 static int wov_read_audio(struct cros_ec_codec_priv *priv)
575 struct ec_param_ec_codec_wov p;
576 struct ec_response_ec_codec_wov_read_audio r;
577 int remain = priv->wov_burst_read ? 16000 : 320;
580 while (remain >= 0) {
581 p.cmd = EC_CODEC_WOV_READ_AUDIO;
582 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
583 (uint8_t *)&p, sizeof(p),
584 (uint8_t *)&r, sizeof(r));
587 "failed to EC_CODEC_WOV_READ_AUDIO\n");
592 dev_dbg(priv->dev, "no data, sleep\n");
593 priv->wov_burst_read = false;
597 wov_queue_enqueue(priv, r.buf, r.len, false);
604 static void wov_copy_work(struct work_struct *w)
606 struct cros_ec_codec_priv *priv =
607 container_of(w, struct cros_ec_codec_priv, wov_copy_work.work);
610 mutex_lock(&priv->wov_dma_lock);
611 if (!priv->wov_substream) {
612 dev_warn(priv->dev, "no pcm substream\n");
616 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM))
617 ret = wov_read_audio_shm(priv);
619 ret = wov_read_audio(priv);
622 schedule_delayed_work(&priv->wov_copy_work,
623 msecs_to_jiffies(10));
625 dev_err(priv->dev, "failed to read audio data\n");
627 mutex_unlock(&priv->wov_dma_lock);
630 static int wov_enable_get(struct snd_kcontrol *kcontrol,
631 struct snd_ctl_elem_value *ucontrol)
633 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
634 struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c);
636 ucontrol->value.integer.value[0] = priv->wov_enabled;
640 static int wov_enable_put(struct snd_kcontrol *kcontrol,
641 struct snd_ctl_elem_value *ucontrol)
643 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
644 struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c);
645 int enabled = ucontrol->value.integer.value[0];
646 struct ec_param_ec_codec_wov p;
649 if (priv->wov_enabled != enabled) {
651 p.cmd = EC_CODEC_WOV_ENABLE;
653 p.cmd = EC_CODEC_WOV_DISABLE;
655 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
656 (uint8_t *)&p, sizeof(p), NULL, 0);
658 dev_err(priv->dev, "failed to %s wov\n",
659 enabled ? "enable" : "disable");
663 priv->wov_enabled = enabled;
669 static int wov_set_lang_shm(struct cros_ec_codec_priv *priv,
670 uint8_t *buf, size_t size, uint8_t *digest)
672 struct ec_param_ec_codec_wov p;
673 struct ec_param_ec_codec_wov_set_lang_shm *pp = &p.set_lang_shm_param;
676 if (size > priv->wov_lang_shm_len) {
677 dev_err(priv->dev, "no enough SHM size: %d\n",
678 priv->wov_lang_shm_len);
682 switch (priv->wov_lang_shm_type) {
683 case EC_CODEC_SHM_TYPE_EC_RAM:
684 memcpy_toio((void __force __iomem *)priv->wov_lang_shm_p,
686 memset_io((void __force __iomem *)priv->wov_lang_shm_p + size,
687 0, priv->wov_lang_shm_len - size);
689 case EC_CODEC_SHM_TYPE_SYSTEM_RAM:
690 memcpy(priv->wov_lang_shm_p, buf, size);
691 memset(priv->wov_lang_shm_p + size, 0,
692 priv->wov_lang_shm_len - size);
694 /* make sure write to memory before calling host command */
699 p.cmd = EC_CODEC_WOV_SET_LANG_SHM;
700 memcpy(pp->hash, digest, SHA256_DIGEST_SIZE);
701 pp->total_len = size;
702 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
703 (uint8_t *)&p, sizeof(p), NULL, 0);
705 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG_SHM\n");
712 static int wov_set_lang(struct cros_ec_codec_priv *priv,
713 uint8_t *buf, size_t size, uint8_t *digest)
715 struct ec_param_ec_codec_wov p;
716 struct ec_param_ec_codec_wov_set_lang *pp = &p.set_lang_param;
720 for (i = 0; i < size; i += req) {
721 req = min(size - i, ARRAY_SIZE(pp->buf));
723 p.cmd = EC_CODEC_WOV_SET_LANG;
724 memcpy(pp->hash, digest, SHA256_DIGEST_SIZE);
725 pp->total_len = size;
727 memcpy(pp->buf, buf + i, req);
729 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
730 (uint8_t *)&p, sizeof(p), NULL, 0);
732 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG\n");
740 static int wov_hotword_model_put(struct snd_kcontrol *kcontrol,
741 const unsigned int __user *bytes,
744 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
745 struct cros_ec_codec_priv *priv =
746 snd_soc_component_get_drvdata(component);
747 struct ec_param_ec_codec_wov p;
748 struct ec_response_ec_codec_wov_get_lang r;
749 uint8_t digest[SHA256_DIGEST_SIZE];
753 /* Skips the TLV header. */
757 dev_dbg(priv->dev, "%s: size=%d\n", __func__, size);
759 buf = memdup_user(bytes, size);
763 sha256(buf, size, digest);
764 dev_dbg(priv->dev, "hash=%*phN\n", SHA256_DIGEST_SIZE, digest);
766 p.cmd = EC_CODEC_WOV_GET_LANG;
767 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
768 (uint8_t *)&p, sizeof(p),
769 (uint8_t *)&r, sizeof(r));
773 if (memcmp(digest, r.hash, SHA256_DIGEST_SIZE) == 0) {
774 dev_dbg(priv->dev, "not updated");
778 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM))
779 ret = wov_set_lang_shm(priv, buf, size, digest);
781 ret = wov_set_lang(priv, buf, size, digest);
788 static struct snd_kcontrol_new wov_controls[] = {
789 SOC_SINGLE_BOOL_EXT("Wake-on-Voice Switch", 0,
790 wov_enable_get, wov_enable_put),
791 SND_SOC_BYTES_TLV("Hotword Model", 0x11000, NULL,
792 wov_hotword_model_put),
795 static struct snd_soc_dai_driver wov_dai_driver = {
796 .name = "Wake on Voice",
798 .stream_name = "WoV Capture",
801 .rates = SNDRV_PCM_RATE_16000,
802 .formats = SNDRV_PCM_FMTBIT_S16_LE,
806 static int wov_host_event(struct notifier_block *nb,
807 unsigned long queued_during_suspend, void *notify)
809 struct cros_ec_codec_priv *priv =
810 container_of(nb, struct cros_ec_codec_priv, wov_notifier);
813 dev_dbg(priv->dev, "%s\n", __func__);
815 host_event = cros_ec_get_host_event(priv->ec_device);
816 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_WOV)) {
817 schedule_delayed_work(&priv->wov_copy_work, 0);
824 static int wov_probe(struct snd_soc_component *component)
826 struct cros_ec_codec_priv *priv =
827 snd_soc_component_get_drvdata(component);
830 mutex_init(&priv->wov_dma_lock);
831 INIT_DELAYED_WORK(&priv->wov_copy_work, wov_copy_work);
833 priv->wov_notifier.notifier_call = wov_host_event;
834 ret = blocking_notifier_chain_register(
835 &priv->ec_device->event_notifier, &priv->wov_notifier);
839 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM)) {
840 priv->wov_lang_shm_p = wov_map_shm(priv,
841 EC_CODEC_SHM_ID_WOV_LANG,
842 &priv->wov_lang_shm_len,
843 &priv->wov_lang_shm_type);
844 if (!priv->wov_lang_shm_p)
848 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM)) {
849 priv->wov_audio_shm_p = wov_map_shm(priv,
850 EC_CODEC_SHM_ID_WOV_AUDIO,
851 &priv->wov_audio_shm_len,
852 &priv->wov_audio_shm_type);
853 if (!priv->wov_audio_shm_p)
857 return dmic_probe(component);
860 static void wov_remove(struct snd_soc_component *component)
862 struct cros_ec_codec_priv *priv =
863 snd_soc_component_get_drvdata(component);
865 blocking_notifier_chain_unregister(
866 &priv->ec_device->event_notifier, &priv->wov_notifier);
869 static int wov_pcm_open(struct snd_soc_component *component,
870 struct snd_pcm_substream *substream)
872 static const struct snd_pcm_hardware hw_param = {
873 .info = SNDRV_PCM_INFO_MMAP |
874 SNDRV_PCM_INFO_INTERLEAVED |
875 SNDRV_PCM_INFO_MMAP_VALID,
876 .formats = SNDRV_PCM_FMTBIT_S16_LE,
877 .rates = SNDRV_PCM_RATE_16000,
880 .period_bytes_min = PAGE_SIZE,
881 .period_bytes_max = 0x20000 / 8,
884 .buffer_bytes_max = 0x20000,
887 return snd_soc_set_runtime_hwparams(substream, &hw_param);
890 static int wov_pcm_hw_params(struct snd_soc_component *component,
891 struct snd_pcm_substream *substream,
892 struct snd_pcm_hw_params *hw_params)
894 struct cros_ec_codec_priv *priv =
895 snd_soc_component_get_drvdata(component);
897 mutex_lock(&priv->wov_dma_lock);
898 priv->wov_substream = substream;
899 priv->wov_rp = priv->wov_wp = 0;
900 priv->wov_dma_offset = 0;
901 priv->wov_burst_read = true;
902 mutex_unlock(&priv->wov_dma_lock);
907 static int wov_pcm_hw_free(struct snd_soc_component *component,
908 struct snd_pcm_substream *substream)
910 struct cros_ec_codec_priv *priv =
911 snd_soc_component_get_drvdata(component);
913 mutex_lock(&priv->wov_dma_lock);
914 wov_queue_dequeue(priv, wov_queue_size(priv));
915 priv->wov_substream = NULL;
916 mutex_unlock(&priv->wov_dma_lock);
918 cancel_delayed_work_sync(&priv->wov_copy_work);
923 static snd_pcm_uframes_t wov_pcm_pointer(struct snd_soc_component *component,
924 struct snd_pcm_substream *substream)
926 struct snd_pcm_runtime *runtime = substream->runtime;
927 struct cros_ec_codec_priv *priv =
928 snd_soc_component_get_drvdata(component);
930 return bytes_to_frames(runtime, priv->wov_dma_offset);
933 static int wov_pcm_new(struct snd_soc_component *component,
934 struct snd_soc_pcm_runtime *rtd)
936 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_VMALLOC,
941 static const struct snd_soc_component_driver wov_component_driver = {
943 .remove = wov_remove,
944 .controls = wov_controls,
945 .num_controls = ARRAY_SIZE(wov_controls),
946 .open = wov_pcm_open,
947 .hw_params = wov_pcm_hw_params,
948 .hw_free = wov_pcm_hw_free,
949 .pointer = wov_pcm_pointer,
950 .pcm_construct = wov_pcm_new,
953 static int cros_ec_codec_platform_probe(struct platform_device *pdev)
955 struct device *dev = &pdev->dev;
956 struct cros_ec_device *ec_device = dev_get_drvdata(pdev->dev.parent);
957 struct cros_ec_codec_priv *priv;
958 struct ec_param_ec_codec p;
959 struct ec_response_ec_codec_get_capabilities r;
962 struct device_node *node;
965 const __be32 *regaddr_p;
968 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
973 regaddr_p = of_get_address(dev->of_node, 0, &ec_shm_size, NULL);
975 priv->ec_shm_addr = of_read_number(regaddr_p, 2);
976 priv->ec_shm_len = ec_shm_size;
978 dev_dbg(dev, "ec_shm_addr=%#llx len=%#x\n",
979 priv->ec_shm_addr, priv->ec_shm_len);
982 node = of_parse_phandle(dev->of_node, "memory-region", 0);
984 ret = of_address_to_resource(node, 0, &res);
986 priv->ap_shm_phys_addr = res.start;
987 priv->ap_shm_len = resource_size(&res);
989 (uint64_t)(uintptr_t)devm_ioremap_wc(
990 dev, priv->ap_shm_phys_addr,
992 priv->ap_shm_last_alloc = priv->ap_shm_phys_addr;
994 dev_dbg(dev, "ap_shm_phys_addr=%#llx len=%#x\n",
995 priv->ap_shm_phys_addr, priv->ap_shm_len);
1001 priv->ec_device = ec_device;
1002 atomic_set(&priv->dmic_probed, 0);
1004 p.cmd = EC_CODEC_GET_CAPABILITIES;
1005 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC,
1006 (uint8_t *)&p, sizeof(p),
1007 (uint8_t *)&r, sizeof(r));
1009 dev_err(dev, "failed to EC_CODEC_GET_CAPABILITIES\n");
1012 priv->ec_capabilities = r.capabilities;
1014 /* Reset EC codec i2s rx. */
1015 p.cmd = EC_CODEC_I2S_RX_RESET;
1016 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
1017 (uint8_t *)&p, sizeof(p), NULL, 0);
1018 if (ret == -ENOPROTOOPT) {
1020 "Missing reset command. Please update EC firmware.\n");
1022 dev_err(dev, "failed to EC_CODEC_I2S_RESET: %d\n", ret);
1026 platform_set_drvdata(pdev, priv);
1028 ret = devm_snd_soc_register_component(dev, &i2s_rx_component_driver,
1029 &i2s_rx_dai_driver, 1);
1033 return devm_snd_soc_register_component(dev, &wov_component_driver,
1034 &wov_dai_driver, 1);
1038 static const struct of_device_id cros_ec_codec_of_match[] = {
1039 { .compatible = "google,cros-ec-codec" },
1042 MODULE_DEVICE_TABLE(of, cros_ec_codec_of_match);
1046 static const struct acpi_device_id cros_ec_codec_acpi_id[] = {
1050 MODULE_DEVICE_TABLE(acpi, cros_ec_codec_acpi_id);
1053 static struct platform_driver cros_ec_codec_platform_driver = {
1055 .name = "cros-ec-codec",
1056 .of_match_table = of_match_ptr(cros_ec_codec_of_match),
1057 .acpi_match_table = ACPI_PTR(cros_ec_codec_acpi_id),
1059 .probe = cros_ec_codec_platform_probe,
1062 module_platform_driver(cros_ec_codec_platform_driver);
1064 MODULE_LICENSE("GPL v2");
1065 MODULE_DESCRIPTION("ChromeOS EC codec driver");
1066 MODULE_AUTHOR("Cheng-Yi Chiang <cychiang@chromium.org>");
1067 MODULE_ALIAS("platform:cros-ec-codec");