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/input.h>
27 #include <sound/core.h>
28 #include <sound/jack.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include "rockchip_i2s.h"
33 #include "../codecs/da7219.h"
34 #include "../codecs/da7219-aad.h"
35 #include "../codecs/rt5514.h"
37 #define DRV_NAME "rk3399-gru-sound"
41 static unsigned int dmic_wakeup_delay
;
43 static struct snd_soc_jack rockchip_sound_jack
;
45 static const struct snd_soc_dapm_widget rockchip_dapm_widgets
[] = {
46 SND_SOC_DAPM_HP("Headphones", NULL
),
47 SND_SOC_DAPM_SPK("Speakers", NULL
),
48 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
49 SND_SOC_DAPM_MIC("Int Mic", NULL
),
50 SND_SOC_DAPM_LINE("HDMI", NULL
),
53 static const struct snd_soc_dapm_route rockchip_dapm_routes
[] = {
55 {"MIC", NULL
, "Headset Mic"},
56 {"DMIC1L", NULL
, "Int Mic"},
57 {"DMIC1R", NULL
, "Int Mic"},
58 {"DMic", NULL
, "Int Mic"},
61 {"Headphones", NULL
, "HPL"},
62 {"Headphones", NULL
, "HPR"},
63 {"Speakers", NULL
, "Speaker"},
67 static const struct snd_kcontrol_new rockchip_controls
[] = {
68 SOC_DAPM_PIN_SWITCH("Headphones"),
69 SOC_DAPM_PIN_SWITCH("Speakers"),
70 SOC_DAPM_PIN_SWITCH("Headset Mic"),
71 SOC_DAPM_PIN_SWITCH("Int Mic"),
72 SOC_DAPM_PIN_SWITCH("HDMI"),
75 static int rockchip_sound_max98357a_hw_params(struct snd_pcm_substream
*substream
,
76 struct snd_pcm_hw_params
*params
)
78 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
82 /* max98357a supports these sample rates */
83 switch (params_rate(params
)) {
88 mclk
= params_rate(params
) * SOUND_FS
;
91 dev_err(rtd
->card
->dev
, "%s() doesn't support this sample rate: %d\n",
92 __func__
, params_rate(params
));
96 ret
= snd_soc_dai_set_sysclk(rtd
->cpu_dai
, 0, mclk
, 0);
98 dev_err(rtd
->card
->dev
, "%s() error setting sysclk to %u: %d\n",
106 static int rockchip_sound_rt5514_hw_params(struct snd_pcm_substream
*substream
,
107 struct snd_pcm_hw_params
*params
)
109 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
110 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
111 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
115 mclk
= params_rate(params
) * SOUND_FS
;
117 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
120 dev_err(rtd
->card
->dev
, "Can't set cpu clock out %d\n", ret
);
124 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT5514_SCLK_S_MCLK
,
125 mclk
, SND_SOC_CLOCK_IN
);
127 dev_err(rtd
->card
->dev
, "%s() error setting sysclk to %u: %d\n",
128 __func__
, params_rate(params
) * 512, ret
);
132 /* Wait for DMIC stable */
133 msleep(dmic_wakeup_delay
);
138 static int rockchip_sound_da7219_hw_params(struct snd_pcm_substream
*substream
,
139 struct snd_pcm_hw_params
*params
)
141 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
142 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
143 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
146 /* in bypass mode, the mclk has to be one of the frequencies below */
147 switch (params_rate(params
)) {
167 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
170 dev_err(codec_dai
->dev
, "Can't set cpu clock out %d\n", ret
);
174 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
177 dev_err(codec_dai
->dev
, "Can't set codec clock in %d\n", ret
);
181 ret
= snd_soc_dai_set_pll(codec_dai
, 0, DA7219_SYSCLK_MCLK
, 0, 0);
183 dev_err(codec_dai
->dev
, "Can't set pll sysclk mclk %d\n", ret
);
190 static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime
*rtd
)
192 struct snd_soc_codec
*codec
= rtd
->codec_dais
[0]->codec
;
193 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
196 /* We need default MCLK and PLL settings for the accessory detection */
197 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, 12288000,
200 dev_err(codec_dai
->dev
, "Init can't set codec clock in %d\n", ret
);
204 ret
= snd_soc_dai_set_pll(codec_dai
, 0, DA7219_SYSCLK_MCLK
, 0, 0);
206 dev_err(codec_dai
->dev
, "Init can't set pll sysclk mclk %d\n", ret
);
210 /* Enable Headset and 4 Buttons Jack detection */
211 ret
= snd_soc_card_jack_new(rtd
->card
, "Headset Jack",
212 SND_JACK_HEADSET
| SND_JACK_LINEOUT
|
213 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
214 SND_JACK_BTN_2
| SND_JACK_BTN_3
,
215 &rockchip_sound_jack
, NULL
, 0);
218 dev_err(rtd
->card
->dev
, "New Headset Jack failed! (%d)\n", ret
);
222 snd_jack_set_key(rockchip_sound_jack
.jack
, SND_JACK_BTN_0
, KEY_MEDIA
);
224 rockchip_sound_jack
.jack
, SND_JACK_BTN_1
, KEY_VOLUMEUP
);
226 rockchip_sound_jack
.jack
, SND_JACK_BTN_2
, KEY_VOLUMEDOWN
);
228 rockchip_sound_jack
.jack
, SND_JACK_BTN_3
, KEY_VOICECOMMAND
);
230 da7219_aad_jack_det(codec
, &rockchip_sound_jack
);
235 static int rockchip_sound_cdndp_hw_params(struct snd_pcm_substream
*substream
,
236 struct snd_pcm_hw_params
*params
)
238 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
239 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
240 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
243 /* in bypass mode, the mclk has to be one of the frequencies below */
244 switch (params_rate(params
)) {
264 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
267 dev_err(codec_dai
->dev
, "Can't set cpu clock out %d\n", ret
);
274 static int rockchip_sound_dmic_hw_params(struct snd_pcm_substream
*substream
,
275 struct snd_pcm_hw_params
*params
)
277 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
281 mclk
= params_rate(params
) * SOUND_FS
;
283 ret
= snd_soc_dai_set_sysclk(rtd
->cpu_dai
, 0, mclk
, 0);
285 dev_err(rtd
->card
->dev
, "%s() error setting sysclk to %u: %d\n",
286 __func__
, mclk
, ret
);
290 /* Wait for DMIC stable */
291 msleep(dmic_wakeup_delay
);
296 static const struct snd_soc_ops rockchip_sound_max98357a_ops
= {
297 .hw_params
= rockchip_sound_max98357a_hw_params
,
300 static const struct snd_soc_ops rockchip_sound_rt5514_ops
= {
301 .hw_params
= rockchip_sound_rt5514_hw_params
,
304 static const struct snd_soc_ops rockchip_sound_da7219_ops
= {
305 .hw_params
= rockchip_sound_da7219_hw_params
,
308 static const struct snd_soc_ops rockchip_sound_cdndp_ops
= {
309 .hw_params
= rockchip_sound_cdndp_hw_params
,
312 static const struct snd_soc_ops rockchip_sound_dmic_ops
= {
313 .hw_params
= rockchip_sound_dmic_hw_params
,
316 static struct snd_soc_card rockchip_sound_card
= {
317 .name
= "rk3399-gru-sound",
318 .owner
= THIS_MODULE
,
319 .dapm_widgets
= rockchip_dapm_widgets
,
320 .num_dapm_widgets
= ARRAY_SIZE(rockchip_dapm_widgets
),
321 .dapm_routes
= rockchip_dapm_routes
,
322 .num_dapm_routes
= ARRAY_SIZE(rockchip_dapm_routes
),
323 .controls
= rockchip_controls
,
324 .num_controls
= ARRAY_SIZE(rockchip_controls
),
336 static const char * const dailink_compat
[] = {
337 [DAILINK_CDNDP
] = "rockchip,rk3399-cdn-dp",
338 [DAILINK_DA7219
] = "dlg,da7219",
339 [DAILINK_DMIC
] = "dmic-codec",
340 [DAILINK_MAX98357A
] = "maxim,max98357a",
341 [DAILINK_RT5514
] = "realtek,rt5514-i2c",
342 [DAILINK_RT5514_DSP
] = "realtek,rt5514-spi",
345 static const struct snd_soc_dai_link rockchip_dais
[] = {
348 .stream_name
= "DP PCM",
349 .codec_dai_name
= "i2s-hifi",
350 .ops
= &rockchip_sound_cdndp_ops
,
351 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
352 SND_SOC_DAIFMT_CBS_CFS
,
356 .stream_name
= "DA7219 PCM",
357 .codec_dai_name
= "da7219-hifi",
358 .init
= rockchip_sound_da7219_init
,
359 .ops
= &rockchip_sound_da7219_ops
,
360 /* set da7219 as slave */
361 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
362 SND_SOC_DAIFMT_CBS_CFS
,
366 .stream_name
= "DMIC PCM",
367 .codec_dai_name
= "dmic-hifi",
368 .ops
= &rockchip_sound_dmic_ops
,
369 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
370 SND_SOC_DAIFMT_CBS_CFS
,
372 [DAILINK_MAX98357A
] = {
374 .stream_name
= "MAX98357A PCM",
375 .codec_dai_name
= "HiFi",
376 .ops
= &rockchip_sound_max98357a_ops
,
377 /* set max98357a as slave */
378 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
379 SND_SOC_DAIFMT_CBS_CFS
,
383 .stream_name
= "RT5514 PCM",
384 .codec_dai_name
= "rt5514-aif1",
385 .ops
= &rockchip_sound_rt5514_ops
,
386 /* set rt5514 as slave */
387 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
388 SND_SOC_DAIFMT_CBS_CFS
,
390 /* RT5514 DSP for voice wakeup via spi bus */
391 [DAILINK_RT5514_DSP
] = {
392 .name
= "RT5514 DSP",
393 .stream_name
= "Wake on Voice",
394 .codec_dai_name
= "rt5514-dsp-cpu-dai",
398 static int rockchip_sound_codec_node_match(struct device_node
*np_codec
)
402 for (i
= 0; i
< ARRAY_SIZE(dailink_compat
); i
++) {
403 if (of_device_is_compatible(np_codec
, dailink_compat
[i
]))
409 static int rockchip_sound_of_parse_dais(struct device
*dev
,
410 struct snd_soc_card
*card
)
412 struct device_node
*np_cpu
, *np_cpu0
, *np_cpu1
;
413 struct device_node
*np_codec
;
414 struct snd_soc_dai_link
*dai
;
417 card
->dai_link
= devm_kzalloc(dev
, sizeof(rockchip_dais
),
422 np_cpu0
= of_parse_phandle(dev
->of_node
, "rockchip,cpu", 0);
423 np_cpu1
= of_parse_phandle(dev
->of_node
, "rockchip,cpu", 1);
426 for (i
= 0; i
< ARRAY_SIZE(rockchip_dais
); i
++) {
427 np_codec
= of_parse_phandle(dev
->of_node
,
428 "rockchip,codec", i
);
432 if (!of_device_is_available(np_codec
))
435 index
= rockchip_sound_codec_node_match(np_codec
);
439 np_cpu
= (index
== DAILINK_CDNDP
) ? np_cpu1
: np_cpu0
;
441 dev_err(dev
, "Missing 'rockchip,cpu' for %s\n",
442 rockchip_dais
[index
].name
);
446 dai
= &card
->dai_link
[card
->num_links
++];
447 *dai
= rockchip_dais
[index
];
449 dai
->codec_of_node
= np_codec
;
450 dai
->platform_of_node
= np_cpu
;
451 dai
->cpu_of_node
= np_cpu
;
457 static int rockchip_sound_probe(struct platform_device
*pdev
)
459 struct snd_soc_card
*card
= &rockchip_sound_card
;
462 ret
= rockchip_sound_of_parse_dais(&pdev
->dev
, card
);
464 dev_err(&pdev
->dev
, "Failed to parse dais: %d\n", ret
);
468 /* Set DMIC wakeup delay */
469 ret
= device_property_read_u32(&pdev
->dev
, "dmic-wakeup-delay-ms",
472 dmic_wakeup_delay
= 0;
474 "no optional property 'dmic-wakeup-delay-ms' found, default: no delay\n");
477 card
->dev
= &pdev
->dev
;
478 return devm_snd_soc_register_card(&pdev
->dev
, card
);
481 static const struct of_device_id rockchip_sound_of_match
[] = {
482 { .compatible
= "rockchip,rk3399-gru-sound", },
486 static struct platform_driver rockchip_sound_driver
= {
487 .probe
= rockchip_sound_probe
,
490 .of_match_table
= rockchip_sound_of_match
,
492 .pm
= &snd_soc_pm_ops
,
497 module_platform_driver(rockchip_sound_driver
);
499 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
500 MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
501 MODULE_LICENSE("GPL v2");
502 MODULE_ALIAS("platform:" DRV_NAME
);
503 MODULE_DEVICE_TABLE(of
, rockchip_sound_of_match
);