tosa.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. static struct snd_soc_card tosa;
  33. #define TOSA_HP 0
  34. #define TOSA_MIC_INT 1
  35. #define TOSA_HEADSET 2
  36. #define TOSA_HP_OFF 3
  37. #define TOSA_SPK_ON 0
  38. #define TOSA_SPK_OFF 1
  39. static int tosa_jack_func;
  40. static int tosa_spk_func;
  41. static void tosa_ext_control(struct snd_soc_codec *codec)
  42. {
  43. struct snd_soc_dapm_context *dapm = &codec->dapm;
  44. /* set up jack connection */
  45. switch (tosa_jack_func) {
  46. case TOSA_HP:
  47. snd_soc_dapm_disable_pin(dapm, "Mic (Internal)");
  48. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  49. snd_soc_dapm_disable_pin(dapm, "Headset Jack");
  50. break;
  51. case TOSA_MIC_INT:
  52. snd_soc_dapm_enable_pin(dapm, "Mic (Internal)");
  53. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  54. snd_soc_dapm_disable_pin(dapm, "Headset Jack");
  55. break;
  56. case TOSA_HEADSET:
  57. snd_soc_dapm_disable_pin(dapm, "Mic (Internal)");
  58. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  59. snd_soc_dapm_enable_pin(dapm, "Headset Jack");
  60. break;
  61. }
  62. if (tosa_spk_func == TOSA_SPK_ON)
  63. snd_soc_dapm_enable_pin(dapm, "Speaker");
  64. else
  65. snd_soc_dapm_disable_pin(dapm, "Speaker");
  66. snd_soc_dapm_sync(dapm);
  67. }
  68. static int tosa_startup(struct snd_pcm_substream *substream)
  69. {
  70. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  71. struct snd_soc_codec *codec = rtd->codec;
  72. mutex_lock(&codec->mutex);
  73. /* check the jack status at stream startup */
  74. tosa_ext_control(codec);
  75. mutex_unlock(&codec->mutex);
  76. return 0;
  77. }
  78. static struct snd_soc_ops tosa_ops = {
  79. .startup = tosa_startup,
  80. };
  81. static int tosa_get_jack(struct snd_kcontrol *kcontrol,
  82. struct snd_ctl_elem_value *ucontrol)
  83. {
  84. ucontrol->value.integer.value[0] = tosa_jack_func;
  85. return 0;
  86. }
  87. static int tosa_set_jack(struct snd_kcontrol *kcontrol,
  88. struct snd_ctl_elem_value *ucontrol)
  89. {
  90. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  91. if (tosa_jack_func == ucontrol->value.integer.value[0])
  92. return 0;
  93. tosa_jack_func = ucontrol->value.integer.value[0];
  94. tosa_ext_control(codec);
  95. return 1;
  96. }
  97. static int tosa_get_spk(struct snd_kcontrol *kcontrol,
  98. struct snd_ctl_elem_value *ucontrol)
  99. {
  100. ucontrol->value.integer.value[0] = tosa_spk_func;
  101. return 0;
  102. }
  103. static int tosa_set_spk(struct snd_kcontrol *kcontrol,
  104. struct snd_ctl_elem_value *ucontrol)
  105. {
  106. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  107. if (tosa_spk_func == ucontrol->value.integer.value[0])
  108. return 0;
  109. tosa_spk_func = ucontrol->value.integer.value[0];
  110. tosa_ext_control(codec);
  111. return 1;
  112. }
  113. /* tosa dapm event handlers */
  114. static int tosa_hp_event(struct snd_soc_dapm_widget *w,
  115. struct snd_kcontrol *k, int event)
  116. {
  117. gpio_set_value(TOSA_GPIO_L_MUTE, SND_SOC_DAPM_EVENT_ON(event) ? 1 :0);
  118. return 0;
  119. }
  120. /* tosa machine dapm widgets */
  121. static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = {
  122. SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event),
  123. SND_SOC_DAPM_HP("Headset Jack", NULL),
  124. SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
  125. SND_SOC_DAPM_SPK("Speaker", NULL),
  126. };
  127. /* tosa audio map */
  128. static const struct snd_soc_dapm_route audio_map[] = {
  129. /* headphone connected to HPOUTL, HPOUTR */
  130. {"Headphone Jack", NULL, "HPOUTL"},
  131. {"Headphone Jack", NULL, "HPOUTR"},
  132. /* ext speaker connected to LOUT2, ROUT2 */
  133. {"Speaker", NULL, "LOUT2"},
  134. {"Speaker", NULL, "ROUT2"},
  135. /* internal mic is connected to mic1, mic2 differential - with bias */
  136. {"MIC1", NULL, "Mic Bias"},
  137. {"MIC2", NULL, "Mic Bias"},
  138. {"Mic Bias", NULL, "Mic (Internal)"},
  139. /* headset is connected to HPOUTR, and LINEINR with bias */
  140. {"Headset Jack", NULL, "HPOUTR"},
  141. {"LINEINR", NULL, "Mic Bias"},
  142. {"Mic Bias", NULL, "Headset Jack"},
  143. };
  144. static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
  145. "Off"};
  146. static const char *spk_function[] = {"On", "Off"};
  147. static const struct soc_enum tosa_enum[] = {
  148. SOC_ENUM_SINGLE_EXT(5, jack_function),
  149. SOC_ENUM_SINGLE_EXT(2, spk_function),
  150. };
  151. static const struct snd_kcontrol_new tosa_controls[] = {
  152. SOC_ENUM_EXT("Jack Function", tosa_enum[0], tosa_get_jack,
  153. tosa_set_jack),
  154. SOC_ENUM_EXT("Speaker Function", tosa_enum[1], tosa_get_spk,
  155. tosa_set_spk),
  156. };
  157. static int tosa_ac97_init(struct snd_soc_pcm_runtime *rtd)
  158. {
  159. struct snd_soc_codec *codec = rtd->codec;
  160. struct snd_soc_dapm_context *dapm = &codec->dapm;
  161. int err;
  162. snd_soc_dapm_nc_pin(dapm, "OUT3");
  163. snd_soc_dapm_nc_pin(dapm, "MONOOUT");
  164. /* add tosa specific controls */
  165. err = snd_soc_add_controls(codec, tosa_controls,
  166. ARRAY_SIZE(tosa_controls));
  167. if (err < 0)
  168. return err;
  169. /* add tosa specific widgets */
  170. snd_soc_dapm_new_controls(dapm, tosa_dapm_widgets,
  171. ARRAY_SIZE(tosa_dapm_widgets));
  172. /* set up tosa specific audio path audio_map */
  173. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  174. snd_soc_dapm_sync(dapm);
  175. return 0;
  176. }
  177. static struct snd_soc_dai_link tosa_dai[] = {
  178. {
  179. .name = "AC97",
  180. .stream_name = "AC97 HiFi",
  181. .cpu_dai_name = "pxa2xx-ac97",
  182. .codec_dai_name = "wm9712-hifi",
  183. .platform_name = "pxa-pcm-audio",
  184. .codec_name = "wm9712-codec",
  185. .init = tosa_ac97_init,
  186. .ops = &tosa_ops,
  187. },
  188. {
  189. .name = "AC97 Aux",
  190. .stream_name = "AC97 Aux",
  191. .cpu_dai_name = "pxa2xx-ac97-aux",
  192. .codec_dai_name = "wm9712-aux",
  193. .platform_name = "pxa-pcm-audio",
  194. .codec_name = "wm9712-codec",
  195. .ops = &tosa_ops,
  196. },
  197. };
  198. static int tosa_probe(struct snd_soc_card *card)
  199. {
  200. int ret;
  201. ret = gpio_request(TOSA_GPIO_L_MUTE, "Headphone Jack");
  202. if (ret)
  203. return ret;
  204. ret = gpio_direction_output(TOSA_GPIO_L_MUTE, 0);
  205. if (ret)
  206. gpio_free(TOSA_GPIO_L_MUTE);
  207. return ret;
  208. }
  209. static int tosa_remove(struct snd_soc_card *card)
  210. {
  211. gpio_free(TOSA_GPIO_L_MUTE);
  212. return 0;
  213. }
  214. static struct snd_soc_card tosa = {
  215. .name = "Tosa",
  216. .dai_link = tosa_dai,
  217. .num_links = ARRAY_SIZE(tosa_dai),
  218. .probe = tosa_probe,
  219. .remove = tosa_remove,
  220. };
  221. static struct platform_device *tosa_snd_device;
  222. static int __init tosa_init(void)
  223. {
  224. int ret;
  225. if (!machine_is_tosa())
  226. return -ENODEV;
  227. tosa_snd_device = platform_device_alloc("soc-audio", -1);
  228. if (!tosa_snd_device) {
  229. ret = -ENOMEM;
  230. goto err_alloc;
  231. }
  232. platform_set_drvdata(tosa_snd_device, &tosa);
  233. ret = platform_device_add(tosa_snd_device);
  234. if (!ret)
  235. return 0;
  236. platform_device_put(tosa_snd_device);
  237. err_alloc:
  238. return ret;
  239. }
  240. static void __exit tosa_exit(void)
  241. {
  242. platform_device_unregister(tosa_snd_device);
  243. }
  244. module_init(tosa_init);
  245. module_exit(tosa_exit);
  246. /* Module information */
  247. MODULE_AUTHOR("Richard Purdie");
  248. MODULE_DESCRIPTION("ALSA SoC Tosa");
  249. MODULE_LICENSE("GPL");