rockchip_rt5645.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 24000:
  73. case 32000:
  74. case 48000:
  75. case 64000:
  76. case 96000:
  77. mclk = 12288000;
  78. break;
  79. case 11025:
  80. case 22050:
  81. case 44100:
  82. case 88200:
  83. mclk = 11289600;
  84. break;
  85. default:
  86. return -EINVAL;
  87. }
  88. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
  89. SND_SOC_CLOCK_OUT);
  90. if (ret < 0) {
  91. dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
  92. return ret;
  93. }
  94. ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  95. SND_SOC_CLOCK_IN);
  96. if (ret < 0) {
  97. dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
  98. return ret;
  99. }
  100. return ret;
  101. }
  102. static int rk_init(struct snd_soc_pcm_runtime *runtime)
  103. {
  104. struct snd_soc_card *card = runtime->card;
  105. int ret;
  106. /* Enable Headset and 4 Buttons Jack detection */
  107. ret = snd_soc_card_jack_new(card, "Headset Jack",
  108. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  109. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  110. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  111. &headset_jack, NULL, 0);
  112. if (ret) {
  113. dev_err(card->dev, "New Headset Jack failed! (%d)\n", ret);
  114. return ret;
  115. }
  116. return rt5645_set_jack_detect(runtime->codec,
  117. &headset_jack,
  118. &headset_jack,
  119. &headset_jack);
  120. }
  121. static struct snd_soc_ops rk_aif1_ops = {
  122. .hw_params = rk_aif1_hw_params,
  123. };
  124. static struct snd_soc_dai_link rk_dailink = {
  125. .name = "rt5645",
  126. .stream_name = "rt5645 PCM",
  127. .codec_dai_name = "rt5645-aif1",
  128. .init = rk_init,
  129. .ops = &rk_aif1_ops,
  130. /* set rt5645 as slave */
  131. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  132. SND_SOC_DAIFMT_CBS_CFS,
  133. };
  134. static struct snd_soc_card snd_soc_card_rk = {
  135. .name = "I2S-RT5650",
  136. .owner = THIS_MODULE,
  137. .dai_link = &rk_dailink,
  138. .num_links = 1,
  139. .dapm_widgets = rk_dapm_widgets,
  140. .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
  141. .dapm_routes = rk_audio_map,
  142. .num_dapm_routes = ARRAY_SIZE(rk_audio_map),
  143. .controls = rk_mc_controls,
  144. .num_controls = ARRAY_SIZE(rk_mc_controls),
  145. };
  146. static int snd_rk_mc_probe(struct platform_device *pdev)
  147. {
  148. int ret = 0;
  149. struct snd_soc_card *card = &snd_soc_card_rk;
  150. struct device_node *np = pdev->dev.of_node;
  151. /* register the soc card */
  152. card->dev = &pdev->dev;
  153. rk_dailink.codec_of_node = of_parse_phandle(np,
  154. "rockchip,audio-codec", 0);
  155. if (!rk_dailink.codec_of_node) {
  156. dev_err(&pdev->dev,
  157. "Property 'rockchip,audio-codec' missing or invalid\n");
  158. return -EINVAL;
  159. }
  160. rk_dailink.cpu_of_node = of_parse_phandle(np,
  161. "rockchip,i2s-controller", 0);
  162. if (!rk_dailink.cpu_of_node) {
  163. dev_err(&pdev->dev,
  164. "Property 'rockchip,i2s-controller' missing or invalid\n");
  165. return -EINVAL;
  166. }
  167. rk_dailink.platform_of_node = rk_dailink.cpu_of_node;
  168. ret = snd_soc_of_parse_card_name(card, "rockchip,model");
  169. if (ret) {
  170. dev_err(&pdev->dev,
  171. "Soc parse card name failed %d\n", ret);
  172. return ret;
  173. }
  174. ret = devm_snd_soc_register_card(&pdev->dev, card);
  175. if (ret) {
  176. dev_err(&pdev->dev,
  177. "Soc register card failed %d\n", ret);
  178. return ret;
  179. }
  180. return ret;
  181. }
  182. static const struct of_device_id rockchip_rt5645_of_match[] = {
  183. { .compatible = "rockchip,rockchip-audio-rt5645", },
  184. {},
  185. };
  186. MODULE_DEVICE_TABLE(of, rockchip_rt5645_of_match);
  187. static struct platform_driver snd_rk_mc_driver = {
  188. .probe = snd_rk_mc_probe,
  189. .driver = {
  190. .name = DRV_NAME,
  191. .pm = &snd_soc_pm_ops,
  192. .of_match_table = rockchip_rt5645_of_match,
  193. },
  194. };
  195. module_platform_driver(snd_rk_mc_driver);
  196. MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
  197. MODULE_DESCRIPTION("Rockchip rt5645 machine ASoC driver");
  198. MODULE_LICENSE("GPL v2");
  199. MODULE_ALIAS("platform:" DRV_NAME);