smartq_wm8987.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. /* set endpoints to not connected */
  130. snd_soc_dapm_nc_pin(dapm, "LINPUT1");
  131. snd_soc_dapm_nc_pin(dapm, "RINPUT1");
  132. snd_soc_dapm_nc_pin(dapm, "OUT3");
  133. snd_soc_dapm_nc_pin(dapm, "ROUT1");
  134. /* set endpoints to default off mode */
  135. snd_soc_dapm_enable_pin(dapm, "Internal Speaker");
  136. snd_soc_dapm_enable_pin(dapm, "Internal Mic");
  137. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  138. /* Headphone jack detection */
  139. err = snd_soc_jack_new(codec, "Headphone Jack",
  140. SND_JACK_HEADPHONE, &smartq_jack);
  141. if (err)
  142. return err;
  143. err = snd_soc_jack_add_pins(&smartq_jack, ARRAY_SIZE(smartq_jack_pins),
  144. smartq_jack_pins);
  145. if (err)
  146. return err;
  147. err = snd_soc_jack_add_gpios(&smartq_jack,
  148. ARRAY_SIZE(smartq_jack_gpios),
  149. smartq_jack_gpios);
  150. return err;
  151. }
  152. static struct snd_soc_dai_link smartq_dai[] = {
  153. {
  154. .name = "wm8987",
  155. .stream_name = "SmartQ Hi-Fi",
  156. .cpu_dai_name = "samsung-i2s.0",
  157. .codec_dai_name = "wm8750-hifi",
  158. .platform_name = "samsung-audio",
  159. .codec_name = "wm8750.0-0x1a",
  160. .init = smartq_wm8987_init,
  161. .ops = &smartq_hifi_ops,
  162. },
  163. };
  164. static struct snd_soc_card snd_soc_smartq = {
  165. .name = "SmartQ",
  166. .dai_link = smartq_dai,
  167. .num_links = ARRAY_SIZE(smartq_dai),
  168. .dapm_widgets = wm8987_dapm_widgets,
  169. .num_dapm_widgets = ARRAY_SIZE(wm8987_dapm_widgets),
  170. .dapm_routes = audio_map,
  171. .num_dapm_routes = ARRAY_SIZE(audio_map),
  172. .controls = wm8987_smartq_controls,
  173. .num_controls = ARRAY_SIZE(wm8987_smartq_controls),
  174. };
  175. static struct platform_device *smartq_snd_device;
  176. static int __init smartq_init(void)
  177. {
  178. int ret;
  179. if (!machine_is_smartq7() && !machine_is_smartq5()) {
  180. pr_info("Only SmartQ is supported by this ASoC driver\n");
  181. return -ENODEV;
  182. }
  183. smartq_snd_device = platform_device_alloc("soc-audio", -1);
  184. if (!smartq_snd_device)
  185. return -ENOMEM;
  186. platform_set_drvdata(smartq_snd_device, &snd_soc_smartq);
  187. ret = platform_device_add(smartq_snd_device);
  188. if (ret) {
  189. platform_device_put(smartq_snd_device);
  190. return ret;
  191. }
  192. /* Initialise GPIOs used by amplifiers */
  193. ret = gpio_request(S3C64XX_GPK(12), "amplifiers shutdown");
  194. if (ret) {
  195. dev_err(&smartq_snd_device->dev, "Failed to register GPK12\n");
  196. goto err_unregister_device;
  197. }
  198. /* Disable amplifiers */
  199. ret = gpio_direction_output(S3C64XX_GPK(12), 1);
  200. if (ret) {
  201. dev_err(&smartq_snd_device->dev, "Failed to configure GPK12\n");
  202. goto err_free_gpio_amp_shut;
  203. }
  204. return 0;
  205. err_free_gpio_amp_shut:
  206. gpio_free(S3C64XX_GPK(12));
  207. err_unregister_device:
  208. platform_device_unregister(smartq_snd_device);
  209. return ret;
  210. }
  211. static void __exit smartq_exit(void)
  212. {
  213. gpio_free(S3C64XX_GPK(12));
  214. snd_soc_jack_free_gpios(&smartq_jack, ARRAY_SIZE(smartq_jack_gpios),
  215. smartq_jack_gpios);
  216. platform_device_unregister(smartq_snd_device);
  217. }
  218. module_init(smartq_init);
  219. module_exit(smartq_exit);
  220. /* Module information */
  221. MODULE_AUTHOR("Maurus Cuelenaere <mcuelenaere@gmail.com>");
  222. MODULE_DESCRIPTION("ALSA SoC SmartQ WM8987");
  223. MODULE_LICENSE("GPL");