corgi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * corgi.c -- SoC audio for Corgi
  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. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/timer.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/gpio.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/soc.h>
  25. #include <asm/mach-types.h>
  26. #include <mach/corgi.h>
  27. #include <mach/audio.h>
  28. #include "../codecs/wm8731.h"
  29. #include "pxa2xx-i2s.h"
  30. #define CORGI_HP 0
  31. #define CORGI_MIC 1
  32. #define CORGI_LINE 2
  33. #define CORGI_HEADSET 3
  34. #define CORGI_HP_OFF 4
  35. #define CORGI_SPK_ON 0
  36. #define CORGI_SPK_OFF 1
  37. /* audio clock in Hz - rounded from 12.235MHz */
  38. #define CORGI_AUDIO_CLOCK 12288000
  39. static int corgi_jack_func;
  40. static int corgi_spk_func;
  41. static void corgi_ext_control(struct snd_soc_dapm_context *dapm)
  42. {
  43. snd_soc_dapm_mutex_lock(dapm);
  44. /* set up jack connection */
  45. switch (corgi_jack_func) {
  46. case CORGI_HP:
  47. /* set = unmute headphone */
  48. gpio_set_value(CORGI_GPIO_MUTE_L, 1);
  49. gpio_set_value(CORGI_GPIO_MUTE_R, 1);
  50. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
  51. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  52. snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
  53. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  54. break;
  55. case CORGI_MIC:
  56. /* reset = mute headphone */
  57. gpio_set_value(CORGI_GPIO_MUTE_L, 0);
  58. gpio_set_value(CORGI_GPIO_MUTE_R, 0);
  59. snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
  60. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  61. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  62. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  63. break;
  64. case CORGI_LINE:
  65. gpio_set_value(CORGI_GPIO_MUTE_L, 0);
  66. gpio_set_value(CORGI_GPIO_MUTE_R, 0);
  67. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
  68. snd_soc_dapm_enable_pin_unlocked(dapm, "Line Jack");
  69. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  70. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  71. break;
  72. case CORGI_HEADSET:
  73. gpio_set_value(CORGI_GPIO_MUTE_L, 0);
  74. gpio_set_value(CORGI_GPIO_MUTE_R, 1);
  75. snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
  76. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  77. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  78. snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
  79. break;
  80. }
  81. if (corgi_spk_func == CORGI_SPK_ON)
  82. snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
  83. else
  84. snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
  85. /* signal a DAPM event */
  86. snd_soc_dapm_sync_unlocked(dapm);
  87. snd_soc_dapm_mutex_unlock(dapm);
  88. }
  89. static int corgi_startup(struct snd_pcm_substream *substream)
  90. {
  91. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  92. /* check the jack status at stream startup */
  93. corgi_ext_control(&rtd->card->dapm);
  94. return 0;
  95. }
  96. /* we need to unmute the HP at shutdown as the mute burns power on corgi */
  97. static void corgi_shutdown(struct snd_pcm_substream *substream)
  98. {
  99. /* set = unmute headphone */
  100. gpio_set_value(CORGI_GPIO_MUTE_L, 1);
  101. gpio_set_value(CORGI_GPIO_MUTE_R, 1);
  102. }
  103. static int corgi_hw_params(struct snd_pcm_substream *substream,
  104. struct snd_pcm_hw_params *params)
  105. {
  106. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  107. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  108. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  109. unsigned int clk = 0;
  110. int ret = 0;
  111. switch (params_rate(params)) {
  112. case 8000:
  113. case 16000:
  114. case 48000:
  115. case 96000:
  116. clk = 12288000;
  117. break;
  118. case 11025:
  119. case 22050:
  120. case 44100:
  121. clk = 11289600;
  122. break;
  123. }
  124. /* set the codec system clock for DAC and ADC */
  125. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
  126. SND_SOC_CLOCK_IN);
  127. if (ret < 0)
  128. return ret;
  129. /* set the I2S system clock as input (unused) */
  130. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  131. SND_SOC_CLOCK_IN);
  132. if (ret < 0)
  133. return ret;
  134. return 0;
  135. }
  136. static struct snd_soc_ops corgi_ops = {
  137. .startup = corgi_startup,
  138. .hw_params = corgi_hw_params,
  139. .shutdown = corgi_shutdown,
  140. };
  141. static int corgi_get_jack(struct snd_kcontrol *kcontrol,
  142. struct snd_ctl_elem_value *ucontrol)
  143. {
  144. ucontrol->value.enumerated.item[0] = corgi_jack_func;
  145. return 0;
  146. }
  147. static int corgi_set_jack(struct snd_kcontrol *kcontrol,
  148. struct snd_ctl_elem_value *ucontrol)
  149. {
  150. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  151. if (corgi_jack_func == ucontrol->value.enumerated.item[0])
  152. return 0;
  153. corgi_jack_func = ucontrol->value.enumerated.item[0];
  154. corgi_ext_control(&card->dapm);
  155. return 1;
  156. }
  157. static int corgi_get_spk(struct snd_kcontrol *kcontrol,
  158. struct snd_ctl_elem_value *ucontrol)
  159. {
  160. ucontrol->value.enumerated.item[0] = corgi_spk_func;
  161. return 0;
  162. }
  163. static int corgi_set_spk(struct snd_kcontrol *kcontrol,
  164. struct snd_ctl_elem_value *ucontrol)
  165. {
  166. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  167. if (corgi_spk_func == ucontrol->value.enumerated.item[0])
  168. return 0;
  169. corgi_spk_func = ucontrol->value.enumerated.item[0];
  170. corgi_ext_control(&card->dapm);
  171. return 1;
  172. }
  173. static int corgi_amp_event(struct snd_soc_dapm_widget *w,
  174. struct snd_kcontrol *k, int event)
  175. {
  176. gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
  177. return 0;
  178. }
  179. static int corgi_mic_event(struct snd_soc_dapm_widget *w,
  180. struct snd_kcontrol *k, int event)
  181. {
  182. gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
  183. return 0;
  184. }
  185. /* corgi machine dapm widgets */
  186. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  187. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  188. SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
  189. SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
  190. SND_SOC_DAPM_LINE("Line Jack", NULL),
  191. SND_SOC_DAPM_HP("Headset Jack", NULL),
  192. };
  193. /* Corgi machine audio map (connections to the codec pins) */
  194. static const struct snd_soc_dapm_route corgi_audio_map[] = {
  195. /* headset Jack - in = micin, out = LHPOUT*/
  196. {"Headset Jack", NULL, "LHPOUT"},
  197. /* headphone connected to LHPOUT1, RHPOUT1 */
  198. {"Headphone Jack", NULL, "LHPOUT"},
  199. {"Headphone Jack", NULL, "RHPOUT"},
  200. /* speaker connected to LOUT, ROUT */
  201. {"Ext Spk", NULL, "ROUT"},
  202. {"Ext Spk", NULL, "LOUT"},
  203. /* mic is connected to MICIN (via right channel of headphone jack) */
  204. {"MICIN", NULL, "Mic Jack"},
  205. /* Same as the above but no mic bias for line signals */
  206. {"MICIN", NULL, "Line Jack"},
  207. };
  208. static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
  209. "Off"};
  210. static const char *spk_function[] = {"On", "Off"};
  211. static const struct soc_enum corgi_enum[] = {
  212. SOC_ENUM_SINGLE_EXT(5, jack_function),
  213. SOC_ENUM_SINGLE_EXT(2, spk_function),
  214. };
  215. static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
  216. SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
  217. corgi_set_jack),
  218. SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
  219. corgi_set_spk),
  220. };
  221. /* corgi digital audio interface glue - connects codec <--> CPU */
  222. static struct snd_soc_dai_link corgi_dai = {
  223. .name = "WM8731",
  224. .stream_name = "WM8731",
  225. .cpu_dai_name = "pxa2xx-i2s",
  226. .codec_dai_name = "wm8731-hifi",
  227. .platform_name = "pxa-pcm-audio",
  228. .codec_name = "wm8731.0-001b",
  229. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  230. SND_SOC_DAIFMT_CBS_CFS,
  231. .ops = &corgi_ops,
  232. };
  233. /* corgi audio machine driver */
  234. static struct snd_soc_card corgi = {
  235. .name = "Corgi",
  236. .owner = THIS_MODULE,
  237. .dai_link = &corgi_dai,
  238. .num_links = 1,
  239. .controls = wm8731_corgi_controls,
  240. .num_controls = ARRAY_SIZE(wm8731_corgi_controls),
  241. .dapm_widgets = wm8731_dapm_widgets,
  242. .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
  243. .dapm_routes = corgi_audio_map,
  244. .num_dapm_routes = ARRAY_SIZE(corgi_audio_map),
  245. .fully_routed = true,
  246. };
  247. static int corgi_probe(struct platform_device *pdev)
  248. {
  249. struct snd_soc_card *card = &corgi;
  250. int ret;
  251. card->dev = &pdev->dev;
  252. ret = devm_snd_soc_register_card(&pdev->dev, card);
  253. if (ret)
  254. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  255. ret);
  256. return ret;
  257. }
  258. static struct platform_driver corgi_driver = {
  259. .driver = {
  260. .name = "corgi-audio",
  261. .pm = &snd_soc_pm_ops,
  262. },
  263. .probe = corgi_probe,
  264. };
  265. module_platform_driver(corgi_driver);
  266. /* Module information */
  267. MODULE_AUTHOR("Richard Purdie");
  268. MODULE_DESCRIPTION("ALSA SoC Corgi");
  269. MODULE_LICENSE("GPL");
  270. MODULE_ALIAS("platform:corgi-audio");