corgi.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 <liam.girdwood@wolfsonmicro.com>
  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. * Revision history
  16. * 30th Nov 2005 Initial version.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/timer.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/platform_device.h>
  24. #include <sound/driver.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/soc.h>
  28. #include <sound/soc-dapm.h>
  29. #include <asm/mach-types.h>
  30. #include <asm/hardware/scoop.h>
  31. #include <asm/arch/pxa-regs.h>
  32. #include <asm/arch/hardware.h>
  33. #include <asm/arch/corgi.h>
  34. #include <asm/arch/audio.h>
  35. #include "../codecs/wm8731.h"
  36. #include "pxa2xx-pcm.h"
  37. #define CORGI_HP 0
  38. #define CORGI_MIC 1
  39. #define CORGI_LINE 2
  40. #define CORGI_HEADSET 3
  41. #define CORGI_HP_OFF 4
  42. #define CORGI_SPK_ON 0
  43. #define CORGI_SPK_OFF 1
  44. /* audio clock in Hz - rounded from 12.235MHz */
  45. #define CORGI_AUDIO_CLOCK 12288000
  46. static int corgi_jack_func;
  47. static int corgi_spk_func;
  48. static void corgi_ext_control(struct snd_soc_codec *codec)
  49. {
  50. int spk = 0, mic = 0, line = 0, hp = 0, hs = 0;
  51. /* set up jack connection */
  52. switch (corgi_jack_func) {
  53. case CORGI_HP:
  54. hp = 1;
  55. /* set = unmute headphone */
  56. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
  57. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
  58. break;
  59. case CORGI_MIC:
  60. mic = 1;
  61. /* reset = mute headphone */
  62. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
  63. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
  64. break;
  65. case CORGI_LINE:
  66. line = 1;
  67. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
  68. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
  69. break;
  70. case CORGI_HEADSET:
  71. hs = 1;
  72. mic = 1;
  73. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
  74. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
  75. break;
  76. }
  77. if (corgi_spk_func == CORGI_SPK_ON)
  78. spk = 1;
  79. /* set the enpoints to their new connetion states */
  80. snd_soc_dapm_set_endpoint(codec, "Ext Spk", spk);
  81. snd_soc_dapm_set_endpoint(codec, "Mic Jack", mic);
  82. snd_soc_dapm_set_endpoint(codec, "Line Jack", line);
  83. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", hp);
  84. snd_soc_dapm_set_endpoint(codec, "Headset Jack", hs);
  85. /* signal a DAPM event */
  86. snd_soc_dapm_sync_endpoints(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 int corgi_shutdown(struct snd_pcm_substream *substream)
  98. {
  99. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100. struct snd_soc_codec *codec = rtd->socdev->codec;
  101. /* set = unmute headphone */
  102. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
  103. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
  104. return 0;
  105. }
  106. static struct snd_soc_ops corgi_ops = {
  107. .startup = corgi_startup,
  108. .shutdown = corgi_shutdown,
  109. };
  110. static int corgi_get_jack(struct snd_kcontrol *kcontrol,
  111. struct snd_ctl_elem_value *ucontrol)
  112. {
  113. ucontrol->value.integer.value[0] = corgi_jack_func;
  114. return 0;
  115. }
  116. static int corgi_set_jack(struct snd_kcontrol *kcontrol,
  117. struct snd_ctl_elem_value *ucontrol)
  118. {
  119. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  120. if (corgi_jack_func == ucontrol->value.integer.value[0])
  121. return 0;
  122. corgi_jack_func = ucontrol->value.integer.value[0];
  123. corgi_ext_control(codec);
  124. return 1;
  125. }
  126. static int corgi_get_spk(struct snd_kcontrol *kcontrol,
  127. struct snd_ctl_elem_value *ucontrol)
  128. {
  129. ucontrol->value.integer.value[0] = corgi_spk_func;
  130. return 0;
  131. }
  132. static int corgi_set_spk(struct snd_kcontrol *kcontrol,
  133. struct snd_ctl_elem_value *ucontrol)
  134. {
  135. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  136. if (corgi_spk_func == ucontrol->value.integer.value[0])
  137. return 0;
  138. corgi_spk_func = ucontrol->value.integer.value[0];
  139. corgi_ext_control(codec);
  140. return 1;
  141. }
  142. static int corgi_amp_event(struct snd_soc_dapm_widget *w, int event)
  143. {
  144. if (SND_SOC_DAPM_EVENT_ON(event))
  145. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
  146. else
  147. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
  148. return 0;
  149. }
  150. static int corgi_mic_event(struct snd_soc_dapm_widget *w, int event)
  151. {
  152. if (SND_SOC_DAPM_EVENT_ON(event))
  153. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
  154. else
  155. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
  156. return 0;
  157. }
  158. /* corgi machine dapm widgets */
  159. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  160. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  161. SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
  162. SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
  163. SND_SOC_DAPM_LINE("Line Jack", NULL),
  164. SND_SOC_DAPM_HP("Headset Jack", NULL),
  165. };
  166. /* Corgi machine audio map (connections to the codec pins) */
  167. static const char *audio_map[][3] = {
  168. /* headset Jack - in = micin, out = LHPOUT*/
  169. {"Headset Jack", NULL, "LHPOUT"},
  170. /* headphone connected to LHPOUT1, RHPOUT1 */
  171. {"Headphone Jack", NULL, "LHPOUT"},
  172. {"Headphone Jack", NULL, "RHPOUT"},
  173. /* speaker connected to LOUT, ROUT */
  174. {"Ext Spk", NULL, "ROUT"},
  175. {"Ext Spk", NULL, "LOUT"},
  176. /* mic is connected to MICIN (via right channel of headphone jack) */
  177. {"MICIN", NULL, "Mic Jack"},
  178. /* Same as the above but no mic bias for line signals */
  179. {"MICIN", NULL, "Line Jack"},
  180. {NULL, NULL, NULL},
  181. };
  182. static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
  183. "Off"};
  184. static const char *spk_function[] = {"On", "Off"};
  185. static const struct soc_enum corgi_enum[] = {
  186. SOC_ENUM_SINGLE_EXT(5, jack_function),
  187. SOC_ENUM_SINGLE_EXT(2, spk_function),
  188. };
  189. static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
  190. SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
  191. corgi_set_jack),
  192. SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
  193. corgi_set_spk),
  194. };
  195. /*
  196. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  197. */
  198. static int corgi_wm8731_init(struct snd_soc_codec *codec)
  199. {
  200. int i, err;
  201. snd_soc_dapm_set_endpoint(codec, "LLINEIN", 0);
  202. snd_soc_dapm_set_endpoint(codec, "RLINEIN", 0);
  203. /* Add corgi specific controls */
  204. for (i = 0; i < ARRAY_SIZE(wm8731_corgi_controls); i++) {
  205. err = snd_ctl_add(codec->card,
  206. snd_soc_cnew(&wm8731_corgi_controls[i],codec, NULL));
  207. if (err < 0)
  208. return err;
  209. }
  210. /* Add corgi specific widgets */
  211. for(i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) {
  212. snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
  213. }
  214. /* Set up corgi specific audio path audio_map */
  215. for(i = 0; audio_map[i][0] != NULL; i++) {
  216. snd_soc_dapm_connect_input(codec, audio_map[i][0],
  217. audio_map[i][1], audio_map[i][2]);
  218. }
  219. snd_soc_dapm_sync_endpoints(codec);
  220. return 0;
  221. }
  222. static unsigned int corgi_config_sysclk(struct snd_soc_pcm_runtime *rtd,
  223. struct snd_soc_clock_info *info)
  224. {
  225. if (info->bclk_master & SND_SOC_DAIFMT_CBS_CFS) {
  226. /* pxa2xx is i2s master */
  227. switch (info->rate) {
  228. case 44100:
  229. case 88200:
  230. /* configure codec digital filters for 44.1, 88.2 */
  231. rtd->codec_dai->config_sysclk(rtd->codec_dai, info,
  232. 11289600);
  233. break;
  234. default:
  235. /* configure codec digital filters for all other rates */
  236. rtd->codec_dai->config_sysclk(rtd->codec_dai, info,
  237. CORGI_AUDIO_CLOCK);
  238. break;
  239. }
  240. /* config pxa i2s as master */
  241. return rtd->cpu_dai->config_sysclk(rtd->cpu_dai, info,
  242. CORGI_AUDIO_CLOCK);
  243. } else {
  244. /* codec is i2s master -
  245. * only configure codec DAI clock and filters */
  246. return rtd->codec_dai->config_sysclk(rtd->codec_dai, info,
  247. CORGI_AUDIO_CLOCK);
  248. }
  249. }
  250. /* corgi digital audio interface glue - connects codec <--> CPU */
  251. static struct snd_soc_dai_link corgi_dai = {
  252. .name = "WM8731",
  253. .stream_name = "WM8731",
  254. .cpu_dai = &pxa_i2s_dai,
  255. .codec_dai = &wm8731_dai,
  256. .init = corgi_wm8731_init,
  257. .config_sysclk = corgi_config_sysclk,
  258. };
  259. /* corgi audio machine driver */
  260. static struct snd_soc_machine snd_soc_machine_corgi = {
  261. .name = "Corgi",
  262. .dai_link = &corgi_dai,
  263. .num_links = 1,
  264. .ops = &corgi_ops,
  265. };
  266. /* corgi audio private data */
  267. static struct wm8731_setup_data corgi_wm8731_setup = {
  268. .i2c_address = 0x1b,
  269. };
  270. /* corgi audio subsystem */
  271. static struct snd_soc_device corgi_snd_devdata = {
  272. .machine = &snd_soc_machine_corgi,
  273. .platform = &pxa2xx_soc_platform,
  274. .codec_dev = &soc_codec_dev_wm8731,
  275. .codec_data = &corgi_wm8731_setup,
  276. };
  277. static struct platform_device *corgi_snd_device;
  278. static int __init corgi_init(void)
  279. {
  280. int ret;
  281. if (!(machine_is_corgi() || machine_is_shepherd() || machine_is_husky()))
  282. return -ENODEV;
  283. corgi_snd_device = platform_device_alloc("soc-audio", -1);
  284. if (!corgi_snd_device)
  285. return -ENOMEM;
  286. platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
  287. corgi_snd_devdata.dev = &corgi_snd_device->dev;
  288. ret = platform_device_add(corgi_snd_device);
  289. if (ret)
  290. platform_device_put(corgi_snd_device);
  291. return ret;
  292. }
  293. static void __exit corgi_exit(void)
  294. {
  295. platform_device_unregister(corgi_snd_device);
  296. }
  297. module_init(corgi_init);
  298. module_exit(corgi_exit);
  299. /* Module information */
  300. MODULE_AUTHOR("Richard Purdie");
  301. MODULE_DESCRIPTION("ALSA SoC Corgi");
  302. MODULE_LICENSE("GPL");