tosa.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * tosa.c -- SoC audio for Tosa
  4. *
  5. * Copyright 2005 Wolfson Microelectronics PLC.
  6. * Copyright 2005 Openedhand Ltd.
  7. *
  8. * Authors: Liam Girdwood <[email protected]>
  9. * Richard Purdie <[email protected]>
  10. *
  11. * GPIO's
  12. * 1 - Jack Insertion
  13. * 5 - Hookswitch (headset answer/hang up switch)
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/device.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/soc.h>
  22. #include <asm/mach-types.h>
  23. #include <linux/platform_data/asoc-pxa.h>
  24. #define TOSA_HP 0
  25. #define TOSA_MIC_INT 1
  26. #define TOSA_HEADSET 2
  27. #define TOSA_HP_OFF 3
  28. #define TOSA_SPK_ON 0
  29. #define TOSA_SPK_OFF 1
  30. static struct gpio_desc *tosa_mute;
  31. static int tosa_jack_func;
  32. static int tosa_spk_func;
  33. static void tosa_ext_control(struct snd_soc_dapm_context *dapm)
  34. {
  35. snd_soc_dapm_mutex_lock(dapm);
  36. /* set up jack connection */
  37. switch (tosa_jack_func) {
  38. case TOSA_HP:
  39. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
  40. snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
  41. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  42. break;
  43. case TOSA_MIC_INT:
  44. snd_soc_dapm_enable_pin_unlocked(dapm, "Mic (Internal)");
  45. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  46. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  47. break;
  48. case TOSA_HEADSET:
  49. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic (Internal)");
  50. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  51. snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
  52. break;
  53. }
  54. if (tosa_spk_func == TOSA_SPK_ON)
  55. snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
  56. else
  57. snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
  58. snd_soc_dapm_sync_unlocked(dapm);
  59. snd_soc_dapm_mutex_unlock(dapm);
  60. }
  61. static int tosa_startup(struct snd_pcm_substream *substream)
  62. {
  63. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  64. /* check the jack status at stream startup */
  65. tosa_ext_control(&rtd->card->dapm);
  66. return 0;
  67. }
  68. static const struct snd_soc_ops tosa_ops = {
  69. .startup = tosa_startup,
  70. };
  71. static int tosa_get_jack(struct snd_kcontrol *kcontrol,
  72. struct snd_ctl_elem_value *ucontrol)
  73. {
  74. ucontrol->value.enumerated.item[0] = tosa_jack_func;
  75. return 0;
  76. }
  77. static int tosa_set_jack(struct snd_kcontrol *kcontrol,
  78. struct snd_ctl_elem_value *ucontrol)
  79. {
  80. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  81. if (tosa_jack_func == ucontrol->value.enumerated.item[0])
  82. return 0;
  83. tosa_jack_func = ucontrol->value.enumerated.item[0];
  84. tosa_ext_control(&card->dapm);
  85. return 1;
  86. }
  87. static int tosa_get_spk(struct snd_kcontrol *kcontrol,
  88. struct snd_ctl_elem_value *ucontrol)
  89. {
  90. ucontrol->value.enumerated.item[0] = tosa_spk_func;
  91. return 0;
  92. }
  93. static int tosa_set_spk(struct snd_kcontrol *kcontrol,
  94. struct snd_ctl_elem_value *ucontrol)
  95. {
  96. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  97. if (tosa_spk_func == ucontrol->value.enumerated.item[0])
  98. return 0;
  99. tosa_spk_func = ucontrol->value.enumerated.item[0];
  100. tosa_ext_control(&card->dapm);
  101. return 1;
  102. }
  103. /* tosa dapm event handlers */
  104. static int tosa_hp_event(struct snd_soc_dapm_widget *w,
  105. struct snd_kcontrol *k, int event)
  106. {
  107. gpiod_set_value(tosa_mute, SND_SOC_DAPM_EVENT_ON(event) ? 1 : 0);
  108. return 0;
  109. }
  110. /* tosa machine dapm widgets */
  111. static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = {
  112. SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event),
  113. SND_SOC_DAPM_HP("Headset Jack", NULL),
  114. SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
  115. SND_SOC_DAPM_SPK("Speaker", NULL),
  116. };
  117. /* tosa audio map */
  118. static const struct snd_soc_dapm_route audio_map[] = {
  119. /* headphone connected to HPOUTL, HPOUTR */
  120. {"Headphone Jack", NULL, "HPOUTL"},
  121. {"Headphone Jack", NULL, "HPOUTR"},
  122. /* ext speaker connected to LOUT2, ROUT2 */
  123. {"Speaker", NULL, "LOUT2"},
  124. {"Speaker", NULL, "ROUT2"},
  125. /* internal mic is connected to mic1, mic2 differential - with bias */
  126. {"MIC1", NULL, "Mic Bias"},
  127. {"MIC2", NULL, "Mic Bias"},
  128. {"Mic Bias", NULL, "Mic (Internal)"},
  129. /* headset is connected to HPOUTR, and LINEINR with bias */
  130. {"Headset Jack", NULL, "HPOUTR"},
  131. {"LINEINR", NULL, "Mic Bias"},
  132. {"Mic Bias", NULL, "Headset Jack"},
  133. };
  134. static const char * const jack_function[] = {"Headphone", "Mic", "Line",
  135. "Headset", "Off"};
  136. static const char * const spk_function[] = {"On", "Off"};
  137. static const struct soc_enum tosa_enum[] = {
  138. SOC_ENUM_SINGLE_EXT(5, jack_function),
  139. SOC_ENUM_SINGLE_EXT(2, spk_function),
  140. };
  141. static const struct snd_kcontrol_new tosa_controls[] = {
  142. SOC_ENUM_EXT("Jack Function", tosa_enum[0], tosa_get_jack,
  143. tosa_set_jack),
  144. SOC_ENUM_EXT("Speaker Function", tosa_enum[1], tosa_get_spk,
  145. tosa_set_spk),
  146. };
  147. SND_SOC_DAILINK_DEFS(ac97,
  148. DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-ac97")),
  149. DAILINK_COMP_ARRAY(COMP_CODEC("wm9712-codec", "wm9712-hifi")),
  150. DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
  151. SND_SOC_DAILINK_DEFS(ac97_aux,
  152. DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-ac97-aux")),
  153. DAILINK_COMP_ARRAY(COMP_CODEC("wm9712-codec", "wm9712-aux")),
  154. DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
  155. static struct snd_soc_dai_link tosa_dai[] = {
  156. {
  157. .name = "AC97",
  158. .stream_name = "AC97 HiFi",
  159. .ops = &tosa_ops,
  160. SND_SOC_DAILINK_REG(ac97),
  161. },
  162. {
  163. .name = "AC97 Aux",
  164. .stream_name = "AC97 Aux",
  165. .ops = &tosa_ops,
  166. SND_SOC_DAILINK_REG(ac97_aux),
  167. },
  168. };
  169. static struct snd_soc_card tosa = {
  170. .name = "Tosa",
  171. .owner = THIS_MODULE,
  172. .dai_link = tosa_dai,
  173. .num_links = ARRAY_SIZE(tosa_dai),
  174. .controls = tosa_controls,
  175. .num_controls = ARRAY_SIZE(tosa_controls),
  176. .dapm_widgets = tosa_dapm_widgets,
  177. .num_dapm_widgets = ARRAY_SIZE(tosa_dapm_widgets),
  178. .dapm_routes = audio_map,
  179. .num_dapm_routes = ARRAY_SIZE(audio_map),
  180. .fully_routed = true,
  181. };
  182. static int tosa_probe(struct platform_device *pdev)
  183. {
  184. struct snd_soc_card *card = &tosa;
  185. int ret;
  186. tosa_mute = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
  187. if (IS_ERR(tosa_mute))
  188. return dev_err_probe(&pdev->dev, PTR_ERR(tosa_mute),
  189. "failed to get L_MUTE GPIO\n");
  190. gpiod_set_consumer_name(tosa_mute, "Headphone Jack");
  191. card->dev = &pdev->dev;
  192. ret = devm_snd_soc_register_card(&pdev->dev, card);
  193. if (ret) {
  194. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  195. ret);
  196. }
  197. return ret;
  198. }
  199. static struct platform_driver tosa_driver = {
  200. .driver = {
  201. .name = "tosa-audio",
  202. .pm = &snd_soc_pm_ops,
  203. },
  204. .probe = tosa_probe,
  205. };
  206. module_platform_driver(tosa_driver);
  207. /* Module information */
  208. MODULE_AUTHOR("Richard Purdie");
  209. MODULE_DESCRIPTION("ALSA SoC Tosa");
  210. MODULE_LICENSE("GPL");
  211. MODULE_ALIAS("platform:tosa-audio");