corgi.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/gpio.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dapm.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-pcm.h"
  30. #include "pxa2xx-i2s.h"
  31. #define CORGI_HP 0
  32. #define CORGI_MIC 1
  33. #define CORGI_LINE 2
  34. #define CORGI_HEADSET 3
  35. #define CORGI_HP_OFF 4
  36. #define CORGI_SPK_ON 0
  37. #define CORGI_SPK_OFF 1
  38. /* audio clock in Hz - rounded from 12.235MHz */
  39. #define CORGI_AUDIO_CLOCK 12288000
  40. static int corgi_jack_func;
  41. static int corgi_spk_func;
  42. static void corgi_ext_control(struct snd_soc_codec *codec)
  43. {
  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(codec, "Mic Jack");
  51. snd_soc_dapm_disable_pin(codec, "Line Jack");
  52. snd_soc_dapm_enable_pin(codec, "Headphone Jack");
  53. snd_soc_dapm_disable_pin(codec, "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(codec, "Mic Jack");
  60. snd_soc_dapm_disable_pin(codec, "Line Jack");
  61. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  62. snd_soc_dapm_disable_pin(codec, "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(codec, "Mic Jack");
  68. snd_soc_dapm_enable_pin(codec, "Line Jack");
  69. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  70. snd_soc_dapm_disable_pin(codec, "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(codec, "Mic Jack");
  76. snd_soc_dapm_disable_pin(codec, "Line Jack");
  77. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  78. snd_soc_dapm_enable_pin(codec, "Headset Jack");
  79. break;
  80. }
  81. if (corgi_spk_func == CORGI_SPK_ON)
  82. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  83. else
  84. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  85. /* signal a DAPM event */
  86. snd_soc_dapm_sync(codec);
  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->socdev->codec;
  92. /* check the jack status at stream startup */
  93. corgi_ext_control(codec);
  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->dai->codec_dai;
  108. struct snd_soc_dai *cpu_dai = rtd->dai->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 codec DAI configuration */
  125. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  126. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  127. if (ret < 0)
  128. return ret;
  129. /* set cpu DAI configuration */
  130. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  131. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  132. if (ret < 0)
  133. return ret;
  134. /* set the codec system clock for DAC and ADC */
  135. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
  136. SND_SOC_CLOCK_IN);
  137. if (ret < 0)
  138. return ret;
  139. /* set the I2S system clock as input (unused) */
  140. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  141. SND_SOC_CLOCK_IN);
  142. if (ret < 0)
  143. return ret;
  144. return 0;
  145. }
  146. static struct snd_soc_ops corgi_ops = {
  147. .startup = corgi_startup,
  148. .hw_params = corgi_hw_params,
  149. .shutdown = corgi_shutdown,
  150. };
  151. static int corgi_get_jack(struct snd_kcontrol *kcontrol,
  152. struct snd_ctl_elem_value *ucontrol)
  153. {
  154. ucontrol->value.integer.value[0] = corgi_jack_func;
  155. return 0;
  156. }
  157. static int corgi_set_jack(struct snd_kcontrol *kcontrol,
  158. struct snd_ctl_elem_value *ucontrol)
  159. {
  160. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  161. if (corgi_jack_func == ucontrol->value.integer.value[0])
  162. return 0;
  163. corgi_jack_func = ucontrol->value.integer.value[0];
  164. corgi_ext_control(codec);
  165. return 1;
  166. }
  167. static int corgi_get_spk(struct snd_kcontrol *kcontrol,
  168. struct snd_ctl_elem_value *ucontrol)
  169. {
  170. ucontrol->value.integer.value[0] = corgi_spk_func;
  171. return 0;
  172. }
  173. static int corgi_set_spk(struct snd_kcontrol *kcontrol,
  174. struct snd_ctl_elem_value *ucontrol)
  175. {
  176. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  177. if (corgi_spk_func == ucontrol->value.integer.value[0])
  178. return 0;
  179. corgi_spk_func = ucontrol->value.integer.value[0];
  180. corgi_ext_control(codec);
  181. return 1;
  182. }
  183. static int corgi_amp_event(struct snd_soc_dapm_widget *w,
  184. struct snd_kcontrol *k, int event)
  185. {
  186. gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
  187. return 0;
  188. }
  189. static int corgi_mic_event(struct snd_soc_dapm_widget *w,
  190. struct snd_kcontrol *k, int event)
  191. {
  192. gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
  193. return 0;
  194. }
  195. /* corgi machine dapm widgets */
  196. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  197. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  198. SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
  199. SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
  200. SND_SOC_DAPM_LINE("Line Jack", NULL),
  201. SND_SOC_DAPM_HP("Headset Jack", NULL),
  202. };
  203. /* Corgi machine audio map (connections to the codec pins) */
  204. static const struct snd_soc_dapm_route audio_map[] = {
  205. /* headset Jack - in = micin, out = LHPOUT*/
  206. {"Headset Jack", NULL, "LHPOUT"},
  207. /* headphone connected to LHPOUT1, RHPOUT1 */
  208. {"Headphone Jack", NULL, "LHPOUT"},
  209. {"Headphone Jack", NULL, "RHPOUT"},
  210. /* speaker connected to LOUT, ROUT */
  211. {"Ext Spk", NULL, "ROUT"},
  212. {"Ext Spk", NULL, "LOUT"},
  213. /* mic is connected to MICIN (via right channel of headphone jack) */
  214. {"MICIN", NULL, "Mic Jack"},
  215. /* Same as the above but no mic bias for line signals */
  216. {"MICIN", NULL, "Line Jack"},
  217. };
  218. static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
  219. "Off"};
  220. static const char *spk_function[] = {"On", "Off"};
  221. static const struct soc_enum corgi_enum[] = {
  222. SOC_ENUM_SINGLE_EXT(5, jack_function),
  223. SOC_ENUM_SINGLE_EXT(2, spk_function),
  224. };
  225. static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
  226. SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
  227. corgi_set_jack),
  228. SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
  229. corgi_set_spk),
  230. };
  231. /*
  232. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  233. */
  234. static int corgi_wm8731_init(struct snd_soc_codec *codec)
  235. {
  236. int i, err;
  237. snd_soc_dapm_nc_pin(codec, "LLINEIN");
  238. snd_soc_dapm_nc_pin(codec, "RLINEIN");
  239. /* Add corgi specific controls */
  240. for (i = 0; i < ARRAY_SIZE(wm8731_corgi_controls); i++) {
  241. err = snd_ctl_add(codec->card,
  242. snd_soc_cnew(&wm8731_corgi_controls[i], codec, NULL));
  243. if (err < 0)
  244. return err;
  245. }
  246. /* Add corgi specific widgets */
  247. snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
  248. ARRAY_SIZE(wm8731_dapm_widgets));
  249. /* Set up corgi specific audio path audio_map */
  250. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  251. snd_soc_dapm_sync(codec);
  252. return 0;
  253. }
  254. /* corgi digital audio interface glue - connects codec <--> CPU */
  255. static struct snd_soc_dai_link corgi_dai = {
  256. .name = "WM8731",
  257. .stream_name = "WM8731",
  258. .cpu_dai = &pxa_i2s_dai,
  259. .codec_dai = &wm8731_dai,
  260. .init = corgi_wm8731_init,
  261. .ops = &corgi_ops,
  262. };
  263. /* corgi audio machine driver */
  264. static struct snd_soc_card snd_soc_corgi = {
  265. .name = "Corgi",
  266. .platform = &pxa2xx_soc_platform,
  267. .dai_link = &corgi_dai,
  268. .num_links = 1,
  269. };
  270. /* corgi audio private data */
  271. static struct wm8731_setup_data corgi_wm8731_setup = {
  272. .i2c_bus = 0,
  273. .i2c_address = 0x1b,
  274. };
  275. /* corgi audio subsystem */
  276. static struct snd_soc_device corgi_snd_devdata = {
  277. .card = &snd_soc_corgi,
  278. .codec_dev = &soc_codec_dev_wm8731,
  279. .codec_data = &corgi_wm8731_setup,
  280. };
  281. static struct platform_device *corgi_snd_device;
  282. static int __init corgi_init(void)
  283. {
  284. int ret;
  285. if (!(machine_is_corgi() || machine_is_shepherd() ||
  286. machine_is_husky()))
  287. return -ENODEV;
  288. corgi_snd_device = platform_device_alloc("soc-audio", -1);
  289. if (!corgi_snd_device)
  290. return -ENOMEM;
  291. platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
  292. corgi_snd_devdata.dev = &corgi_snd_device->dev;
  293. ret = platform_device_add(corgi_snd_device);
  294. if (ret)
  295. platform_device_put(corgi_snd_device);
  296. return ret;
  297. }
  298. static void __exit corgi_exit(void)
  299. {
  300. platform_device_unregister(corgi_snd_device);
  301. }
  302. module_init(corgi_init);
  303. module_exit(corgi_exit);
  304. /* Module information */
  305. MODULE_AUTHOR("Richard Purdie");
  306. MODULE_DESCRIPTION("ALSA SoC Corgi");
  307. MODULE_LICENSE("GPL");