tosa.c 7.3 KB

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