poodle.c 8.1 KB

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