poodle.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/driver.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 <asm/arch/pxa-regs.h>
  29. #include <asm/arch/hardware.h>
  30. #include <asm/arch/poodle.h>
  31. #include <asm/arch/audio.h>
  32. #include "../codecs/wm8731.h"
  33. #include "pxa2xx-pcm.h"
  34. #include "pxa2xx-i2s.h"
  35. #define POODLE_HP 1
  36. #define POODLE_HP_OFF 0
  37. #define POODLE_SPK_ON 1
  38. #define POODLE_SPK_OFF 0
  39. /* audio clock in Hz - rounded from 12.235MHz */
  40. #define POODLE_AUDIO_CLOCK 12288000
  41. static int poodle_jack_func;
  42. static int poodle_spk_func;
  43. static void poodle_ext_control(struct snd_soc_codec *codec)
  44. {
  45. int spk = 0;
  46. /* set up jack connection */
  47. if (poodle_jack_func == POODLE_HP) {
  48. /* set = unmute headphone */
  49. locomo_gpio_write(&poodle_locomo_device.dev,
  50. POODLE_LOCOMO_GPIO_MUTE_L, 1);
  51. locomo_gpio_write(&poodle_locomo_device.dev,
  52. POODLE_LOCOMO_GPIO_MUTE_R, 1);
  53. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 1);
  54. } else {
  55. locomo_gpio_write(&poodle_locomo_device.dev,
  56. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  57. locomo_gpio_write(&poodle_locomo_device.dev,
  58. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  59. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
  60. }
  61. if (poodle_spk_func == POODLE_SPK_ON)
  62. spk = 1;
  63. /* set the enpoints to their new connetion states */
  64. snd_soc_dapm_set_endpoint(codec, "Ext Spk", spk);
  65. /* signal a DAPM event */
  66. snd_soc_dapm_sync_endpoints(codec);
  67. }
  68. static int poodle_startup(struct snd_pcm_substream *substream)
  69. {
  70. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  71. struct snd_soc_codec *codec = rtd->socdev->codec;
  72. /* check the jack status at stream startup */
  73. poodle_ext_control(codec);
  74. return 0;
  75. }
  76. /* we need to unmute the HP at shutdown as the mute burns power on poodle */
  77. static int poodle_shutdown(struct snd_pcm_substream *substream)
  78. {
  79. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  80. struct snd_soc_codec *codec = rtd->socdev->codec;
  81. /* set = unmute headphone */
  82. locomo_gpio_write(&poodle_locomo_device.dev,
  83. POODLE_LOCOMO_GPIO_MUTE_L, 1);
  84. locomo_gpio_write(&poodle_locomo_device.dev,
  85. POODLE_LOCOMO_GPIO_MUTE_R, 1);
  86. return 0;
  87. }
  88. static int poodle_hw_params(struct snd_pcm_substream *substream,
  89. struct snd_pcm_hw_params *params)
  90. {
  91. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  92. struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
  93. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  94. unsigned int clk = 0;
  95. int ret = 0;
  96. switch (params_rate(params)) {
  97. case 8000:
  98. case 16000:
  99. case 48000:
  100. case 96000:
  101. clk = 12288000;
  102. break;
  103. case 11025:
  104. case 22050:
  105. case 44100:
  106. clk = 11289600;
  107. break;
  108. }
  109. /* set codec DAI configuration */
  110. ret = codec_dai->dai_ops.set_fmt(codec_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 cpu DAI configuration */
  115. ret = cpu_dai->dai_ops.set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  116. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  117. if (ret < 0)
  118. return ret;
  119. /* set the codec system clock for DAC and ADC */
  120. ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8731_SYSCLK, clk,
  121. SND_SOC_CLOCK_IN);
  122. if (ret < 0)
  123. return ret;
  124. /* set the I2S system clock as input (unused) */
  125. ret = cpu_dai->dai_ops.set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  126. SND_SOC_CLOCK_IN);
  127. if (ret < 0)
  128. return ret;
  129. return 0;
  130. }
  131. static struct snd_soc_ops poodle_ops = {
  132. .startup = poodle_startup,
  133. .hw_params = poodle_hw_params,
  134. .shutdown = poodle_shutdown,
  135. };
  136. static int poodle_get_jack(struct snd_kcontrol *kcontrol,
  137. struct snd_ctl_elem_value *ucontrol)
  138. {
  139. ucontrol->value.integer.value[0] = poodle_jack_func;
  140. return 0;
  141. }
  142. static int poodle_set_jack(struct snd_kcontrol *kcontrol,
  143. struct snd_ctl_elem_value *ucontrol)
  144. {
  145. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  146. if (poodle_jack_func == ucontrol->value.integer.value[0])
  147. return 0;
  148. poodle_jack_func = ucontrol->value.integer.value[0];
  149. poodle_ext_control(codec);
  150. return 1;
  151. }
  152. static int poodle_get_spk(struct snd_kcontrol *kcontrol,
  153. struct snd_ctl_elem_value *ucontrol)
  154. {
  155. ucontrol->value.integer.value[0] = poodle_spk_func;
  156. return 0;
  157. }
  158. static int poodle_set_spk(struct snd_kcontrol *kcontrol,
  159. struct snd_ctl_elem_value *ucontrol)
  160. {
  161. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  162. if (poodle_spk_func == ucontrol->value.integer.value[0])
  163. return 0;
  164. poodle_spk_func = ucontrol->value.integer.value[0];
  165. poodle_ext_control(codec);
  166. return 1;
  167. }
  168. static int poodle_amp_event(struct snd_soc_dapm_widget *w, 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 audio_mapnections to the codec pins */
  184. static const char *audio_map[][3] = {
  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. {NULL, NULL, NULL},
  192. };
  193. static const char *jack_function[] = {"Off", "Headphone"};
  194. static const char *spk_function[] = {"Off", "On"};
  195. static const struct soc_enum poodle_enum[] = {
  196. SOC_ENUM_SINGLE_EXT(2, jack_function),
  197. SOC_ENUM_SINGLE_EXT(2, spk_function),
  198. };
  199. static const snd_kcontrol_new_t wm8731_poodle_controls[] = {
  200. SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
  201. poodle_set_jack),
  202. SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
  203. poodle_set_spk),
  204. };
  205. /*
  206. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  207. */
  208. static int poodle_wm8731_init(struct snd_soc_codec *codec)
  209. {
  210. int i, err;
  211. snd_soc_dapm_set_endpoint(codec, "LLINEIN", 0);
  212. snd_soc_dapm_set_endpoint(codec, "RLINEIN", 0);
  213. snd_soc_dapm_set_endpoint(codec, "MICIN", 1);
  214. /* Add poodle specific controls */
  215. for (i = 0; i < ARRAY_SIZE(wm8731_poodle_controls); i++) {
  216. err = snd_ctl_add(codec->card,
  217. snd_soc_cnew(&wm8731_poodle_controls[i],codec, NULL));
  218. if (err < 0)
  219. return err;
  220. }
  221. /* Add poodle specific widgets */
  222. for (i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) {
  223. snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
  224. }
  225. /* Set up poodle specific audio path audio_map */
  226. for (i = 0; audio_map[i][0] != NULL; i++) {
  227. snd_soc_dapm_connect_input(codec, audio_map[i][0],
  228. audio_map[i][1], audio_map[i][2]);
  229. }
  230. snd_soc_dapm_sync_endpoints(codec);
  231. return 0;
  232. }
  233. /* poodle digital audio interface glue - connects codec <--> CPU */
  234. static struct snd_soc_dai_link poodle_dai = {
  235. .name = "WM8731",
  236. .stream_name = "WM8731",
  237. .cpu_dai = &pxa_i2s_dai,
  238. .codec_dai = &wm8731_dai,
  239. .init = poodle_wm8731_init,
  240. .ops = &poodle_ops,
  241. };
  242. /* poodle audio machine driver */
  243. static struct snd_soc_machine snd_soc_machine_poodle = {
  244. .name = "Poodle",
  245. .dai_link = &poodle_dai,
  246. .num_links = 1,
  247. };
  248. /* poodle audio private data */
  249. static struct wm8731_setup_data poodle_wm8731_setup = {
  250. .i2c_address = 0x1b,
  251. };
  252. /* poodle audio subsystem */
  253. static struct snd_soc_device poodle_snd_devdata = {
  254. .machine = &snd_soc_machine_poodle,
  255. .platform = &pxa2xx_soc_platform,
  256. .codec_dev = &soc_codec_dev_wm8731,
  257. .codec_data = &poodle_wm8731_setup,
  258. };
  259. static struct platform_device *poodle_snd_device;
  260. static int __init poodle_init(void)
  261. {
  262. int ret;
  263. if (!machine_is_poodle())
  264. return -ENODEV;
  265. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  266. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  267. /* should we mute HP at startup - burning power ?*/
  268. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  269. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  270. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  271. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  272. poodle_snd_device = platform_device_alloc("soc-audio", -1);
  273. if (!poodle_snd_device)
  274. return -ENOMEM;
  275. platform_set_drvdata(poodle_snd_device, &poodle_snd_devdata);
  276. poodle_snd_devdata.dev = &poodle_snd_device->dev;
  277. ret = platform_device_add(poodle_snd_device);
  278. if (ret)
  279. platform_device_put(poodle_snd_device);
  280. return ret;
  281. }
  282. static void __exit poodle_exit(void)
  283. {
  284. platform_device_unregister(poodle_snd_device);
  285. }
  286. module_init(poodle_init);
  287. module_exit(poodle_exit);
  288. /* Module information */
  289. MODULE_AUTHOR("Richard Purdie");
  290. MODULE_DESCRIPTION("ALSA SoC Poodle");
  291. MODULE_LICENSE("GPL");