tosa.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #define TOSA_HP 0
  31. #define TOSA_MIC_INT 1
  32. #define TOSA_HEADSET 2
  33. #define TOSA_HP_OFF 3
  34. #define TOSA_SPK_ON 0
  35. #define TOSA_SPK_OFF 1
  36. static int tosa_jack_func;
  37. static int tosa_spk_func;
  38. static void tosa_ext_control(struct snd_soc_dapm_context *dapm)
  39. {
  40. snd_soc_dapm_mutex_lock(dapm);
  41. /* set up jack connection */
  42. switch (tosa_jack_func) {
  43. case TOSA_HP:
  44. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
  45. snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
  46. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  47. break;
  48. case TOSA_MIC_INT:
  49. snd_soc_dapm_enable_pin_unlocked(dapm, "Mic (Internal)");
  50. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  51. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  52. break;
  53. case TOSA_HEADSET:
  54. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
  55. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  56. snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
  57. break;
  58. }
  59. if (tosa_spk_func == TOSA_SPK_ON)
  60. snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
  61. else
  62. snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
  63. snd_soc_dapm_sync_unlocked(dapm);
  64. snd_soc_dapm_mutex_unlock(dapm);
  65. }
  66. static int tosa_startup(struct snd_pcm_substream *substream)
  67. {
  68. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  69. /* check the jack status at stream startup */
  70. tosa_ext_control(&rtd->card->dapm);
  71. return 0;
  72. }
  73. static const struct snd_soc_ops tosa_ops = {
  74. .startup = tosa_startup,
  75. };
  76. static int tosa_get_jack(struct snd_kcontrol *kcontrol,
  77. struct snd_ctl_elem_value *ucontrol)
  78. {
  79. ucontrol->value.enumerated.item[0] = tosa_jack_func;
  80. return 0;
  81. }
  82. static int tosa_set_jack(struct snd_kcontrol *kcontrol,
  83. struct snd_ctl_elem_value *ucontrol)
  84. {
  85. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  86. if (tosa_jack_func == ucontrol->value.enumerated.item[0])
  87. return 0;
  88. tosa_jack_func = ucontrol->value.enumerated.item[0];
  89. tosa_ext_control(&card->dapm);
  90. return 1;
  91. }
  92. static int tosa_get_spk(struct snd_kcontrol *kcontrol,
  93. struct snd_ctl_elem_value *ucontrol)
  94. {
  95. ucontrol->value.enumerated.item[0] = tosa_spk_func;
  96. return 0;
  97. }
  98. static int tosa_set_spk(struct snd_kcontrol *kcontrol,
  99. struct snd_ctl_elem_value *ucontrol)
  100. {
  101. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  102. if (tosa_spk_func == ucontrol->value.enumerated.item[0])
  103. return 0;
  104. tosa_spk_func = ucontrol->value.enumerated.item[0];
  105. tosa_ext_control(&card->dapm);
  106. return 1;
  107. }
  108. /* tosa dapm event handlers */
  109. static int tosa_hp_event(struct snd_soc_dapm_widget *w,
  110. struct snd_kcontrol *k, int event)
  111. {
  112. gpio_set_value(TOSA_GPIO_L_MUTE, SND_SOC_DAPM_EVENT_ON(event) ? 1 : 0);
  113. return 0;
  114. }
  115. /* tosa machine dapm widgets */
  116. static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = {
  117. SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event),
  118. SND_SOC_DAPM_HP("Headset Jack", NULL),
  119. SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
  120. SND_SOC_DAPM_SPK("Speaker", NULL),
  121. };
  122. /* tosa audio map */
  123. static const struct snd_soc_dapm_route audio_map[] = {
  124. /* headphone connected to HPOUTL, HPOUTR */
  125. {"Headphone Jack", NULL, "HPOUTL"},
  126. {"Headphone Jack", NULL, "HPOUTR"},
  127. /* ext speaker connected to LOUT2, ROUT2 */
  128. {"Speaker", NULL, "LOUT2"},
  129. {"Speaker", NULL, "ROUT2"},
  130. /* internal mic is connected to mic1, mic2 differential - with bias */
  131. {"MIC1", NULL, "Mic Bias"},
  132. {"MIC2", NULL, "Mic Bias"},
  133. {"Mic Bias", NULL, "Mic (Internal)"},
  134. /* headset is connected to HPOUTR, and LINEINR with bias */
  135. {"Headset Jack", NULL, "HPOUTR"},
  136. {"LINEINR", NULL, "Mic Bias"},
  137. {"Mic Bias", NULL, "Headset Jack"},
  138. };
  139. static const char * const jack_function[] = {"Headphone", "Mic", "Line",
  140. "Headset", "Off"};
  141. static const char * const spk_function[] = {"On", "Off"};
  142. static const struct soc_enum tosa_enum[] = {
  143. SOC_ENUM_SINGLE_EXT(5, jack_function),
  144. SOC_ENUM_SINGLE_EXT(2, spk_function),
  145. };
  146. static const struct snd_kcontrol_new tosa_controls[] = {
  147. SOC_ENUM_EXT("Jack Function", tosa_enum[0], tosa_get_jack,
  148. tosa_set_jack),
  149. SOC_ENUM_EXT("Speaker Function", tosa_enum[1], tosa_get_spk,
  150. tosa_set_spk),
  151. };
  152. static struct snd_soc_dai_link tosa_dai[] = {
  153. {
  154. .name = "AC97",
  155. .stream_name = "AC97 HiFi",
  156. .cpu_dai_name = "pxa2xx-ac97",
  157. .codec_dai_name = "wm9712-hifi",
  158. .platform_name = "pxa-pcm-audio",
  159. .codec_name = "wm9712-codec",
  160. .ops = &tosa_ops,
  161. },
  162. {
  163. .name = "AC97 Aux",
  164. .stream_name = "AC97 Aux",
  165. .cpu_dai_name = "pxa2xx-ac97-aux",
  166. .codec_dai_name = "wm9712-aux",
  167. .platform_name = "pxa-pcm-audio",
  168. .codec_name = "wm9712-codec",
  169. .ops = &tosa_ops,
  170. },
  171. };
  172. static struct snd_soc_card tosa = {
  173. .name = "Tosa",
  174. .owner = THIS_MODULE,
  175. .dai_link = tosa_dai,
  176. .num_links = ARRAY_SIZE(tosa_dai),
  177. .controls = tosa_controls,
  178. .num_controls = ARRAY_SIZE(tosa_controls),
  179. .dapm_widgets = tosa_dapm_widgets,
  180. .num_dapm_widgets = ARRAY_SIZE(tosa_dapm_widgets),
  181. .dapm_routes = audio_map,
  182. .num_dapm_routes = ARRAY_SIZE(audio_map),
  183. .fully_routed = true,
  184. };
  185. static int tosa_probe(struct platform_device *pdev)
  186. {
  187. struct snd_soc_card *card = &tosa;
  188. int ret;
  189. ret = gpio_request_one(TOSA_GPIO_L_MUTE, GPIOF_OUT_INIT_LOW,
  190. "Headphone Jack");
  191. if (ret)
  192. return ret;
  193. card->dev = &pdev->dev;
  194. ret = devm_snd_soc_register_card(&pdev->dev, card);
  195. if (ret) {
  196. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  197. ret);
  198. gpio_free(TOSA_GPIO_L_MUTE);
  199. }
  200. return ret;
  201. }
  202. static int tosa_remove(struct platform_device *pdev)
  203. {
  204. gpio_free(TOSA_GPIO_L_MUTE);
  205. return 0;
  206. }
  207. static struct platform_driver tosa_driver = {
  208. .driver = {
  209. .name = "tosa-audio",
  210. .pm = &snd_soc_pm_ops,
  211. },
  212. .probe = tosa_probe,
  213. .remove = tosa_remove,
  214. };
  215. module_platform_driver(tosa_driver);
  216. /* Module information */
  217. MODULE_AUTHOR("Richard Purdie");
  218. MODULE_DESCRIPTION("ALSA SoC Tosa");
  219. MODULE_LICENSE("GPL");
  220. MODULE_ALIAS("platform:tosa-audio");