smartq_wm8987.c 6.9 KB

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