corgi.c 8.9 KB

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