x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / sound / soc / rockchip / rk3399_gru_sound.c
blob21ac8d6cce3a054d1a055423d332129289dce278
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/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"
39 #define SOUND_FS 256
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),
52 static const struct snd_soc_dapm_route rockchip_dapm_routes[] = {
53 /* Input Lines */
54 {"MIC", NULL, "Headset Mic"},
55 {"DMIC1L", NULL, "Int Mic"},
56 {"DMIC1R", NULL, "Int Mic"},
58 /* Output Lines */
59 {"Headphones", NULL, "HPL"},
60 {"Headphones", NULL, "HPR"},
61 {"Speakers", NULL, "Speaker"},
64 static const struct snd_kcontrol_new rockchip_controls[] = {
65 SOC_DAPM_PIN_SWITCH("Headphones"),
66 SOC_DAPM_PIN_SWITCH("Speakers"),
67 SOC_DAPM_PIN_SWITCH("Headset Mic"),
68 SOC_DAPM_PIN_SWITCH("Int Mic"),
71 static int rockchip_sound_max98357a_hw_params(struct snd_pcm_substream *substream,
72 struct snd_pcm_hw_params *params)
74 struct snd_soc_pcm_runtime *rtd = substream->private_data;
75 unsigned int mclk;
76 int ret;
78 /* max98357a supports these sample rates */
79 switch (params_rate(params)) {
80 case 8000:
81 case 16000:
82 case 48000:
83 case 96000:
84 mclk = params_rate(params) * SOUND_FS;
85 break;
86 default:
87 dev_err(rtd->card->dev, "%s() doesn't support this sample rate: %d\n",
88 __func__, params_rate(params));
89 return -EINVAL;
92 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
93 if (ret) {
94 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
95 __func__, mclk, ret);
96 return ret;
99 return 0;
102 static int rockchip_sound_rt5514_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;
108 unsigned int mclk;
109 int ret;
111 mclk = params_rate(params) * SOUND_FS;
113 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
114 SND_SOC_CLOCK_OUT);
115 if (ret < 0) {
116 dev_err(rtd->card->dev, "Can't set cpu clock out %d\n", ret);
117 return ret;
120 ret = snd_soc_dai_set_sysclk(codec_dai, RT5514_SCLK_S_MCLK,
121 mclk, SND_SOC_CLOCK_IN);
122 if (ret) {
123 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
124 __func__, params_rate(params) * 512, ret);
125 return ret;
128 /* Wait for DMIC stable */
129 msleep(dmic_wakeup_delay);
131 return 0;
134 static int rockchip_sound_da7219_hw_params(struct snd_pcm_substream *substream,
135 struct snd_pcm_hw_params *params)
137 struct snd_soc_pcm_runtime *rtd = substream->private_data;
138 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
139 struct snd_soc_dai *codec_dai = rtd->codec_dai;
140 int mclk, ret;
142 /* in bypass mode, the mclk has to be one of the frequencies below */
143 switch (params_rate(params)) {
144 case 8000:
145 case 16000:
146 case 24000:
147 case 32000:
148 case 48000:
149 case 64000:
150 case 96000:
151 mclk = 12288000;
152 break;
153 case 11025:
154 case 22050:
155 case 44100:
156 case 88200:
157 mclk = 11289600;
158 break;
159 default:
160 return -EINVAL;
163 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
164 SND_SOC_CLOCK_OUT);
165 if (ret < 0) {
166 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
167 return ret;
170 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
171 SND_SOC_CLOCK_IN);
172 if (ret < 0) {
173 dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
174 return ret;
177 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
178 if (ret < 0) {
179 dev_err(codec_dai->dev, "Can't set pll sysclk mclk %d\n", ret);
180 return ret;
183 return 0;
186 static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime *rtd)
188 struct snd_soc_codec *codec = rtd->codec_dais[0]->codec;
189 struct snd_soc_dai *codec_dai = rtd->codec_dai;
190 int ret;
192 /* We need default MCLK and PLL settings for the accessory detection */
193 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 12288000,
194 SND_SOC_CLOCK_IN);
195 if (ret < 0) {
196 dev_err(codec_dai->dev, "Init can't set codec clock in %d\n", ret);
197 return ret;
200 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
201 if (ret < 0) {
202 dev_err(codec_dai->dev, "Init can't set pll sysclk mclk %d\n", ret);
203 return ret;
206 /* Enable Headset and 4 Buttons Jack detection */
207 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
208 SND_JACK_HEADSET | SND_JACK_LINEOUT |
209 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
210 SND_JACK_BTN_2 | SND_JACK_BTN_3,
211 &rockchip_sound_jack, NULL, 0);
213 if (ret) {
214 dev_err(rtd->card->dev, "New Headset Jack failed! (%d)\n", ret);
215 return ret;
218 snd_jack_set_key(rockchip_sound_jack.jack, SND_JACK_BTN_0, KEY_MEDIA);
219 snd_jack_set_key(
220 rockchip_sound_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
221 snd_jack_set_key(
222 rockchip_sound_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
223 snd_jack_set_key(
224 rockchip_sound_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
226 da7219_aad_jack_det(codec, &rockchip_sound_jack);
228 return 0;
231 static int rockchip_sound_cdndp_hw_params(struct snd_pcm_substream *substream,
232 struct snd_pcm_hw_params *params)
234 struct snd_soc_pcm_runtime *rtd = substream->private_data;
235 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
236 struct snd_soc_dai *codec_dai = rtd->codec_dai;
237 int mclk, ret;
239 /* in bypass mode, the mclk has to be one of the frequencies below */
240 switch (params_rate(params)) {
241 case 8000:
242 case 16000:
243 case 24000:
244 case 32000:
245 case 48000:
246 case 64000:
247 case 96000:
248 mclk = 12288000;
249 break;
250 case 11025:
251 case 22050:
252 case 44100:
253 case 88200:
254 mclk = 11289600;
255 break;
256 default:
257 return -EINVAL;
260 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
261 SND_SOC_CLOCK_OUT);
262 if (ret < 0) {
263 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
264 return ret;
267 return 0;
270 static int rockchip_sound_dmic_hw_params(struct snd_pcm_substream *substream,
271 struct snd_pcm_hw_params *params)
273 struct snd_soc_pcm_runtime *rtd = substream->private_data;
274 unsigned int mclk;
275 int ret;
277 mclk = params_rate(params) * SOUND_FS;
279 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
280 if (ret) {
281 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
282 __func__, mclk, ret);
283 return ret;
286 /* Wait for DMIC stable */
287 msleep(dmic_wakeup_delay);
289 return 0;
292 static const struct snd_soc_ops rockchip_sound_max98357a_ops = {
293 .hw_params = rockchip_sound_max98357a_hw_params,
296 static const struct snd_soc_ops rockchip_sound_rt5514_ops = {
297 .hw_params = rockchip_sound_rt5514_hw_params,
300 static const struct snd_soc_ops rockchip_sound_da7219_ops = {
301 .hw_params = rockchip_sound_da7219_hw_params,
304 static const struct snd_soc_ops rockchip_sound_cdndp_ops = {
305 .hw_params = rockchip_sound_cdndp_hw_params,
308 static const struct snd_soc_ops rockchip_sound_dmic_ops = {
309 .hw_params = rockchip_sound_dmic_hw_params,
312 static struct snd_soc_card rockchip_sound_card = {
313 .name = "rk3399-gru-sound",
314 .owner = THIS_MODULE,
315 .dapm_widgets = rockchip_dapm_widgets,
316 .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
317 .dapm_routes = rockchip_dapm_routes,
318 .num_dapm_routes = ARRAY_SIZE(rockchip_dapm_routes),
319 .controls = rockchip_controls,
320 .num_controls = ARRAY_SIZE(rockchip_controls),
323 enum {
324 DAILINK_CDNDP,
325 DAILINK_DA7219,
326 DAILINK_DMIC,
327 DAILINK_MAX98357A,
328 DAILINK_RT5514,
329 DAILINK_RT5514_DSP,
332 static const char * const dailink_compat[] = {
333 [DAILINK_CDNDP] = "rockchip,rk3399-cdn-dp",
334 [DAILINK_DA7219] = "dlg,da7219",
335 [DAILINK_DMIC] = "dmic-codec",
336 [DAILINK_MAX98357A] = "maxim,max98357a",
337 [DAILINK_RT5514] = "realtek,rt5514-i2c",
338 [DAILINK_RT5514_DSP] = "realtek,rt5514-spi",
341 static const struct snd_soc_dai_link rockchip_dais[] = {
342 [DAILINK_CDNDP] = {
343 .name = "DP",
344 .stream_name = "DP PCM",
345 .codec_dai_name = "i2s-hifi",
346 .ops = &rockchip_sound_cdndp_ops,
347 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
348 SND_SOC_DAIFMT_CBS_CFS,
350 [DAILINK_DA7219] = {
351 .name = "DA7219",
352 .stream_name = "DA7219 PCM",
353 .codec_dai_name = "da7219-hifi",
354 .init = rockchip_sound_da7219_init,
355 .ops = &rockchip_sound_da7219_ops,
356 /* set da7219 as slave */
357 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
358 SND_SOC_DAIFMT_CBS_CFS,
360 [DAILINK_DMIC] = {
361 .name = "DMIC",
362 .stream_name = "DMIC PCM",
363 .codec_dai_name = "dmic-hifi",
364 .ops = &rockchip_sound_dmic_ops,
365 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
366 SND_SOC_DAIFMT_CBS_CFS,
368 [DAILINK_MAX98357A] = {
369 .name = "MAX98357A",
370 .stream_name = "MAX98357A PCM",
371 .codec_dai_name = "HiFi",
372 .ops = &rockchip_sound_max98357a_ops,
373 /* set max98357a as slave */
374 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
375 SND_SOC_DAIFMT_CBS_CFS,
377 [DAILINK_RT5514] = {
378 .name = "RT5514",
379 .stream_name = "RT5514 PCM",
380 .codec_dai_name = "rt5514-aif1",
381 .ops = &rockchip_sound_rt5514_ops,
382 /* set rt5514 as slave */
383 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
384 SND_SOC_DAIFMT_CBS_CFS,
386 /* RT5514 DSP for voice wakeup via spi bus */
387 [DAILINK_RT5514_DSP] = {
388 .name = "RT5514 DSP",
389 .stream_name = "Wake on Voice",
390 .codec_name = "snd-soc-dummy",
391 .codec_dai_name = "snd-soc-dummy-dai",
395 static int rockchip_sound_codec_node_match(struct device_node *np_codec)
397 int i;
399 for (i = 0; i < ARRAY_SIZE(dailink_compat); i++) {
400 if (of_device_is_compatible(np_codec, dailink_compat[i]))
401 return i;
403 return -1;
406 static int rockchip_sound_of_parse_dais(struct device *dev,
407 struct snd_soc_card *card)
409 struct device_node *np_cpu, *np_cpu0, *np_cpu1;
410 struct device_node *np_codec;
411 struct snd_soc_dai_link *dai;
412 int i, index;
414 card->dai_link = devm_kzalloc(dev, sizeof(rockchip_dais),
415 GFP_KERNEL);
416 if (!card->dai_link)
417 return -ENOMEM;
419 np_cpu0 = of_parse_phandle(dev->of_node, "rockchip,cpu", 0);
420 np_cpu1 = of_parse_phandle(dev->of_node, "rockchip,cpu", 1);
422 card->num_links = 0;
423 for (i = 0; i < ARRAY_SIZE(rockchip_dais); i++) {
424 np_codec = of_parse_phandle(dev->of_node,
425 "rockchip,codec", i);
426 if (!np_codec)
427 break;
429 if (!of_device_is_available(np_codec))
430 continue;
432 index = rockchip_sound_codec_node_match(np_codec);
433 if (index < 0)
434 continue;
436 switch (index) {
437 case DAILINK_CDNDP:
438 np_cpu = np_cpu1;
439 break;
440 case DAILINK_RT5514_DSP:
441 np_cpu = np_codec;
442 break;
443 default:
444 np_cpu = np_cpu0;
445 break;
448 if (!np_cpu) {
449 dev_err(dev, "Missing 'rockchip,cpu' for %s\n",
450 rockchip_dais[index].name);
451 return -EINVAL;
454 dai = &card->dai_link[card->num_links++];
455 *dai = rockchip_dais[index];
457 if (!dai->codec_name)
458 dai->codec_of_node = np_codec;
459 dai->platform_of_node = np_cpu;
460 dai->cpu_of_node = np_cpu;
463 return 0;
466 static int rockchip_sound_probe(struct platform_device *pdev)
468 struct snd_soc_card *card = &rockchip_sound_card;
469 int ret;
471 ret = rockchip_sound_of_parse_dais(&pdev->dev, card);
472 if (ret < 0) {
473 dev_err(&pdev->dev, "Failed to parse dais: %d\n", ret);
474 return ret;
477 /* Set DMIC wakeup delay */
478 ret = device_property_read_u32(&pdev->dev, "dmic-wakeup-delay-ms",
479 &dmic_wakeup_delay);
480 if (ret) {
481 dmic_wakeup_delay = 0;
482 dev_dbg(&pdev->dev,
483 "no optional property 'dmic-wakeup-delay-ms' found, default: no delay\n");
486 card->dev = &pdev->dev;
487 return devm_snd_soc_register_card(&pdev->dev, card);
490 static const struct of_device_id rockchip_sound_of_match[] = {
491 { .compatible = "rockchip,rk3399-gru-sound", },
495 static struct platform_driver rockchip_sound_driver = {
496 .probe = rockchip_sound_probe,
497 .driver = {
498 .name = DRV_NAME,
499 .of_match_table = rockchip_sound_of_match,
500 #ifdef CONFIG_PM
501 .pm = &snd_soc_pm_ops,
502 #endif
506 module_platform_driver(rockchip_sound_driver);
508 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
509 MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
510 MODULE_LICENSE("GPL v2");
511 MODULE_ALIAS("platform:" DRV_NAME);
512 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);