smartq_wm8987.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* sound/soc/samsung/smartq_wm8987.c
  2. *
  3. * Copyright 2010 Maurus Cuelenaere <mcuelenaere@gmail.com>
  4. *
  5. * Based on smdk6410_wm8987.c
  6. * Copyright 2007 Wolfson Microelectronics PLC. - linux@wolfsonmicro.com
  7. * Graeme Gregory - graeme.gregory@wolfsonmicro.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/gpio.h>
  16. #include <sound/soc.h>
  17. #include <sound/jack.h>
  18. #include <asm/mach-types.h>
  19. #include "i2s.h"
  20. #include "../codecs/wm8750.h"
  21. /*
  22. * WM8987 is register compatible with WM8750, so using that as base driver.
  23. */
  24. static struct snd_soc_card snd_soc_smartq;
  25. static int smartq_hifi_hw_params(struct snd_pcm_substream *substream,
  26. struct snd_pcm_hw_params *params)
  27. {
  28. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  29. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  30. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  31. unsigned int clk = 0;
  32. int ret;
  33. switch (params_rate(params)) {
  34. case 8000:
  35. case 16000:
  36. case 32000:
  37. case 48000:
  38. case 96000:
  39. clk = 12288000;
  40. break;
  41. case 11025:
  42. case 22050:
  43. case 44100:
  44. case 88200:
  45. clk = 11289600;
  46. break;
  47. }
  48. /* set codec DAI configuration */
  49. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  50. SND_SOC_DAIFMT_NB_NF |
  51. SND_SOC_DAIFMT_CBS_CFS);
  52. if (ret < 0)
  53. return ret;
  54. /* set cpu DAI configuration */
  55. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  56. SND_SOC_DAIFMT_NB_NF |
  57. SND_SOC_DAIFMT_CBS_CFS);
  58. if (ret < 0)
  59. return ret;
  60. /* Use PCLK for I2S signal generation */
  61. ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_RCLKSRC_0,
  62. 0, SND_SOC_CLOCK_IN);
  63. if (ret < 0)
  64. return ret;
  65. /* Gate the RCLK output on PAD */
  66. ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK,
  67. 0, SND_SOC_CLOCK_IN);
  68. if (ret < 0)
  69. return ret;
  70. /* set the codec system clock for DAC and ADC */
  71. ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
  72. SND_SOC_CLOCK_IN);
  73. if (ret < 0)
  74. return ret;
  75. return 0;
  76. }
  77. /*
  78. * SmartQ WM8987 HiFi DAI operations.
  79. */
  80. static struct snd_soc_ops smartq_hifi_ops = {
  81. .hw_params = smartq_hifi_hw_params,
  82. };
  83. static struct snd_soc_jack smartq_jack;
  84. static struct snd_soc_jack_pin smartq_jack_pins[] = {
  85. /* Disable speaker when headphone is plugged in */
  86. {
  87. .pin = "Internal Speaker",
  88. .mask = SND_JACK_HEADPHONE,
  89. },
  90. };
  91. static struct snd_soc_jack_gpio smartq_jack_gpios[] = {
  92. {
  93. .gpio = S3C64XX_GPL(12),
  94. .name = "headphone detect",
  95. .report = SND_JACK_HEADPHONE,
  96. .debounce_time = 200,
  97. },
  98. };
  99. static const struct snd_kcontrol_new wm8987_smartq_controls[] = {
  100. SOC_DAPM_PIN_SWITCH("Internal Speaker"),
  101. SOC_DAPM_PIN_SWITCH("Headphone Jack"),
  102. SOC_DAPM_PIN_SWITCH("Internal Mic"),
  103. };
  104. static int smartq_speaker_event(struct snd_soc_dapm_widget *w,
  105. struct snd_kcontrol *k,
  106. int event)
  107. {
  108. gpio_set_value(S3C64XX_GPK(12), SND_SOC_DAPM_EVENT_OFF(event));
  109. return 0;
  110. }
  111. static const struct snd_soc_dapm_widget wm8987_dapm_widgets[] = {
  112. SND_SOC_DAPM_SPK("Internal Speaker", smartq_speaker_event),
  113. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  114. SND_SOC_DAPM_MIC("Internal Mic", NULL),
  115. };
  116. static const struct snd_soc_dapm_route audio_map[] = {
  117. {"Headphone Jack", NULL, "LOUT2"},
  118. {"Headphone Jack", NULL, "ROUT2"},
  119. {"Internal Speaker", NULL, "LOUT2"},
  120. {"Internal Speaker", NULL, "ROUT2"},
  121. {"Mic Bias", NULL, "Internal Mic"},
  122. {"LINPUT2", NULL, "Mic Bias"},
  123. };
  124. static int smartq_wm8987_init(struct snd_soc_pcm_runtime *rtd)
  125. {
  126. struct snd_soc_codec *codec = rtd->codec;
  127. struct snd_soc_dapm_context *dapm = &codec->dapm;
  128. int err = 0;
  129. /* Add SmartQ specific widgets */
  130. snd_soc_dapm_new_controls(dapm, wm8987_dapm_widgets,
  131. ARRAY_SIZE(wm8987_dapm_widgets));
  132. /* add SmartQ specific controls */
  133. err = snd_soc_add_controls(codec, wm8987_smartq_controls,
  134. ARRAY_SIZE(wm8987_smartq_controls));
  135. if (err < 0)
  136. return err;
  137. /* setup SmartQ specific audio path */
  138. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  139. /* set endpoints to not connected */
  140. snd_soc_dapm_nc_pin(dapm, "LINPUT1");
  141. snd_soc_dapm_nc_pin(dapm, "RINPUT1");
  142. snd_soc_dapm_nc_pin(dapm, "OUT3");
  143. snd_soc_dapm_nc_pin(dapm, "ROUT1");
  144. /* set endpoints to default off mode */
  145. snd_soc_dapm_enable_pin(dapm, "Internal Speaker");
  146. snd_soc_dapm_enable_pin(dapm, "Internal Mic");
  147. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  148. err = snd_soc_dapm_sync(dapm);
  149. if (err)
  150. return err;
  151. /* Headphone jack detection */
  152. err = snd_soc_jack_new(codec, "Headphone Jack",
  153. SND_JACK_HEADPHONE, &smartq_jack);
  154. if (err)
  155. return err;
  156. err = snd_soc_jack_add_pins(&smartq_jack, ARRAY_SIZE(smartq_jack_pins),
  157. smartq_jack_pins);
  158. if (err)
  159. return err;
  160. err = snd_soc_jack_add_gpios(&smartq_jack,
  161. ARRAY_SIZE(smartq_jack_gpios),
  162. smartq_jack_gpios);
  163. return err;
  164. }
  165. static struct snd_soc_dai_link smartq_dai[] = {
  166. {
  167. .name = "wm8987",
  168. .stream_name = "SmartQ Hi-Fi",
  169. .cpu_dai_name = "samsung-i2s.0",
  170. .codec_dai_name = "wm8750-hifi",
  171. .platform_name = "samsung-audio",
  172. .codec_name = "wm8750-codec.0-0x1a",
  173. .init = smartq_wm8987_init,
  174. .ops = &smartq_hifi_ops,
  175. },
  176. };
  177. static struct snd_soc_card snd_soc_smartq = {
  178. .name = "SmartQ",
  179. .dai_link = smartq_dai,
  180. .num_links = ARRAY_SIZE(smartq_dai),
  181. };
  182. static struct platform_device *smartq_snd_device;
  183. static int __init smartq_init(void)
  184. {
  185. int ret;
  186. if (!machine_is_smartq7() && !machine_is_smartq5()) {
  187. pr_info("Only SmartQ is supported by this ASoC driver\n");
  188. return -ENODEV;
  189. }
  190. smartq_snd_device = platform_device_alloc("soc-audio", -1);
  191. if (!smartq_snd_device)
  192. return -ENOMEM;
  193. platform_set_drvdata(smartq_snd_device, &snd_soc_smartq);
  194. ret = platform_device_add(smartq_snd_device);
  195. if (ret) {
  196. platform_device_put(smartq_snd_device);
  197. return ret;
  198. }
  199. /* Initialise GPIOs used by amplifiers */
  200. ret = gpio_request(S3C64XX_GPK(12), "amplifiers shutdown");
  201. if (ret) {
  202. dev_err(&smartq_snd_device->dev, "Failed to register GPK12\n");
  203. goto err_unregister_device;
  204. }
  205. /* Disable amplifiers */
  206. ret = gpio_direction_output(S3C64XX_GPK(12), 1);
  207. if (ret) {
  208. dev_err(&smartq_snd_device->dev, "Failed to configure GPK12\n");
  209. goto err_free_gpio_amp_shut;
  210. }
  211. return 0;
  212. err_free_gpio_amp_shut:
  213. gpio_free(S3C64XX_GPK(12));
  214. err_unregister_device:
  215. platform_device_unregister(smartq_snd_device);
  216. return ret;
  217. }
  218. static void __exit smartq_exit(void)
  219. {
  220. gpio_free(S3C64XX_GPK(12));
  221. snd_soc_jack_free_gpios(&smartq_jack, ARRAY_SIZE(smartq_jack_gpios),
  222. smartq_jack_gpios);
  223. platform_device_unregister(smartq_snd_device);
  224. }
  225. module_init(smartq_init);
  226. module_exit(smartq_exit);
  227. /* Module information */
  228. MODULE_AUTHOR("Maurus Cuelenaere <mcuelenaere@gmail.com>");
  229. MODULE_DESCRIPTION("ALSA SoC SmartQ WM8987");
  230. MODULE_LICENSE("GPL");