rockchip_rt5645.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Rockchip machine ASoC driver for boards using a RT5645/RT5650 CODEC.
  3. *
  4. * Copyright (c) 2015, 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 <sound/core.h>
  26. #include <sound/jack.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/soc.h>
  30. #include "rockchip_i2s.h"
  31. #define DRV_NAME "rockchip-snd-rt5645"
  32. static struct snd_soc_jack headset_jack;
  33. /* Jack detect via rt5645 driver. */
  34. extern int rt5645_set_jack_detect(struct snd_soc_codec *codec,
  35. struct snd_soc_jack *hp_jack, struct snd_soc_jack *mic_jack,
  36. struct snd_soc_jack *btn_jack);
  37. static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
  38. SND_SOC_DAPM_HP("Headphones", NULL),
  39. SND_SOC_DAPM_SPK("Speakers", NULL),
  40. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  41. SND_SOC_DAPM_MIC("Int Mic", NULL),
  42. };
  43. static const struct snd_soc_dapm_route rk_audio_map[] = {
  44. /* Input Lines */
  45. {"DMIC L2", NULL, "Int Mic"},
  46. {"DMIC R2", NULL, "Int Mic"},
  47. {"RECMIXL", NULL, "Headset Mic"},
  48. {"RECMIXR", NULL, "Headset Mic"},
  49. /* Output Lines */
  50. {"Headphones", NULL, "HPOR"},
  51. {"Headphones", NULL, "HPOL"},
  52. {"Speakers", NULL, "SPOL"},
  53. {"Speakers", NULL, "SPOR"},
  54. };
  55. static const struct snd_kcontrol_new rk_mc_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 rk_aif1_hw_params(struct snd_pcm_substream *substream,
  62. struct snd_pcm_hw_params *params)
  63. {
  64. int ret = 0;
  65. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  66. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  67. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  68. int mclk;
  69. switch (params_rate(params)) {
  70. case 8000:
  71. case 16000:
  72. case 48000:
  73. case 96000:
  74. mclk = 12288000;
  75. break;
  76. case 44100:
  77. mclk = 11289600;
  78. break;
  79. default:
  80. return -EINVAL;
  81. }
  82. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
  83. SND_SOC_CLOCK_OUT);
  84. if (ret < 0) {
  85. dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
  86. return ret;
  87. }
  88. ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  89. SND_SOC_CLOCK_IN);
  90. if (ret < 0) {
  91. dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
  92. return ret;
  93. }
  94. return ret;
  95. }
  96. static int rk_init(struct snd_soc_pcm_runtime *runtime)
  97. {
  98. struct snd_soc_card *card = runtime->card;
  99. int ret;
  100. /* Enable Headset and 4 Buttons Jack detection */
  101. ret = snd_soc_card_jack_new(card, "Headset Jack",
  102. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  103. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  104. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  105. &headset_jack, NULL, 0);
  106. if (ret) {
  107. dev_err(card->dev, "New Headset Jack failed! (%d)\n", ret);
  108. return ret;
  109. }
  110. return rt5645_set_jack_detect(runtime->codec,
  111. &headset_jack,
  112. &headset_jack,
  113. &headset_jack);
  114. }
  115. static struct snd_soc_ops rk_aif1_ops = {
  116. .hw_params = rk_aif1_hw_params,
  117. };
  118. static struct snd_soc_dai_link rk_dailink = {
  119. .name = "rt5645",
  120. .stream_name = "rt5645 PCM",
  121. .codec_dai_name = "rt5645-aif1",
  122. .init = rk_init,
  123. .ops = &rk_aif1_ops,
  124. /* set rt5645 as slave */
  125. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  126. SND_SOC_DAIFMT_CBS_CFS,
  127. };
  128. static struct snd_soc_card snd_soc_card_rk = {
  129. .name = "I2S-RT5650",
  130. .owner = THIS_MODULE,
  131. .dai_link = &rk_dailink,
  132. .num_links = 1,
  133. .dapm_widgets = rk_dapm_widgets,
  134. .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
  135. .dapm_routes = rk_audio_map,
  136. .num_dapm_routes = ARRAY_SIZE(rk_audio_map),
  137. .controls = rk_mc_controls,
  138. .num_controls = ARRAY_SIZE(rk_mc_controls),
  139. };
  140. static int snd_rk_mc_probe(struct platform_device *pdev)
  141. {
  142. int ret = 0;
  143. struct snd_soc_card *card = &snd_soc_card_rk;
  144. struct device_node *np = pdev->dev.of_node;
  145. /* register the soc card */
  146. card->dev = &pdev->dev;
  147. rk_dailink.codec_of_node = of_parse_phandle(np,
  148. "rockchip,audio-codec", 0);
  149. if (!rk_dailink.codec_of_node) {
  150. dev_err(&pdev->dev,
  151. "Property 'rockchip,audio-codec' missing or invalid\n");
  152. return -EINVAL;
  153. }
  154. rk_dailink.cpu_of_node = of_parse_phandle(np,
  155. "rockchip,i2s-controller", 0);
  156. if (!rk_dailink.cpu_of_node) {
  157. dev_err(&pdev->dev,
  158. "Property 'rockchip,i2s-controller' missing or invalid\n");
  159. return -EINVAL;
  160. }
  161. rk_dailink.platform_of_node = rk_dailink.cpu_of_node;
  162. ret = snd_soc_of_parse_card_name(card, "rockchip,model");
  163. if (ret) {
  164. dev_err(&pdev->dev,
  165. "Soc parse card name failed %d\n", ret);
  166. return ret;
  167. }
  168. ret = devm_snd_soc_register_card(&pdev->dev, card);
  169. if (ret) {
  170. dev_err(&pdev->dev,
  171. "Soc register card failed %d\n", ret);
  172. return ret;
  173. }
  174. return ret;
  175. }
  176. static const struct of_device_id rockchip_rt5645_of_match[] = {
  177. { .compatible = "rockchip,rockchip-audio-rt5645", },
  178. {},
  179. };
  180. MODULE_DEVICE_TABLE(of, rockchip_rt5645_of_match);
  181. static struct platform_driver snd_rk_mc_driver = {
  182. .probe = snd_rk_mc_probe,
  183. .driver = {
  184. .name = DRV_NAME,
  185. .pm = &snd_soc_pm_ops,
  186. .of_match_table = rockchip_rt5645_of_match,
  187. },
  188. };
  189. module_platform_driver(snd_rk_mc_driver);
  190. MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
  191. MODULE_DESCRIPTION("Rockchip rt5645 machine ASoC driver");
  192. MODULE_LICENSE("GPL v2");
  193. MODULE_ALIAS("platform:" DRV_NAME);