rk3399_gru_sound.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/input.h>
  26. #include <sound/core.h>
  27. #include <sound/jack.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/soc.h>
  31. #include "rockchip_i2s.h"
  32. #include "../codecs/da7219.h"
  33. #include "../codecs/da7219-aad.h"
  34. #include "../codecs/rt5514.h"
  35. #define DRV_NAME "rk3399-gru-sound"
  36. #define SOUND_FS 256
  37. unsigned int rt5514_dmic_delay;
  38. static struct snd_soc_jack rockchip_sound_jack;
  39. static const struct snd_soc_dapm_widget rockchip_dapm_widgets[] = {
  40. SND_SOC_DAPM_HP("Headphones", NULL),
  41. SND_SOC_DAPM_SPK("Speakers", NULL),
  42. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  43. SND_SOC_DAPM_MIC("Int Mic", NULL),
  44. };
  45. static const struct snd_soc_dapm_route rockchip_dapm_routes[] = {
  46. /* Input Lines */
  47. {"MIC", NULL, "Headset Mic"},
  48. {"DMIC1L", NULL, "Int Mic"},
  49. {"DMIC1R", NULL, "Int Mic"},
  50. /* Output Lines */
  51. {"Headphones", NULL, "HPL"},
  52. {"Headphones", NULL, "HPR"},
  53. {"Speakers", NULL, "Speaker"},
  54. };
  55. static const struct snd_kcontrol_new rockchip_controls[] = {
  56. SOC_DAPM_PIN_SWITCH("Headphones"),
  57. SOC_DAPM_PIN_SWITCH("Speakers"),
  58. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  59. SOC_DAPM_PIN_SWITCH("Int Mic"),
  60. };
  61. static int rockchip_sound_max98357a_hw_params(struct snd_pcm_substream *substream,
  62. struct snd_pcm_hw_params *params)
  63. {
  64. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  65. unsigned int mclk;
  66. int ret;
  67. /* max98357a supports these sample rates */
  68. switch (params_rate(params)) {
  69. case 8000:
  70. case 16000:
  71. case 48000:
  72. case 96000:
  73. mclk = params_rate(params) * SOUND_FS;
  74. break;
  75. default:
  76. dev_err(rtd->card->dev, "%s() doesn't support this sample rate: %d\n",
  77. __func__, params_rate(params));
  78. return -EINVAL;
  79. }
  80. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk, 0);
  81. if (ret) {
  82. dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
  83. __func__, mclk, ret);
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. static int rockchip_sound_rt5514_hw_params(struct snd_pcm_substream *substream,
  89. struct snd_pcm_hw_params *params)
  90. {
  91. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  92. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  93. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  94. unsigned int mclk;
  95. int ret;
  96. mclk = params_rate(params) * SOUND_FS;
  97. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
  98. SND_SOC_CLOCK_OUT);
  99. if (ret < 0) {
  100. dev_err(rtd->card->dev, "Can't set cpu clock out %d\n", ret);
  101. return ret;
  102. }
  103. ret = snd_soc_dai_set_sysclk(codec_dai, RT5514_SCLK_S_MCLK,
  104. mclk, SND_SOC_CLOCK_IN);
  105. if (ret) {
  106. dev_err(rtd->card->dev, "%s() error setting sysclk to %u: %d\n",
  107. __func__, params_rate(params) * 512, ret);
  108. return ret;
  109. }
  110. /* Wait for DMIC stable */
  111. msleep(rt5514_dmic_delay);
  112. return 0;
  113. }
  114. static int rockchip_sound_da7219_hw_params(struct snd_pcm_substream *substream,
  115. struct snd_pcm_hw_params *params)
  116. {
  117. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  118. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  119. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  120. int mclk, ret;
  121. /* in bypass mode, the mclk has to be one of the frequencies below */
  122. switch (params_rate(params)) {
  123. case 8000:
  124. case 16000:
  125. case 24000:
  126. case 32000:
  127. case 48000:
  128. case 64000:
  129. case 96000:
  130. mclk = 12288000;
  131. break;
  132. case 11025:
  133. case 22050:
  134. case 44100:
  135. case 88200:
  136. mclk = 11289600;
  137. break;
  138. default:
  139. return -EINVAL;
  140. }
  141. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
  142. SND_SOC_CLOCK_OUT);
  143. if (ret < 0) {
  144. dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
  145. return ret;
  146. }
  147. ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  148. SND_SOC_CLOCK_IN);
  149. if (ret < 0) {
  150. dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
  151. return ret;
  152. }
  153. ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
  154. if (ret < 0) {
  155. dev_err(codec_dai->dev, "Can't set pll sysclk mclk %d\n", ret);
  156. return ret;
  157. }
  158. return 0;
  159. }
  160. static int rockchip_sound_da7219_init(struct snd_soc_pcm_runtime *rtd)
  161. {
  162. struct snd_soc_codec *codec = rtd->codec_dais[0]->codec;
  163. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  164. int ret;
  165. /* We need default MCLK and PLL settings for the accessory detection */
  166. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 12288000,
  167. SND_SOC_CLOCK_IN);
  168. if (ret < 0) {
  169. dev_err(codec_dai->dev, "Init can't set codec clock in %d\n", ret);
  170. return ret;
  171. }
  172. ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
  173. if (ret < 0) {
  174. dev_err(codec_dai->dev, "Init can't set pll sysclk mclk %d\n", ret);
  175. return ret;
  176. }
  177. /* Enable Headset and 4 Buttons Jack detection */
  178. ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
  179. SND_JACK_HEADSET | SND_JACK_LINEOUT |
  180. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  181. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  182. &rockchip_sound_jack, NULL, 0);
  183. if (ret) {
  184. dev_err(rtd->card->dev, "New Headset Jack failed! (%d)\n", ret);
  185. return ret;
  186. }
  187. snd_jack_set_key(rockchip_sound_jack.jack, SND_JACK_BTN_0, KEY_MEDIA);
  188. snd_jack_set_key(
  189. rockchip_sound_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
  190. snd_jack_set_key(
  191. rockchip_sound_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
  192. snd_jack_set_key(
  193. rockchip_sound_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
  194. da7219_aad_jack_det(codec, &rockchip_sound_jack);
  195. return 0;
  196. }
  197. static struct snd_soc_ops rockchip_sound_max98357a_ops = {
  198. .hw_params = rockchip_sound_max98357a_hw_params,
  199. };
  200. static struct snd_soc_ops rockchip_sound_rt5514_ops = {
  201. .hw_params = rockchip_sound_rt5514_hw_params,
  202. };
  203. static struct snd_soc_ops rockchip_sound_da7219_ops = {
  204. .hw_params = rockchip_sound_da7219_hw_params,
  205. };
  206. enum {
  207. DAILINK_MAX98357A,
  208. DAILINK_RT5514,
  209. DAILINK_DA7219,
  210. DAILINK_RT5514_DSP,
  211. };
  212. #define DAILINK_ENTITIES (DAILINK_DA7219 + 1)
  213. static struct snd_soc_dai_link rockchip_dailinks[] = {
  214. [DAILINK_MAX98357A] = {
  215. .name = "MAX98357A",
  216. .stream_name = "MAX98357A PCM",
  217. .codec_dai_name = "HiFi",
  218. .ops = &rockchip_sound_max98357a_ops,
  219. /* set max98357a as slave */
  220. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  221. SND_SOC_DAIFMT_CBS_CFS,
  222. },
  223. [DAILINK_RT5514] = {
  224. .name = "RT5514",
  225. .stream_name = "RT5514 PCM",
  226. .codec_dai_name = "rt5514-aif1",
  227. .ops = &rockchip_sound_rt5514_ops,
  228. /* set rt5514 as slave */
  229. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  230. SND_SOC_DAIFMT_CBS_CFS,
  231. },
  232. [DAILINK_DA7219] = {
  233. .name = "DA7219",
  234. .stream_name = "DA7219 PCM",
  235. .codec_dai_name = "da7219-hifi",
  236. .init = rockchip_sound_da7219_init,
  237. .ops = &rockchip_sound_da7219_ops,
  238. /* set da7219 as slave */
  239. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  240. SND_SOC_DAIFMT_CBS_CFS,
  241. },
  242. /* RT5514 DSP for voice wakeup via spi bus */
  243. [DAILINK_RT5514_DSP] = {
  244. .name = "RT5514 DSP",
  245. .stream_name = "Wake on Voice",
  246. .codec_name = "snd-soc-dummy",
  247. .codec_dai_name = "snd-soc-dummy-dai",
  248. },
  249. };
  250. static struct snd_soc_card rockchip_sound_card = {
  251. .name = "rk3399-gru-sound",
  252. .owner = THIS_MODULE,
  253. .dai_link = rockchip_dailinks,
  254. .num_links = ARRAY_SIZE(rockchip_dailinks),
  255. .dapm_widgets = rockchip_dapm_widgets,
  256. .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
  257. .dapm_routes = rockchip_dapm_routes,
  258. .num_dapm_routes = ARRAY_SIZE(rockchip_dapm_routes),
  259. .controls = rockchip_controls,
  260. .num_controls = ARRAY_SIZE(rockchip_controls),
  261. };
  262. static int rockchip_sound_match_stub(struct device *dev, void *data)
  263. {
  264. return 1;
  265. }
  266. static int rockchip_sound_probe(struct platform_device *pdev)
  267. {
  268. struct snd_soc_card *card = &rockchip_sound_card;
  269. struct device_node *cpu_node;
  270. struct device *dev;
  271. struct device_driver *drv;
  272. int i, ret;
  273. cpu_node = of_parse_phandle(pdev->dev.of_node, "rockchip,cpu", 0);
  274. if (!cpu_node) {
  275. dev_err(&pdev->dev, "Property 'rockchip,cpu' missing or invalid\n");
  276. return -EINVAL;
  277. }
  278. for (i = 0; i < DAILINK_ENTITIES; i++) {
  279. rockchip_dailinks[i].platform_of_node = cpu_node;
  280. rockchip_dailinks[i].cpu_of_node = cpu_node;
  281. rockchip_dailinks[i].codec_of_node =
  282. of_parse_phandle(pdev->dev.of_node, "rockchip,codec", i);
  283. if (!rockchip_dailinks[i].codec_of_node) {
  284. dev_err(&pdev->dev,
  285. "Property[%d] 'rockchip,codec' missing or invalid\n", i);
  286. return -EINVAL;
  287. }
  288. }
  289. /**
  290. * To acquire the spi driver of the rt5514 and set the dai-links names
  291. * for soc_bind_dai_link
  292. */
  293. drv = driver_find("rt5514", &spi_bus_type);
  294. if (!drv) {
  295. dev_err(&pdev->dev, "Can not find the rt5514 driver at the spi bus\n");
  296. return -EINVAL;
  297. }
  298. dev = driver_find_device(drv, NULL, NULL, rockchip_sound_match_stub);
  299. if (!dev) {
  300. dev_err(&pdev->dev, "Can not find the rt5514 device\n");
  301. return -ENODEV;
  302. }
  303. /* Set DMIC delay */
  304. ret = device_property_read_u32(&pdev->dev, "dmic-delay",
  305. &rt5514_dmic_delay);
  306. if (ret) {
  307. rt5514_dmic_delay = 0;
  308. dev_dbg(&pdev->dev,
  309. "no optional property 'dmic-delay' found, default: no delay\n");
  310. }
  311. rockchip_dailinks[DAILINK_RT5514_DSP].cpu_name = kstrdup_const(dev_name(dev), GFP_KERNEL);
  312. rockchip_dailinks[DAILINK_RT5514_DSP].cpu_dai_name = kstrdup_const(dev_name(dev), GFP_KERNEL);
  313. rockchip_dailinks[DAILINK_RT5514_DSP].platform_name = kstrdup_const(dev_name(dev), GFP_KERNEL);
  314. card->dev = &pdev->dev;
  315. platform_set_drvdata(pdev, card);
  316. ret = devm_snd_soc_register_card(&pdev->dev, card);
  317. if (ret)
  318. dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
  319. __func__, ret);
  320. return ret;
  321. }
  322. static const struct of_device_id rockchip_sound_of_match[] = {
  323. { .compatible = "rockchip,rk3399-gru-sound", },
  324. {},
  325. };
  326. static struct platform_driver rockchip_sound_driver = {
  327. .probe = rockchip_sound_probe,
  328. .driver = {
  329. .name = DRV_NAME,
  330. .of_match_table = rockchip_sound_of_match,
  331. #ifdef CONFIG_PM
  332. .pm = &snd_soc_pm_ops,
  333. #endif
  334. },
  335. };
  336. module_platform_driver(rockchip_sound_driver);
  337. MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
  338. MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
  339. MODULE_LICENSE("GPL v2");
  340. MODULE_ALIAS("platform:" DRV_NAME);
  341. MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);