smartq_wm8987.c 7.0 KB

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