corgi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #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 <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/soc.h>
  23. #include <sound/soc-dapm.h>
  24. #include <asm/mach-types.h>
  25. #include <asm/hardware/scoop.h>
  26. #include <asm/arch/pxa-regs.h>
  27. #include <asm/arch/hardware.h>
  28. #include <asm/arch/corgi.h>
  29. #include <asm/arch/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. int spk = 0, mic = 0, line = 0, hp = 0, hs = 0;
  47. /* set up jack connection */
  48. switch (corgi_jack_func) {
  49. case CORGI_HP:
  50. hp = 1;
  51. /* set = unmute headphone */
  52. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
  53. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
  54. break;
  55. case CORGI_MIC:
  56. mic = 1;
  57. /* reset = mute headphone */
  58. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
  59. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
  60. break;
  61. case CORGI_LINE:
  62. line = 1;
  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_HEADSET:
  67. hs = 1;
  68. mic = 1;
  69. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
  70. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
  71. break;
  72. }
  73. if (corgi_spk_func == CORGI_SPK_ON)
  74. spk = 1;
  75. /* set the enpoints to their new connetion states */
  76. snd_soc_dapm_set_endpoint(codec, "Ext Spk", spk);
  77. snd_soc_dapm_set_endpoint(codec, "Mic Jack", mic);
  78. snd_soc_dapm_set_endpoint(codec, "Line Jack", line);
  79. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", hp);
  80. snd_soc_dapm_set_endpoint(codec, "Headset Jack", hs);
  81. /* signal a DAPM event */
  82. snd_soc_dapm_sync_endpoints(codec);
  83. }
  84. static int corgi_startup(struct snd_pcm_substream *substream)
  85. {
  86. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  87. struct snd_soc_codec *codec = rtd->socdev->codec;
  88. /* check the jack status at stream startup */
  89. corgi_ext_control(codec);
  90. return 0;
  91. }
  92. /* we need to unmute the HP at shutdown as the mute burns power on corgi */
  93. static int corgi_shutdown(struct snd_pcm_substream *substream)
  94. {
  95. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  96. struct snd_soc_codec *codec = rtd->socdev->codec;
  97. /* set = unmute headphone */
  98. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
  99. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
  100. return 0;
  101. }
  102. static int corgi_hw_params(struct snd_pcm_substream *substream,
  103. struct snd_pcm_hw_params *params)
  104. {
  105. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  106. struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
  107. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  108. unsigned int clk = 0;
  109. int ret = 0;
  110. switch (params_rate(params)) {
  111. case 8000:
  112. case 16000:
  113. case 48000:
  114. case 96000:
  115. clk = 12288000;
  116. break;
  117. case 11025:
  118. case 22050:
  119. case 44100:
  120. clk = 11289600;
  121. break;
  122. }
  123. /* set codec DAI configuration */
  124. ret = codec_dai->dai_ops.set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  125. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  126. if (ret < 0)
  127. return ret;
  128. /* set cpu DAI configuration */
  129. ret = cpu_dai->dai_ops.set_fmt(cpu_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 the codec system clock for DAC and ADC */
  134. ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8731_SYSCLK, clk,
  135. SND_SOC_CLOCK_IN);
  136. if (ret < 0)
  137. return ret;
  138. /* set the I2S system clock as input (unused) */
  139. ret = cpu_dai->dai_ops.set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  140. SND_SOC_CLOCK_IN);
  141. if (ret < 0)
  142. return ret;
  143. return 0;
  144. }
  145. static struct snd_soc_ops corgi_ops = {
  146. .startup = corgi_startup,
  147. .hw_params = corgi_hw_params,
  148. .shutdown = corgi_shutdown,
  149. };
  150. static int corgi_get_jack(struct snd_kcontrol *kcontrol,
  151. struct snd_ctl_elem_value *ucontrol)
  152. {
  153. ucontrol->value.integer.value[0] = corgi_jack_func;
  154. return 0;
  155. }
  156. static int corgi_set_jack(struct snd_kcontrol *kcontrol,
  157. struct snd_ctl_elem_value *ucontrol)
  158. {
  159. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  160. if (corgi_jack_func == ucontrol->value.integer.value[0])
  161. return 0;
  162. corgi_jack_func = ucontrol->value.integer.value[0];
  163. corgi_ext_control(codec);
  164. return 1;
  165. }
  166. static int corgi_get_spk(struct snd_kcontrol *kcontrol,
  167. struct snd_ctl_elem_value *ucontrol)
  168. {
  169. ucontrol->value.integer.value[0] = corgi_spk_func;
  170. return 0;
  171. }
  172. static int corgi_set_spk(struct snd_kcontrol *kcontrol,
  173. struct snd_ctl_elem_value *ucontrol)
  174. {
  175. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  176. if (corgi_spk_func == ucontrol->value.integer.value[0])
  177. return 0;
  178. corgi_spk_func = ucontrol->value.integer.value[0];
  179. corgi_ext_control(codec);
  180. return 1;
  181. }
  182. static int corgi_amp_event(struct snd_soc_dapm_widget *w,
  183. struct snd_kcontrol *k, int event)
  184. {
  185. if (SND_SOC_DAPM_EVENT_ON(event))
  186. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
  187. else
  188. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
  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. if (SND_SOC_DAPM_EVENT_ON(event))
  195. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
  196. else
  197. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
  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_set_endpoint(codec, "LLINEIN", 0);
  243. snd_soc_dapm_set_endpoint(codec, "RLINEIN", 0);
  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_endpoints(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_address = 0x1b,
  277. };
  278. /* corgi audio subsystem */
  279. static struct snd_soc_device corgi_snd_devdata = {
  280. .machine = &snd_soc_machine_corgi,
  281. .platform = &pxa2xx_soc_platform,
  282. .codec_dev = &soc_codec_dev_wm8731,
  283. .codec_data = &corgi_wm8731_setup,
  284. };
  285. static struct platform_device *corgi_snd_device;
  286. static int __init corgi_init(void)
  287. {
  288. int ret;
  289. if (!(machine_is_corgi() || machine_is_shepherd() ||
  290. machine_is_husky()))
  291. return -ENODEV;
  292. corgi_snd_device = platform_device_alloc("soc-audio", -1);
  293. if (!corgi_snd_device)
  294. return -ENOMEM;
  295. platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
  296. corgi_snd_devdata.dev = &corgi_snd_device->dev;
  297. ret = platform_device_add(corgi_snd_device);
  298. if (ret)
  299. platform_device_put(corgi_snd_device);
  300. return ret;
  301. }
  302. static void __exit corgi_exit(void)
  303. {
  304. platform_device_unregister(corgi_snd_device);
  305. }
  306. module_init(corgi_init);
  307. module_exit(corgi_exit);
  308. /* Module information */
  309. MODULE_AUTHOR("Richard Purdie");
  310. MODULE_DESCRIPTION("ALSA SoC Corgi");
  311. MODULE_LICENSE("GPL");