Linux 4.15.6
[linux/fpc-iii.git] / sound / soc / rockchip / rk3399_gru_sound.c
blobd64fbbd505448b4c90a04a40d5ebc90dd83fd42b
1 /*
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
13 * more details.
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"
40 #define SOUND_FS 256
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;
66 unsigned int mclk;
67 int ret;
69 /* max98357a supports these sample rates */
70 switch (params_rate(params)) {
71 case 8000:
72 case 16000:
73 case 48000:
74 case 96000:
75 mclk = params_rate(params) * SOUND_FS;
76 break;
77 default:
78 dev_err(rtd->card->dev, "%s() doesn't support this sample rate: %d\n",
79 __func__, params_rate(params));
80 return -EINVAL;
83 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
84 if (ret) {
85 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
86 __func__, mclk, ret);
87 return ret;
90 return 0;
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;
99 unsigned int mclk;
100 int ret;
102 mclk = params_rate(params) * SOUND_FS;
104 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
105 SND_SOC_CLOCK_OUT);
106 if (ret < 0) {
107 dev_err(rtd->card->dev, "Can't set cpu clock out %d\n", ret);
108 return ret;
111 ret = snd_soc_dai_set_sysclk(codec_dai, RT5514_SCLK_S_MCLK,
112 mclk, SND_SOC_CLOCK_IN);
113 if (ret) {
114 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
115 __func__, params_rate(params) * 512, ret);
116 return ret;
119 /* Wait for DMIC stable */
120 msleep(dmic_wakeup_delay);
122 return 0;
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;
131 int mclk, ret;
133 /* in bypass mode, the mclk has to be one of the frequencies below */
134 switch (params_rate(params)) {
135 case 8000:
136 case 16000:
137 case 24000:
138 case 32000:
139 case 48000:
140 case 64000:
141 case 96000:
142 mclk = 12288000;
143 break;
144 case 11025:
145 case 22050:
146 case 44100:
147 case 88200:
148 mclk = 11289600;
149 break;
150 default:
151 return -EINVAL;
154 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
155 SND_SOC_CLOCK_OUT);
156 if (ret < 0) {
157 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
158 return ret;
161 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
162 SND_SOC_CLOCK_IN);
163 if (ret < 0) {
164 dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
165 return ret;
168 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
169 if (ret < 0) {
170 dev_err(codec_dai->dev, "Can't set pll sysclk mclk %d\n", ret);
171 return ret;
174 return 0;
177 static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime *rtd)
179 struct snd_soc_codec *codec = rtd->codec_dais[0]->codec;
180 struct snd_soc_dai *codec_dai = rtd->codec_dai;
181 int ret;
183 /* We need default MCLK and PLL settings for the accessory detection */
184 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 12288000,
185 SND_SOC_CLOCK_IN);
186 if (ret < 0) {
187 dev_err(codec_dai->dev, "Init can't set codec clock in %d\n", ret);
188 return ret;
191 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
192 if (ret < 0) {
193 dev_err(codec_dai->dev, "Init can't set pll sysclk mclk %d\n", ret);
194 return 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);
204 if (ret) {
205 dev_err(rtd->card->dev, "New Headset Jack failed! (%d)\n", ret);
206 return ret;
209 snd_jack_set_key(rockchip_sound_jack.jack, SND_JACK_BTN_0, KEY_MEDIA);
210 snd_jack_set_key(
211 rockchip_sound_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
212 snd_jack_set_key(
213 rockchip_sound_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
214 snd_jack_set_key(
215 rockchip_sound_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
217 da7219_aad_jack_det(codec, &rockchip_sound_jack);
219 return 0;
222 static int rockchip_sound_cdndp_hw_params(struct snd_pcm_substream *substream,
223 struct snd_pcm_hw_params *params)
225 struct snd_soc_pcm_runtime *rtd = substream->private_data;
226 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
227 struct snd_soc_dai *codec_dai = rtd->codec_dai;
228 int mclk, ret;
230 /* in bypass mode, the mclk has to be one of the frequencies below */
231 switch (params_rate(params)) {
232 case 8000:
233 case 16000:
234 case 24000:
235 case 32000:
236 case 48000:
237 case 64000:
238 case 96000:
239 mclk = 12288000;
240 break;
241 case 11025:
242 case 22050:
243 case 44100:
244 case 88200:
245 mclk = 11289600;
246 break;
247 default:
248 return -EINVAL;
251 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
252 SND_SOC_CLOCK_OUT);
253 if (ret < 0) {
254 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
255 return ret;
258 return 0;
261 static int rockchip_sound_dmic_hw_params(struct snd_pcm_substream *substream,
262 struct snd_pcm_hw_params *params)
264 struct snd_soc_pcm_runtime *rtd = substream->private_data;
265 unsigned int mclk;
266 int ret;
268 mclk = params_rate(params) * SOUND_FS;
270 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
271 if (ret) {
272 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
273 __func__, mclk, ret);
274 return ret;
277 /* Wait for DMIC stable */
278 msleep(dmic_wakeup_delay);
280 return 0;
283 static const struct snd_soc_ops rockchip_sound_max98357a_ops = {
284 .hw_params = rockchip_sound_max98357a_hw_params,
287 static const struct snd_soc_ops rockchip_sound_rt5514_ops = {
288 .hw_params = rockchip_sound_rt5514_hw_params,
291 static const struct snd_soc_ops rockchip_sound_da7219_ops = {
292 .hw_params = rockchip_sound_da7219_hw_params,
295 static const struct snd_soc_ops rockchip_sound_cdndp_ops = {
296 .hw_params = rockchip_sound_cdndp_hw_params,
299 static const struct snd_soc_ops rockchip_sound_dmic_ops = {
300 .hw_params = rockchip_sound_dmic_hw_params,
303 static struct snd_soc_card rockchip_sound_card = {
304 .name = "rk3399-gru-sound",
305 .owner = THIS_MODULE,
306 .dapm_widgets = rockchip_dapm_widgets,
307 .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
308 .controls = rockchip_controls,
309 .num_controls = ARRAY_SIZE(rockchip_controls),
312 enum {
313 DAILINK_CDNDP,
314 DAILINK_DA7219,
315 DAILINK_DMIC,
316 DAILINK_MAX98357A,
317 DAILINK_RT5514,
318 DAILINK_RT5514_DSP,
321 static const struct snd_soc_dai_link rockchip_dais[] = {
322 [DAILINK_CDNDP] = {
323 .name = "DP",
324 .stream_name = "DP PCM",
325 .codec_dai_name = "i2s-hifi",
326 .ops = &rockchip_sound_cdndp_ops,
327 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
328 SND_SOC_DAIFMT_CBS_CFS,
330 [DAILINK_DA7219] = {
331 .name = "DA7219",
332 .stream_name = "DA7219 PCM",
333 .codec_dai_name = "da7219-hifi",
334 .init = rockchip_sound_da7219_init,
335 .ops = &rockchip_sound_da7219_ops,
336 /* set da7219 as slave */
337 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
338 SND_SOC_DAIFMT_CBS_CFS,
340 [DAILINK_DMIC] = {
341 .name = "DMIC",
342 .stream_name = "DMIC PCM",
343 .codec_dai_name = "dmic-hifi",
344 .ops = &rockchip_sound_dmic_ops,
345 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
346 SND_SOC_DAIFMT_CBS_CFS,
348 [DAILINK_MAX98357A] = {
349 .name = "MAX98357A",
350 .stream_name = "MAX98357A PCM",
351 .codec_dai_name = "HiFi",
352 .ops = &rockchip_sound_max98357a_ops,
353 /* set max98357a as slave */
354 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
355 SND_SOC_DAIFMT_CBS_CFS,
357 [DAILINK_RT5514] = {
358 .name = "RT5514",
359 .stream_name = "RT5514 PCM",
360 .codec_dai_name = "rt5514-aif1",
361 .ops = &rockchip_sound_rt5514_ops,
362 /* set rt5514 as slave */
363 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
364 SND_SOC_DAIFMT_CBS_CFS,
366 /* RT5514 DSP for voice wakeup via spi bus */
367 [DAILINK_RT5514_DSP] = {
368 .name = "RT5514 DSP",
369 .stream_name = "Wake on Voice",
370 .codec_dai_name = "rt5514-dsp-cpu-dai",
374 static const struct snd_soc_dapm_route rockchip_sound_cdndp_routes[] = {
375 /* Output */
376 {"HDMI", NULL, "TX"},
379 static const struct snd_soc_dapm_route rockchip_sound_da7219_routes[] = {
380 /* Output */
381 {"Headphones", NULL, "HPL"},
382 {"Headphones", NULL, "HPR"},
384 /* Input */
385 {"MIC", NULL, "Headset Mic"},
388 static const struct snd_soc_dapm_route rockchip_sound_dmic_routes[] = {
389 /* Input */
390 {"DMic", NULL, "Int Mic"},
393 static const struct snd_soc_dapm_route rockchip_sound_max98357a_routes[] = {
394 /* Output */
395 {"Speakers", NULL, "Speaker"},
398 static const struct snd_soc_dapm_route rockchip_sound_rt5514_routes[] = {
399 /* Input */
400 {"DMIC1L", NULL, "Int Mic"},
401 {"DMIC1R", NULL, "Int Mic"},
404 struct rockchip_sound_route {
405 const struct snd_soc_dapm_route *routes;
406 int num_routes;
409 static const struct rockchip_sound_route rockchip_routes[] = {
410 [DAILINK_CDNDP] = {
411 .routes = rockchip_sound_cdndp_routes,
412 .num_routes = ARRAY_SIZE(rockchip_sound_cdndp_routes),
414 [DAILINK_DA7219] = {
415 .routes = rockchip_sound_da7219_routes,
416 .num_routes = ARRAY_SIZE(rockchip_sound_da7219_routes),
418 [DAILINK_DMIC] = {
419 .routes = rockchip_sound_dmic_routes,
420 .num_routes = ARRAY_SIZE(rockchip_sound_dmic_routes),
422 [DAILINK_MAX98357A] = {
423 .routes = rockchip_sound_max98357a_routes,
424 .num_routes = ARRAY_SIZE(rockchip_sound_max98357a_routes),
426 [DAILINK_RT5514] = {
427 .routes = rockchip_sound_rt5514_routes,
428 .num_routes = ARRAY_SIZE(rockchip_sound_rt5514_routes),
430 [DAILINK_RT5514_DSP] = {},
433 struct dailink_match_data {
434 const char *compatible;
435 struct bus_type *bus_type;
438 static const struct dailink_match_data dailink_match[] = {
439 [DAILINK_CDNDP] = {
440 .compatible = "rockchip,rk3399-cdn-dp",
442 [DAILINK_DA7219] = {
443 .compatible = "dlg,da7219",
445 [DAILINK_DMIC] = {
446 .compatible = "dmic-codec",
448 [DAILINK_MAX98357A] = {
449 .compatible = "maxim,max98357a",
451 [DAILINK_RT5514] = {
452 .compatible = "realtek,rt5514",
453 .bus_type = &i2c_bus_type,
455 [DAILINK_RT5514_DSP] = {
456 .compatible = "realtek,rt5514",
457 .bus_type = &spi_bus_type,
461 static int of_dev_node_match(struct device *dev, void *data)
463 return dev->of_node == data;
466 static int rockchip_sound_codec_node_match(struct device_node *np_codec)
468 struct device *dev;
469 int i;
471 for (i = 0; i < ARRAY_SIZE(dailink_match); i++) {
472 if (!of_device_is_compatible(np_codec,
473 dailink_match[i].compatible))
474 continue;
476 if (dailink_match[i].bus_type) {
477 dev = bus_find_device(dailink_match[i].bus_type, NULL,
478 np_codec, of_dev_node_match);
479 if (!dev)
480 continue;
481 put_device(dev);
484 return i;
486 return -1;
489 static int rockchip_sound_of_parse_dais(struct device *dev,
490 struct snd_soc_card *card)
492 struct device_node *np_cpu, *np_cpu0, *np_cpu1;
493 struct device_node *np_codec;
494 struct snd_soc_dai_link *dai;
495 struct snd_soc_dapm_route *routes;
496 int i, index;
497 int num_routes;
499 card->dai_link = devm_kzalloc(dev, sizeof(rockchip_dais),
500 GFP_KERNEL);
501 if (!card->dai_link)
502 return -ENOMEM;
504 num_routes = 0;
505 for (i = 0; i < ARRAY_SIZE(rockchip_routes); i++)
506 num_routes += rockchip_routes[i].num_routes;
507 routes = devm_kzalloc(dev, num_routes * sizeof(*routes),
508 GFP_KERNEL);
509 if (!routes)
510 return -ENOMEM;
511 card->dapm_routes = routes;
513 np_cpu0 = of_parse_phandle(dev->of_node, "rockchip,cpu", 0);
514 np_cpu1 = of_parse_phandle(dev->of_node, "rockchip,cpu", 1);
516 card->num_dapm_routes = 0;
517 card->num_links = 0;
518 for (i = 0; i < ARRAY_SIZE(rockchip_dais); i++) {
519 np_codec = of_parse_phandle(dev->of_node,
520 "rockchip,codec", i);
521 if (!np_codec)
522 break;
524 if (!of_device_is_available(np_codec))
525 continue;
527 index = rockchip_sound_codec_node_match(np_codec);
528 if (index < 0)
529 continue;
531 np_cpu = (index == DAILINK_CDNDP) ? np_cpu1 : np_cpu0;
532 if (!np_cpu) {
533 dev_err(dev, "Missing 'rockchip,cpu' for %s\n",
534 rockchip_dais[index].name);
535 return -EINVAL;
538 dai = &card->dai_link[card->num_links++];
539 *dai = rockchip_dais[index];
541 dai->codec_of_node = np_codec;
542 dai->platform_of_node = np_cpu;
543 dai->cpu_of_node = np_cpu;
545 if (card->num_dapm_routes + rockchip_routes[index].num_routes >
546 num_routes) {
547 dev_err(dev, "Too many routes\n");
548 return -EINVAL;
551 memcpy(routes + card->num_dapm_routes,
552 rockchip_routes[index].routes,
553 rockchip_routes[index].num_routes * sizeof(*routes));
554 card->num_dapm_routes += rockchip_routes[index].num_routes;
557 return 0;
560 static int rockchip_sound_probe(struct platform_device *pdev)
562 struct snd_soc_card *card = &rockchip_sound_card;
563 int ret;
565 ret = rockchip_sound_of_parse_dais(&pdev->dev, card);
566 if (ret < 0) {
567 dev_err(&pdev->dev, "Failed to parse dais: %d\n", ret);
568 return ret;
571 /* Set DMIC wakeup delay */
572 ret = device_property_read_u32(&pdev->dev, "dmic-wakeup-delay-ms",
573 &dmic_wakeup_delay);
574 if (ret) {
575 dmic_wakeup_delay = 0;
576 dev_dbg(&pdev->dev,
577 "no optional property 'dmic-wakeup-delay-ms' found, default: no delay\n");
580 card->dev = &pdev->dev;
581 return devm_snd_soc_register_card(&pdev->dev, card);
584 static const struct of_device_id rockchip_sound_of_match[] = {
585 { .compatible = "rockchip,rk3399-gru-sound", },
589 static struct platform_driver rockchip_sound_driver = {
590 .probe = rockchip_sound_probe,
591 .driver = {
592 .name = DRV_NAME,
593 .of_match_table = rockchip_sound_of_match,
594 #ifdef CONFIG_PM
595 .pm = &snd_soc_pm_ops,
596 #endif
600 module_platform_driver(rockchip_sound_driver);
602 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
603 MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
604 MODULE_LICENSE("GPL v2");
605 MODULE_ALIAS("platform:" DRV_NAME);
606 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);