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 mclk
= params_rate(params
) * SOUND_FS
;
60 ret
= snd_soc_dai_set_sysclk(rtd
->cpu_dai
, 0, mclk
, 0);
62 dev_err(rtd
->card
->dev
, "%s() error setting sysclk to %u: %d\n",
70 static int rockchip_sound_rt5514_hw_params(struct snd_pcm_substream
*substream
,
71 struct snd_pcm_hw_params
*params
)
73 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
74 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
75 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
79 mclk
= params_rate(params
) * SOUND_FS
;
81 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
84 dev_err(rtd
->card
->dev
, "Can't set cpu clock out %d\n", ret
);
88 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT5514_SCLK_S_MCLK
,
89 mclk
, SND_SOC_CLOCK_IN
);
91 dev_err(rtd
->card
->dev
, "%s() error setting sysclk to %u: %d\n",
92 __func__
, params_rate(params
) * 512, ret
);
96 /* Wait for DMIC stable */
97 msleep(dmic_wakeup_delay
);
102 static int rockchip_sound_da7219_hw_params(struct snd_pcm_substream
*substream
,
103 struct snd_pcm_hw_params
*params
)
105 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
106 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
107 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
110 /* in bypass mode, the mclk has to be one of the frequencies below */
111 switch (params_rate(params
)) {
131 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
134 dev_err(codec_dai
->dev
, "Can't set cpu clock out %d\n", ret
);
138 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
141 dev_err(codec_dai
->dev
, "Can't set codec clock in %d\n", ret
);
145 ret
= snd_soc_dai_set_pll(codec_dai
, 0, DA7219_SYSCLK_MCLK
, 0, 0);
147 dev_err(codec_dai
->dev
, "Can't set pll sysclk mclk %d\n", ret
);
154 static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime
*rtd
)
156 struct snd_soc_component
*component
= rtd
->codec_dais
[0]->component
;
157 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
160 /* We need default MCLK and PLL settings for the accessory detection */
161 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, 12288000,
164 dev_err(codec_dai
->dev
, "Init 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
, "Init can't set pll sysclk mclk %d\n", ret
);
174 /* Enable Headset and 4 Buttons Jack detection */
175 ret
= snd_soc_card_jack_new(rtd
->card
, "Headset Jack",
176 SND_JACK_HEADSET
| SND_JACK_LINEOUT
|
177 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
178 SND_JACK_BTN_2
| SND_JACK_BTN_3
,
179 &rockchip_sound_jack
, NULL
, 0);
182 dev_err(rtd
->card
->dev
, "New Headset Jack failed! (%d)\n", ret
);
187 rockchip_sound_jack
.jack
, SND_JACK_BTN_0
, KEY_PLAYPAUSE
);
189 rockchip_sound_jack
.jack
, SND_JACK_BTN_1
, KEY_VOLUMEUP
);
191 rockchip_sound_jack
.jack
, SND_JACK_BTN_2
, KEY_VOLUMEDOWN
);
193 rockchip_sound_jack
.jack
, SND_JACK_BTN_3
, KEY_VOICECOMMAND
);
195 da7219_aad_jack_det(component
, &rockchip_sound_jack
);
200 static int rockchip_sound_dmic_hw_params(struct snd_pcm_substream
*substream
,
201 struct snd_pcm_hw_params
*params
)
203 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
207 mclk
= params_rate(params
) * SOUND_FS
;
209 ret
= snd_soc_dai_set_sysclk(rtd
->cpu_dai
, 0, mclk
, 0);
211 dev_err(rtd
->card
->dev
, "%s() error setting sysclk to %u: %d\n",
212 __func__
, mclk
, ret
);
216 /* Wait for DMIC stable */
217 msleep(dmic_wakeup_delay
);
222 static const struct snd_soc_ops rockchip_sound_max98357a_ops
= {
223 .hw_params
= rockchip_sound_max98357a_hw_params
,
226 static const struct snd_soc_ops rockchip_sound_rt5514_ops
= {
227 .hw_params
= rockchip_sound_rt5514_hw_params
,
230 static const struct snd_soc_ops rockchip_sound_da7219_ops
= {
231 .hw_params
= rockchip_sound_da7219_hw_params
,
234 static const struct snd_soc_ops rockchip_sound_dmic_ops
= {
235 .hw_params
= rockchip_sound_dmic_hw_params
,
238 static struct snd_soc_card rockchip_sound_card
= {
239 .name
= "rk3399-gru-sound",
240 .owner
= THIS_MODULE
,
241 .dapm_widgets
= rockchip_dapm_widgets
,
242 .num_dapm_widgets
= ARRAY_SIZE(rockchip_dapm_widgets
),
243 .controls
= rockchip_controls
,
244 .num_controls
= ARRAY_SIZE(rockchip_controls
),
256 SND_SOC_DAILINK_DEFS(cdndp
,
257 DAILINK_COMP_ARRAY(COMP_EMPTY()),
258 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "spdif-hifi")),
259 DAILINK_COMP_ARRAY(COMP_EMPTY()));
261 SND_SOC_DAILINK_DEFS(da7219
,
262 DAILINK_COMP_ARRAY(COMP_EMPTY()),
263 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "da7219-hifi")),
264 DAILINK_COMP_ARRAY(COMP_EMPTY()));
266 SND_SOC_DAILINK_DEFS(dmic
,
267 DAILINK_COMP_ARRAY(COMP_EMPTY()),
268 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "dmic-hifi")),
269 DAILINK_COMP_ARRAY(COMP_EMPTY()));
271 SND_SOC_DAILINK_DEFS(max98357a
,
272 DAILINK_COMP_ARRAY(COMP_EMPTY()),
273 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "HiFi")),
274 DAILINK_COMP_ARRAY(COMP_EMPTY()));
276 SND_SOC_DAILINK_DEFS(rt5514
,
277 DAILINK_COMP_ARRAY(COMP_EMPTY()),
278 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "rt5514-aif1")),
279 DAILINK_COMP_ARRAY(COMP_EMPTY()));
281 SND_SOC_DAILINK_DEFS(rt5514_dsp
,
282 DAILINK_COMP_ARRAY(COMP_EMPTY()),
283 DAILINK_COMP_ARRAY(COMP_DUMMY()),
284 DAILINK_COMP_ARRAY(COMP_EMPTY()));
286 static const struct snd_soc_dai_link rockchip_dais
[] = {
289 .stream_name
= "DP PCM",
290 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
291 SND_SOC_DAIFMT_CBS_CFS
,
292 SND_SOC_DAILINK_REG(cdndp
),
296 .stream_name
= "DA7219 PCM",
297 .init
= rockchip_sound_da7219_init
,
298 .ops
= &rockchip_sound_da7219_ops
,
299 /* set da7219 as slave */
300 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
301 SND_SOC_DAIFMT_CBS_CFS
,
302 SND_SOC_DAILINK_REG(da7219
),
306 .stream_name
= "DMIC PCM",
307 .ops
= &rockchip_sound_dmic_ops
,
308 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
309 SND_SOC_DAIFMT_CBS_CFS
,
310 SND_SOC_DAILINK_REG(dmic
),
312 [DAILINK_MAX98357A
] = {
314 .stream_name
= "MAX98357A PCM",
315 .ops
= &rockchip_sound_max98357a_ops
,
316 /* set max98357a as slave */
317 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
318 SND_SOC_DAIFMT_CBS_CFS
,
319 SND_SOC_DAILINK_REG(max98357a
),
323 .stream_name
= "RT5514 PCM",
324 .ops
= &rockchip_sound_rt5514_ops
,
325 /* set rt5514 as slave */
326 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
327 SND_SOC_DAIFMT_CBS_CFS
,
328 SND_SOC_DAILINK_REG(rt5514
),
330 /* RT5514 DSP for voice wakeup via spi bus */
331 [DAILINK_RT5514_DSP
] = {
332 .name
= "RT5514 DSP",
333 .stream_name
= "Wake on Voice",
334 SND_SOC_DAILINK_REG(rt5514_dsp
),
338 static const struct snd_soc_dapm_route rockchip_sound_cdndp_routes
[] = {
340 {"HDMI", NULL
, "TX"},
343 static const struct snd_soc_dapm_route rockchip_sound_da7219_routes
[] = {
345 {"Headphones", NULL
, "HPL"},
346 {"Headphones", NULL
, "HPR"},
349 {"MIC", NULL
, "Headset Mic"},
352 static const struct snd_soc_dapm_route rockchip_sound_dmic_routes
[] = {
354 {"DMic", NULL
, "Int Mic"},
357 static const struct snd_soc_dapm_route rockchip_sound_max98357a_routes
[] = {
359 {"Speakers", NULL
, "Speaker"},
362 static const struct snd_soc_dapm_route rockchip_sound_rt5514_routes
[] = {
364 {"DMIC1L", NULL
, "Int Mic"},
365 {"DMIC1R", NULL
, "Int Mic"},
368 struct rockchip_sound_route
{
369 const struct snd_soc_dapm_route
*routes
;
373 static const struct rockchip_sound_route rockchip_routes
[] = {
375 .routes
= rockchip_sound_cdndp_routes
,
376 .num_routes
= ARRAY_SIZE(rockchip_sound_cdndp_routes
),
379 .routes
= rockchip_sound_da7219_routes
,
380 .num_routes
= ARRAY_SIZE(rockchip_sound_da7219_routes
),
383 .routes
= rockchip_sound_dmic_routes
,
384 .num_routes
= ARRAY_SIZE(rockchip_sound_dmic_routes
),
386 [DAILINK_MAX98357A
] = {
387 .routes
= rockchip_sound_max98357a_routes
,
388 .num_routes
= ARRAY_SIZE(rockchip_sound_max98357a_routes
),
391 .routes
= rockchip_sound_rt5514_routes
,
392 .num_routes
= ARRAY_SIZE(rockchip_sound_rt5514_routes
),
394 [DAILINK_RT5514_DSP
] = {},
397 struct dailink_match_data
{
398 const char *compatible
;
399 struct bus_type
*bus_type
;
402 static const struct dailink_match_data dailink_match
[] = {
404 .compatible
= "rockchip,rk3399-cdn-dp",
407 .compatible
= "dlg,da7219",
410 .compatible
= "dmic-codec",
412 [DAILINK_MAX98357A
] = {
413 .compatible
= "maxim,max98357a",
416 .compatible
= "realtek,rt5514",
417 .bus_type
= &i2c_bus_type
,
419 [DAILINK_RT5514_DSP
] = {
420 .compatible
= "realtek,rt5514",
421 .bus_type
= &spi_bus_type
,
425 static int rockchip_sound_codec_node_match(struct device_node
*np_codec
)
430 for (i
= 0; i
< ARRAY_SIZE(dailink_match
); i
++) {
431 if (!of_device_is_compatible(np_codec
,
432 dailink_match
[i
].compatible
))
435 if (dailink_match
[i
].bus_type
) {
436 dev
= bus_find_device_by_of_node(dailink_match
[i
].bus_type
,
448 static int rockchip_sound_of_parse_dais(struct device
*dev
,
449 struct snd_soc_card
*card
)
451 struct device_node
*np_cpu
, *np_cpu0
, *np_cpu1
;
452 struct device_node
*np_codec
;
453 struct snd_soc_dai_link
*dai
;
454 struct snd_soc_dapm_route
*routes
;
458 card
->dai_link
= devm_kzalloc(dev
, sizeof(rockchip_dais
),
464 for (i
= 0; i
< ARRAY_SIZE(rockchip_routes
); i
++)
465 num_routes
+= rockchip_routes
[i
].num_routes
;
466 routes
= devm_kcalloc(dev
, num_routes
, sizeof(*routes
),
470 card
->dapm_routes
= routes
;
472 np_cpu0
= of_parse_phandle(dev
->of_node
, "rockchip,cpu", 0);
473 np_cpu1
= of_parse_phandle(dev
->of_node
, "rockchip,cpu", 1);
475 card
->num_dapm_routes
= 0;
477 for (i
= 0; i
< ARRAY_SIZE(rockchip_dais
); i
++) {
478 np_codec
= of_parse_phandle(dev
->of_node
,
479 "rockchip,codec", i
);
483 if (!of_device_is_available(np_codec
))
486 index
= rockchip_sound_codec_node_match(np_codec
);
494 case DAILINK_RT5514_DSP
:
503 dev_err(dev
, "Missing 'rockchip,cpu' for %s\n",
504 rockchip_dais
[index
].name
);
508 dai
= &card
->dai_link
[card
->num_links
++];
509 *dai
= rockchip_dais
[index
];
511 if (!dai
->codecs
->name
)
512 dai
->codecs
->of_node
= np_codec
;
513 dai
->platforms
->of_node
= np_cpu
;
514 dai
->cpus
->of_node
= np_cpu
;
516 if (card
->num_dapm_routes
+ rockchip_routes
[index
].num_routes
>
518 dev_err(dev
, "Too many routes\n");
522 memcpy(routes
+ card
->num_dapm_routes
,
523 rockchip_routes
[index
].routes
,
524 rockchip_routes
[index
].num_routes
* sizeof(*routes
));
525 card
->num_dapm_routes
+= rockchip_routes
[index
].num_routes
;
531 static int rockchip_sound_probe(struct platform_device
*pdev
)
533 struct snd_soc_card
*card
= &rockchip_sound_card
;
536 ret
= rockchip_sound_of_parse_dais(&pdev
->dev
, card
);
538 dev_err(&pdev
->dev
, "Failed to parse dais: %d\n", ret
);
542 /* Set DMIC wakeup delay */
543 ret
= device_property_read_u32(&pdev
->dev
, "dmic-wakeup-delay-ms",
546 dmic_wakeup_delay
= 0;
548 "no optional property 'dmic-wakeup-delay-ms' found, default: no delay\n");
551 card
->dev
= &pdev
->dev
;
552 return devm_snd_soc_register_card(&pdev
->dev
, card
);
555 static const struct of_device_id rockchip_sound_of_match
[] = {
556 { .compatible
= "rockchip,rk3399-gru-sound", },
560 static struct platform_driver rockchip_sound_driver
= {
561 .probe
= rockchip_sound_probe
,
564 .of_match_table
= rockchip_sound_of_match
,
566 .pm
= &snd_soc_pm_ops
,
571 module_platform_driver(rockchip_sound_driver
);
573 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
574 MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
575 MODULE_LICENSE("GPL v2");
576 MODULE_ALIAS("platform:" DRV_NAME
);
577 MODULE_DEVICE_TABLE(of
, rockchip_sound_of_match
);