rockchip_rt5645.c 6.2 KB

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