corgi.c 9.6 KB

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