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;
49 uint32_t i2s_rx_bclk_ratio;
53 uint8_t *wov_audio_shm_p;
54 uint32_t wov_audio_shm_len;
55 uint8_t wov_audio_shm_type;
56 uint8_t *wov_lang_shm_p;
57 uint32_t wov_lang_shm_len;
58 uint8_t wov_lang_shm_type;
60 struct mutex wov_dma_lock;
61 uint8_t wov_buf[64000];
62 uint32_t wov_rp, wov_wp;
63 size_t wov_dma_offset;
65 struct snd_pcm_substream *wov_substream;
66 struct delayed_work wov_copy_work;
67 struct notifier_block wov_notifier;
70 static int ec_codec_capable(struct cros_ec_codec_priv *priv, uint8_t cap)
72 return priv->ec_capabilities & BIT(cap);
75 static int send_ec_host_command(struct cros_ec_device *ec_dev, uint32_t cmd,
76 uint8_t *out, size_t outsize,
77 uint8_t *in, size_t insize)
80 struct cros_ec_command *msg;
82 msg = kmalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
88 msg->outsize = outsize;
92 memcpy(msg->data, out, outsize);
94 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
99 memcpy(in, msg->data, insize);
107 static int calculate_sha256(struct cros_ec_codec_priv *priv,
108 uint8_t *buf, uint32_t size, uint8_t *digest)
110 struct crypto_shash *tfm;
112 tfm = crypto_alloc_shash("sha256", CRYPTO_ALG_TYPE_SHASH, 0);
114 dev_err(priv->dev, "can't alloc shash\n");
119 SHASH_DESC_ON_STACK(desc, tfm);
123 crypto_shash_digest(desc, buf, size, digest);
124 shash_desc_zero(desc);
127 crypto_free_shash(tfm);
133 bin2hex(digest_str, digest, 32);
135 dev_dbg(priv->dev, "hash=%s\n", digest_str);
142 static int dmic_get_gain(struct snd_kcontrol *kcontrol,
143 struct snd_ctl_elem_value *ucontrol)
145 struct snd_soc_component *component =
146 snd_soc_kcontrol_component(kcontrol);
147 struct cros_ec_codec_priv *priv =
148 snd_soc_component_get_drvdata(component);
149 struct ec_param_ec_codec_dmic p;
150 struct ec_response_ec_codec_dmic_get_gain_idx r;
153 p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX;
154 p.get_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_0;
155 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
156 (uint8_t *)&p, sizeof(p),
157 (uint8_t *)&r, sizeof(r));
160 ucontrol->value.integer.value[0] = r.gain;
162 p.cmd = EC_CODEC_DMIC_GET_GAIN_IDX;
163 p.get_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1;
164 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
165 (uint8_t *)&p, sizeof(p),
166 (uint8_t *)&r, sizeof(r));
169 ucontrol->value.integer.value[1] = r.gain;
174 static int dmic_put_gain(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
177 struct snd_soc_component *component =
178 snd_soc_kcontrol_component(kcontrol);
179 struct cros_ec_codec_priv *priv =
180 snd_soc_component_get_drvdata(component);
181 struct soc_mixer_control *control =
182 (struct soc_mixer_control *)kcontrol->private_value;
183 int max_dmic_gain = control->max;
184 int left = ucontrol->value.integer.value[0];
185 int right = ucontrol->value.integer.value[1];
186 struct ec_param_ec_codec_dmic p;
189 if (left > max_dmic_gain || right > max_dmic_gain)
192 dev_dbg(component->dev, "set mic gain to %u, %u\n", left, right);
194 p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX;
195 p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_0;
196 p.set_gain_idx_param.gain = left;
197 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
198 (uint8_t *)&p, sizeof(p), NULL, 0);
202 p.cmd = EC_CODEC_DMIC_SET_GAIN_IDX;
203 p.set_gain_idx_param.channel = EC_CODEC_DMIC_CHANNEL_1;
204 p.set_gain_idx_param.gain = right;
205 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
206 (uint8_t *)&p, sizeof(p), NULL, 0);
209 static const DECLARE_TLV_DB_SCALE(dmic_gain_tlv, 0, 100, 0);
215 static struct snd_kcontrol_new dmic_controls[] = {
217 SOC_DOUBLE_EXT_TLV("EC Mic Gain", SND_SOC_NOPM, SND_SOC_NOPM,
218 0, 0, 0, dmic_get_gain, dmic_put_gain,
222 static int dmic_probe(struct snd_soc_component *component)
224 struct cros_ec_codec_priv *priv =
225 snd_soc_component_get_drvdata(component);
226 struct device *dev = priv->dev;
227 struct soc_mixer_control *control;
228 struct ec_param_ec_codec_dmic p;
229 struct ec_response_ec_codec_dmic_get_max_gain r;
232 if (!atomic_add_unless(&priv->dmic_probed, 1, 1))
235 p.cmd = EC_CODEC_DMIC_GET_MAX_GAIN;
237 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
238 (uint8_t *)&p, sizeof(p),
239 (uint8_t *)&r, sizeof(r));
241 dev_warn(dev, "get_max_gain() unsupported\n");
245 dev_dbg(dev, "max gain = %d\n", r.max_gain);
247 control = (struct soc_mixer_control *)
248 dmic_controls[DMIC_CTL_GAIN].private_value;
249 control->max = r.max_gain;
250 control->platform_max = r.max_gain;
252 return snd_soc_add_component_controls(component,
253 &dmic_controls[DMIC_CTL_GAIN], 1);
256 static int i2s_rx_hw_params(struct snd_pcm_substream *substream,
257 struct snd_pcm_hw_params *params,
258 struct snd_soc_dai *dai)
260 struct snd_soc_component *component = dai->component;
261 struct cros_ec_codec_priv *priv =
262 snd_soc_component_get_drvdata(component);
263 struct ec_param_ec_codec_i2s_rx p;
264 enum ec_codec_i2s_rx_sample_depth depth;
268 if (params_rate(params) != 48000)
271 switch (params_format(params)) {
272 case SNDRV_PCM_FORMAT_S16_LE:
273 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_16;
275 case SNDRV_PCM_FORMAT_S24_LE:
276 depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_24;
282 dev_dbg(component->dev, "set depth to %u\n", depth);
284 p.cmd = EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH;
285 p.set_sample_depth_param.depth = depth;
286 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
287 (uint8_t *)&p, sizeof(p), NULL, 0);
291 if (priv->i2s_rx_bclk_ratio)
292 bclk = params_rate(params) * priv->i2s_rx_bclk_ratio;
294 bclk = snd_soc_params_to_bclk(params);
296 dev_dbg(component->dev, "set bclk to %u\n", bclk);
298 p.cmd = EC_CODEC_I2S_RX_SET_BCLK;
299 p.set_bclk_param.bclk = bclk;
300 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
301 (uint8_t *)&p, sizeof(p), NULL, 0);
304 static int i2s_rx_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
306 struct snd_soc_component *component = dai->component;
307 struct cros_ec_codec_priv *priv =
308 snd_soc_component_get_drvdata(component);
310 priv->i2s_rx_bclk_ratio = ratio;
314 static int i2s_rx_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
316 struct snd_soc_component *component = dai->component;
317 struct cros_ec_codec_priv *priv =
318 snd_soc_component_get_drvdata(component);
319 struct ec_param_ec_codec_i2s_rx p;
320 enum ec_codec_i2s_rx_daifmt daifmt;
322 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
323 case SND_SOC_DAIFMT_CBS_CFS:
329 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
330 case SND_SOC_DAIFMT_NB_NF:
336 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
337 case SND_SOC_DAIFMT_I2S:
338 daifmt = EC_CODEC_I2S_RX_DAIFMT_I2S;
340 case SND_SOC_DAIFMT_RIGHT_J:
341 daifmt = EC_CODEC_I2S_RX_DAIFMT_RIGHT_J;
343 case SND_SOC_DAIFMT_LEFT_J:
344 daifmt = EC_CODEC_I2S_RX_DAIFMT_LEFT_J;
350 dev_dbg(component->dev, "set format to %u\n", daifmt);
352 p.cmd = EC_CODEC_I2S_RX_SET_DAIFMT;
353 p.set_daifmt_param.daifmt = daifmt;
354 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
355 (uint8_t *)&p, sizeof(p), NULL, 0);
358 static const struct snd_soc_dai_ops i2s_rx_dai_ops = {
359 .hw_params = i2s_rx_hw_params,
360 .set_fmt = i2s_rx_set_fmt,
361 .set_bclk_ratio = i2s_rx_set_bclk_ratio,
364 static int i2s_rx_event(struct snd_soc_dapm_widget *w,
365 struct snd_kcontrol *kcontrol, int event)
367 struct snd_soc_component *component =
368 snd_soc_dapm_to_component(w->dapm);
369 struct cros_ec_codec_priv *priv =
370 snd_soc_component_get_drvdata(component);
371 struct ec_param_ec_codec_i2s_rx p;
374 case SND_SOC_DAPM_PRE_PMU:
375 dev_dbg(component->dev, "enable I2S RX\n");
376 p.cmd = EC_CODEC_I2S_RX_ENABLE;
378 case SND_SOC_DAPM_PRE_PMD:
379 dev_dbg(component->dev, "disable I2S RX\n");
380 p.cmd = EC_CODEC_I2S_RX_DISABLE;
386 return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
387 (uint8_t *)&p, sizeof(p), NULL, 0);
390 static struct snd_soc_dapm_widget i2s_rx_dapm_widgets[] = {
391 SND_SOC_DAPM_INPUT("DMIC"),
392 SND_SOC_DAPM_SUPPLY("I2S RX Enable", SND_SOC_NOPM, 0, 0, i2s_rx_event,
393 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
394 SND_SOC_DAPM_AIF_OUT("I2S RX", "I2S Capture", 0, SND_SOC_NOPM, 0, 0),
397 static struct snd_soc_dapm_route i2s_rx_dapm_routes[] = {
398 {"I2S RX", NULL, "DMIC"},
399 {"I2S RX", NULL, "I2S RX Enable"},
402 static struct snd_soc_dai_driver i2s_rx_dai_driver = {
403 .name = "EC Codec I2S RX",
405 .stream_name = "I2S Capture",
408 .rates = SNDRV_PCM_RATE_48000,
409 .formats = SNDRV_PCM_FMTBIT_S16_LE |
410 SNDRV_PCM_FMTBIT_S24_LE,
412 .ops = &i2s_rx_dai_ops,
415 static int i2s_rx_probe(struct snd_soc_component *component)
417 return dmic_probe(component);
420 static const struct snd_soc_component_driver i2s_rx_component_driver = {
421 .probe = i2s_rx_probe,
422 .dapm_widgets = i2s_rx_dapm_widgets,
423 .num_dapm_widgets = ARRAY_SIZE(i2s_rx_dapm_widgets),
424 .dapm_routes = i2s_rx_dapm_routes,
425 .num_dapm_routes = ARRAY_SIZE(i2s_rx_dapm_routes),
428 static void *wov_map_shm(struct cros_ec_codec_priv *priv,
429 uint8_t shm_id, uint32_t *len, uint8_t *type)
431 struct ec_param_ec_codec p;
432 struct ec_response_ec_codec_get_shm_addr r;
433 uint32_t req, offset;
435 p.cmd = EC_CODEC_GET_SHM_ADDR;
436 p.get_shm_addr_param.shm_id = shm_id;
437 if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC,
438 (uint8_t *)&p, sizeof(p),
439 (uint8_t *)&r, sizeof(r)) < 0) {
440 dev_err(priv->dev, "failed to EC_CODEC_GET_SHM_ADDR\n");
444 dev_dbg(priv->dev, "phys_addr=%#llx, len=%#x\n", r.phys_addr, r.len);
450 case EC_CODEC_SHM_TYPE_EC_RAM:
451 return (void __force *)devm_ioremap_wc(priv->dev,
452 r.phys_addr + priv->ec_shm_addr, r.len);
453 case EC_CODEC_SHM_TYPE_SYSTEM_RAM:
455 dev_err(priv->dev, "unknown status\n");
459 req = round_up(r.len, PAGE_SIZE);
460 dev_dbg(priv->dev, "round up from %u to %u\n", r.len, req);
462 if (priv->ap_shm_last_alloc + req >
463 priv->ap_shm_phys_addr + priv->ap_shm_len) {
464 dev_err(priv->dev, "insufficient space for AP SHM\n");
468 dev_dbg(priv->dev, "alloc AP SHM addr=%#llx, len=%#x\n",
469 priv->ap_shm_last_alloc, req);
471 p.cmd = EC_CODEC_SET_SHM_ADDR;
472 p.set_shm_addr_param.phys_addr = priv->ap_shm_last_alloc;
473 p.set_shm_addr_param.len = req;
474 p.set_shm_addr_param.shm_id = shm_id;
475 if (send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC,
476 (uint8_t *)&p, sizeof(p),
478 dev_err(priv->dev, "failed to EC_CODEC_SET_SHM_ADDR\n");
483 * Note: EC codec only requests for `r.len' but we allocate
484 * round up PAGE_SIZE `req'.
486 offset = priv->ap_shm_last_alloc - priv->ap_shm_phys_addr;
487 priv->ap_shm_last_alloc += req;
489 return (void *)(uintptr_t)(priv->ap_shm_addr + offset);
495 static bool wov_queue_full(struct cros_ec_codec_priv *priv)
497 return ((priv->wov_wp + 1) % sizeof(priv->wov_buf)) == priv->wov_rp;
500 static size_t wov_queue_size(struct cros_ec_codec_priv *priv)
502 if (priv->wov_wp >= priv->wov_rp)
503 return priv->wov_wp - priv->wov_rp;
505 return sizeof(priv->wov_buf) - priv->wov_rp + priv->wov_wp;
508 static void wov_queue_dequeue(struct cros_ec_codec_priv *priv, size_t len)
510 struct snd_pcm_runtime *runtime = priv->wov_substream->runtime;
514 req = min(len, runtime->dma_bytes - priv->wov_dma_offset);
515 if (priv->wov_wp >= priv->wov_rp)
516 req = min(req, (size_t)priv->wov_wp - priv->wov_rp);
518 req = min(req, sizeof(priv->wov_buf) - priv->wov_rp);
520 memcpy(runtime->dma_area + priv->wov_dma_offset,
521 priv->wov_buf + priv->wov_rp, req);
523 priv->wov_dma_offset += req;
524 if (priv->wov_dma_offset == runtime->dma_bytes)
525 priv->wov_dma_offset = 0;
528 if (priv->wov_rp == sizeof(priv->wov_buf))
534 snd_pcm_period_elapsed(priv->wov_substream);
537 static void wov_queue_try_dequeue(struct cros_ec_codec_priv *priv)
539 size_t period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream);
541 while (period_bytes && wov_queue_size(priv) >= period_bytes) {
542 wov_queue_dequeue(priv, period_bytes);
543 period_bytes = snd_pcm_lib_period_bytes(priv->wov_substream);
547 static void wov_queue_enqueue(struct cros_ec_codec_priv *priv,
548 uint8_t *addr, size_t len, bool iomem)
553 if (wov_queue_full(priv)) {
554 wov_queue_try_dequeue(priv);
556 if (wov_queue_full(priv)) {
557 dev_err(priv->dev, "overrun detected\n");
562 if (priv->wov_wp >= priv->wov_rp)
563 req = sizeof(priv->wov_buf) - priv->wov_wp;
565 /* Note: waste 1-byte to differentiate full and empty */
566 req = priv->wov_rp - priv->wov_wp - 1;
570 memcpy_fromio(priv->wov_buf + priv->wov_wp,
571 (void __force __iomem *)addr, req);
573 memcpy(priv->wov_buf + priv->wov_wp, addr, req);
576 if (priv->wov_wp == sizeof(priv->wov_buf))
583 wov_queue_try_dequeue(priv);
586 static int wov_read_audio_shm(struct cros_ec_codec_priv *priv)
588 struct ec_param_ec_codec_wov p;
589 struct ec_response_ec_codec_wov_read_audio_shm r;
592 p.cmd = EC_CODEC_WOV_READ_AUDIO_SHM;
593 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
594 (uint8_t *)&p, sizeof(p),
595 (uint8_t *)&r, sizeof(r));
597 dev_err(priv->dev, "failed to EC_CODEC_WOV_READ_AUDIO_SHM\n");
602 dev_dbg(priv->dev, "no data, sleep\n");
604 wov_queue_enqueue(priv, priv->wov_audio_shm_p + r.offset, r.len,
605 priv->wov_audio_shm_type == EC_CODEC_SHM_TYPE_EC_RAM);
609 static int wov_read_audio(struct cros_ec_codec_priv *priv)
611 struct ec_param_ec_codec_wov p;
612 struct ec_response_ec_codec_wov_read_audio r;
613 int remain = priv->wov_burst_read ? 16000 : 320;
616 while (remain >= 0) {
617 p.cmd = EC_CODEC_WOV_READ_AUDIO;
618 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
619 (uint8_t *)&p, sizeof(p),
620 (uint8_t *)&r, sizeof(r));
623 "failed to EC_CODEC_WOV_READ_AUDIO\n");
628 dev_dbg(priv->dev, "no data, sleep\n");
629 priv->wov_burst_read = false;
633 wov_queue_enqueue(priv, r.buf, r.len, false);
640 static void wov_copy_work(struct work_struct *w)
642 struct cros_ec_codec_priv *priv =
643 container_of(w, struct cros_ec_codec_priv, wov_copy_work.work);
646 mutex_lock(&priv->wov_dma_lock);
647 if (!priv->wov_substream) {
648 dev_warn(priv->dev, "no pcm substream\n");
652 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM))
653 ret = wov_read_audio_shm(priv);
655 ret = wov_read_audio(priv);
658 schedule_delayed_work(&priv->wov_copy_work,
659 msecs_to_jiffies(10));
661 dev_err(priv->dev, "failed to read audio data\n");
663 mutex_unlock(&priv->wov_dma_lock);
666 static int wov_enable_get(struct snd_kcontrol *kcontrol,
667 struct snd_ctl_elem_value *ucontrol)
669 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
670 struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c);
672 ucontrol->value.integer.value[0] = priv->wov_enabled;
676 static int wov_enable_put(struct snd_kcontrol *kcontrol,
677 struct snd_ctl_elem_value *ucontrol)
679 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
680 struct cros_ec_codec_priv *priv = snd_soc_component_get_drvdata(c);
681 int enabled = ucontrol->value.integer.value[0];
682 struct ec_param_ec_codec_wov p;
685 if (priv->wov_enabled != enabled) {
687 p.cmd = EC_CODEC_WOV_ENABLE;
689 p.cmd = EC_CODEC_WOV_DISABLE;
691 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
692 (uint8_t *)&p, sizeof(p), NULL, 0);
694 dev_err(priv->dev, "failed to %s wov\n",
695 enabled ? "enable" : "disable");
699 priv->wov_enabled = enabled;
705 static int wov_set_lang_shm(struct cros_ec_codec_priv *priv,
706 uint8_t *buf, size_t size, uint8_t *digest)
708 struct ec_param_ec_codec_wov p;
709 struct ec_param_ec_codec_wov_set_lang_shm *pp = &p.set_lang_shm_param;
712 if (size > priv->wov_lang_shm_len) {
713 dev_err(priv->dev, "no enough SHM size: %d\n",
714 priv->wov_lang_shm_len);
718 switch (priv->wov_lang_shm_type) {
719 case EC_CODEC_SHM_TYPE_EC_RAM:
720 memcpy_toio((void __force __iomem *)priv->wov_lang_shm_p,
722 memset_io((void __force __iomem *)priv->wov_lang_shm_p + size,
723 0, priv->wov_lang_shm_len - size);
725 case EC_CODEC_SHM_TYPE_SYSTEM_RAM:
726 memcpy(priv->wov_lang_shm_p, buf, size);
727 memset(priv->wov_lang_shm_p + size, 0,
728 priv->wov_lang_shm_len - size);
730 /* make sure write to memory before calling host command */
735 p.cmd = EC_CODEC_WOV_SET_LANG_SHM;
736 memcpy(pp->hash, digest, SHA256_DIGEST_SIZE);
737 pp->total_len = size;
738 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
739 (uint8_t *)&p, sizeof(p), NULL, 0);
741 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG_SHM\n");
748 static int wov_set_lang(struct cros_ec_codec_priv *priv,
749 uint8_t *buf, size_t size, uint8_t *digest)
751 struct ec_param_ec_codec_wov p;
752 struct ec_param_ec_codec_wov_set_lang *pp = &p.set_lang_param;
756 for (i = 0; i < size; i += req) {
757 req = min(size - i, ARRAY_SIZE(pp->buf));
759 p.cmd = EC_CODEC_WOV_SET_LANG;
760 memcpy(pp->hash, digest, SHA256_DIGEST_SIZE);
761 pp->total_len = size;
763 memcpy(pp->buf, buf + i, req);
765 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
766 (uint8_t *)&p, sizeof(p), NULL, 0);
768 dev_err(priv->dev, "failed to EC_CODEC_WOV_SET_LANG\n");
776 static int wov_hotword_model_put(struct snd_kcontrol *kcontrol,
777 const unsigned int __user *bytes,
780 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
781 struct cros_ec_codec_priv *priv =
782 snd_soc_component_get_drvdata(component);
783 struct ec_param_ec_codec_wov p;
784 struct ec_response_ec_codec_wov_get_lang r;
785 uint8_t digest[SHA256_DIGEST_SIZE];
789 /* Skips the TLV header. */
793 dev_dbg(priv->dev, "%s: size=%d\n", __func__, size);
795 buf = memdup_user(bytes, size);
799 ret = calculate_sha256(priv, buf, size, digest);
803 p.cmd = EC_CODEC_WOV_GET_LANG;
804 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,
805 (uint8_t *)&p, sizeof(p),
806 (uint8_t *)&r, sizeof(r));
810 if (memcmp(digest, r.hash, SHA256_DIGEST_SIZE) == 0) {
811 dev_dbg(priv->dev, "not updated");
815 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM))
816 ret = wov_set_lang_shm(priv, buf, size, digest);
818 ret = wov_set_lang(priv, buf, size, digest);
825 static struct snd_kcontrol_new wov_controls[] = {
826 SOC_SINGLE_BOOL_EXT("Wake-on-Voice Switch", 0,
827 wov_enable_get, wov_enable_put),
828 SND_SOC_BYTES_TLV("Hotword Model", 0x11000, NULL,
829 wov_hotword_model_put),
832 static struct snd_soc_dai_driver wov_dai_driver = {
833 .name = "Wake on Voice",
835 .stream_name = "WoV Capture",
838 .rates = SNDRV_PCM_RATE_16000,
839 .formats = SNDRV_PCM_FMTBIT_S16_LE,
843 static int wov_host_event(struct notifier_block *nb,
844 unsigned long queued_during_suspend, void *notify)
846 struct cros_ec_codec_priv *priv =
847 container_of(nb, struct cros_ec_codec_priv, wov_notifier);
850 dev_dbg(priv->dev, "%s\n", __func__);
852 host_event = cros_ec_get_host_event(priv->ec_device);
853 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_WOV)) {
854 schedule_delayed_work(&priv->wov_copy_work, 0);
861 static int wov_probe(struct snd_soc_component *component)
863 struct cros_ec_codec_priv *priv =
864 snd_soc_component_get_drvdata(component);
867 mutex_init(&priv->wov_dma_lock);
868 INIT_DELAYED_WORK(&priv->wov_copy_work, wov_copy_work);
870 priv->wov_notifier.notifier_call = wov_host_event;
871 ret = blocking_notifier_chain_register(
872 &priv->ec_device->event_notifier, &priv->wov_notifier);
876 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_LANG_SHM)) {
877 priv->wov_lang_shm_p = wov_map_shm(priv,
878 EC_CODEC_SHM_ID_WOV_LANG,
879 &priv->wov_lang_shm_len,
880 &priv->wov_lang_shm_type);
881 if (!priv->wov_lang_shm_p)
885 if (ec_codec_capable(priv, EC_CODEC_CAP_WOV_AUDIO_SHM)) {
886 priv->wov_audio_shm_p = wov_map_shm(priv,
887 EC_CODEC_SHM_ID_WOV_AUDIO,
888 &priv->wov_audio_shm_len,
889 &priv->wov_audio_shm_type);
890 if (!priv->wov_audio_shm_p)
894 return dmic_probe(component);
897 static void wov_remove(struct snd_soc_component *component)
899 struct cros_ec_codec_priv *priv =
900 snd_soc_component_get_drvdata(component);
902 blocking_notifier_chain_unregister(
903 &priv->ec_device->event_notifier, &priv->wov_notifier);
906 static int wov_pcm_open(struct snd_soc_component *component,
907 struct snd_pcm_substream *substream)
909 static const struct snd_pcm_hardware hw_param = {
910 .info = SNDRV_PCM_INFO_MMAP |
911 SNDRV_PCM_INFO_INTERLEAVED |
912 SNDRV_PCM_INFO_MMAP_VALID,
913 .formats = SNDRV_PCM_FMTBIT_S16_LE,
914 .rates = SNDRV_PCM_RATE_16000,
917 .period_bytes_min = PAGE_SIZE,
918 .period_bytes_max = 0x20000 / 8,
921 .buffer_bytes_max = 0x20000,
924 return snd_soc_set_runtime_hwparams(substream, &hw_param);
927 static int wov_pcm_hw_params(struct snd_soc_component *component,
928 struct snd_pcm_substream *substream,
929 struct snd_pcm_hw_params *hw_params)
931 struct cros_ec_codec_priv *priv =
932 snd_soc_component_get_drvdata(component);
934 mutex_lock(&priv->wov_dma_lock);
935 priv->wov_substream = substream;
936 priv->wov_rp = priv->wov_wp = 0;
937 priv->wov_dma_offset = 0;
938 priv->wov_burst_read = true;
939 mutex_unlock(&priv->wov_dma_lock);
944 static int wov_pcm_hw_free(struct snd_soc_component *component,
945 struct snd_pcm_substream *substream)
947 struct cros_ec_codec_priv *priv =
948 snd_soc_component_get_drvdata(component);
950 mutex_lock(&priv->wov_dma_lock);
951 wov_queue_dequeue(priv, wov_queue_size(priv));
952 priv->wov_substream = NULL;
953 mutex_unlock(&priv->wov_dma_lock);
955 cancel_delayed_work_sync(&priv->wov_copy_work);
960 static snd_pcm_uframes_t wov_pcm_pointer(struct snd_soc_component *component,
961 struct snd_pcm_substream *substream)
963 struct snd_pcm_runtime *runtime = substream->runtime;
964 struct cros_ec_codec_priv *priv =
965 snd_soc_component_get_drvdata(component);
967 return bytes_to_frames(runtime, priv->wov_dma_offset);
970 static int wov_pcm_new(struct snd_soc_component *component,
971 struct snd_soc_pcm_runtime *rtd)
973 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_VMALLOC,
978 static const struct snd_soc_component_driver wov_component_driver = {
980 .remove = wov_remove,
981 .controls = wov_controls,
982 .num_controls = ARRAY_SIZE(wov_controls),
983 .open = wov_pcm_open,
984 .hw_params = wov_pcm_hw_params,
985 .hw_free = wov_pcm_hw_free,
986 .pointer = wov_pcm_pointer,
987 .pcm_construct = wov_pcm_new,
990 static int cros_ec_codec_platform_probe(struct platform_device *pdev)
992 struct device *dev = &pdev->dev;
993 struct cros_ec_device *ec_device = dev_get_drvdata(pdev->dev.parent);
994 struct cros_ec_codec_priv *priv;
995 struct ec_param_ec_codec p;
996 struct ec_response_ec_codec_get_capabilities r;
999 struct device_node *node;
1000 struct resource res;
1002 const __be32 *regaddr_p;
1005 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1010 regaddr_p = of_get_address(dev->of_node, 0, &ec_shm_size, NULL);
1012 priv->ec_shm_addr = of_read_number(regaddr_p, 2);
1013 priv->ec_shm_len = ec_shm_size;
1015 dev_dbg(dev, "ec_shm_addr=%#llx len=%#x\n",
1016 priv->ec_shm_addr, priv->ec_shm_len);
1019 node = of_parse_phandle(dev->of_node, "memory-region", 0);
1021 ret = of_address_to_resource(node, 0, &res);
1023 priv->ap_shm_phys_addr = res.start;
1024 priv->ap_shm_len = resource_size(&res);
1026 (uint64_t)(uintptr_t)devm_ioremap_wc(
1027 dev, priv->ap_shm_phys_addr,
1029 priv->ap_shm_last_alloc = priv->ap_shm_phys_addr;
1031 dev_dbg(dev, "ap_shm_phys_addr=%#llx len=%#x\n",
1032 priv->ap_shm_phys_addr, priv->ap_shm_len);
1038 priv->ec_device = ec_device;
1039 atomic_set(&priv->dmic_probed, 0);
1041 p.cmd = EC_CODEC_GET_CAPABILITIES;
1042 ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC,
1043 (uint8_t *)&p, sizeof(p),
1044 (uint8_t *)&r, sizeof(r));
1046 dev_err(dev, "failed to EC_CODEC_GET_CAPABILITIES\n");
1049 priv->ec_capabilities = r.capabilities;
1051 platform_set_drvdata(pdev, priv);
1053 ret = devm_snd_soc_register_component(dev, &i2s_rx_component_driver,
1054 &i2s_rx_dai_driver, 1);
1058 return devm_snd_soc_register_component(dev, &wov_component_driver,
1059 &wov_dai_driver, 1);
1063 static const struct of_device_id cros_ec_codec_of_match[] = {
1064 { .compatible = "google,cros-ec-codec" },
1067 MODULE_DEVICE_TABLE(of, cros_ec_codec_of_match);
1070 static const struct acpi_device_id cros_ec_codec_acpi_id[] = {
1074 MODULE_DEVICE_TABLE(acpi, cros_ec_codec_acpi_id);
1076 static struct platform_driver cros_ec_codec_platform_driver = {
1078 .name = "cros-ec-codec",
1079 .of_match_table = of_match_ptr(cros_ec_codec_of_match),
1080 .acpi_match_table = ACPI_PTR(cros_ec_codec_acpi_id),
1082 .probe = cros_ec_codec_platform_probe,
1085 module_platform_driver(cros_ec_codec_platform_driver);
1087 MODULE_LICENSE("GPL v2");
1088 MODULE_DESCRIPTION("ChromeOS EC codec driver");
1089 MODULE_AUTHOR("Cheng-Yi Chiang <cychiang@chromium.org>");
1090 MODULE_ALIAS("platform:cros-ec-codec");