poodle.c 8.7 KB

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