2 * Rockchip machine ASoC driver for boards using MAX98357A/RT5514/DA7219
4 * Copyright (c) 2016, ROCKCHIP CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/gpio.h>
23 #include <linux/of_gpio.h>
24 #include <linux/delay.h>
25 #include <linux/spi/spi.h>
26 #include <linux/i2c.h>
27 #include <linux/input.h>
28 #include <sound/core.h>
29 #include <sound/jack.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/soc.h>
33 #include "rockchip_i2s.h"
34 #include "../codecs/da7219.h"
35 #include "../codecs/da7219-aad.h"
36 #include "../codecs/rt5514.h"
38 #define DRV_NAME "rk3399-gru-sound"
42 static unsigned int dmic_wakeup_delay;
44 static struct snd_soc_jack rockchip_sound_jack;
46 static const struct snd_soc_dapm_widget rockchip_dapm_widgets[] = {
47 SND_SOC_DAPM_HP("Headphones", NULL),
48 SND_SOC_DAPM_SPK("Speakers", NULL),
49 SND_SOC_DAPM_MIC("Headset Mic", NULL),
50 SND_SOC_DAPM_MIC("Int Mic", NULL),
51 SND_SOC_DAPM_LINE("HDMI", NULL),
54 static const struct snd_kcontrol_new rockchip_controls[] = {
55 SOC_DAPM_PIN_SWITCH("Headphones"),
56 SOC_DAPM_PIN_SWITCH("Speakers"),
57 SOC_DAPM_PIN_SWITCH("Headset Mic"),
58 SOC_DAPM_PIN_SWITCH("Int Mic"),
59 SOC_DAPM_PIN_SWITCH("HDMI"),
62 static int rockchip_sound_max98357a_hw_params(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params *params)
65 struct snd_soc_pcm_runtime *rtd = substream->private_data;
69 /* max98357a supports these sample rates */
70 switch (params_rate(params)) {
75 mclk = params_rate(params) * SOUND_FS;
78 dev_err(rtd->card->dev, "%s() doesn't support this sample rate: %d\n",
79 __func__, params_rate(params));
83 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
85 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
93 static int rockchip_sound_rt5514_hw_params(struct snd_pcm_substream *substream,
94 struct snd_pcm_hw_params *params)
96 struct snd_soc_pcm_runtime *rtd = substream->private_data;
97 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
98 struct snd_soc_dai *codec_dai = rtd->codec_dai;
102 mclk = params_rate(params) * SOUND_FS;
104 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
107 dev_err(rtd->card->dev, "Can't set cpu clock out %d\n", ret);
111 ret = snd_soc_dai_set_sysclk(codec_dai, RT5514_SCLK_S_MCLK,
112 mclk, SND_SOC_CLOCK_IN);
114 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
115 __func__, params_rate(params) * 512, ret);
119 /* Wait for DMIC stable */
120 msleep(dmic_wakeup_delay);
125 static int rockchip_sound_da7219_hw_params(struct snd_pcm_substream *substream,
126 struct snd_pcm_hw_params *params)
128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
129 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
130 struct snd_soc_dai *codec_dai = rtd->codec_dai;
133 /* in bypass mode, the mclk has to be one of the frequencies below */
134 switch (params_rate(params)) {
154 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
157 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
161 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
164 dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
168 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
170 dev_err(codec_dai->dev, "Can't set pll sysclk mclk %d\n", ret);
177 static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime *rtd)
179 struct snd_soc_component *component = rtd->codec_dais[0]->component;
180 struct snd_soc_dai *codec_dai = rtd->codec_dai;
183 /* We need default MCLK and PLL settings for the accessory detection */
184 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 12288000,
187 dev_err(codec_dai->dev, "Init can't set codec clock in %d\n", ret);
191 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
193 dev_err(codec_dai->dev, "Init can't set pll sysclk mclk %d\n", ret);
197 /* Enable Headset and 4 Buttons Jack detection */
198 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
199 SND_JACK_HEADSET | SND_JACK_LINEOUT |
200 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
201 SND_JACK_BTN_2 | SND_JACK_BTN_3,
202 &rockchip_sound_jack, NULL, 0);
205 dev_err(rtd->card->dev, "New Headset Jack failed! (%d)\n", ret);
210 rockchip_sound_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
212 rockchip_sound_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
214 rockchip_sound_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
216 rockchip_sound_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
218 da7219_aad_jack_det(component, &rockchip_sound_jack);
223 static int rockchip_sound_dmic_hw_params(struct snd_pcm_substream *substream,
224 struct snd_pcm_hw_params *params)
226 struct snd_soc_pcm_runtime *rtd = substream->private_data;
230 mclk = params_rate(params) * SOUND_FS;
232 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
234 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
235 __func__, mclk, ret);
239 /* Wait for DMIC stable */
240 msleep(dmic_wakeup_delay);
245 static const struct snd_soc_ops rockchip_sound_max98357a_ops = {
246 .hw_params = rockchip_sound_max98357a_hw_params,
249 static const struct snd_soc_ops rockchip_sound_rt5514_ops = {
250 .hw_params = rockchip_sound_rt5514_hw_params,
253 static const struct snd_soc_ops rockchip_sound_da7219_ops = {
254 .hw_params = rockchip_sound_da7219_hw_params,
257 static const struct snd_soc_ops rockchip_sound_dmic_ops = {
258 .hw_params = rockchip_sound_dmic_hw_params,
261 static struct snd_soc_card rockchip_sound_card = {
262 .name = "rk3399-gru-sound",
263 .owner = THIS_MODULE,
264 .dapm_widgets = rockchip_dapm_widgets,
265 .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
266 .controls = rockchip_controls,
267 .num_controls = ARRAY_SIZE(rockchip_controls),
279 static const struct snd_soc_dai_link rockchip_dais[] = {
282 .stream_name = "DP PCM",
283 .codec_dai_name = "spdif-hifi",
284 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
285 SND_SOC_DAIFMT_CBS_CFS,
289 .stream_name = "DA7219 PCM",
290 .codec_dai_name = "da7219-hifi",
291 .init = rockchip_sound_da7219_init,
292 .ops = &rockchip_sound_da7219_ops,
293 /* set da7219 as slave */
294 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
295 SND_SOC_DAIFMT_CBS_CFS,
299 .stream_name = "DMIC PCM",
300 .codec_dai_name = "dmic-hifi",
301 .ops = &rockchip_sound_dmic_ops,
302 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
303 SND_SOC_DAIFMT_CBS_CFS,
305 [DAILINK_MAX98357A] = {
307 .stream_name = "MAX98357A PCM",
308 .codec_dai_name = "HiFi",
309 .ops = &rockchip_sound_max98357a_ops,
310 /* set max98357a as slave */
311 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
312 SND_SOC_DAIFMT_CBS_CFS,
316 .stream_name = "RT5514 PCM",
317 .codec_dai_name = "rt5514-aif1",
318 .ops = &rockchip_sound_rt5514_ops,
319 /* set rt5514 as slave */
320 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
321 SND_SOC_DAIFMT_CBS_CFS,
323 /* RT5514 DSP for voice wakeup via spi bus */
324 [DAILINK_RT5514_DSP] = {
325 .name = "RT5514 DSP",
326 .stream_name = "Wake on Voice",
327 .codec_name = "snd-soc-dummy",
328 .codec_dai_name = "snd-soc-dummy-dai",
332 static const struct snd_soc_dapm_route rockchip_sound_cdndp_routes[] = {
334 {"HDMI", NULL, "TX"},
337 static const struct snd_soc_dapm_route rockchip_sound_da7219_routes[] = {
339 {"Headphones", NULL, "HPL"},
340 {"Headphones", NULL, "HPR"},
343 {"MIC", NULL, "Headset Mic"},
346 static const struct snd_soc_dapm_route rockchip_sound_dmic_routes[] = {
348 {"DMic", NULL, "Int Mic"},
351 static const struct snd_soc_dapm_route rockchip_sound_max98357a_routes[] = {
353 {"Speakers", NULL, "Speaker"},
356 static const struct snd_soc_dapm_route rockchip_sound_rt5514_routes[] = {
358 {"DMIC1L", NULL, "Int Mic"},
359 {"DMIC1R", NULL, "Int Mic"},
362 struct rockchip_sound_route {
363 const struct snd_soc_dapm_route *routes;
367 static const struct rockchip_sound_route rockchip_routes[] = {
369 .routes = rockchip_sound_cdndp_routes,
370 .num_routes = ARRAY_SIZE(rockchip_sound_cdndp_routes),
373 .routes = rockchip_sound_da7219_routes,
374 .num_routes = ARRAY_SIZE(rockchip_sound_da7219_routes),
377 .routes = rockchip_sound_dmic_routes,
378 .num_routes = ARRAY_SIZE(rockchip_sound_dmic_routes),
380 [DAILINK_MAX98357A] = {
381 .routes = rockchip_sound_max98357a_routes,
382 .num_routes = ARRAY_SIZE(rockchip_sound_max98357a_routes),
385 .routes = rockchip_sound_rt5514_routes,
386 .num_routes = ARRAY_SIZE(rockchip_sound_rt5514_routes),
388 [DAILINK_RT5514_DSP] = {},
391 struct dailink_match_data {
392 const char *compatible;
393 struct bus_type *bus_type;
396 static const struct dailink_match_data dailink_match[] = {
398 .compatible = "rockchip,rk3399-cdn-dp",
401 .compatible = "dlg,da7219",
404 .compatible = "dmic-codec",
406 [DAILINK_MAX98357A] = {
407 .compatible = "maxim,max98357a",
410 .compatible = "realtek,rt5514",
411 .bus_type = &i2c_bus_type,
413 [DAILINK_RT5514_DSP] = {
414 .compatible = "realtek,rt5514",
415 .bus_type = &spi_bus_type,
419 static int of_dev_node_match(struct device *dev, void *data)
421 return dev->of_node == data;
424 static int rockchip_sound_codec_node_match(struct device_node *np_codec)
429 for (i = 0; i < ARRAY_SIZE(dailink_match); i++) {
430 if (!of_device_is_compatible(np_codec,
431 dailink_match[i].compatible))
434 if (dailink_match[i].bus_type) {
435 dev = bus_find_device(dailink_match[i].bus_type, NULL,
436 np_codec, of_dev_node_match);
447 static int rockchip_sound_of_parse_dais(struct device *dev,
448 struct snd_soc_card *card)
450 struct device_node *np_cpu, *np_cpu0, *np_cpu1;
451 struct device_node *np_codec;
452 struct snd_soc_dai_link *dai;
453 struct snd_soc_dapm_route *routes;
457 card->dai_link = devm_kzalloc(dev, sizeof(rockchip_dais),
463 for (i = 0; i < ARRAY_SIZE(rockchip_routes); i++)
464 num_routes += rockchip_routes[i].num_routes;
465 routes = devm_kzalloc(dev, num_routes * sizeof(*routes),
469 card->dapm_routes = routes;
471 np_cpu0 = of_parse_phandle(dev->of_node, "rockchip,cpu", 0);
472 np_cpu1 = of_parse_phandle(dev->of_node, "rockchip,cpu", 1);
474 card->num_dapm_routes = 0;
476 for (i = 0; i < ARRAY_SIZE(rockchip_dais); i++) {
477 np_codec = of_parse_phandle(dev->of_node,
478 "rockchip,codec", i);
482 if (!of_device_is_available(np_codec))
485 index = rockchip_sound_codec_node_match(np_codec);
493 case DAILINK_RT5514_DSP:
502 dev_err(dev, "Missing 'rockchip,cpu' for %s\n",
503 rockchip_dais[index].name);
507 dai = &card->dai_link[card->num_links++];
508 *dai = rockchip_dais[index];
510 if (!dai->codec_name)
511 dai->codec_of_node = np_codec;
512 dai->platform_of_node = np_cpu;
513 dai->cpu_of_node = np_cpu;
515 if (card->num_dapm_routes + rockchip_routes[index].num_routes >
517 dev_err(dev, "Too many routes\n");
521 memcpy(routes + card->num_dapm_routes,
522 rockchip_routes[index].routes,
523 rockchip_routes[index].num_routes * sizeof(*routes));
524 card->num_dapm_routes += rockchip_routes[index].num_routes;
530 static int rockchip_sound_probe(struct platform_device *pdev)
532 struct snd_soc_card *card = &rockchip_sound_card;
535 ret = rockchip_sound_of_parse_dais(&pdev->dev, card);
537 dev_err(&pdev->dev, "Failed to parse dais: %d\n", ret);
541 /* Set DMIC wakeup delay */
542 ret = device_property_read_u32(&pdev->dev, "dmic-wakeup-delay-ms",
545 dmic_wakeup_delay = 0;
547 "no optional property 'dmic-wakeup-delay-ms' found, default: no delay\n");
550 card->dev = &pdev->dev;
551 return devm_snd_soc_register_card(&pdev->dev, card);
554 static const struct of_device_id rockchip_sound_of_match[] = {
555 { .compatible = "rockchip,rk3399-gru-sound", },
559 static struct platform_driver rockchip_sound_driver = {
560 .probe = rockchip_sound_probe,
563 .of_match_table = rockchip_sound_of_match,
565 .pm = &snd_soc_pm_ops,
570 module_platform_driver(rockchip_sound_driver);
572 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
573 MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
574 MODULE_LICENSE("GPL v2");
575 MODULE_ALIAS("platform:" DRV_NAME);
576 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);