tosa.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * tosa.c -- SoC audio for Tosa
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Copyright 2005 Openedhand Ltd.
  6. *
  7. * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
  8. * Richard Purdie <richard@openedhand.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * GPIO's
  16. * 1 - Jack Insertion
  17. * 5 - Hookswitch (headset answer/hang up switch)
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/device.h>
  23. #include <linux/gpio.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/soc.h>
  27. #include <asm/mach-types.h>
  28. #include <mach/tosa.h>
  29. #include <mach/audio.h>
  30. #include "../codecs/wm9712.h"
  31. #include "pxa2xx-ac97.h"
  32. #define TOSA_HP 0
  33. #define TOSA_MIC_INT 1
  34. #define TOSA_HEADSET 2
  35. #define TOSA_HP_OFF 3
  36. #define TOSA_SPK_ON 0
  37. #define TOSA_SPK_OFF 1
  38. static int tosa_jack_func;
  39. static int tosa_spk_func;
  40. static void tosa_ext_control(struct snd_soc_codec *codec)
  41. {
  42. struct snd_soc_dapm_context *dapm = &codec->dapm;
  43. /* set up jack connection */
  44. switch (tosa_jack_func) {
  45. case TOSA_HP:
  46. snd_soc_dapm_disable_pin(dapm, "Mic (Internal)");
  47. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  48. snd_soc_dapm_disable_pin(dapm, "Headset Jack");
  49. break;
  50. case TOSA_MIC_INT:
  51. snd_soc_dapm_enable_pin(dapm, "Mic (Internal)");
  52. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  53. snd_soc_dapm_disable_pin(dapm, "Headset Jack");
  54. break;
  55. case TOSA_HEADSET:
  56. snd_soc_dapm_disable_pin(dapm, "Mic (Internal)");
  57. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  58. snd_soc_dapm_enable_pin(dapm, "Headset Jack");
  59. break;
  60. }
  61. if (tosa_spk_func == TOSA_SPK_ON)
  62. snd_soc_dapm_enable_pin(dapm, "Speaker");
  63. else
  64. snd_soc_dapm_disable_pin(dapm, "Speaker");
  65. snd_soc_dapm_sync(dapm);
  66. }
  67. static int tosa_startup(struct snd_pcm_substream *substream)
  68. {
  69. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  70. struct snd_soc_codec *codec = rtd->codec;
  71. mutex_lock(&codec->mutex);
  72. /* check the jack status at stream startup */
  73. tosa_ext_control(codec);
  74. mutex_unlock(&codec->mutex);
  75. return 0;
  76. }
  77. static struct snd_soc_ops tosa_ops = {
  78. .startup = tosa_startup,
  79. };
  80. static int tosa_get_jack(struct snd_kcontrol *kcontrol,
  81. struct snd_ctl_elem_value *ucontrol)
  82. {
  83. ucontrol->value.integer.value[0] = tosa_jack_func;
  84. return 0;
  85. }
  86. static int tosa_set_jack(struct snd_kcontrol *kcontrol,
  87. struct snd_ctl_elem_value *ucontrol)
  88. {
  89. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  90. if (tosa_jack_func == ucontrol->value.integer.value[0])
  91. return 0;
  92. tosa_jack_func = ucontrol->value.integer.value[0];
  93. tosa_ext_control(codec);
  94. return 1;
  95. }
  96. static int tosa_get_spk(struct snd_kcontrol *kcontrol,
  97. struct snd_ctl_elem_value *ucontrol)
  98. {
  99. ucontrol->value.integer.value[0] = tosa_spk_func;
  100. return 0;
  101. }
  102. static int tosa_set_spk(struct snd_kcontrol *kcontrol,
  103. struct snd_ctl_elem_value *ucontrol)
  104. {
  105. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  106. if (tosa_spk_func == ucontrol->value.integer.value[0])
  107. return 0;
  108. tosa_spk_func = ucontrol->value.integer.value[0];
  109. tosa_ext_control(codec);
  110. return 1;
  111. }
  112. /* tosa dapm event handlers */
  113. static int tosa_hp_event(struct snd_soc_dapm_widget *w,
  114. struct snd_kcontrol *k, int event)
  115. {
  116. gpio_set_value(TOSA_GPIO_L_MUTE, SND_SOC_DAPM_EVENT_ON(event) ? 1 :0);
  117. return 0;
  118. }
  119. /* tosa machine dapm widgets */
  120. static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = {
  121. SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event),
  122. SND_SOC_DAPM_HP("Headset Jack", NULL),
  123. SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
  124. SND_SOC_DAPM_SPK("Speaker", NULL),
  125. };
  126. /* tosa audio map */
  127. static const struct snd_soc_dapm_route audio_map[] = {
  128. /* headphone connected to HPOUTL, HPOUTR */
  129. {"Headphone Jack", NULL, "HPOUTL"},
  130. {"Headphone Jack", NULL, "HPOUTR"},
  131. /* ext speaker connected to LOUT2, ROUT2 */
  132. {"Speaker", NULL, "LOUT2"},
  133. {"Speaker", NULL, "ROUT2"},
  134. /* internal mic is connected to mic1, mic2 differential - with bias */
  135. {"MIC1", NULL, "Mic Bias"},
  136. {"MIC2", NULL, "Mic Bias"},
  137. {"Mic Bias", NULL, "Mic (Internal)"},
  138. /* headset is connected to HPOUTR, and LINEINR with bias */
  139. {"Headset Jack", NULL, "HPOUTR"},
  140. {"LINEINR", NULL, "Mic Bias"},
  141. {"Mic Bias", NULL, "Headset Jack"},
  142. };
  143. static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
  144. "Off"};
  145. static const char *spk_function[] = {"On", "Off"};
  146. static const struct soc_enum tosa_enum[] = {
  147. SOC_ENUM_SINGLE_EXT(5, jack_function),
  148. SOC_ENUM_SINGLE_EXT(2, spk_function),
  149. };
  150. static const struct snd_kcontrol_new tosa_controls[] = {
  151. SOC_ENUM_EXT("Jack Function", tosa_enum[0], tosa_get_jack,
  152. tosa_set_jack),
  153. SOC_ENUM_EXT("Speaker Function", tosa_enum[1], tosa_get_spk,
  154. tosa_set_spk),
  155. };
  156. static int tosa_ac97_init(struct snd_soc_pcm_runtime *rtd)
  157. {
  158. struct snd_soc_codec *codec = rtd->codec;
  159. struct snd_soc_dapm_context *dapm = &codec->dapm;
  160. int err;
  161. snd_soc_dapm_nc_pin(dapm, "OUT3");
  162. snd_soc_dapm_nc_pin(dapm, "MONOOUT");
  163. /* add tosa specific controls */
  164. err = snd_soc_add_codec_controls(codec, tosa_controls,
  165. ARRAY_SIZE(tosa_controls));
  166. if (err < 0)
  167. return err;
  168. /* add tosa specific widgets */
  169. snd_soc_dapm_new_controls(dapm, tosa_dapm_widgets,
  170. ARRAY_SIZE(tosa_dapm_widgets));
  171. /* set up tosa specific audio path audio_map */
  172. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  173. return 0;
  174. }
  175. static struct snd_soc_dai_link tosa_dai[] = {
  176. {
  177. .name = "AC97",
  178. .stream_name = "AC97 HiFi",
  179. .cpu_dai_name = "pxa2xx-ac97",
  180. .codec_dai_name = "wm9712-hifi",
  181. .platform_name = "pxa-pcm-audio",
  182. .codec_name = "wm9712-codec",
  183. .init = tosa_ac97_init,
  184. .ops = &tosa_ops,
  185. },
  186. {
  187. .name = "AC97 Aux",
  188. .stream_name = "AC97 Aux",
  189. .cpu_dai_name = "pxa2xx-ac97-aux",
  190. .codec_dai_name = "wm9712-aux",
  191. .platform_name = "pxa-pcm-audio",
  192. .codec_name = "wm9712-codec",
  193. .ops = &tosa_ops,
  194. },
  195. };
  196. static struct snd_soc_card tosa = {
  197. .name = "Tosa",
  198. .owner = THIS_MODULE,
  199. .dai_link = tosa_dai,
  200. .num_links = ARRAY_SIZE(tosa_dai),
  201. };
  202. static int __devinit tosa_probe(struct platform_device *pdev)
  203. {
  204. struct snd_soc_card *card = &tosa;
  205. int ret;
  206. ret = gpio_request_one(TOSA_GPIO_L_MUTE, GPIOF_OUT_INIT_LOW,
  207. "Headphone Jack");
  208. if (ret)
  209. return ret;
  210. card->dev = &pdev->dev;
  211. ret = snd_soc_register_card(card);
  212. if (ret) {
  213. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  214. ret);
  215. gpio_free(TOSA_GPIO_L_MUTE);
  216. }
  217. return ret;
  218. }
  219. static int __devexit tosa_remove(struct platform_device *pdev)
  220. {
  221. struct snd_soc_card *card = platform_get_drvdata(pdev);
  222. gpio_free(TOSA_GPIO_L_MUTE);
  223. snd_soc_unregister_card(card);
  224. return 0;
  225. }
  226. static struct platform_driver tosa_driver = {
  227. .driver = {
  228. .name = "tosa-audio",
  229. .owner = THIS_MODULE,
  230. },
  231. .probe = tosa_probe,
  232. .remove = __devexit_p(tosa_remove),
  233. };
  234. module_platform_driver(tosa_driver);
  235. /* Module information */
  236. MODULE_AUTHOR("Richard Purdie");
  237. MODULE_DESCRIPTION("ALSA SoC Tosa");
  238. MODULE_LICENSE("GPL");
  239. MODULE_ALIAS("platform:tosa-audio");