blob: fa6cd1de828be95991dee4eaea9dec40f69d9fef [file] [log] [blame]
Xing Zhengc6eac8a2016-08-03 16:10:00 +08001/*
2 * Rockchip machine ASoC driver for boards using MAX98357A/RT5514/DA7219
3 *
4 * Copyright (c) 2016, ROCKCHIP CORPORATION. All rights reserved.
5 *
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.
9 *
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.
14 *
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/>.
17 */
18
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>
Jeffy Chen7e0dc9a2017-09-19 20:57:59 +080026#include <linux/i2c.h>
Cheng-Yi Chiang22b93ea2016-09-16 07:10:06 +080027#include <linux/input.h>
Xing Zhengc6eac8a2016-08-03 16:10:00 +080028#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"
37
38#define DRV_NAME "rk3399-gru-sound"
39
40#define SOUND_FS 256
41
Jeffy Chenf628c4e2017-08-22 23:35:48 +080042static unsigned int dmic_wakeup_delay;
Wonjoon Lee3a6f9dc2016-09-22 21:50:06 +080043
Xing Zhengc6eac8a2016-08-03 16:10:00 +080044static struct snd_soc_jack rockchip_sound_jack;
45
46static 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),
Jeffy Chene7251482017-09-18 19:17:59 +080051 SND_SOC_DAPM_LINE("HDMI", NULL),
Xing Zhengc6eac8a2016-08-03 16:10:00 +080052};
53
Xing Zhengc6eac8a2016-08-03 16:10:00 +080054static 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"),
Jeffy Chene7251482017-09-18 19:17:59 +080059 SOC_DAPM_PIN_SWITCH("HDMI"),
Xing Zhengc6eac8a2016-08-03 16:10:00 +080060};
61
62static int rockchip_sound_max98357a_hw_params(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params *params)
64{
65 struct snd_soc_pcm_runtime *rtd = substream->private_data;
66 unsigned int mclk;
67 int ret;
68
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;
81 }
82
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;
88 }
89
90 return 0;
91}
92
93static int rockchip_sound_rt5514_hw_params(struct snd_pcm_substream *substream,
94 struct snd_pcm_hw_params *params)
95{
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;
101
102 mclk = params_rate(params) * SOUND_FS;
103
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;
109 }
110
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;
117 }
118
Wonjoon Lee3a6f9dc2016-09-22 21:50:06 +0800119 /* Wait for DMIC stable */
Jeffy Chenf628c4e2017-08-22 23:35:48 +0800120 msleep(dmic_wakeup_delay);
Wonjoon Lee3a6f9dc2016-09-22 21:50:06 +0800121
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800122 return 0;
123}
124
125static int rockchip_sound_da7219_hw_params(struct snd_pcm_substream *substream,
126 struct snd_pcm_hw_params *params)
127{
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;
132
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;
152 }
153
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;
159 }
160
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;
166 }
167
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;
172 }
173
174 return 0;
175}
176
177static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime *rtd)
178{
179 struct snd_soc_codec *codec = rtd->codec_dais[0]->codec;
180 struct snd_soc_dai *codec_dai = rtd->codec_dai;
181 int ret;
182
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;
189 }
190
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;
195 }
196
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);
203
204 if (ret) {
205 dev_err(rtd->card->dev, "New Headset Jack failed! (%d)\n", ret);
206 return ret;
207 }
208
Benson Leungaf1b1ce2017-11-22 12:56:42 -0800209 snd_jack_set_key(
210 rockchip_sound_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
Cheng-Yi Chiang22b93ea2016-09-16 07:10:06 +0800211 snd_jack_set_key(
212 rockchip_sound_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
213 snd_jack_set_key(
214 rockchip_sound_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
215 snd_jack_set_key(
216 rockchip_sound_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
217
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800218 da7219_aad_jack_det(codec, &rockchip_sound_jack);
219
220 return 0;
221}
222
Jeffy Chen3313faf2017-08-24 12:52:25 +0800223static int rockchip_sound_cdndp_hw_params(struct snd_pcm_substream *substream,
224 struct snd_pcm_hw_params *params)
225{
226 struct snd_soc_pcm_runtime *rtd = substream->private_data;
227 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
228 struct snd_soc_dai *codec_dai = rtd->codec_dai;
229 int mclk, ret;
230
231 /* in bypass mode, the mclk has to be one of the frequencies below */
232 switch (params_rate(params)) {
233 case 8000:
234 case 16000:
235 case 24000:
236 case 32000:
237 case 48000:
238 case 64000:
239 case 96000:
240 mclk = 12288000;
241 break;
242 case 11025:
243 case 22050:
244 case 44100:
245 case 88200:
246 mclk = 11289600;
247 break;
248 default:
249 return -EINVAL;
250 }
251
252 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
253 SND_SOC_CLOCK_OUT);
254 if (ret < 0) {
255 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
256 return ret;
257 }
258
259 return 0;
260}
261
Jeffy Chen626d84d2017-08-24 12:52:26 +0800262static int rockchip_sound_dmic_hw_params(struct snd_pcm_substream *substream,
263 struct snd_pcm_hw_params *params)
264{
265 struct snd_soc_pcm_runtime *rtd = substream->private_data;
266 unsigned int mclk;
267 int ret;
268
269 mclk = params_rate(params) * SOUND_FS;
270
271 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
272 if (ret) {
273 dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
274 __func__, mclk, ret);
275 return ret;
276 }
277
278 /* Wait for DMIC stable */
279 msleep(dmic_wakeup_delay);
280
281 return 0;
282}
283
Julia Lawall705e9992016-10-15 16:55:47 +0200284static const struct snd_soc_ops rockchip_sound_max98357a_ops = {
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800285 .hw_params = rockchip_sound_max98357a_hw_params,
286};
287
Julia Lawall705e9992016-10-15 16:55:47 +0200288static const struct snd_soc_ops rockchip_sound_rt5514_ops = {
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800289 .hw_params = rockchip_sound_rt5514_hw_params,
290};
291
Julia Lawall705e9992016-10-15 16:55:47 +0200292static const struct snd_soc_ops rockchip_sound_da7219_ops = {
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800293 .hw_params = rockchip_sound_da7219_hw_params,
294};
295
Jeffy Chen1fe165b2017-08-30 17:58:14 +0800296static const struct snd_soc_ops rockchip_sound_cdndp_ops = {
Jeffy Chen3313faf2017-08-24 12:52:25 +0800297 .hw_params = rockchip_sound_cdndp_hw_params,
298};
299
Jeffy Chen1fe165b2017-08-30 17:58:14 +0800300static const struct snd_soc_ops rockchip_sound_dmic_ops = {
Jeffy Chen626d84d2017-08-24 12:52:26 +0800301 .hw_params = rockchip_sound_dmic_hw_params,
302};
303
Jeffy Chen0d529542017-08-24 12:52:24 +0800304static struct snd_soc_card rockchip_sound_card = {
305 .name = "rk3399-gru-sound",
306 .owner = THIS_MODULE,
307 .dapm_widgets = rockchip_dapm_widgets,
308 .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
Jeffy Chen0d529542017-08-24 12:52:24 +0800309 .controls = rockchip_controls,
310 .num_controls = ARRAY_SIZE(rockchip_controls),
311};
312
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800313enum {
Jeffy Chen3313faf2017-08-24 12:52:25 +0800314 DAILINK_CDNDP,
Jeffy Chen0d529542017-08-24 12:52:24 +0800315 DAILINK_DA7219,
Jeffy Chen626d84d2017-08-24 12:52:26 +0800316 DAILINK_DMIC,
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800317 DAILINK_MAX98357A,
318 DAILINK_RT5514,
Xing Zhenge5abe952016-08-19 21:56:12 +0800319 DAILINK_RT5514_DSP,
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800320};
321
Jeffy Chen0d529542017-08-24 12:52:24 +0800322static const struct snd_soc_dai_link rockchip_dais[] = {
Jeffy Chen3313faf2017-08-24 12:52:25 +0800323 [DAILINK_CDNDP] = {
324 .name = "DP",
325 .stream_name = "DP PCM",
326 .codec_dai_name = "i2s-hifi",
327 .ops = &rockchip_sound_cdndp_ops,
328 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
329 SND_SOC_DAIFMT_CBS_CFS,
330 },
Jeffy Chen0d529542017-08-24 12:52:24 +0800331 [DAILINK_DA7219] = {
332 .name = "DA7219",
333 .stream_name = "DA7219 PCM",
334 .codec_dai_name = "da7219-hifi",
335 .init = rockchip_sound_da7219_init,
336 .ops = &rockchip_sound_da7219_ops,
337 /* set da7219 as slave */
338 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
339 SND_SOC_DAIFMT_CBS_CFS,
340 },
Jeffy Chen626d84d2017-08-24 12:52:26 +0800341 [DAILINK_DMIC] = {
342 .name = "DMIC",
343 .stream_name = "DMIC PCM",
344 .codec_dai_name = "dmic-hifi",
345 .ops = &rockchip_sound_dmic_ops,
346 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
347 SND_SOC_DAIFMT_CBS_CFS,
348 },
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800349 [DAILINK_MAX98357A] = {
350 .name = "MAX98357A",
351 .stream_name = "MAX98357A PCM",
352 .codec_dai_name = "HiFi",
353 .ops = &rockchip_sound_max98357a_ops,
354 /* set max98357a as slave */
355 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
356 SND_SOC_DAIFMT_CBS_CFS,
357 },
358 [DAILINK_RT5514] = {
359 .name = "RT5514",
360 .stream_name = "RT5514 PCM",
361 .codec_dai_name = "rt5514-aif1",
362 .ops = &rockchip_sound_rt5514_ops,
363 /* set rt5514 as slave */
364 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
365 SND_SOC_DAIFMT_CBS_CFS,
366 },
Xing Zhenge5abe952016-08-19 21:56:12 +0800367 /* RT5514 DSP for voice wakeup via spi bus */
368 [DAILINK_RT5514_DSP] = {
369 .name = "RT5514 DSP",
370 .stream_name = "Wake on Voice",
Jeffy Chen72cfb0f2017-08-24 12:52:21 +0800371 .codec_dai_name = "rt5514-dsp-cpu-dai",
Xing Zhenge5abe952016-08-19 21:56:12 +0800372 },
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800373};
374
Jeffy Chend9f9c162017-09-19 20:57:58 +0800375static const struct snd_soc_dapm_route rockchip_sound_cdndp_routes[] = {
376 /* Output */
377 {"HDMI", NULL, "TX"},
378};
379
380static const struct snd_soc_dapm_route rockchip_sound_da7219_routes[] = {
381 /* Output */
382 {"Headphones", NULL, "HPL"},
383 {"Headphones", NULL, "HPR"},
384
385 /* Input */
386 {"MIC", NULL, "Headset Mic"},
387};
388
389static const struct snd_soc_dapm_route rockchip_sound_dmic_routes[] = {
390 /* Input */
391 {"DMic", NULL, "Int Mic"},
392};
393
394static const struct snd_soc_dapm_route rockchip_sound_max98357a_routes[] = {
395 /* Output */
396 {"Speakers", NULL, "Speaker"},
397};
398
399static const struct snd_soc_dapm_route rockchip_sound_rt5514_routes[] = {
400 /* Input */
401 {"DMIC1L", NULL, "Int Mic"},
402 {"DMIC1R", NULL, "Int Mic"},
403};
404
405struct rockchip_sound_route {
406 const struct snd_soc_dapm_route *routes;
407 int num_routes;
408};
409
410static const struct rockchip_sound_route rockchip_routes[] = {
411 [DAILINK_CDNDP] = {
412 .routes = rockchip_sound_cdndp_routes,
413 .num_routes = ARRAY_SIZE(rockchip_sound_cdndp_routes),
414 },
415 [DAILINK_DA7219] = {
416 .routes = rockchip_sound_da7219_routes,
417 .num_routes = ARRAY_SIZE(rockchip_sound_da7219_routes),
418 },
419 [DAILINK_DMIC] = {
420 .routes = rockchip_sound_dmic_routes,
421 .num_routes = ARRAY_SIZE(rockchip_sound_dmic_routes),
422 },
423 [DAILINK_MAX98357A] = {
424 .routes = rockchip_sound_max98357a_routes,
425 .num_routes = ARRAY_SIZE(rockchip_sound_max98357a_routes),
426 },
427 [DAILINK_RT5514] = {
428 .routes = rockchip_sound_rt5514_routes,
429 .num_routes = ARRAY_SIZE(rockchip_sound_rt5514_routes),
430 },
431 [DAILINK_RT5514_DSP] = {},
432};
433
Jeffy Chen7e0dc9a2017-09-19 20:57:59 +0800434struct dailink_match_data {
435 const char *compatible;
436 struct bus_type *bus_type;
437};
438
439static const struct dailink_match_data dailink_match[] = {
440 [DAILINK_CDNDP] = {
441 .compatible = "rockchip,rk3399-cdn-dp",
442 },
443 [DAILINK_DA7219] = {
444 .compatible = "dlg,da7219",
445 },
446 [DAILINK_DMIC] = {
447 .compatible = "dmic-codec",
448 },
449 [DAILINK_MAX98357A] = {
450 .compatible = "maxim,max98357a",
451 },
452 [DAILINK_RT5514] = {
453 .compatible = "realtek,rt5514",
454 .bus_type = &i2c_bus_type,
455 },
456 [DAILINK_RT5514_DSP] = {
457 .compatible = "realtek,rt5514",
458 .bus_type = &spi_bus_type,
459 },
460};
461
462static int of_dev_node_match(struct device *dev, void *data)
463{
464 return dev->of_node == data;
465}
466
Jeffy Chen0d529542017-08-24 12:52:24 +0800467static int rockchip_sound_codec_node_match(struct device_node *np_codec)
Xing Zhenge5abe952016-08-19 21:56:12 +0800468{
Jeffy Chen7e0dc9a2017-09-19 20:57:59 +0800469 struct device *dev;
Jeffy Chen0d529542017-08-24 12:52:24 +0800470 int i;
471
Jeffy Chen7e0dc9a2017-09-19 20:57:59 +0800472 for (i = 0; i < ARRAY_SIZE(dailink_match); i++) {
473 if (!of_device_is_compatible(np_codec,
474 dailink_match[i].compatible))
475 continue;
476
477 if (dailink_match[i].bus_type) {
478 dev = bus_find_device(dailink_match[i].bus_type, NULL,
479 np_codec, of_dev_node_match);
480 if (!dev)
481 continue;
482 put_device(dev);
483 }
484
485 return i;
Jeffy Chen0d529542017-08-24 12:52:24 +0800486 }
487 return -1;
488}
489
490static int rockchip_sound_of_parse_dais(struct device *dev,
491 struct snd_soc_card *card)
492{
Jeffy Chen3313faf2017-08-24 12:52:25 +0800493 struct device_node *np_cpu, *np_cpu0, *np_cpu1;
Jeffy Chen0d529542017-08-24 12:52:24 +0800494 struct device_node *np_codec;
495 struct snd_soc_dai_link *dai;
Jeffy Chend9f9c162017-09-19 20:57:58 +0800496 struct snd_soc_dapm_route *routes;
Jeffy Chen0d529542017-08-24 12:52:24 +0800497 int i, index;
Douglas Anderson8eae6c22017-09-29 15:03:24 -0700498 int num_routes;
Jeffy Chen0d529542017-08-24 12:52:24 +0800499
500 card->dai_link = devm_kzalloc(dev, sizeof(rockchip_dais),
501 GFP_KERNEL);
502 if (!card->dai_link)
503 return -ENOMEM;
504
Douglas Anderson8eae6c22017-09-29 15:03:24 -0700505 num_routes = 0;
506 for (i = 0; i < ARRAY_SIZE(rockchip_routes); i++)
507 num_routes += rockchip_routes[i].num_routes;
508 routes = devm_kzalloc(dev, num_routes * sizeof(*routes),
Jeffy Chend9f9c162017-09-19 20:57:58 +0800509 GFP_KERNEL);
510 if (!routes)
511 return -ENOMEM;
512 card->dapm_routes = routes;
513
Jeffy Chen3313faf2017-08-24 12:52:25 +0800514 np_cpu0 = of_parse_phandle(dev->of_node, "rockchip,cpu", 0);
515 np_cpu1 = of_parse_phandle(dev->of_node, "rockchip,cpu", 1);
Jeffy Chen0d529542017-08-24 12:52:24 +0800516
Jeffy Chend9f9c162017-09-19 20:57:58 +0800517 card->num_dapm_routes = 0;
Jeffy Chen0d529542017-08-24 12:52:24 +0800518 card->num_links = 0;
519 for (i = 0; i < ARRAY_SIZE(rockchip_dais); i++) {
520 np_codec = of_parse_phandle(dev->of_node,
521 "rockchip,codec", i);
522 if (!np_codec)
523 break;
524
525 if (!of_device_is_available(np_codec))
526 continue;
527
528 index = rockchip_sound_codec_node_match(np_codec);
529 if (index < 0)
530 continue;
531
Jeffy Chen3313faf2017-08-24 12:52:25 +0800532 np_cpu = (index == DAILINK_CDNDP) ? np_cpu1 : np_cpu0;
Jeffy Chen0d529542017-08-24 12:52:24 +0800533 if (!np_cpu) {
534 dev_err(dev, "Missing 'rockchip,cpu' for %s\n",
535 rockchip_dais[index].name);
536 return -EINVAL;
537 }
538
539 dai = &card->dai_link[card->num_links++];
540 *dai = rockchip_dais[index];
541
542 dai->codec_of_node = np_codec;
543 dai->platform_of_node = np_cpu;
544 dai->cpu_of_node = np_cpu;
Jeffy Chend9f9c162017-09-19 20:57:58 +0800545
Douglas Anderson8eae6c22017-09-29 15:03:24 -0700546 if (card->num_dapm_routes + rockchip_routes[index].num_routes >
547 num_routes) {
548 dev_err(dev, "Too many routes\n");
549 return -EINVAL;
550 }
551
Jeffy Chend9f9c162017-09-19 20:57:58 +0800552 memcpy(routes + card->num_dapm_routes,
553 rockchip_routes[index].routes,
554 rockchip_routes[index].num_routes * sizeof(*routes));
555 card->num_dapm_routes += rockchip_routes[index].num_routes;
Jeffy Chen0d529542017-08-24 12:52:24 +0800556 }
557
558 return 0;
Xing Zhenge5abe952016-08-19 21:56:12 +0800559}
560
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800561static int rockchip_sound_probe(struct platform_device *pdev)
562{
563 struct snd_soc_card *card = &rockchip_sound_card;
Jeffy Chen0d529542017-08-24 12:52:24 +0800564 int ret;
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800565
Jeffy Chen0d529542017-08-24 12:52:24 +0800566 ret = rockchip_sound_of_parse_dais(&pdev->dev, card);
567 if (ret < 0) {
568 dev_err(&pdev->dev, "Failed to parse dais: %d\n", ret);
569 return ret;
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800570 }
571
Jeffy Chenf628c4e2017-08-22 23:35:48 +0800572 /* Set DMIC wakeup delay */
573 ret = device_property_read_u32(&pdev->dev, "dmic-wakeup-delay-ms",
574 &dmic_wakeup_delay);
Wonjoon Lee3a6f9dc2016-09-22 21:50:06 +0800575 if (ret) {
Jeffy Chenf628c4e2017-08-22 23:35:48 +0800576 dmic_wakeup_delay = 0;
Wonjoon Lee3a6f9dc2016-09-22 21:50:06 +0800577 dev_dbg(&pdev->dev,
Jeffy Chenf628c4e2017-08-22 23:35:48 +0800578 "no optional property 'dmic-wakeup-delay-ms' found, default: no delay\n");
Wonjoon Lee3a6f9dc2016-09-22 21:50:06 +0800579 }
580
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800581 card->dev = &pdev->dev;
Jeffy Chen0d529542017-08-24 12:52:24 +0800582 return devm_snd_soc_register_card(&pdev->dev, card);
Xing Zhengc6eac8a2016-08-03 16:10:00 +0800583}
584
585static const struct of_device_id rockchip_sound_of_match[] = {
586 { .compatible = "rockchip,rk3399-gru-sound", },
587 {},
588};
589
590static struct platform_driver rockchip_sound_driver = {
591 .probe = rockchip_sound_probe,
592 .driver = {
593 .name = DRV_NAME,
594 .of_match_table = rockchip_sound_of_match,
595#ifdef CONFIG_PM
596 .pm = &snd_soc_pm_ops,
597#endif
598 },
599};
600
601module_platform_driver(rockchip_sound_driver);
602
603MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
604MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
605MODULE_LICENSE("GPL v2");
606MODULE_ALIAS("platform:" DRV_NAME);
607MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);