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/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/delay.h>
14 #include <linux/spi/spi.h>
15 #include <linux/i2c.h>
16 #include <linux/input.h>
17 #include <sound/core.h>
18 #include <sound/jack.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include "rockchip_i2s.h"
23 #include "../codecs/da7219.h"
24 #include "../codecs/da7219-aad.h"
25 #include "../codecs/rt5514.h"
27 #define DRV_NAME "rk3399-gru-sound"
31 static unsigned int dmic_wakeup_delay;
33 static struct snd_soc_jack rockchip_sound_jack;
35 static const struct snd_soc_dapm_widget rockchip_dapm_widgets[] = {
36 SND_SOC_DAPM_HP("Headphones", NULL),
37 SND_SOC_DAPM_SPK("Speakers", NULL),
38 SND_SOC_DAPM_MIC("Headset Mic", NULL),
39 SND_SOC_DAPM_MIC("Int Mic", NULL),
40 SND_SOC_DAPM_LINE("HDMI", NULL),
43 static const struct snd_kcontrol_new rockchip_controls[] = {
44 SOC_DAPM_PIN_SWITCH("Headphones"),
45 SOC_DAPM_PIN_SWITCH("Speakers"),
46 SOC_DAPM_PIN_SWITCH("Headset Mic"),
47 SOC_DAPM_PIN_SWITCH("Int Mic"),
48 SOC_DAPM_PIN_SWITCH("HDMI"),
51 static int rockchip_sound_max98357a_hw_params(struct snd_pcm_substream *substream,
52 struct snd_pcm_hw_params *params)
54 struct snd_soc_pcm_runtime *rtd = substream->private_data;
58 /* max98357a supports these sample rates */
59 switch (params_rate(params)) {
64 mclk = params_rate(params) * SOUND_FS;
67 dev_err(rtd->card->dev, "%s() doesn't support this sample rate: %d\n",
68 __func__, params_rate(params));
72 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
74 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
82 static int rockchip_sound_rt5514_hw_params(struct snd_pcm_substream *substream,
83 struct snd_pcm_hw_params *params)
85 struct snd_soc_pcm_runtime *rtd = substream->private_data;
86 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
87 struct snd_soc_dai *codec_dai = rtd->codec_dai;
91 mclk = params_rate(params) * SOUND_FS;
93 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
96 dev_err(rtd->card->dev, "Can't set cpu clock out %d\n", ret);
100 ret = snd_soc_dai_set_sysclk(codec_dai, RT5514_SCLK_S_MCLK,
101 mclk, SND_SOC_CLOCK_IN);
103 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
104 __func__, params_rate(params) * 512, ret);
108 /* Wait for DMIC stable */
109 msleep(dmic_wakeup_delay);
114 static int rockchip_sound_da7219_hw_params(struct snd_pcm_substream *substream,
115 struct snd_pcm_hw_params *params)
117 struct snd_soc_pcm_runtime *rtd = substream->private_data;
118 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
119 struct snd_soc_dai *codec_dai = rtd->codec_dai;
122 /* in bypass mode, the mclk has to be one of the frequencies below */
123 switch (params_rate(params)) {
143 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
146 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
150 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
153 dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
157 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
159 dev_err(codec_dai->dev, "Can't set pll sysclk mclk %d\n", ret);
166 static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime *rtd)
168 struct snd_soc_component *component = rtd->codec_dais[0]->component;
169 struct snd_soc_dai *codec_dai = rtd->codec_dai;
172 /* We need default MCLK and PLL settings for the accessory detection */
173 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 12288000,
176 dev_err(codec_dai->dev, "Init can't set codec clock in %d\n", ret);
180 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
182 dev_err(codec_dai->dev, "Init can't set pll sysclk mclk %d\n", ret);
186 /* Enable Headset and 4 Buttons Jack detection */
187 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
188 SND_JACK_HEADSET | SND_JACK_LINEOUT |
189 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
190 SND_JACK_BTN_2 | SND_JACK_BTN_3,
191 &rockchip_sound_jack, NULL, 0);
194 dev_err(rtd->card->dev, "New Headset Jack failed! (%d)\n", ret);
199 rockchip_sound_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
201 rockchip_sound_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
203 rockchip_sound_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
205 rockchip_sound_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
207 da7219_aad_jack_det(component, &rockchip_sound_jack);
212 static int rockchip_sound_dmic_hw_params(struct snd_pcm_substream *substream,
213 struct snd_pcm_hw_params *params)
215 struct snd_soc_pcm_runtime *rtd = substream->private_data;
219 mclk = params_rate(params) * SOUND_FS;
221 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
223 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
224 __func__, mclk, ret);
228 /* Wait for DMIC stable */
229 msleep(dmic_wakeup_delay);
234 static const struct snd_soc_ops rockchip_sound_max98357a_ops = {
235 .hw_params = rockchip_sound_max98357a_hw_params,
238 static const struct snd_soc_ops rockchip_sound_rt5514_ops = {
239 .hw_params = rockchip_sound_rt5514_hw_params,
242 static const struct snd_soc_ops rockchip_sound_da7219_ops = {
243 .hw_params = rockchip_sound_da7219_hw_params,
246 static const struct snd_soc_ops rockchip_sound_dmic_ops = {
247 .hw_params = rockchip_sound_dmic_hw_params,
250 static struct snd_soc_card rockchip_sound_card = {
251 .name = "rk3399-gru-sound",
252 .owner = THIS_MODULE,
253 .dapm_widgets = rockchip_dapm_widgets,
254 .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
255 .controls = rockchip_controls,
256 .num_controls = ARRAY_SIZE(rockchip_controls),
268 static const struct snd_soc_dai_link rockchip_dais[] = {
271 .stream_name = "DP PCM",
272 .codec_dai_name = "spdif-hifi",
273 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
274 SND_SOC_DAIFMT_CBS_CFS,
278 .stream_name = "DA7219 PCM",
279 .codec_dai_name = "da7219-hifi",
280 .init = rockchip_sound_da7219_init,
281 .ops = &rockchip_sound_da7219_ops,
282 /* set da7219 as slave */
283 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
284 SND_SOC_DAIFMT_CBS_CFS,
288 .stream_name = "DMIC PCM",
289 .codec_dai_name = "dmic-hifi",
290 .ops = &rockchip_sound_dmic_ops,
291 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
292 SND_SOC_DAIFMT_CBS_CFS,
294 [DAILINK_MAX98357A] = {
296 .stream_name = "MAX98357A PCM",
297 .codec_dai_name = "HiFi",
298 .ops = &rockchip_sound_max98357a_ops,
299 /* set max98357a as slave */
300 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
301 SND_SOC_DAIFMT_CBS_CFS,
305 .stream_name = "RT5514 PCM",
306 .codec_dai_name = "rt5514-aif1",
307 .ops = &rockchip_sound_rt5514_ops,
308 /* set rt5514 as slave */
309 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
310 SND_SOC_DAIFMT_CBS_CFS,
312 /* RT5514 DSP for voice wakeup via spi bus */
313 [DAILINK_RT5514_DSP] = {
314 .name = "RT5514 DSP",
315 .stream_name = "Wake on Voice",
316 .codec_name = "snd-soc-dummy",
317 .codec_dai_name = "snd-soc-dummy-dai",
321 static const struct snd_soc_dapm_route rockchip_sound_cdndp_routes[] = {
323 {"HDMI", NULL, "TX"},
326 static const struct snd_soc_dapm_route rockchip_sound_da7219_routes[] = {
328 {"Headphones", NULL, "HPL"},
329 {"Headphones", NULL, "HPR"},
332 {"MIC", NULL, "Headset Mic"},
335 static const struct snd_soc_dapm_route rockchip_sound_dmic_routes[] = {
337 {"DMic", NULL, "Int Mic"},
340 static const struct snd_soc_dapm_route rockchip_sound_max98357a_routes[] = {
342 {"Speakers", NULL, "Speaker"},
345 static const struct snd_soc_dapm_route rockchip_sound_rt5514_routes[] = {
347 {"DMIC1L", NULL, "Int Mic"},
348 {"DMIC1R", NULL, "Int Mic"},
351 struct rockchip_sound_route {
352 const struct snd_soc_dapm_route *routes;
356 static const struct rockchip_sound_route rockchip_routes[] = {
358 .routes = rockchip_sound_cdndp_routes,
359 .num_routes = ARRAY_SIZE(rockchip_sound_cdndp_routes),
362 .routes = rockchip_sound_da7219_routes,
363 .num_routes = ARRAY_SIZE(rockchip_sound_da7219_routes),
366 .routes = rockchip_sound_dmic_routes,
367 .num_routes = ARRAY_SIZE(rockchip_sound_dmic_routes),
369 [DAILINK_MAX98357A] = {
370 .routes = rockchip_sound_max98357a_routes,
371 .num_routes = ARRAY_SIZE(rockchip_sound_max98357a_routes),
374 .routes = rockchip_sound_rt5514_routes,
375 .num_routes = ARRAY_SIZE(rockchip_sound_rt5514_routes),
377 [DAILINK_RT5514_DSP] = {},
380 struct dailink_match_data {
381 const char *compatible;
382 struct bus_type *bus_type;
385 static const struct dailink_match_data dailink_match[] = {
387 .compatible = "rockchip,rk3399-cdn-dp",
390 .compatible = "dlg,da7219",
393 .compatible = "dmic-codec",
395 [DAILINK_MAX98357A] = {
396 .compatible = "maxim,max98357a",
399 .compatible = "realtek,rt5514",
400 .bus_type = &i2c_bus_type,
402 [DAILINK_RT5514_DSP] = {
403 .compatible = "realtek,rt5514",
404 .bus_type = &spi_bus_type,
408 static int of_dev_node_match(struct device *dev, void *data)
410 return dev->of_node == data;
413 static int rockchip_sound_codec_node_match(struct device_node *np_codec)
418 for (i = 0; i < ARRAY_SIZE(dailink_match); i++) {
419 if (!of_device_is_compatible(np_codec,
420 dailink_match[i].compatible))
423 if (dailink_match[i].bus_type) {
424 dev = bus_find_device(dailink_match[i].bus_type, NULL,
425 np_codec, of_dev_node_match);
436 static int rockchip_sound_of_parse_dais(struct device *dev,
437 struct snd_soc_card *card)
439 struct device_node *np_cpu, *np_cpu0, *np_cpu1;
440 struct device_node *np_codec;
441 struct snd_soc_dai_link *dai;
442 struct snd_soc_dapm_route *routes;
446 card->dai_link = devm_kzalloc(dev, sizeof(rockchip_dais),
452 for (i = 0; i < ARRAY_SIZE(rockchip_routes); i++)
453 num_routes += rockchip_routes[i].num_routes;
454 routes = devm_kcalloc(dev, num_routes, sizeof(*routes),
458 card->dapm_routes = routes;
460 np_cpu0 = of_parse_phandle(dev->of_node, "rockchip,cpu", 0);
461 np_cpu1 = of_parse_phandle(dev->of_node, "rockchip,cpu", 1);
463 card->num_dapm_routes = 0;
465 for (i = 0; i < ARRAY_SIZE(rockchip_dais); i++) {
466 np_codec = of_parse_phandle(dev->of_node,
467 "rockchip,codec", i);
471 if (!of_device_is_available(np_codec))
474 index = rockchip_sound_codec_node_match(np_codec);
482 case DAILINK_RT5514_DSP:
491 dev_err(dev, "Missing 'rockchip,cpu' for %s\n",
492 rockchip_dais[index].name);
496 dai = &card->dai_link[card->num_links++];
497 *dai = rockchip_dais[index];
499 if (!dai->codec_name)
500 dai->codec_of_node = np_codec;
501 dai->platform_of_node = np_cpu;
502 dai->cpu_of_node = np_cpu;
504 if (card->num_dapm_routes + rockchip_routes[index].num_routes >
506 dev_err(dev, "Too many routes\n");
510 memcpy(routes + card->num_dapm_routes,
511 rockchip_routes[index].routes,
512 rockchip_routes[index].num_routes * sizeof(*routes));
513 card->num_dapm_routes += rockchip_routes[index].num_routes;
519 static int rockchip_sound_probe(struct platform_device *pdev)
521 struct snd_soc_card *card = &rockchip_sound_card;
524 ret = rockchip_sound_of_parse_dais(&pdev->dev, card);
526 dev_err(&pdev->dev, "Failed to parse dais: %d\n", ret);
530 /* Set DMIC wakeup delay */
531 ret = device_property_read_u32(&pdev->dev, "dmic-wakeup-delay-ms",
534 dmic_wakeup_delay = 0;
536 "no optional property 'dmic-wakeup-delay-ms' found, default: no delay\n");
539 card->dev = &pdev->dev;
540 return devm_snd_soc_register_card(&pdev->dev, card);
543 static const struct of_device_id rockchip_sound_of_match[] = {
544 { .compatible = "rockchip,rk3399-gru-sound", },
548 static struct platform_driver rockchip_sound_driver = {
549 .probe = rockchip_sound_probe,
552 .of_match_table = rockchip_sound_of_match,
554 .pm = &snd_soc_pm_ops,
559 module_platform_driver(rockchip_sound_driver);
561 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
562 MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
563 MODULE_LICENSE("GPL v2");
564 MODULE_ALIAS("platform:" DRV_NAME);
565 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);