rk3399_gru_sound.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  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. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/gpio.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/delay.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/i2c.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"
  36. #define DRV_NAME "rk3399-gru-sound"
  37. #define SOUND_FS 256
  38. static unsigned int dmic_wakeup_delay;
  39. static struct snd_soc_jack rockchip_sound_jack;
  40. static const struct snd_soc_dapm_widget rockchip_dapm_widgets[] = {
  41. SND_SOC_DAPM_HP("Headphones", NULL),
  42. SND_SOC_DAPM_SPK("Speakers", NULL),
  43. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  44. SND_SOC_DAPM_MIC("Int Mic", NULL),
  45. SND_SOC_DAPM_LINE("HDMI", NULL),
  46. };
  47. static const struct snd_kcontrol_new rockchip_controls[] = {
  48. SOC_DAPM_PIN_SWITCH("Headphones"),
  49. SOC_DAPM_PIN_SWITCH("Speakers"),
  50. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  51. SOC_DAPM_PIN_SWITCH("Int Mic"),
  52. SOC_DAPM_PIN_SWITCH("HDMI"),
  53. };
  54. static int rockchip_sound_max98357a_hw_params(struct snd_pcm_substream *substream,
  55. struct snd_pcm_hw_params *params)
  56. {
  57. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  58. unsigned int mclk;
  59. int ret;
  60. /* max98357a supports these sample rates */
  61. switch (params_rate(params)) {
  62. case 8000:
  63. case 16000:
  64. case 48000:
  65. case 96000:
  66. mclk = params_rate(params) * SOUND_FS;
  67. break;
  68. default:
  69. dev_err(rtd->card->dev, "%s() doesn't support this sample rate: %d\n",
  70. __func__, params_rate(params));
  71. return -EINVAL;
  72. }
  73. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
  74. if (ret) {
  75. dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
  76. __func__, mclk, ret);
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. static int rockchip_sound_rt5514_hw_params(struct snd_pcm_substream *substream,
  82. struct snd_pcm_hw_params *params)
  83. {
  84. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  85. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  86. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  87. unsigned int mclk;
  88. int ret;
  89. mclk = params_rate(params) * SOUND_FS;
  90. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
  91. SND_SOC_CLOCK_OUT);
  92. if (ret < 0) {
  93. dev_err(rtd->card->dev, "Can't set cpu clock out %d\n", ret);
  94. return ret;
  95. }
  96. ret = snd_soc_dai_set_sysclk(codec_dai, RT5514_SCLK_S_MCLK,
  97. mclk, SND_SOC_CLOCK_IN);
  98. if (ret) {
  99. dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
  100. __func__, params_rate(params) * 512, ret);
  101. return ret;
  102. }
  103. /* Wait for DMIC stable */
  104. msleep(dmic_wakeup_delay);
  105. return 0;
  106. }
  107. static int rockchip_sound_da7219_hw_params(struct snd_pcm_substream *substream,
  108. struct snd_pcm_hw_params *params)
  109. {
  110. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  111. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  112. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  113. int mclk, ret;
  114. /* in bypass mode, the mclk has to be one of the frequencies below */
  115. switch (params_rate(params)) {
  116. case 8000:
  117. case 16000:
  118. case 24000:
  119. case 32000:
  120. case 48000:
  121. case 64000:
  122. case 96000:
  123. mclk = 12288000;
  124. break;
  125. case 11025:
  126. case 22050:
  127. case 44100:
  128. case 88200:
  129. mclk = 11289600;
  130. break;
  131. default:
  132. return -EINVAL;
  133. }
  134. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
  135. SND_SOC_CLOCK_OUT);
  136. if (ret < 0) {
  137. dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
  138. return ret;
  139. }
  140. ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  141. SND_SOC_CLOCK_IN);
  142. if (ret < 0) {
  143. dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
  144. return ret;
  145. }
  146. ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
  147. if (ret < 0) {
  148. dev_err(codec_dai->dev, "Can't set pll sysclk mclk %d\n", ret);
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime *rtd)
  154. {
  155. struct snd_soc_component *component = rtd->codec_dais[0]->component;
  156. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  157. int ret;
  158. /* We need default MCLK and PLL settings for the accessory detection */
  159. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 12288000,
  160. SND_SOC_CLOCK_IN);
  161. if (ret < 0) {
  162. dev_err(codec_dai->dev, "Init can't set codec clock in %d\n", ret);
  163. return ret;
  164. }
  165. ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
  166. if (ret < 0) {
  167. dev_err(codec_dai->dev, "Init can't set pll sysclk mclk %d\n", ret);
  168. return ret;
  169. }
  170. /* Enable Headset and 4 Buttons Jack detection */
  171. ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
  172. SND_JACK_HEADSET | SND_JACK_LINEOUT |
  173. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  174. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  175. &rockchip_sound_jack, NULL, 0);
  176. if (ret) {
  177. dev_err(rtd->card->dev, "New Headset Jack failed! (%d)\n", ret);
  178. return ret;
  179. }
  180. snd_jack_set_key(
  181. rockchip_sound_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  182. snd_jack_set_key(
  183. rockchip_sound_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
  184. snd_jack_set_key(
  185. rockchip_sound_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
  186. snd_jack_set_key(
  187. rockchip_sound_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
  188. da7219_aad_jack_det(component, &rockchip_sound_jack);
  189. return 0;
  190. }
  191. static int rockchip_sound_dmic_hw_params(struct snd_pcm_substream *substream,
  192. struct snd_pcm_hw_params *params)
  193. {
  194. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  195. unsigned int mclk;
  196. int ret;
  197. mclk = params_rate(params) * SOUND_FS;
  198. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
  199. if (ret) {
  200. dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
  201. __func__, mclk, ret);
  202. return ret;
  203. }
  204. /* Wait for DMIC stable */
  205. msleep(dmic_wakeup_delay);
  206. return 0;
  207. }
  208. static const struct snd_soc_ops rockchip_sound_max98357a_ops = {
  209. .hw_params = rockchip_sound_max98357a_hw_params,
  210. };
  211. static const struct snd_soc_ops rockchip_sound_rt5514_ops = {
  212. .hw_params = rockchip_sound_rt5514_hw_params,
  213. };
  214. static const struct snd_soc_ops rockchip_sound_da7219_ops = {
  215. .hw_params = rockchip_sound_da7219_hw_params,
  216. };
  217. static const struct snd_soc_ops rockchip_sound_dmic_ops = {
  218. .hw_params = rockchip_sound_dmic_hw_params,
  219. };
  220. static struct snd_soc_card rockchip_sound_card = {
  221. .name = "rk3399-gru-sound",
  222. .owner = THIS_MODULE,
  223. .dapm_widgets = rockchip_dapm_widgets,
  224. .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
  225. .controls = rockchip_controls,
  226. .num_controls = ARRAY_SIZE(rockchip_controls),
  227. };
  228. enum {
  229. DAILINK_CDNDP,
  230. DAILINK_DA7219,
  231. DAILINK_DMIC,
  232. DAILINK_MAX98357A,
  233. DAILINK_RT5514,
  234. DAILINK_RT5514_DSP,
  235. };
  236. static const struct snd_soc_dai_link rockchip_dais[] = {
  237. [DAILINK_CDNDP] = {
  238. .name = "DP",
  239. .stream_name = "DP PCM",
  240. .codec_dai_name = "spdif-hifi",
  241. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  242. SND_SOC_DAIFMT_CBS_CFS,
  243. },
  244. [DAILINK_DA7219] = {
  245. .name = "DA7219",
  246. .stream_name = "DA7219 PCM",
  247. .codec_dai_name = "da7219-hifi",
  248. .init = rockchip_sound_da7219_init,
  249. .ops = &rockchip_sound_da7219_ops,
  250. /* set da7219 as slave */
  251. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  252. SND_SOC_DAIFMT_CBS_CFS,
  253. },
  254. [DAILINK_DMIC] = {
  255. .name = "DMIC",
  256. .stream_name = "DMIC PCM",
  257. .codec_dai_name = "dmic-hifi",
  258. .ops = &rockchip_sound_dmic_ops,
  259. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  260. SND_SOC_DAIFMT_CBS_CFS,
  261. },
  262. [DAILINK_MAX98357A] = {
  263. .name = "MAX98357A",
  264. .stream_name = "MAX98357A PCM",
  265. .codec_dai_name = "HiFi",
  266. .ops = &rockchip_sound_max98357a_ops,
  267. /* set max98357a as slave */
  268. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  269. SND_SOC_DAIFMT_CBS_CFS,
  270. },
  271. [DAILINK_RT5514] = {
  272. .name = "RT5514",
  273. .stream_name = "RT5514 PCM",
  274. .codec_dai_name = "rt5514-aif1",
  275. .ops = &rockchip_sound_rt5514_ops,
  276. /* set rt5514 as slave */
  277. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  278. SND_SOC_DAIFMT_CBS_CFS,
  279. },
  280. /* RT5514 DSP for voice wakeup via spi bus */
  281. [DAILINK_RT5514_DSP] = {
  282. .name = "RT5514 DSP",
  283. .stream_name = "Wake on Voice",
  284. .codec_name = "snd-soc-dummy",
  285. .codec_dai_name = "snd-soc-dummy-dai",
  286. },
  287. };
  288. static const struct snd_soc_dapm_route rockchip_sound_cdndp_routes[] = {
  289. /* Output */
  290. {"HDMI", NULL, "TX"},
  291. };
  292. static const struct snd_soc_dapm_route rockchip_sound_da7219_routes[] = {
  293. /* Output */
  294. {"Headphones", NULL, "HPL"},
  295. {"Headphones", NULL, "HPR"},
  296. /* Input */
  297. {"MIC", NULL, "Headset Mic"},
  298. };
  299. static const struct snd_soc_dapm_route rockchip_sound_dmic_routes[] = {
  300. /* Input */
  301. {"DMic", NULL, "Int Mic"},
  302. };
  303. static const struct snd_soc_dapm_route rockchip_sound_max98357a_routes[] = {
  304. /* Output */
  305. {"Speakers", NULL, "Speaker"},
  306. };
  307. static const struct snd_soc_dapm_route rockchip_sound_rt5514_routes[] = {
  308. /* Input */
  309. {"DMIC1L", NULL, "Int Mic"},
  310. {"DMIC1R", NULL, "Int Mic"},
  311. };
  312. struct rockchip_sound_route {
  313. const struct snd_soc_dapm_route *routes;
  314. int num_routes;
  315. };
  316. static const struct rockchip_sound_route rockchip_routes[] = {
  317. [DAILINK_CDNDP] = {
  318. .routes = rockchip_sound_cdndp_routes,
  319. .num_routes = ARRAY_SIZE(rockchip_sound_cdndp_routes),
  320. },
  321. [DAILINK_DA7219] = {
  322. .routes = rockchip_sound_da7219_routes,
  323. .num_routes = ARRAY_SIZE(rockchip_sound_da7219_routes),
  324. },
  325. [DAILINK_DMIC] = {
  326. .routes = rockchip_sound_dmic_routes,
  327. .num_routes = ARRAY_SIZE(rockchip_sound_dmic_routes),
  328. },
  329. [DAILINK_MAX98357A] = {
  330. .routes = rockchip_sound_max98357a_routes,
  331. .num_routes = ARRAY_SIZE(rockchip_sound_max98357a_routes),
  332. },
  333. [DAILINK_RT5514] = {
  334. .routes = rockchip_sound_rt5514_routes,
  335. .num_routes = ARRAY_SIZE(rockchip_sound_rt5514_routes),
  336. },
  337. [DAILINK_RT5514_DSP] = {},
  338. };
  339. struct dailink_match_data {
  340. const char *compatible;
  341. struct bus_type *bus_type;
  342. };
  343. static const struct dailink_match_data dailink_match[] = {
  344. [DAILINK_CDNDP] = {
  345. .compatible = "rockchip,rk3399-cdn-dp",
  346. },
  347. [DAILINK_DA7219] = {
  348. .compatible = "dlg,da7219",
  349. },
  350. [DAILINK_DMIC] = {
  351. .compatible = "dmic-codec",
  352. },
  353. [DAILINK_MAX98357A] = {
  354. .compatible = "maxim,max98357a",
  355. },
  356. [DAILINK_RT5514] = {
  357. .compatible = "realtek,rt5514",
  358. .bus_type = &i2c_bus_type,
  359. },
  360. [DAILINK_RT5514_DSP] = {
  361. .compatible = "realtek,rt5514",
  362. .bus_type = &spi_bus_type,
  363. },
  364. };
  365. static int of_dev_node_match(struct device *dev, void *data)
  366. {
  367. return dev->of_node == data;
  368. }
  369. static int rockchip_sound_codec_node_match(struct device_node *np_codec)
  370. {
  371. struct device *dev;
  372. int i;
  373. for (i = 0; i < ARRAY_SIZE(dailink_match); i++) {
  374. if (!of_device_is_compatible(np_codec,
  375. dailink_match[i].compatible))
  376. continue;
  377. if (dailink_match[i].bus_type) {
  378. dev = bus_find_device(dailink_match[i].bus_type, NULL,
  379. np_codec, of_dev_node_match);
  380. if (!dev)
  381. continue;
  382. put_device(dev);
  383. }
  384. return i;
  385. }
  386. return -1;
  387. }
  388. static int rockchip_sound_of_parse_dais(struct device *dev,
  389. struct snd_soc_card *card)
  390. {
  391. struct device_node *np_cpu, *np_cpu0, *np_cpu1;
  392. struct device_node *np_codec;
  393. struct snd_soc_dai_link *dai;
  394. struct snd_soc_dapm_route *routes;
  395. int i, index;
  396. int num_routes;
  397. card->dai_link = devm_kzalloc(dev, sizeof(rockchip_dais),
  398. GFP_KERNEL);
  399. if (!card->dai_link)
  400. return -ENOMEM;
  401. num_routes = 0;
  402. for (i = 0; i < ARRAY_SIZE(rockchip_routes); i++)
  403. num_routes += rockchip_routes[i].num_routes;
  404. routes = devm_kcalloc(dev, num_routes, sizeof(*routes),
  405. GFP_KERNEL);
  406. if (!routes)
  407. return -ENOMEM;
  408. card->dapm_routes = routes;
  409. np_cpu0 = of_parse_phandle(dev->of_node, "rockchip,cpu", 0);
  410. np_cpu1 = of_parse_phandle(dev->of_node, "rockchip,cpu", 1);
  411. card->num_dapm_routes = 0;
  412. card->num_links = 0;
  413. for (i = 0; i < ARRAY_SIZE(rockchip_dais); i++) {
  414. np_codec = of_parse_phandle(dev->of_node,
  415. "rockchip,codec", i);
  416. if (!np_codec)
  417. break;
  418. if (!of_device_is_available(np_codec))
  419. continue;
  420. index = rockchip_sound_codec_node_match(np_codec);
  421. if (index < 0)
  422. continue;
  423. switch (index) {
  424. case DAILINK_CDNDP:
  425. np_cpu = np_cpu1;
  426. break;
  427. case DAILINK_RT5514_DSP:
  428. np_cpu = np_codec;
  429. break;
  430. default:
  431. np_cpu = np_cpu0;
  432. break;
  433. }
  434. if (!np_cpu) {
  435. dev_err(dev, "Missing 'rockchip,cpu' for %s\n",
  436. rockchip_dais[index].name);
  437. return -EINVAL;
  438. }
  439. dai = &card->dai_link[card->num_links++];
  440. *dai = rockchip_dais[index];
  441. if (!dai->codec_name)
  442. dai->codec_of_node = np_codec;
  443. dai->platform_of_node = np_cpu;
  444. dai->cpu_of_node = np_cpu;
  445. if (card->num_dapm_routes + rockchip_routes[index].num_routes >
  446. num_routes) {
  447. dev_err(dev, "Too many routes\n");
  448. return -EINVAL;
  449. }
  450. memcpy(routes + card->num_dapm_routes,
  451. rockchip_routes[index].routes,
  452. rockchip_routes[index].num_routes * sizeof(*routes));
  453. card->num_dapm_routes += rockchip_routes[index].num_routes;
  454. }
  455. return 0;
  456. }
  457. static int rockchip_sound_probe(struct platform_device *pdev)
  458. {
  459. struct snd_soc_card *card = &rockchip_sound_card;
  460. int ret;
  461. ret = rockchip_sound_of_parse_dais(&pdev->dev, card);
  462. if (ret < 0) {
  463. dev_err(&pdev->dev, "Failed to parse dais: %d\n", ret);
  464. return ret;
  465. }
  466. /* Set DMIC wakeup delay */
  467. ret = device_property_read_u32(&pdev->dev, "dmic-wakeup-delay-ms",
  468. &dmic_wakeup_delay);
  469. if (ret) {
  470. dmic_wakeup_delay = 0;
  471. dev_dbg(&pdev->dev,
  472. "no optional property 'dmic-wakeup-delay-ms' found, default: no delay\n");
  473. }
  474. card->dev = &pdev->dev;
  475. return devm_snd_soc_register_card(&pdev->dev, card);
  476. }
  477. static const struct of_device_id rockchip_sound_of_match[] = {
  478. { .compatible = "rockchip,rk3399-gru-sound", },
  479. {},
  480. };
  481. static struct platform_driver rockchip_sound_driver = {
  482. .probe = rockchip_sound_probe,
  483. .driver = {
  484. .name = DRV_NAME,
  485. .of_match_table = rockchip_sound_of_match,
  486. #ifdef CONFIG_PM
  487. .pm = &snd_soc_pm_ops,
  488. #endif
  489. },
  490. };
  491. module_platform_driver(rockchip_sound_driver);
  492. MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
  493. MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
  494. MODULE_LICENSE("GPL v2");
  495. MODULE_ALIAS("platform:" DRV_NAME);
  496. MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);