poodle.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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-pcm.h"
  32. #include "pxa2xx-i2s.h"
  33. #define POODLE_HP 1
  34. #define POODLE_HP_OFF 0
  35. #define POODLE_SPK_ON 1
  36. #define POODLE_SPK_OFF 0
  37. /* audio clock in Hz - rounded from 12.235MHz */
  38. #define POODLE_AUDIO_CLOCK 12288000
  39. static int poodle_jack_func;
  40. static int poodle_spk_func;
  41. static void poodle_ext_control(struct snd_soc_codec *codec)
  42. {
  43. /* set up jack connection */
  44. if (poodle_jack_func == POODLE_HP) {
  45. /* set = unmute headphone */
  46. locomo_gpio_write(&poodle_locomo_device.dev,
  47. POODLE_LOCOMO_GPIO_MUTE_L, 1);
  48. locomo_gpio_write(&poodle_locomo_device.dev,
  49. POODLE_LOCOMO_GPIO_MUTE_R, 1);
  50. snd_soc_dapm_enable_pin(codec, "Headphone Jack");
  51. } else {
  52. locomo_gpio_write(&poodle_locomo_device.dev,
  53. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  54. locomo_gpio_write(&poodle_locomo_device.dev,
  55. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  56. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  57. }
  58. /* set the enpoints to their new connetion states */
  59. if (poodle_spk_func == POODLE_SPK_ON)
  60. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  61. else
  62. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  63. /* signal a DAPM event */
  64. snd_soc_dapm_sync(codec);
  65. }
  66. static int poodle_startup(struct snd_pcm_substream *substream)
  67. {
  68. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  69. struct snd_soc_codec *codec = rtd->socdev->card->codec;
  70. /* check the jack status at stream startup */
  71. poodle_ext_control(codec);
  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->dai->codec_dai;
  88. struct snd_soc_dai *cpu_dai = rtd->dai->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 codec DAI configuration */
  105. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  106. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  107. if (ret < 0)
  108. return ret;
  109. /* set cpu DAI configuration */
  110. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  111. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  112. if (ret < 0)
  113. return ret;
  114. /* set the codec system clock for DAC and ADC */
  115. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
  116. SND_SOC_CLOCK_IN);
  117. if (ret < 0)
  118. return ret;
  119. /* set the I2S system clock as input (unused) */
  120. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  121. SND_SOC_CLOCK_IN);
  122. if (ret < 0)
  123. return ret;
  124. return 0;
  125. }
  126. static struct snd_soc_ops poodle_ops = {
  127. .startup = poodle_startup,
  128. .hw_params = poodle_hw_params,
  129. .shutdown = poodle_shutdown,
  130. };
  131. static int poodle_get_jack(struct snd_kcontrol *kcontrol,
  132. struct snd_ctl_elem_value *ucontrol)
  133. {
  134. ucontrol->value.integer.value[0] = poodle_jack_func;
  135. return 0;
  136. }
  137. static int poodle_set_jack(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol)
  139. {
  140. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  141. if (poodle_jack_func == ucontrol->value.integer.value[0])
  142. return 0;
  143. poodle_jack_func = ucontrol->value.integer.value[0];
  144. poodle_ext_control(codec);
  145. return 1;
  146. }
  147. static int poodle_get_spk(struct snd_kcontrol *kcontrol,
  148. struct snd_ctl_elem_value *ucontrol)
  149. {
  150. ucontrol->value.integer.value[0] = poodle_spk_func;
  151. return 0;
  152. }
  153. static int poodle_set_spk(struct snd_kcontrol *kcontrol,
  154. struct snd_ctl_elem_value *ucontrol)
  155. {
  156. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  157. if (poodle_spk_func == ucontrol->value.integer.value[0])
  158. return 0;
  159. poodle_spk_func = ucontrol->value.integer.value[0];
  160. poodle_ext_control(codec);
  161. return 1;
  162. }
  163. static int poodle_amp_event(struct snd_soc_dapm_widget *w,
  164. struct snd_kcontrol *k, int event)
  165. {
  166. if (SND_SOC_DAPM_EVENT_ON(event))
  167. locomo_gpio_write(&poodle_locomo_device.dev,
  168. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  169. else
  170. locomo_gpio_write(&poodle_locomo_device.dev,
  171. POODLE_LOCOMO_GPIO_AMP_ON, 1);
  172. return 0;
  173. }
  174. /* poodle machine dapm widgets */
  175. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  176. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  177. SND_SOC_DAPM_SPK("Ext Spk", poodle_amp_event),
  178. };
  179. /* Corgi machine connections to the codec pins */
  180. static const struct snd_soc_dapm_route audio_map[] = {
  181. /* headphone connected to LHPOUT1, RHPOUT1 */
  182. {"Headphone Jack", NULL, "LHPOUT"},
  183. {"Headphone Jack", NULL, "RHPOUT"},
  184. /* speaker connected to LOUT, ROUT */
  185. {"Ext Spk", NULL, "ROUT"},
  186. {"Ext Spk", NULL, "LOUT"},
  187. };
  188. static const char *jack_function[] = {"Off", "Headphone"};
  189. static const char *spk_function[] = {"Off", "On"};
  190. static const struct soc_enum poodle_enum[] = {
  191. SOC_ENUM_SINGLE_EXT(2, jack_function),
  192. SOC_ENUM_SINGLE_EXT(2, spk_function),
  193. };
  194. static const struct snd_kcontrol_new wm8731_poodle_controls[] = {
  195. SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
  196. poodle_set_jack),
  197. SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
  198. poodle_set_spk),
  199. };
  200. /*
  201. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  202. */
  203. static int poodle_wm8731_init(struct snd_soc_codec *codec)
  204. {
  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 = &pxa_i2s_dai,
  227. .codec_dai = &wm8731_dai,
  228. .init = poodle_wm8731_init,
  229. .ops = &poodle_ops,
  230. };
  231. /* poodle audio machine driver */
  232. static struct snd_soc_card snd_soc_poodle = {
  233. .name = "Poodle",
  234. .platform = &pxa2xx_soc_platform,
  235. .dai_link = &poodle_dai,
  236. .num_links = 1,
  237. };
  238. /* poodle audio subsystem */
  239. static struct snd_soc_device poodle_snd_devdata = {
  240. .card = &snd_soc_poodle,
  241. .codec_dev = &soc_codec_dev_wm8731,
  242. };
  243. static struct platform_device *poodle_snd_device;
  244. static int __init poodle_init(void)
  245. {
  246. int ret;
  247. if (!machine_is_poodle())
  248. return -ENODEV;
  249. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  250. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  251. /* should we mute HP at startup - burning power ?*/
  252. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  253. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  254. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  255. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  256. poodle_snd_device = platform_device_alloc("soc-audio", -1);
  257. if (!poodle_snd_device)
  258. return -ENOMEM;
  259. platform_set_drvdata(poodle_snd_device, &poodle_snd_devdata);
  260. poodle_snd_devdata.dev = &poodle_snd_device->dev;
  261. ret = platform_device_add(poodle_snd_device);
  262. if (ret)
  263. platform_device_put(poodle_snd_device);
  264. return ret;
  265. }
  266. static void __exit poodle_exit(void)
  267. {
  268. platform_device_unregister(poodle_snd_device);
  269. }
  270. module_init(poodle_init);
  271. module_exit(poodle_exit);
  272. /* Module information */
  273. MODULE_AUTHOR("Richard Purdie");
  274. MODULE_DESCRIPTION("ALSA SoC Poodle");
  275. MODULE_LICENSE("GPL");