1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip machine ASoC driver for boards using MAX98357A/RT5514/DA7219
5 * Copyright (c) 2016, ROCKCHIP CORPORATION. All rights reserved.
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/delay.h>
12 #include <linux/spi/spi.h>
13 #include <linux/i2c.h>
14 #include <linux/input.h>
15 #include <sound/core.h>
16 #include <sound/jack.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include "rockchip_i2s.h"
21 #include "../codecs/da7219.h"
22 #include "../codecs/rt5514.h"
24 #define DRV_NAME "rk3399-gru-sound"
28 static unsigned int dmic_wakeup_delay;
30 static struct snd_soc_jack rockchip_sound_jack;
32 /* Headset jack detection DAPM pins */
33 static struct snd_soc_jack_pin rockchip_sound_jack_pins[] = {
36 .mask = SND_JACK_HEADPHONE,
40 .mask = SND_JACK_MICROPHONE,
44 .mask = SND_JACK_LINEOUT,
48 static const struct snd_soc_dapm_widget rockchip_dapm_widgets[] = {
49 SND_SOC_DAPM_HP("Headphones", NULL),
50 SND_SOC_DAPM_SPK("Speakers", NULL),
51 SND_SOC_DAPM_MIC("Headset Mic", NULL),
52 SND_SOC_DAPM_LINE("Line Out", NULL),
53 SND_SOC_DAPM_MIC("Int Mic", NULL),
54 SND_SOC_DAPM_LINE("HDMI", NULL),
57 static const struct snd_kcontrol_new rockchip_controls[] = {
58 SOC_DAPM_PIN_SWITCH("Headphones"),
59 SOC_DAPM_PIN_SWITCH("Speakers"),
60 SOC_DAPM_PIN_SWITCH("Headset Mic"),
61 SOC_DAPM_PIN_SWITCH("Line Out"),
62 SOC_DAPM_PIN_SWITCH("Int Mic"),
63 SOC_DAPM_PIN_SWITCH("HDMI"),
66 static int rockchip_sound_max98357a_hw_params(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params *params)
69 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
73 mclk = params_rate(params) * SOUND_FS;
75 ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_cpu(rtd, 0), 0, mclk, 0);
77 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
85 static int rockchip_sound_rt5514_hw_params(struct snd_pcm_substream *substream,
86 struct snd_pcm_hw_params *params)
88 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
89 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
90 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
94 mclk = params_rate(params) * SOUND_FS;
96 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
99 dev_err(rtd->card->dev, "Can't set cpu clock out %d\n", ret);
103 ret = snd_soc_dai_set_sysclk(codec_dai, RT5514_SCLK_S_MCLK,
104 mclk, SND_SOC_CLOCK_IN);
106 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
107 __func__, params_rate(params) * 512, ret);
111 /* Wait for DMIC stable */
112 msleep(dmic_wakeup_delay);
117 static int rockchip_sound_da7219_hw_params(struct snd_pcm_substream *substream,
118 struct snd_pcm_hw_params *params)
120 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
121 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
122 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
125 /* in bypass mode, the mclk has to be one of the frequencies below */
126 switch (params_rate(params)) {
146 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
149 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
153 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
156 dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
160 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
162 dev_err(codec_dai->dev, "Can't set pll sysclk mclk %d\n", ret);
169 static struct snd_soc_jack cdn_dp_card_jack;
171 static int rockchip_sound_cdndp_init(struct snd_soc_pcm_runtime *rtd)
173 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
174 struct snd_soc_card *card = rtd->card;
177 /* Enable jack detection. */
178 ret = snd_soc_card_jack_new(card, "DP Jack", SND_JACK_LINEOUT,
181 dev_err(card->dev, "Can't create DP Jack %d\n", ret);
185 return snd_soc_component_set_jack(component, &cdn_dp_card_jack, NULL);
188 static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime *rtd)
190 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
191 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
194 /* We need default MCLK and PLL settings for the accessory detection */
195 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 12288000,
198 dev_err(codec_dai->dev, "Init can't set codec clock in %d\n", ret);
202 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
204 dev_err(codec_dai->dev, "Init can't set pll sysclk mclk %d\n", ret);
208 /* Enable Headset and 4 Buttons Jack detection */
209 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
210 SND_JACK_HEADSET | SND_JACK_LINEOUT |
211 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
212 SND_JACK_BTN_2 | SND_JACK_BTN_3,
213 &rockchip_sound_jack,
214 rockchip_sound_jack_pins,
215 ARRAY_SIZE(rockchip_sound_jack_pins));
218 dev_err(rtd->card->dev, "New Headset Jack failed! (%d)\n", ret);
223 rockchip_sound_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
225 rockchip_sound_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
227 rockchip_sound_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
229 rockchip_sound_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
231 snd_soc_component_set_jack(component, &rockchip_sound_jack, NULL);
236 static int rockchip_sound_dmic_hw_params(struct snd_pcm_substream *substream,
237 struct snd_pcm_hw_params *params)
239 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
243 mclk = params_rate(params) * SOUND_FS;
245 ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_cpu(rtd, 0), 0, mclk, 0);
247 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
248 __func__, mclk, ret);
252 /* Wait for DMIC stable */
253 msleep(dmic_wakeup_delay);
258 static int rockchip_sound_startup(struct snd_pcm_substream *substream)
260 struct snd_pcm_runtime *runtime = substream->runtime;
262 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
263 return snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
267 static const struct snd_soc_ops rockchip_sound_max98357a_ops = {
268 .startup = rockchip_sound_startup,
269 .hw_params = rockchip_sound_max98357a_hw_params,
272 static const struct snd_soc_ops rockchip_sound_rt5514_ops = {
273 .startup = rockchip_sound_startup,
274 .hw_params = rockchip_sound_rt5514_hw_params,
277 static const struct snd_soc_ops rockchip_sound_da7219_ops = {
278 .startup = rockchip_sound_startup,
279 .hw_params = rockchip_sound_da7219_hw_params,
282 static const struct snd_soc_ops rockchip_sound_dmic_ops = {
283 .startup = rockchip_sound_startup,
284 .hw_params = rockchip_sound_dmic_hw_params,
287 static struct snd_soc_card rockchip_sound_card = {
288 .name = "rk3399-gru-sound",
289 .owner = THIS_MODULE,
290 .dapm_widgets = rockchip_dapm_widgets,
291 .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
292 .controls = rockchip_controls,
293 .num_controls = ARRAY_SIZE(rockchip_controls),
305 SND_SOC_DAILINK_DEFS(cdndp,
306 DAILINK_COMP_ARRAY(COMP_EMPTY()),
307 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "spdif-hifi")),
308 DAILINK_COMP_ARRAY(COMP_EMPTY()));
310 SND_SOC_DAILINK_DEFS(da7219,
311 DAILINK_COMP_ARRAY(COMP_EMPTY()),
312 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "da7219-hifi")),
313 DAILINK_COMP_ARRAY(COMP_EMPTY()));
315 SND_SOC_DAILINK_DEFS(dmic,
316 DAILINK_COMP_ARRAY(COMP_EMPTY()),
317 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "dmic-hifi")),
318 DAILINK_COMP_ARRAY(COMP_EMPTY()));
320 SND_SOC_DAILINK_DEFS(max98357a,
321 DAILINK_COMP_ARRAY(COMP_EMPTY()),
322 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
323 DAILINK_COMP_ARRAY(COMP_EMPTY()));
325 SND_SOC_DAILINK_DEFS(rt5514,
326 DAILINK_COMP_ARRAY(COMP_EMPTY()),
327 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5514-aif1")),
328 DAILINK_COMP_ARRAY(COMP_EMPTY()));
330 SND_SOC_DAILINK_DEFS(rt5514_dsp,
331 DAILINK_COMP_ARRAY(COMP_EMPTY()),
332 DAILINK_COMP_ARRAY(COMP_DUMMY()),
333 DAILINK_COMP_ARRAY(COMP_EMPTY()));
335 static const struct snd_soc_dai_link rockchip_dais[] = {
338 .stream_name = "DP PCM",
339 .init = rockchip_sound_cdndp_init,
340 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
341 SND_SOC_DAIFMT_CBS_CFS,
342 SND_SOC_DAILINK_REG(cdndp),
346 .stream_name = "DA7219 PCM",
347 .init = rockchip_sound_da7219_init,
348 .ops = &rockchip_sound_da7219_ops,
349 /* set da7219 as slave */
350 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
351 SND_SOC_DAIFMT_CBS_CFS,
352 SND_SOC_DAILINK_REG(da7219),
356 .stream_name = "DMIC PCM",
357 .ops = &rockchip_sound_dmic_ops,
358 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
359 SND_SOC_DAIFMT_CBS_CFS,
360 SND_SOC_DAILINK_REG(dmic),
362 [DAILINK_MAX98357A] = {
364 .stream_name = "MAX98357A PCM",
365 .ops = &rockchip_sound_max98357a_ops,
366 /* set max98357a as slave */
367 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
368 SND_SOC_DAIFMT_CBS_CFS,
369 SND_SOC_DAILINK_REG(max98357a),
373 .stream_name = "RT5514 PCM",
374 .ops = &rockchip_sound_rt5514_ops,
375 /* set rt5514 as slave */
376 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
377 SND_SOC_DAIFMT_CBS_CFS,
378 SND_SOC_DAILINK_REG(rt5514),
380 /* RT5514 DSP for voice wakeup via spi bus */
381 [DAILINK_RT5514_DSP] = {
382 .name = "RT5514 DSP",
383 .stream_name = "Wake on Voice",
384 SND_SOC_DAILINK_REG(rt5514_dsp),
388 static const struct snd_soc_dapm_route rockchip_sound_cdndp_routes[] = {
390 {"HDMI", NULL, "TX"},
393 static const struct snd_soc_dapm_route rockchip_sound_da7219_routes[] = {
395 {"Headphones", NULL, "HPL"},
396 {"Headphones", NULL, "HPR"},
399 {"MIC", NULL, "Headset Mic"},
402 static const struct snd_soc_dapm_route rockchip_sound_dmic_routes[] = {
404 {"DMic", NULL, "Int Mic"},
407 static const struct snd_soc_dapm_route rockchip_sound_max98357a_routes[] = {
409 {"Speakers", NULL, "Speaker"},
412 static const struct snd_soc_dapm_route rockchip_sound_rt5514_routes[] = {
414 {"DMIC1L", NULL, "Int Mic"},
415 {"DMIC1R", NULL, "Int Mic"},
418 struct rockchip_sound_route {
419 const struct snd_soc_dapm_route *routes;
423 static const struct rockchip_sound_route rockchip_routes[] = {
425 .routes = rockchip_sound_cdndp_routes,
426 .num_routes = ARRAY_SIZE(rockchip_sound_cdndp_routes),
429 .routes = rockchip_sound_da7219_routes,
430 .num_routes = ARRAY_SIZE(rockchip_sound_da7219_routes),
433 .routes = rockchip_sound_dmic_routes,
434 .num_routes = ARRAY_SIZE(rockchip_sound_dmic_routes),
436 [DAILINK_MAX98357A] = {
437 .routes = rockchip_sound_max98357a_routes,
438 .num_routes = ARRAY_SIZE(rockchip_sound_max98357a_routes),
441 .routes = rockchip_sound_rt5514_routes,
442 .num_routes = ARRAY_SIZE(rockchip_sound_rt5514_routes),
444 [DAILINK_RT5514_DSP] = {},
447 struct dailink_match_data {
448 const char *compatible;
449 struct bus_type *bus_type;
452 static const struct dailink_match_data dailink_match[] = {
454 .compatible = "rockchip,rk3399-cdn-dp",
457 .compatible = "dlg,da7219",
460 .compatible = "dmic-codec",
462 [DAILINK_MAX98357A] = {
463 .compatible = "maxim,max98357a",
466 .compatible = "realtek,rt5514",
467 .bus_type = &i2c_bus_type,
469 [DAILINK_RT5514_DSP] = {
470 .compatible = "realtek,rt5514",
471 .bus_type = &spi_bus_type,
475 static int rockchip_sound_codec_node_match(struct device_node *np_codec)
480 for (i = 0; i < ARRAY_SIZE(dailink_match); i++) {
481 if (!of_device_is_compatible(np_codec,
482 dailink_match[i].compatible))
485 if (dailink_match[i].bus_type) {
486 dev = bus_find_device_by_of_node(dailink_match[i].bus_type,
498 static int rockchip_sound_of_parse_dais(struct device *dev,
499 struct snd_soc_card *card)
501 struct device_node *np_cpu, *np_cpu0, *np_cpu1;
502 struct device_node *np_codec;
503 struct snd_soc_dai_link *dai;
504 struct snd_soc_dapm_route *routes;
508 card->dai_link = devm_kzalloc(dev, sizeof(rockchip_dais),
514 for (i = 0; i < ARRAY_SIZE(rockchip_routes); i++)
515 num_routes += rockchip_routes[i].num_routes;
516 routes = devm_kcalloc(dev, num_routes, sizeof(*routes),
520 card->dapm_routes = routes;
522 np_cpu0 = of_parse_phandle(dev->of_node, "rockchip,cpu", 0);
523 np_cpu1 = of_parse_phandle(dev->of_node, "rockchip,cpu", 1);
525 card->num_dapm_routes = 0;
527 for (i = 0; i < ARRAY_SIZE(rockchip_dais); i++) {
528 np_codec = of_parse_phandle(dev->of_node,
529 "rockchip,codec", i);
533 if (!of_device_is_available(np_codec))
536 index = rockchip_sound_codec_node_match(np_codec);
544 case DAILINK_RT5514_DSP:
553 dev_err(dev, "Missing 'rockchip,cpu' for %s\n",
554 rockchip_dais[index].name);
558 dai = &card->dai_link[card->num_links++];
559 *dai = rockchip_dais[index];
561 if (!dai->codecs->name)
562 dai->codecs->of_node = np_codec;
563 dai->platforms->of_node = np_cpu;
564 dai->cpus->of_node = np_cpu;
566 if (card->num_dapm_routes + rockchip_routes[index].num_routes >
568 dev_err(dev, "Too many routes\n");
572 memcpy(routes + card->num_dapm_routes,
573 rockchip_routes[index].routes,
574 rockchip_routes[index].num_routes * sizeof(*routes));
575 card->num_dapm_routes += rockchip_routes[index].num_routes;
581 static int rockchip_sound_probe(struct platform_device *pdev)
583 struct snd_soc_card *card = &rockchip_sound_card;
586 ret = rockchip_sound_of_parse_dais(&pdev->dev, card);
588 dev_err(&pdev->dev, "Failed to parse dais: %d\n", ret);
592 /* Set DMIC wakeup delay */
593 ret = device_property_read_u32(&pdev->dev, "dmic-wakeup-delay-ms",
596 dmic_wakeup_delay = 0;
598 "no optional property 'dmic-wakeup-delay-ms' found, default: no delay\n");
601 card->dev = &pdev->dev;
602 return devm_snd_soc_register_card(&pdev->dev, card);
605 static const struct of_device_id rockchip_sound_of_match[] = {
606 { .compatible = "rockchip,rk3399-gru-sound", },
610 static struct platform_driver rockchip_sound_driver = {
611 .probe = rockchip_sound_probe,
614 .of_match_table = rockchip_sound_of_match,
616 .pm = &snd_soc_pm_ops,
621 module_platform_driver(rockchip_sound_driver);
623 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
624 MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
625 MODULE_LICENSE("GPL v2");
626 MODULE_ALIAS("platform:" DRV_NAME);
627 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);