smartq_wm8987.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/consumer.h>
  16. #include <linux/module.h>
  17. #include <sound/soc.h>
  18. #include <sound/jack.h>
  19. #include "i2s.h"
  20. #include "../codecs/wm8750.h"
  21. /*
  22. * WM8987 is register compatible with WM8750, so using that as base driver.
  23. */
  24. static struct snd_soc_card snd_soc_smartq;
  25. static int smartq_hifi_hw_params(struct snd_pcm_substream *substream,
  26. struct snd_pcm_hw_params *params)
  27. {
  28. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  29. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  30. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  31. unsigned int clk = 0;
  32. int ret;
  33. switch (params_rate(params)) {
  34. case 8000:
  35. case 16000:
  36. case 32000:
  37. case 48000:
  38. case 96000:
  39. clk = 12288000;
  40. break;
  41. case 11025:
  42. case 22050:
  43. case 44100:
  44. case 88200:
  45. clk = 11289600;
  46. break;
  47. }
  48. /* Use PCLK for I2S signal generation */
  49. ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_RCLKSRC_0,
  50. 0, SND_SOC_CLOCK_IN);
  51. if (ret < 0)
  52. return ret;
  53. /* Gate the RCLK output on PAD */
  54. ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK,
  55. 0, SND_SOC_CLOCK_IN);
  56. if (ret < 0)
  57. return ret;
  58. /* set the codec system clock for DAC and ADC */
  59. ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
  60. SND_SOC_CLOCK_IN);
  61. if (ret < 0)
  62. return ret;
  63. return 0;
  64. }
  65. /*
  66. * SmartQ WM8987 HiFi DAI operations.
  67. */
  68. static struct snd_soc_ops smartq_hifi_ops = {
  69. .hw_params = smartq_hifi_hw_params,
  70. };
  71. static struct snd_soc_jack smartq_jack;
  72. static struct snd_soc_jack_pin smartq_jack_pins[] = {
  73. /* Disable speaker when headphone is plugged in */
  74. {
  75. .pin = "Internal Speaker",
  76. .mask = SND_JACK_HEADPHONE,
  77. },
  78. };
  79. static struct snd_soc_jack_gpio smartq_jack_gpios[] = {
  80. {
  81. .gpio = -1,
  82. .name = "headphone detect",
  83. .report = SND_JACK_HEADPHONE,
  84. .debounce_time = 200,
  85. },
  86. };
  87. static const struct snd_kcontrol_new wm8987_smartq_controls[] = {
  88. SOC_DAPM_PIN_SWITCH("Internal Speaker"),
  89. SOC_DAPM_PIN_SWITCH("Headphone Jack"),
  90. SOC_DAPM_PIN_SWITCH("Internal Mic"),
  91. };
  92. static int smartq_speaker_event(struct snd_soc_dapm_widget *w,
  93. struct snd_kcontrol *k,
  94. int event)
  95. {
  96. struct gpio_desc *gpio = snd_soc_card_get_drvdata(&snd_soc_smartq);
  97. gpiod_set_value(gpio, SND_SOC_DAPM_EVENT_OFF(event));
  98. return 0;
  99. }
  100. static const struct snd_soc_dapm_widget wm8987_dapm_widgets[] = {
  101. SND_SOC_DAPM_SPK("Internal Speaker", smartq_speaker_event),
  102. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  103. SND_SOC_DAPM_MIC("Internal Mic", NULL),
  104. };
  105. static const struct snd_soc_dapm_route audio_map[] = {
  106. {"Headphone Jack", NULL, "LOUT2"},
  107. {"Headphone Jack", NULL, "ROUT2"},
  108. {"Internal Speaker", NULL, "LOUT2"},
  109. {"Internal Speaker", NULL, "ROUT2"},
  110. {"Mic Bias", NULL, "Internal Mic"},
  111. {"LINPUT2", NULL, "Mic Bias"},
  112. };
  113. static int smartq_wm8987_init(struct snd_soc_pcm_runtime *rtd)
  114. {
  115. struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
  116. int err = 0;
  117. /* set endpoints to not connected */
  118. snd_soc_dapm_nc_pin(dapm, "LINPUT1");
  119. snd_soc_dapm_nc_pin(dapm, "RINPUT1");
  120. snd_soc_dapm_nc_pin(dapm, "OUT3");
  121. snd_soc_dapm_nc_pin(dapm, "ROUT1");
  122. /* Headphone jack detection */
  123. err = snd_soc_card_jack_new(rtd->card, "Headphone Jack",
  124. SND_JACK_HEADPHONE, &smartq_jack,
  125. smartq_jack_pins,
  126. ARRAY_SIZE(smartq_jack_pins));
  127. if (err)
  128. return err;
  129. err = snd_soc_jack_add_gpios(&smartq_jack,
  130. ARRAY_SIZE(smartq_jack_gpios),
  131. smartq_jack_gpios);
  132. return err;
  133. }
  134. static int smartq_wm8987_card_remove(struct snd_soc_card *card)
  135. {
  136. snd_soc_jack_free_gpios(&smartq_jack, ARRAY_SIZE(smartq_jack_gpios),
  137. smartq_jack_gpios);
  138. return 0;
  139. }
  140. static struct snd_soc_dai_link smartq_dai[] = {
  141. {
  142. .name = "wm8987",
  143. .stream_name = "SmartQ Hi-Fi",
  144. .cpu_dai_name = "samsung-i2s.0",
  145. .codec_dai_name = "wm8750-hifi",
  146. .platform_name = "samsung-i2s.0",
  147. .codec_name = "wm8750.0-0x1a",
  148. .init = smartq_wm8987_init,
  149. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  150. SND_SOC_DAIFMT_CBS_CFS,
  151. .ops = &smartq_hifi_ops,
  152. },
  153. };
  154. static struct snd_soc_card snd_soc_smartq = {
  155. .name = "SmartQ",
  156. .owner = THIS_MODULE,
  157. .remove = smartq_wm8987_card_remove,
  158. .dai_link = smartq_dai,
  159. .num_links = ARRAY_SIZE(smartq_dai),
  160. .dapm_widgets = wm8987_dapm_widgets,
  161. .num_dapm_widgets = ARRAY_SIZE(wm8987_dapm_widgets),
  162. .dapm_routes = audio_map,
  163. .num_dapm_routes = ARRAY_SIZE(audio_map),
  164. .controls = wm8987_smartq_controls,
  165. .num_controls = ARRAY_SIZE(wm8987_smartq_controls),
  166. };
  167. static int smartq_probe(struct platform_device *pdev)
  168. {
  169. struct gpio_desc *gpio;
  170. int ret;
  171. platform_set_drvdata(pdev, &snd_soc_smartq);
  172. /* Initialise GPIOs used by amplifiers */
  173. gpio = devm_gpiod_get(&pdev->dev, "amplifiers shutdown",
  174. GPIOD_OUT_HIGH);
  175. if (IS_ERR(gpio)) {
  176. dev_err(&pdev->dev, "Failed to register GPK12\n");
  177. ret = PTR_ERR(gpio);
  178. goto out;
  179. }
  180. snd_soc_card_set_drvdata(&snd_soc_smartq, gpio);
  181. ret = devm_snd_soc_register_card(&pdev->dev, &snd_soc_smartq);
  182. if (ret)
  183. dev_err(&pdev->dev, "Failed to register card\n");
  184. out:
  185. return ret;
  186. }
  187. static struct platform_driver smartq_driver = {
  188. .driver = {
  189. .name = "smartq-audio",
  190. },
  191. .probe = smartq_probe,
  192. };
  193. module_platform_driver(smartq_driver);
  194. /* Module information */
  195. MODULE_AUTHOR("Maurus Cuelenaere <mcuelenaere@gmail.com>");
  196. MODULE_DESCRIPTION("ALSA SoC SmartQ WM8987");
  197. MODULE_LICENSE("GPL");