poodle.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.enumerated.item[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.enumerated.item[0])
  129. return 0;
  130. poodle_jack_func = ucontrol->value.enumerated.item[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.enumerated.item[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.enumerated.item[0])
  145. return 0;
  146. poodle_spk_func = ucontrol->value.enumerated.item[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. SND_SOC_DAPM_MIC("Microphone", NULL),
  166. };
  167. /* Corgi machine connections to the codec pins */
  168. static const struct snd_soc_dapm_route poodle_audio_map[] = {
  169. /* headphone connected to LHPOUT1, RHPOUT1 */
  170. {"Headphone Jack", NULL, "LHPOUT"},
  171. {"Headphone Jack", NULL, "RHPOUT"},
  172. /* speaker connected to LOUT, ROUT */
  173. {"Ext Spk", NULL, "ROUT"},
  174. {"Ext Spk", NULL, "LOUT"},
  175. {"MICIN", NULL, "Microphone"},
  176. };
  177. static const char *jack_function[] = {"Off", "Headphone"};
  178. static const char *spk_function[] = {"Off", "On"};
  179. static const struct soc_enum poodle_enum[] = {
  180. SOC_ENUM_SINGLE_EXT(2, jack_function),
  181. SOC_ENUM_SINGLE_EXT(2, spk_function),
  182. };
  183. static const struct snd_kcontrol_new wm8731_poodle_controls[] = {
  184. SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
  185. poodle_set_jack),
  186. SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
  187. poodle_set_spk),
  188. };
  189. /* poodle digital audio interface glue - connects codec <--> CPU */
  190. static struct snd_soc_dai_link poodle_dai = {
  191. .name = "WM8731",
  192. .stream_name = "WM8731",
  193. .cpu_dai_name = "pxa2xx-i2s",
  194. .codec_dai_name = "wm8731-hifi",
  195. .platform_name = "pxa-pcm-audio",
  196. .codec_name = "wm8731.0-001b",
  197. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  198. SND_SOC_DAIFMT_CBS_CFS,
  199. .ops = &poodle_ops,
  200. };
  201. /* poodle audio machine driver */
  202. static struct snd_soc_card poodle = {
  203. .name = "Poodle",
  204. .dai_link = &poodle_dai,
  205. .num_links = 1,
  206. .owner = THIS_MODULE,
  207. .controls = wm8731_poodle_controls,
  208. .num_controls = ARRAY_SIZE(wm8731_poodle_controls),
  209. .dapm_widgets = wm8731_dapm_widgets,
  210. .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
  211. .dapm_routes = poodle_audio_map,
  212. .num_dapm_routes = ARRAY_SIZE(poodle_audio_map),
  213. .fully_routed = true,
  214. };
  215. static int poodle_probe(struct platform_device *pdev)
  216. {
  217. struct snd_soc_card *card = &poodle;
  218. int ret;
  219. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  220. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  221. /* should we mute HP at startup - burning power ?*/
  222. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  223. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  224. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  225. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  226. card->dev = &pdev->dev;
  227. ret = devm_snd_soc_register_card(&pdev->dev, card);
  228. if (ret)
  229. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  230. ret);
  231. return ret;
  232. }
  233. static struct platform_driver poodle_driver = {
  234. .driver = {
  235. .name = "poodle-audio",
  236. .pm = &snd_soc_pm_ops,
  237. },
  238. .probe = poodle_probe,
  239. };
  240. module_platform_driver(poodle_driver);
  241. /* Module information */
  242. MODULE_AUTHOR("Richard Purdie");
  243. MODULE_DESCRIPTION("ALSA SoC Poodle");
  244. MODULE_LICENSE("GPL");
  245. MODULE_ALIAS("platform:poodle-audio");