rockchip_max98090.c 6.1 KB

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