corgi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 codec DAI configuration */
  127. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  128. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  129. if (ret < 0)
  130. return ret;
  131. /* set cpu DAI configuration */
  132. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  133. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  134. if (ret < 0)
  135. return ret;
  136. /* set the codec system clock for DAC and ADC */
  137. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
  138. SND_SOC_CLOCK_IN);
  139. if (ret < 0)
  140. return ret;
  141. /* set the I2S system clock as input (unused) */
  142. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  143. SND_SOC_CLOCK_IN);
  144. if (ret < 0)
  145. return ret;
  146. return 0;
  147. }
  148. static struct snd_soc_ops corgi_ops = {
  149. .startup = corgi_startup,
  150. .hw_params = corgi_hw_params,
  151. .shutdown = corgi_shutdown,
  152. };
  153. static int corgi_get_jack(struct snd_kcontrol *kcontrol,
  154. struct snd_ctl_elem_value *ucontrol)
  155. {
  156. ucontrol->value.integer.value[0] = corgi_jack_func;
  157. return 0;
  158. }
  159. static int corgi_set_jack(struct snd_kcontrol *kcontrol,
  160. struct snd_ctl_elem_value *ucontrol)
  161. {
  162. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  163. if (corgi_jack_func == ucontrol->value.integer.value[0])
  164. return 0;
  165. corgi_jack_func = ucontrol->value.integer.value[0];
  166. corgi_ext_control(codec);
  167. return 1;
  168. }
  169. static int corgi_get_spk(struct snd_kcontrol *kcontrol,
  170. struct snd_ctl_elem_value *ucontrol)
  171. {
  172. ucontrol->value.integer.value[0] = corgi_spk_func;
  173. return 0;
  174. }
  175. static int corgi_set_spk(struct snd_kcontrol *kcontrol,
  176. struct snd_ctl_elem_value *ucontrol)
  177. {
  178. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  179. if (corgi_spk_func == ucontrol->value.integer.value[0])
  180. return 0;
  181. corgi_spk_func = ucontrol->value.integer.value[0];
  182. corgi_ext_control(codec);
  183. return 1;
  184. }
  185. static int corgi_amp_event(struct snd_soc_dapm_widget *w,
  186. struct snd_kcontrol *k, int event)
  187. {
  188. gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
  189. return 0;
  190. }
  191. static int corgi_mic_event(struct snd_soc_dapm_widget *w,
  192. struct snd_kcontrol *k, int event)
  193. {
  194. gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
  195. return 0;
  196. }
  197. /* corgi machine dapm widgets */
  198. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  199. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  200. SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
  201. SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
  202. SND_SOC_DAPM_LINE("Line Jack", NULL),
  203. SND_SOC_DAPM_HP("Headset Jack", NULL),
  204. };
  205. /* Corgi machine audio map (connections to the codec pins) */
  206. static const struct snd_soc_dapm_route audio_map[] = {
  207. /* headset Jack - in = micin, out = LHPOUT*/
  208. {"Headset Jack", NULL, "LHPOUT"},
  209. /* headphone connected to LHPOUT1, RHPOUT1 */
  210. {"Headphone Jack", NULL, "LHPOUT"},
  211. {"Headphone Jack", NULL, "RHPOUT"},
  212. /* speaker connected to LOUT, ROUT */
  213. {"Ext Spk", NULL, "ROUT"},
  214. {"Ext Spk", NULL, "LOUT"},
  215. /* mic is connected to MICIN (via right channel of headphone jack) */
  216. {"MICIN", NULL, "Mic Jack"},
  217. /* Same as the above but no mic bias for line signals */
  218. {"MICIN", NULL, "Line Jack"},
  219. };
  220. static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
  221. "Off"};
  222. static const char *spk_function[] = {"On", "Off"};
  223. static const struct soc_enum corgi_enum[] = {
  224. SOC_ENUM_SINGLE_EXT(5, jack_function),
  225. SOC_ENUM_SINGLE_EXT(2, spk_function),
  226. };
  227. static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
  228. SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
  229. corgi_set_jack),
  230. SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
  231. corgi_set_spk),
  232. };
  233. /*
  234. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  235. */
  236. static int corgi_wm8731_init(struct snd_soc_pcm_runtime *rtd)
  237. {
  238. struct snd_soc_codec *codec = rtd->codec;
  239. struct snd_soc_dapm_context *dapm = &codec->dapm;
  240. int err;
  241. snd_soc_dapm_nc_pin(dapm, "LLINEIN");
  242. snd_soc_dapm_nc_pin(dapm, "RLINEIN");
  243. /* Add corgi specific controls */
  244. err = snd_soc_add_controls(codec, wm8731_corgi_controls,
  245. ARRAY_SIZE(wm8731_corgi_controls));
  246. if (err < 0)
  247. return err;
  248. /* Add corgi specific widgets */
  249. snd_soc_dapm_new_controls(dapm, wm8731_dapm_widgets,
  250. ARRAY_SIZE(wm8731_dapm_widgets));
  251. /* Set up corgi specific audio path audio_map */
  252. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  253. snd_soc_dapm_sync(dapm);
  254. return 0;
  255. }
  256. /* corgi digital audio interface glue - connects codec <--> CPU */
  257. static struct snd_soc_dai_link corgi_dai = {
  258. .name = "WM8731",
  259. .stream_name = "WM8731",
  260. .cpu_dai_name = "pxa2xx-i2s",
  261. .codec_dai_name = "wm8731-hifi",
  262. .platform_name = "pxa-pcm-audio",
  263. .codec_name = "wm8731-codec-0.001b",
  264. .init = corgi_wm8731_init,
  265. .ops = &corgi_ops,
  266. };
  267. /* corgi audio machine driver */
  268. static struct snd_soc_card snd_soc_corgi = {
  269. .name = "Corgi",
  270. .dai_link = &corgi_dai,
  271. .num_links = 1,
  272. };
  273. static struct platform_device *corgi_snd_device;
  274. static int __init corgi_init(void)
  275. {
  276. int ret;
  277. if (!(machine_is_corgi() || machine_is_shepherd() ||
  278. machine_is_husky()))
  279. return -ENODEV;
  280. corgi_snd_device = platform_device_alloc("soc-audio", -1);
  281. if (!corgi_snd_device)
  282. return -ENOMEM;
  283. platform_set_drvdata(corgi_snd_device, &snd_soc_corgi);
  284. ret = platform_device_add(corgi_snd_device);
  285. if (ret)
  286. platform_device_put(corgi_snd_device);
  287. return ret;
  288. }
  289. static void __exit corgi_exit(void)
  290. {
  291. platform_device_unregister(corgi_snd_device);
  292. }
  293. module_init(corgi_init);
  294. module_exit(corgi_exit);
  295. /* Module information */
  296. MODULE_AUTHOR("Richard Purdie");
  297. MODULE_DESCRIPTION("ALSA SoC Corgi");
  298. MODULE_LICENSE("GPL");