smartq_wm8987.c 6.6 KB

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