rockchip_max98090.c 6.2 KB

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