poodle.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * poodle.c -- SoC audio for Poodle
  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. */
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/timer.h>
  19. #include <linux/i2c.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <asm/mach-types.h>
  27. #include <asm/hardware/locomo.h>
  28. #include <mach/poodle.h>
  29. #include <mach/audio.h>
  30. #include "../codecs/wm8731.h"
  31. #include "pxa2xx-i2s.h"
  32. #define POODLE_HP 1
  33. #define POODLE_HP_OFF 0
  34. #define POODLE_SPK_ON 1
  35. #define POODLE_SPK_OFF 0
  36. /* audio clock in Hz - rounded from 12.235MHz */
  37. #define POODLE_AUDIO_CLOCK 12288000
  38. static int poodle_jack_func;
  39. static int poodle_spk_func;
  40. static void poodle_ext_control(struct snd_soc_codec *codec)
  41. {
  42. /* set up jack connection */
  43. if (poodle_jack_func == POODLE_HP) {
  44. /* set = unmute headphone */
  45. locomo_gpio_write(&poodle_locomo_device.dev,
  46. POODLE_LOCOMO_GPIO_MUTE_L, 1);
  47. locomo_gpio_write(&poodle_locomo_device.dev,
  48. POODLE_LOCOMO_GPIO_MUTE_R, 1);
  49. snd_soc_dapm_enable_pin(codec, "Headphone Jack");
  50. } else {
  51. locomo_gpio_write(&poodle_locomo_device.dev,
  52. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  53. locomo_gpio_write(&poodle_locomo_device.dev,
  54. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  55. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  56. }
  57. /* set the enpoints to their new connetion states */
  58. if (poodle_spk_func == POODLE_SPK_ON)
  59. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  60. else
  61. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  62. /* signal a DAPM event */
  63. snd_soc_dapm_sync(codec);
  64. }
  65. static int poodle_startup(struct snd_pcm_substream *substream)
  66. {
  67. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  68. struct snd_soc_codec *codec = rtd->codec;
  69. /* check the jack status at stream startup */
  70. poodle_ext_control(codec);
  71. return 0;
  72. }
  73. /* we need to unmute the HP at shutdown as the mute burns power on poodle */
  74. static void poodle_shutdown(struct snd_pcm_substream *substream)
  75. {
  76. /* set = unmute headphone */
  77. locomo_gpio_write(&poodle_locomo_device.dev,
  78. POODLE_LOCOMO_GPIO_MUTE_L, 1);
  79. locomo_gpio_write(&poodle_locomo_device.dev,
  80. POODLE_LOCOMO_GPIO_MUTE_R, 1);
  81. }
  82. static int poodle_hw_params(struct snd_pcm_substream *substream,
  83. struct snd_pcm_hw_params *params)
  84. {
  85. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  86. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  87. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  88. unsigned int clk = 0;
  89. int ret = 0;
  90. switch (params_rate(params)) {
  91. case 8000:
  92. case 16000:
  93. case 48000:
  94. case 96000:
  95. clk = 12288000;
  96. break;
  97. case 11025:
  98. case 22050:
  99. case 44100:
  100. clk = 11289600;
  101. break;
  102. }
  103. /* set codec DAI configuration */
  104. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  105. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  106. if (ret < 0)
  107. return ret;
  108. /* set cpu DAI configuration */
  109. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  110. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  111. if (ret < 0)
  112. return ret;
  113. /* set the codec system clock for DAC and ADC */
  114. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
  115. SND_SOC_CLOCK_IN);
  116. if (ret < 0)
  117. return ret;
  118. /* set the I2S system clock as input (unused) */
  119. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  120. SND_SOC_CLOCK_IN);
  121. if (ret < 0)
  122. return ret;
  123. return 0;
  124. }
  125. static struct snd_soc_ops poodle_ops = {
  126. .startup = poodle_startup,
  127. .hw_params = poodle_hw_params,
  128. .shutdown = poodle_shutdown,
  129. };
  130. static int poodle_get_jack(struct snd_kcontrol *kcontrol,
  131. struct snd_ctl_elem_value *ucontrol)
  132. {
  133. ucontrol->value.integer.value[0] = poodle_jack_func;
  134. return 0;
  135. }
  136. static int poodle_set_jack(struct snd_kcontrol *kcontrol,
  137. struct snd_ctl_elem_value *ucontrol)
  138. {
  139. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  140. if (poodle_jack_func == ucontrol->value.integer.value[0])
  141. return 0;
  142. poodle_jack_func = ucontrol->value.integer.value[0];
  143. poodle_ext_control(codec);
  144. return 1;
  145. }
  146. static int poodle_get_spk(struct snd_kcontrol *kcontrol,
  147. struct snd_ctl_elem_value *ucontrol)
  148. {
  149. ucontrol->value.integer.value[0] = poodle_spk_func;
  150. return 0;
  151. }
  152. static int poodle_set_spk(struct snd_kcontrol *kcontrol,
  153. struct snd_ctl_elem_value *ucontrol)
  154. {
  155. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  156. if (poodle_spk_func == ucontrol->value.integer.value[0])
  157. return 0;
  158. poodle_spk_func = ucontrol->value.integer.value[0];
  159. poodle_ext_control(codec);
  160. return 1;
  161. }
  162. static int poodle_amp_event(struct snd_soc_dapm_widget *w,
  163. struct snd_kcontrol *k, int event)
  164. {
  165. if (SND_SOC_DAPM_EVENT_ON(event))
  166. locomo_gpio_write(&poodle_locomo_device.dev,
  167. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  168. else
  169. locomo_gpio_write(&poodle_locomo_device.dev,
  170. POODLE_LOCOMO_GPIO_AMP_ON, 1);
  171. return 0;
  172. }
  173. /* poodle machine dapm widgets */
  174. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  175. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  176. SND_SOC_DAPM_SPK("Ext Spk", poodle_amp_event),
  177. };
  178. /* Corgi machine connections to the codec pins */
  179. static const struct snd_soc_dapm_route audio_map[] = {
  180. /* headphone connected to LHPOUT1, RHPOUT1 */
  181. {"Headphone Jack", NULL, "LHPOUT"},
  182. {"Headphone Jack", NULL, "RHPOUT"},
  183. /* speaker connected to LOUT, ROUT */
  184. {"Ext Spk", NULL, "ROUT"},
  185. {"Ext Spk", NULL, "LOUT"},
  186. };
  187. static const char *jack_function[] = {"Off", "Headphone"};
  188. static const char *spk_function[] = {"Off", "On"};
  189. static const struct soc_enum poodle_enum[] = {
  190. SOC_ENUM_SINGLE_EXT(2, jack_function),
  191. SOC_ENUM_SINGLE_EXT(2, spk_function),
  192. };
  193. static const struct snd_kcontrol_new wm8731_poodle_controls[] = {
  194. SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
  195. poodle_set_jack),
  196. SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
  197. poodle_set_spk),
  198. };
  199. /*
  200. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  201. */
  202. static int poodle_wm8731_init(struct snd_soc_pcm_runtime *rtd)
  203. {
  204. struct snd_soc_codec *codec = rtd->codec;
  205. int err;
  206. snd_soc_dapm_nc_pin(codec, "LLINEIN");
  207. snd_soc_dapm_nc_pin(codec, "RLINEIN");
  208. snd_soc_dapm_enable_pin(codec, "MICIN");
  209. /* Add poodle specific controls */
  210. err = snd_soc_add_controls(codec, wm8731_poodle_controls,
  211. ARRAY_SIZE(wm8731_poodle_controls));
  212. if (err < 0)
  213. return err;
  214. /* Add poodle specific widgets */
  215. snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
  216. ARRAY_SIZE(wm8731_dapm_widgets));
  217. /* Set up poodle specific audio path audio_map */
  218. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  219. snd_soc_dapm_sync(codec);
  220. return 0;
  221. }
  222. /* poodle digital audio interface glue - connects codec <--> CPU */
  223. static struct snd_soc_dai_link poodle_dai = {
  224. .name = "WM8731",
  225. .stream_name = "WM8731",
  226. .cpu_dai_name = "pxa2xx-i2s",
  227. .codec_dai_name = "wm8731-hifi",
  228. .platform_name = "pxa-pcm-audio",
  229. .codec_name = "wm8731-codec.0-001a",
  230. .init = poodle_wm8731_init,
  231. .ops = &poodle_ops,
  232. };
  233. /* poodle audio machine driver */
  234. static struct snd_soc_card snd_soc_poodle = {
  235. .name = "Poodle",
  236. .dai_link = &poodle_dai,
  237. .num_links = 1,
  238. .owner = THIS_MODULE,
  239. };
  240. static struct platform_device *poodle_snd_device;
  241. static int __init poodle_init(void)
  242. {
  243. int ret;
  244. if (!machine_is_poodle())
  245. return -ENODEV;
  246. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  247. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  248. /* should we mute HP at startup - burning power ?*/
  249. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  250. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  251. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  252. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  253. poodle_snd_device = platform_device_alloc("soc-audio", -1);
  254. if (!poodle_snd_device)
  255. return -ENOMEM;
  256. platform_set_drvdata(poodle_snd_device, &snd_soc_poodle);
  257. ret = platform_device_add(poodle_snd_device);
  258. if (ret)
  259. platform_device_put(poodle_snd_device);
  260. return ret;
  261. }
  262. static void __exit poodle_exit(void)
  263. {
  264. platform_device_unregister(poodle_snd_device);
  265. }
  266. module_init(poodle_init);
  267. module_exit(poodle_exit);
  268. /* Module information */
  269. MODULE_AUTHOR("Richard Purdie");
  270. MODULE_DESCRIPTION("ALSA SoC Poodle");
  271. MODULE_LICENSE("GPL");