corgi.c 8.9 KB

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