poodle.c 7.9 KB

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