tosa.c 7.1 KB

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