tobermory.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Tobermory audio support
  3. *
  4. * Copyright 2011 Wolfson Microelectronics
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <sound/soc.h>
  12. #include <sound/soc-dapm.h>
  13. #include <sound/jack.h>
  14. #include <linux/gpio.h>
  15. #include <linux/module.h>
  16. #include "../codecs/wm8962.h"
  17. static int sample_rate = 44100;
  18. static int tobermory_set_bias_level(struct snd_soc_card *card,
  19. struct snd_soc_dapm_context *dapm,
  20. enum snd_soc_bias_level level)
  21. {
  22. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  23. int ret;
  24. if (dapm->dev != codec_dai->dev)
  25. return 0;
  26. switch (level) {
  27. case SND_SOC_BIAS_PREPARE:
  28. if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
  29. ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  30. WM8962_FLL_MCLK, 32768,
  31. sample_rate * 512);
  32. if (ret < 0)
  33. pr_err("Failed to start FLL: %d\n", ret);
  34. ret = snd_soc_dai_set_sysclk(codec_dai,
  35. WM8962_SYSCLK_FLL,
  36. sample_rate * 512,
  37. SND_SOC_CLOCK_IN);
  38. if (ret < 0) {
  39. pr_err("Failed to set SYSCLK: %d\n", ret);
  40. snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  41. 0, 0, 0);
  42. return ret;
  43. }
  44. }
  45. break;
  46. default:
  47. break;
  48. }
  49. return 0;
  50. }
  51. static int tobermory_set_bias_level_post(struct snd_soc_card *card,
  52. struct snd_soc_dapm_context *dapm,
  53. enum snd_soc_bias_level level)
  54. {
  55. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  56. int ret;
  57. if (dapm->dev != codec_dai->dev)
  58. return 0;
  59. switch (level) {
  60. case SND_SOC_BIAS_STANDBY:
  61. ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
  62. 32768, SND_SOC_CLOCK_IN);
  63. if (ret < 0) {
  64. pr_err("Failed to switch away from FLL: %d\n", ret);
  65. return ret;
  66. }
  67. ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  68. 0, 0, 0);
  69. if (ret < 0) {
  70. pr_err("Failed to stop FLL: %d\n", ret);
  71. return ret;
  72. }
  73. break;
  74. default:
  75. break;
  76. }
  77. dapm->bias_level = level;
  78. return 0;
  79. }
  80. static int tobermory_hw_params(struct snd_pcm_substream *substream,
  81. struct snd_pcm_hw_params *params)
  82. {
  83. sample_rate = params_rate(params);
  84. return 0;
  85. }
  86. static struct snd_soc_ops tobermory_ops = {
  87. .hw_params = tobermory_hw_params,
  88. };
  89. static struct snd_soc_dai_link tobermory_dai[] = {
  90. {
  91. .name = "CPU",
  92. .stream_name = "CPU",
  93. .cpu_dai_name = "samsung-i2s.0",
  94. .codec_dai_name = "wm8962",
  95. .platform_name = "samsung-i2s.0",
  96. .codec_name = "wm8962.1-001a",
  97. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  98. | SND_SOC_DAIFMT_CBM_CFM,
  99. .ops = &tobermory_ops,
  100. },
  101. };
  102. static const struct snd_kcontrol_new controls[] = {
  103. SOC_DAPM_PIN_SWITCH("Main Speaker"),
  104. SOC_DAPM_PIN_SWITCH("DMIC"),
  105. };
  106. static struct snd_soc_dapm_widget widgets[] = {
  107. SND_SOC_DAPM_HP("Headphone", NULL),
  108. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  109. SND_SOC_DAPM_MIC("DMIC", NULL),
  110. SND_SOC_DAPM_MIC("AMIC", NULL),
  111. SND_SOC_DAPM_SPK("Main Speaker", NULL),
  112. };
  113. static struct snd_soc_dapm_route audio_paths[] = {
  114. { "Headphone", NULL, "HPOUTL" },
  115. { "Headphone", NULL, "HPOUTR" },
  116. { "Main Speaker", NULL, "SPKOUTL" },
  117. { "Main Speaker", NULL, "SPKOUTR" },
  118. { "Headset Mic", NULL, "MICBIAS" },
  119. { "IN4L", NULL, "Headset Mic" },
  120. { "IN4R", NULL, "Headset Mic" },
  121. { "AMIC", NULL, "MICBIAS" },
  122. { "IN1L", NULL, "AMIC" },
  123. { "IN1R", NULL, "AMIC" },
  124. { "DMIC", NULL, "MICBIAS" },
  125. { "DMICDAT", NULL, "DMIC" },
  126. };
  127. static struct snd_soc_jack tobermory_headset;
  128. /* Headset jack detection DAPM pins */
  129. static struct snd_soc_jack_pin tobermory_headset_pins[] = {
  130. {
  131. .pin = "Headset Mic",
  132. .mask = SND_JACK_MICROPHONE,
  133. },
  134. {
  135. .pin = "Headphone",
  136. .mask = SND_JACK_MICROPHONE,
  137. },
  138. };
  139. static int tobermory_late_probe(struct snd_soc_card *card)
  140. {
  141. struct snd_soc_codec *codec = card->rtd[0].codec;
  142. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  143. int ret;
  144. ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
  145. 32768, SND_SOC_CLOCK_IN);
  146. if (ret < 0)
  147. return ret;
  148. ret = snd_soc_jack_new(codec, "Headset",
  149. SND_JACK_HEADSET | SND_JACK_BTN_0,
  150. &tobermory_headset);
  151. if (ret)
  152. return ret;
  153. ret = snd_soc_jack_add_pins(&tobermory_headset,
  154. ARRAY_SIZE(tobermory_headset_pins),
  155. tobermory_headset_pins);
  156. if (ret)
  157. return ret;
  158. wm8962_mic_detect(codec, &tobermory_headset);
  159. return 0;
  160. }
  161. static struct snd_soc_card tobermory = {
  162. .name = "Tobermory",
  163. .owner = THIS_MODULE,
  164. .dai_link = tobermory_dai,
  165. .num_links = ARRAY_SIZE(tobermory_dai),
  166. .set_bias_level = tobermory_set_bias_level,
  167. .set_bias_level_post = tobermory_set_bias_level_post,
  168. .controls = controls,
  169. .num_controls = ARRAY_SIZE(controls),
  170. .dapm_widgets = widgets,
  171. .num_dapm_widgets = ARRAY_SIZE(widgets),
  172. .dapm_routes = audio_paths,
  173. .num_dapm_routes = ARRAY_SIZE(audio_paths),
  174. .fully_routed = true,
  175. .late_probe = tobermory_late_probe,
  176. };
  177. static int tobermory_probe(struct platform_device *pdev)
  178. {
  179. struct snd_soc_card *card = &tobermory;
  180. int ret;
  181. card->dev = &pdev->dev;
  182. ret = devm_snd_soc_register_card(&pdev->dev, card);
  183. if (ret)
  184. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  185. ret);
  186. return ret;
  187. }
  188. static struct platform_driver tobermory_driver = {
  189. .driver = {
  190. .name = "tobermory",
  191. .owner = THIS_MODULE,
  192. .pm = &snd_soc_pm_ops,
  193. },
  194. .probe = tobermory_probe,
  195. };
  196. module_platform_driver(tobermory_driver);
  197. MODULE_DESCRIPTION("Tobermory audio support");
  198. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  199. MODULE_LICENSE("GPL");
  200. MODULE_ALIAS("platform:tobermory");