tobermory.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. return ret;
  41. }
  42. }
  43. break;
  44. default:
  45. break;
  46. }
  47. return 0;
  48. }
  49. static int tobermory_set_bias_level_post(struct snd_soc_card *card,
  50. struct snd_soc_dapm_context *dapm,
  51. enum snd_soc_bias_level level)
  52. {
  53. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  54. int ret;
  55. if (dapm->dev != codec_dai->dev)
  56. return 0;
  57. switch (level) {
  58. case SND_SOC_BIAS_STANDBY:
  59. ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
  60. 32768, SND_SOC_CLOCK_IN);
  61. if (ret < 0) {
  62. pr_err("Failed to switch away from FLL: %d\n", ret);
  63. return ret;
  64. }
  65. ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  66. 0, 0, 0);
  67. if (ret < 0) {
  68. pr_err("Failed to stop FLL: %d\n", ret);
  69. return ret;
  70. }
  71. break;
  72. default:
  73. break;
  74. }
  75. dapm->bias_level = level;
  76. return 0;
  77. }
  78. static int tobermory_hw_params(struct snd_pcm_substream *substream,
  79. struct snd_pcm_hw_params *params)
  80. {
  81. sample_rate = params_rate(params);
  82. return 0;
  83. }
  84. static struct snd_soc_ops tobermory_ops = {
  85. .hw_params = tobermory_hw_params,
  86. };
  87. static struct snd_soc_dai_link tobermory_dai[] = {
  88. {
  89. .name = "CPU",
  90. .stream_name = "CPU",
  91. .cpu_dai_name = "samsung-i2s.0",
  92. .codec_dai_name = "wm8962",
  93. .platform_name = "samsung-audio",
  94. .codec_name = "wm8962.1-001a",
  95. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  96. | SND_SOC_DAIFMT_CBM_CFM,
  97. .ops = &tobermory_ops,
  98. },
  99. };
  100. static const struct snd_kcontrol_new controls[] = {
  101. SOC_DAPM_PIN_SWITCH("Main Speaker"),
  102. SOC_DAPM_PIN_SWITCH("DMIC"),
  103. };
  104. static struct snd_soc_dapm_widget widgets[] = {
  105. SND_SOC_DAPM_HP("Headphone", NULL),
  106. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  107. SND_SOC_DAPM_MIC("DMIC", NULL),
  108. SND_SOC_DAPM_MIC("AMIC", NULL),
  109. SND_SOC_DAPM_SPK("Main Speaker", NULL),
  110. };
  111. static struct snd_soc_dapm_route audio_paths[] = {
  112. { "Headphone", NULL, "HPOUTL" },
  113. { "Headphone", NULL, "HPOUTR" },
  114. { "Main Speaker", NULL, "SPKOUTL" },
  115. { "Main Speaker", NULL, "SPKOUTR" },
  116. { "Headset Mic", NULL, "MICBIAS" },
  117. { "IN4L", NULL, "Headset Mic" },
  118. { "IN4R", NULL, "Headset Mic" },
  119. { "AMIC", NULL, "MICBIAS" },
  120. { "IN1L", NULL, "AMIC" },
  121. { "IN1R", NULL, "AMIC" },
  122. { "DMIC", NULL, "MICBIAS" },
  123. { "DMICDAT", NULL, "DMIC" },
  124. };
  125. static struct snd_soc_jack tobermory_headset;
  126. /* Headset jack detection DAPM pins */
  127. static struct snd_soc_jack_pin tobermory_headset_pins[] = {
  128. {
  129. .pin = "Headset Mic",
  130. .mask = SND_JACK_MICROPHONE,
  131. },
  132. {
  133. .pin = "Headphone",
  134. .mask = SND_JACK_MICROPHONE,
  135. },
  136. };
  137. static int tobermory_late_probe(struct snd_soc_card *card)
  138. {
  139. struct snd_soc_codec *codec = card->rtd[0].codec;
  140. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  141. int ret;
  142. ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
  143. 32768, SND_SOC_CLOCK_IN);
  144. if (ret < 0)
  145. return ret;
  146. ret = snd_soc_jack_new(codec, "Headset",
  147. SND_JACK_HEADSET | SND_JACK_BTN_0,
  148. &tobermory_headset);
  149. if (ret)
  150. return ret;
  151. ret = snd_soc_jack_add_pins(&tobermory_headset,
  152. ARRAY_SIZE(tobermory_headset_pins),
  153. tobermory_headset_pins);
  154. if (ret)
  155. return ret;
  156. wm8962_mic_detect(codec, &tobermory_headset);
  157. return 0;
  158. }
  159. static struct snd_soc_card tobermory = {
  160. .name = "Tobermory",
  161. .owner = THIS_MODULE,
  162. .dai_link = tobermory_dai,
  163. .num_links = ARRAY_SIZE(tobermory_dai),
  164. .set_bias_level = tobermory_set_bias_level,
  165. .set_bias_level_post = tobermory_set_bias_level_post,
  166. .controls = controls,
  167. .num_controls = ARRAY_SIZE(controls),
  168. .dapm_widgets = widgets,
  169. .num_dapm_widgets = ARRAY_SIZE(widgets),
  170. .dapm_routes = audio_paths,
  171. .num_dapm_routes = ARRAY_SIZE(audio_paths),
  172. .fully_routed = true,
  173. .late_probe = tobermory_late_probe,
  174. };
  175. static __devinit int tobermory_probe(struct platform_device *pdev)
  176. {
  177. struct snd_soc_card *card = &tobermory;
  178. int ret;
  179. card->dev = &pdev->dev;
  180. ret = snd_soc_register_card(card);
  181. if (ret) {
  182. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  183. ret);
  184. return ret;
  185. }
  186. return 0;
  187. }
  188. static int __devexit tobermory_remove(struct platform_device *pdev)
  189. {
  190. struct snd_soc_card *card = platform_get_drvdata(pdev);
  191. snd_soc_unregister_card(card);
  192. return 0;
  193. }
  194. static struct platform_driver tobermory_driver = {
  195. .driver = {
  196. .name = "tobermory",
  197. .owner = THIS_MODULE,
  198. .pm = &snd_soc_pm_ops,
  199. },
  200. .probe = tobermory_probe,
  201. .remove = __devexit_p(tobermory_remove),
  202. };
  203. module_platform_driver(tobermory_driver);
  204. MODULE_DESCRIPTION("Tobermory audio support");
  205. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  206. MODULE_LICENSE("GPL");
  207. MODULE_ALIAS("platform:tobermory");