smartq_wm8987.c 6.2 KB

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