poodle.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <liam.girdwood@wolfsonmicro.com>
  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 <asm/arch/pxa-regs.h>
  28. #include <asm/arch/hardware.h>
  29. #include <asm/arch/poodle.h>
  30. #include <asm/arch/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 int poodle_shutdown(struct snd_pcm_substream *substream)
  77. {
  78. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  79. struct snd_soc_codec *codec = rtd->socdev->codec;
  80. /* set = unmute headphone */
  81. locomo_gpio_write(&poodle_locomo_device.dev,
  82. POODLE_LOCOMO_GPIO_MUTE_L, 1);
  83. locomo_gpio_write(&poodle_locomo_device.dev,
  84. POODLE_LOCOMO_GPIO_MUTE_R, 1);
  85. return 0;
  86. }
  87. static int poodle_hw_params(struct snd_pcm_substream *substream,
  88. struct snd_pcm_hw_params *params)
  89. {
  90. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  91. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  92. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  93. unsigned int clk = 0;
  94. int ret = 0;
  95. switch (params_rate(params)) {
  96. case 8000:
  97. case 16000:
  98. case 48000:
  99. case 96000:
  100. clk = 12288000;
  101. break;
  102. case 11025:
  103. case 22050:
  104. case 44100:
  105. clk = 11289600;
  106. break;
  107. }
  108. /* set codec DAI configuration */
  109. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  110. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  111. if (ret < 0)
  112. return ret;
  113. /* set cpu DAI configuration */
  114. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  115. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  116. if (ret < 0)
  117. return ret;
  118. /* set the codec system clock for DAC and ADC */
  119. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
  120. SND_SOC_CLOCK_IN);
  121. if (ret < 0)
  122. return ret;
  123. /* set the I2S system clock as input (unused) */
  124. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  125. SND_SOC_CLOCK_IN);
  126. if (ret < 0)
  127. return ret;
  128. return 0;
  129. }
  130. static struct snd_soc_ops poodle_ops = {
  131. .startup = poodle_startup,
  132. .hw_params = poodle_hw_params,
  133. .shutdown = poodle_shutdown,
  134. };
  135. static int poodle_get_jack(struct snd_kcontrol *kcontrol,
  136. struct snd_ctl_elem_value *ucontrol)
  137. {
  138. ucontrol->value.integer.value[0] = poodle_jack_func;
  139. return 0;
  140. }
  141. static int poodle_set_jack(struct snd_kcontrol *kcontrol,
  142. struct snd_ctl_elem_value *ucontrol)
  143. {
  144. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  145. if (poodle_jack_func == ucontrol->value.integer.value[0])
  146. return 0;
  147. poodle_jack_func = ucontrol->value.integer.value[0];
  148. poodle_ext_control(codec);
  149. return 1;
  150. }
  151. static int poodle_get_spk(struct snd_kcontrol *kcontrol,
  152. struct snd_ctl_elem_value *ucontrol)
  153. {
  154. ucontrol->value.integer.value[0] = poodle_spk_func;
  155. return 0;
  156. }
  157. static int poodle_set_spk(struct snd_kcontrol *kcontrol,
  158. struct snd_ctl_elem_value *ucontrol)
  159. {
  160. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  161. if (poodle_spk_func == ucontrol->value.integer.value[0])
  162. return 0;
  163. poodle_spk_func = ucontrol->value.integer.value[0];
  164. poodle_ext_control(codec);
  165. return 1;
  166. }
  167. static int poodle_amp_event(struct snd_soc_dapm_widget *w,
  168. struct snd_kcontrol *k, int event)
  169. {
  170. if (SND_SOC_DAPM_EVENT_ON(event))
  171. locomo_gpio_write(&poodle_locomo_device.dev,
  172. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  173. else
  174. locomo_gpio_write(&poodle_locomo_device.dev,
  175. POODLE_LOCOMO_GPIO_AMP_ON, 1);
  176. return 0;
  177. }
  178. /* poodle machine dapm widgets */
  179. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  180. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  181. SND_SOC_DAPM_SPK("Ext Spk", poodle_amp_event),
  182. };
  183. /* Corgi machine connections to the codec pins */
  184. static const struct snd_soc_dapm_route audio_map[] = {
  185. /* headphone connected to LHPOUT1, RHPOUT1 */
  186. {"Headphone Jack", NULL, "LHPOUT"},
  187. {"Headphone Jack", NULL, "RHPOUT"},
  188. /* speaker connected to LOUT, ROUT */
  189. {"Ext Spk", NULL, "ROUT"},
  190. {"Ext Spk", NULL, "LOUT"},
  191. };
  192. static const char *jack_function[] = {"Off", "Headphone"};
  193. static const char *spk_function[] = {"Off", "On"};
  194. static const struct soc_enum poodle_enum[] = {
  195. SOC_ENUM_SINGLE_EXT(2, jack_function),
  196. SOC_ENUM_SINGLE_EXT(2, spk_function),
  197. };
  198. static const snd_kcontrol_new_t wm8731_poodle_controls[] = {
  199. SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
  200. poodle_set_jack),
  201. SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
  202. poodle_set_spk),
  203. };
  204. /*
  205. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  206. */
  207. static int poodle_wm8731_init(struct snd_soc_codec *codec)
  208. {
  209. int i, err;
  210. snd_soc_dapm_disable_pin(codec, "LLINEIN");
  211. snd_soc_dapm_disable_pin(codec, "RLINEIN");
  212. snd_soc_dapm_enable_pin(codec, "MICIN");
  213. /* Add poodle specific controls */
  214. for (i = 0; i < ARRAY_SIZE(wm8731_poodle_controls); i++) {
  215. err = snd_ctl_add(codec->card,
  216. snd_soc_cnew(&wm8731_poodle_controls[i], codec, NULL));
  217. if (err < 0)
  218. return err;
  219. }
  220. /* Add poodle specific widgets */
  221. snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
  222. ARRAY_SIZE(wm8731_dapm_widgets));
  223. /* Set up poodle specific audio path audio_map */
  224. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  225. snd_soc_dapm_sync(codec);
  226. return 0;
  227. }
  228. /* poodle digital audio interface glue - connects codec <--> CPU */
  229. static struct snd_soc_dai_link poodle_dai = {
  230. .name = "WM8731",
  231. .stream_name = "WM8731",
  232. .cpu_dai = &pxa_i2s_dai,
  233. .codec_dai = &wm8731_dai,
  234. .init = poodle_wm8731_init,
  235. .ops = &poodle_ops,
  236. };
  237. /* poodle audio machine driver */
  238. static struct snd_soc_machine snd_soc_machine_poodle = {
  239. .name = "Poodle",
  240. .dai_link = &poodle_dai,
  241. .num_links = 1,
  242. };
  243. /* poodle audio private data */
  244. static struct wm8731_setup_data poodle_wm8731_setup = {
  245. .i2c_address = 0x1b,
  246. };
  247. /* poodle audio subsystem */
  248. static struct snd_soc_device poodle_snd_devdata = {
  249. .machine = &snd_soc_machine_poodle,
  250. .platform = &pxa2xx_soc_platform,
  251. .codec_dev = &soc_codec_dev_wm8731,
  252. .codec_data = &poodle_wm8731_setup,
  253. };
  254. static struct platform_device *poodle_snd_device;
  255. static int __init poodle_init(void)
  256. {
  257. int ret;
  258. if (!machine_is_poodle())
  259. return -ENODEV;
  260. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  261. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  262. /* should we mute HP at startup - burning power ?*/
  263. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  264. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  265. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  266. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  267. poodle_snd_device = platform_device_alloc("soc-audio", -1);
  268. if (!poodle_snd_device)
  269. return -ENOMEM;
  270. platform_set_drvdata(poodle_snd_device, &poodle_snd_devdata);
  271. poodle_snd_devdata.dev = &poodle_snd_device->dev;
  272. ret = platform_device_add(poodle_snd_device);
  273. if (ret)
  274. platform_device_put(poodle_snd_device);
  275. return ret;
  276. }
  277. static void __exit poodle_exit(void)
  278. {
  279. platform_device_unregister(poodle_snd_device);
  280. }
  281. module_init(poodle_init);
  282. module_exit(poodle_exit);
  283. /* Module information */
  284. MODULE_AUTHOR("Richard Purdie");
  285. MODULE_DESCRIPTION("ALSA SoC Poodle");
  286. MODULE_LICENSE("GPL");