poodle.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <asm/mach-types.h>
  26. #include <asm/hardware/locomo.h>
  27. #include <mach/poodle.h>
  28. #include <mach/audio.h>
  29. #include "../codecs/wm8731.h"
  30. #include "pxa2xx-i2s.h"
  31. #define POODLE_HP 1
  32. #define POODLE_HP_OFF 0
  33. #define POODLE_SPK_ON 1
  34. #define POODLE_SPK_OFF 0
  35. /* audio clock in Hz - rounded from 12.235MHz */
  36. #define POODLE_AUDIO_CLOCK 12288000
  37. static int poodle_jack_func;
  38. static int poodle_spk_func;
  39. static void poodle_ext_control(struct snd_soc_codec *codec)
  40. {
  41. struct snd_soc_dapm_context *dapm = &codec->dapm;
  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(dapm, "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(dapm, "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(dapm, "Ext Spk");
  60. else
  61. snd_soc_dapm_disable_pin(dapm, "Ext Spk");
  62. /* signal a DAPM event */
  63. snd_soc_dapm_sync(dapm);
  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. mutex_lock(&codec->mutex);
  70. /* check the jack status at stream startup */
  71. poodle_ext_control(codec);
  72. mutex_unlock(&codec->mutex);
  73. return 0;
  74. }
  75. /* we need to unmute the HP at shutdown as the mute burns power on poodle */
  76. static void poodle_shutdown(struct snd_pcm_substream *substream)
  77. {
  78. /* set = unmute headphone */
  79. locomo_gpio_write(&poodle_locomo_device.dev,
  80. POODLE_LOCOMO_GPIO_MUTE_L, 1);
  81. locomo_gpio_write(&poodle_locomo_device.dev,
  82. POODLE_LOCOMO_GPIO_MUTE_R, 1);
  83. }
  84. static int poodle_hw_params(struct snd_pcm_substream *substream,
  85. struct snd_pcm_hw_params *params)
  86. {
  87. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  88. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  89. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  90. unsigned int clk = 0;
  91. int ret = 0;
  92. switch (params_rate(params)) {
  93. case 8000:
  94. case 16000:
  95. case 48000:
  96. case 96000:
  97. clk = 12288000;
  98. break;
  99. case 11025:
  100. case 22050:
  101. case 44100:
  102. clk = 11289600;
  103. break;
  104. }
  105. /* set the codec system clock for DAC and ADC */
  106. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
  107. SND_SOC_CLOCK_IN);
  108. if (ret < 0)
  109. return ret;
  110. /* set the I2S system clock as input (unused) */
  111. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  112. SND_SOC_CLOCK_IN);
  113. if (ret < 0)
  114. return ret;
  115. return 0;
  116. }
  117. static struct snd_soc_ops poodle_ops = {
  118. .startup = poodle_startup,
  119. .hw_params = poodle_hw_params,
  120. .shutdown = poodle_shutdown,
  121. };
  122. static int poodle_get_jack(struct snd_kcontrol *kcontrol,
  123. struct snd_ctl_elem_value *ucontrol)
  124. {
  125. ucontrol->value.integer.value[0] = poodle_jack_func;
  126. return 0;
  127. }
  128. static int poodle_set_jack(struct snd_kcontrol *kcontrol,
  129. struct snd_ctl_elem_value *ucontrol)
  130. {
  131. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  132. if (poodle_jack_func == ucontrol->value.integer.value[0])
  133. return 0;
  134. poodle_jack_func = ucontrol->value.integer.value[0];
  135. poodle_ext_control(codec);
  136. return 1;
  137. }
  138. static int poodle_get_spk(struct snd_kcontrol *kcontrol,
  139. struct snd_ctl_elem_value *ucontrol)
  140. {
  141. ucontrol->value.integer.value[0] = poodle_spk_func;
  142. return 0;
  143. }
  144. static int poodle_set_spk(struct snd_kcontrol *kcontrol,
  145. struct snd_ctl_elem_value *ucontrol)
  146. {
  147. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  148. if (poodle_spk_func == ucontrol->value.integer.value[0])
  149. return 0;
  150. poodle_spk_func = ucontrol->value.integer.value[0];
  151. poodle_ext_control(codec);
  152. return 1;
  153. }
  154. static int poodle_amp_event(struct snd_soc_dapm_widget *w,
  155. struct snd_kcontrol *k, int event)
  156. {
  157. if (SND_SOC_DAPM_EVENT_ON(event))
  158. locomo_gpio_write(&poodle_locomo_device.dev,
  159. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  160. else
  161. locomo_gpio_write(&poodle_locomo_device.dev,
  162. POODLE_LOCOMO_GPIO_AMP_ON, 1);
  163. return 0;
  164. }
  165. /* poodle machine dapm widgets */
  166. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  167. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  168. SND_SOC_DAPM_SPK("Ext Spk", poodle_amp_event),
  169. };
  170. /* Corgi machine connections to the codec pins */
  171. static const struct snd_soc_dapm_route poodle_audio_map[] = {
  172. /* headphone connected to LHPOUT1, RHPOUT1 */
  173. {"Headphone Jack", NULL, "LHPOUT"},
  174. {"Headphone Jack", NULL, "RHPOUT"},
  175. /* speaker connected to LOUT, ROUT */
  176. {"Ext Spk", NULL, "ROUT"},
  177. {"Ext Spk", NULL, "LOUT"},
  178. };
  179. static const char *jack_function[] = {"Off", "Headphone"};
  180. static const char *spk_function[] = {"Off", "On"};
  181. static const struct soc_enum poodle_enum[] = {
  182. SOC_ENUM_SINGLE_EXT(2, jack_function),
  183. SOC_ENUM_SINGLE_EXT(2, spk_function),
  184. };
  185. static const struct snd_kcontrol_new wm8731_poodle_controls[] = {
  186. SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
  187. poodle_set_jack),
  188. SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
  189. poodle_set_spk),
  190. };
  191. /*
  192. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  193. */
  194. static int poodle_wm8731_init(struct snd_soc_pcm_runtime *rtd)
  195. {
  196. struct snd_soc_codec *codec = rtd->codec;
  197. struct snd_soc_dapm_context *dapm = &codec->dapm;
  198. snd_soc_dapm_nc_pin(dapm, "LLINEIN");
  199. snd_soc_dapm_nc_pin(dapm, "RLINEIN");
  200. snd_soc_dapm_enable_pin(dapm, "MICIN");
  201. return 0;
  202. }
  203. /* poodle digital audio interface glue - connects codec <--> CPU */
  204. static struct snd_soc_dai_link poodle_dai = {
  205. .name = "WM8731",
  206. .stream_name = "WM8731",
  207. .cpu_dai_name = "pxa2xx-i2s",
  208. .codec_dai_name = "wm8731-hifi",
  209. .platform_name = "pxa-pcm-audio",
  210. .codec_name = "wm8731.0-001b",
  211. .init = poodle_wm8731_init,
  212. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  213. SND_SOC_DAIFMT_CBS_CFS,
  214. .ops = &poodle_ops,
  215. };
  216. /* poodle audio machine driver */
  217. static struct snd_soc_card poodle = {
  218. .name = "Poodle",
  219. .dai_link = &poodle_dai,
  220. .num_links = 1,
  221. .owner = THIS_MODULE,
  222. .controls = wm8731_poodle_controls,
  223. .num_controls = ARRAY_SIZE(wm8731_poodle_controls),
  224. .dapm_widgets = wm8731_dapm_widgets,
  225. .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
  226. .dapm_routes = poodle_audio_map,
  227. .num_dapm_routes = ARRAY_SIZE(poodle_audio_map),
  228. };
  229. static int __devinit poodle_probe(struct platform_device *pdev)
  230. {
  231. struct snd_soc_card *card = &poodle;
  232. int ret;
  233. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  234. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  235. /* should we mute HP at startup - burning power ?*/
  236. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  237. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  238. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  239. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  240. card->dev = &pdev->dev;
  241. ret = snd_soc_register_card(card);
  242. if (ret)
  243. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  244. ret);
  245. return ret;
  246. }
  247. static int __devexit poodle_remove(struct platform_device *pdev)
  248. {
  249. struct snd_soc_card *card = platform_get_drvdata(pdev);
  250. snd_soc_unregister_card(card);
  251. return 0;
  252. }
  253. static struct platform_driver poodle_driver = {
  254. .driver = {
  255. .name = "poodle-audio",
  256. .owner = THIS_MODULE,
  257. },
  258. .probe = poodle_probe,
  259. .remove = __devexit_p(poodle_remove),
  260. };
  261. module_platform_driver(poodle_driver);
  262. /* Module information */
  263. MODULE_AUTHOR("Richard Purdie");
  264. MODULE_DESCRIPTION("ALSA SoC Poodle");
  265. MODULE_LICENSE("GPL");
  266. MODULE_ALIAS("platform:poodle-audio");