smartq_wm8987.c 6.7 KB

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