corgi.c 9.8 KB

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