poodle.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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/pxa-regs.h>
  29. #include <mach/hardware.h>
  30. #include <mach/poodle.h>
  31. #include <mach/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. /* set up jack connection */
  46. if (poodle_jack_func == POODLE_HP) {
  47. /* set = unmute headphone */
  48. locomo_gpio_write(&poodle_locomo_device.dev,
  49. POODLE_LOCOMO_GPIO_MUTE_L, 1);
  50. locomo_gpio_write(&poodle_locomo_device.dev,
  51. POODLE_LOCOMO_GPIO_MUTE_R, 1);
  52. snd_soc_dapm_enable_pin(codec, "Headphone Jack");
  53. } else {
  54. locomo_gpio_write(&poodle_locomo_device.dev,
  55. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  56. locomo_gpio_write(&poodle_locomo_device.dev,
  57. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  58. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  59. }
  60. /* set the enpoints to their new connetion states */
  61. if (poodle_spk_func == POODLE_SPK_ON)
  62. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  63. else
  64. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  65. /* signal a DAPM event */
  66. snd_soc_dapm_sync(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->card->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 void poodle_shutdown(struct snd_pcm_substream *substream)
  78. {
  79. /* set = unmute headphone */
  80. locomo_gpio_write(&poodle_locomo_device.dev,
  81. POODLE_LOCOMO_GPIO_MUTE_L, 1);
  82. locomo_gpio_write(&poodle_locomo_device.dev,
  83. POODLE_LOCOMO_GPIO_MUTE_R, 1);
  84. }
  85. static int poodle_hw_params(struct snd_pcm_substream *substream,
  86. struct snd_pcm_hw_params *params)
  87. {
  88. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  89. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  90. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  91. unsigned int clk = 0;
  92. int ret = 0;
  93. switch (params_rate(params)) {
  94. case 8000:
  95. case 16000:
  96. case 48000:
  97. case 96000:
  98. clk = 12288000;
  99. break;
  100. case 11025:
  101. case 22050:
  102. case 44100:
  103. clk = 11289600;
  104. break;
  105. }
  106. /* set codec DAI configuration */
  107. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  108. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  109. if (ret < 0)
  110. return ret;
  111. /* set cpu DAI configuration */
  112. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  113. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  114. if (ret < 0)
  115. return ret;
  116. /* set the codec system clock for DAC and ADC */
  117. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
  118. SND_SOC_CLOCK_IN);
  119. if (ret < 0)
  120. return ret;
  121. /* set the I2S system clock as input (unused) */
  122. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  123. SND_SOC_CLOCK_IN);
  124. if (ret < 0)
  125. return ret;
  126. return 0;
  127. }
  128. static struct snd_soc_ops poodle_ops = {
  129. .startup = poodle_startup,
  130. .hw_params = poodle_hw_params,
  131. .shutdown = poodle_shutdown,
  132. };
  133. static int poodle_get_jack(struct snd_kcontrol *kcontrol,
  134. struct snd_ctl_elem_value *ucontrol)
  135. {
  136. ucontrol->value.integer.value[0] = poodle_jack_func;
  137. return 0;
  138. }
  139. static int poodle_set_jack(struct snd_kcontrol *kcontrol,
  140. struct snd_ctl_elem_value *ucontrol)
  141. {
  142. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  143. if (poodle_jack_func == ucontrol->value.integer.value[0])
  144. return 0;
  145. poodle_jack_func = ucontrol->value.integer.value[0];
  146. poodle_ext_control(codec);
  147. return 1;
  148. }
  149. static int poodle_get_spk(struct snd_kcontrol *kcontrol,
  150. struct snd_ctl_elem_value *ucontrol)
  151. {
  152. ucontrol->value.integer.value[0] = poodle_spk_func;
  153. return 0;
  154. }
  155. static int poodle_set_spk(struct snd_kcontrol *kcontrol,
  156. struct snd_ctl_elem_value *ucontrol)
  157. {
  158. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  159. if (poodle_spk_func == ucontrol->value.integer.value[0])
  160. return 0;
  161. poodle_spk_func = ucontrol->value.integer.value[0];
  162. poodle_ext_control(codec);
  163. return 1;
  164. }
  165. static int poodle_amp_event(struct snd_soc_dapm_widget *w,
  166. struct snd_kcontrol *k, int event)
  167. {
  168. if (SND_SOC_DAPM_EVENT_ON(event))
  169. locomo_gpio_write(&poodle_locomo_device.dev,
  170. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  171. else
  172. locomo_gpio_write(&poodle_locomo_device.dev,
  173. POODLE_LOCOMO_GPIO_AMP_ON, 1);
  174. return 0;
  175. }
  176. /* poodle machine dapm widgets */
  177. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  178. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  179. SND_SOC_DAPM_SPK("Ext Spk", poodle_amp_event),
  180. };
  181. /* Corgi machine connections to the codec pins */
  182. static const struct snd_soc_dapm_route audio_map[] = {
  183. /* headphone connected to LHPOUT1, RHPOUT1 */
  184. {"Headphone Jack", NULL, "LHPOUT"},
  185. {"Headphone Jack", NULL, "RHPOUT"},
  186. /* speaker connected to LOUT, ROUT */
  187. {"Ext Spk", NULL, "ROUT"},
  188. {"Ext Spk", NULL, "LOUT"},
  189. };
  190. static const char *jack_function[] = {"Off", "Headphone"};
  191. static const char *spk_function[] = {"Off", "On"};
  192. static const struct soc_enum poodle_enum[] = {
  193. SOC_ENUM_SINGLE_EXT(2, jack_function),
  194. SOC_ENUM_SINGLE_EXT(2, spk_function),
  195. };
  196. static const struct snd_kcontrol_new wm8731_poodle_controls[] = {
  197. SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
  198. poodle_set_jack),
  199. SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
  200. poodle_set_spk),
  201. };
  202. /*
  203. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  204. */
  205. static int poodle_wm8731_init(struct snd_soc_codec *codec)
  206. {
  207. int err;
  208. snd_soc_dapm_nc_pin(codec, "LLINEIN");
  209. snd_soc_dapm_nc_pin(codec, "RLINEIN");
  210. snd_soc_dapm_enable_pin(codec, "MICIN");
  211. /* Add poodle specific controls */
  212. err = snd_soc_add_controls(codec, wm8731_poodle_controls,
  213. ARRAY_SIZE(wm8731_poodle_controls));
  214. if (err < 0)
  215. return err;
  216. /* Add poodle specific widgets */
  217. snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
  218. ARRAY_SIZE(wm8731_dapm_widgets));
  219. /* Set up poodle specific audio path audio_map */
  220. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  221. snd_soc_dapm_sync(codec);
  222. return 0;
  223. }
  224. /* poodle digital audio interface glue - connects codec <--> CPU */
  225. static struct snd_soc_dai_link poodle_dai = {
  226. .name = "WM8731",
  227. .stream_name = "WM8731",
  228. .cpu_dai = &pxa_i2s_dai,
  229. .codec_dai = &wm8731_dai,
  230. .init = poodle_wm8731_init,
  231. .ops = &poodle_ops,
  232. };
  233. /* poodle audio machine driver */
  234. static struct snd_soc_card snd_soc_poodle = {
  235. .name = "Poodle",
  236. .platform = &pxa2xx_soc_platform,
  237. .dai_link = &poodle_dai,
  238. .num_links = 1,
  239. };
  240. /*
  241. * FIXME: This is a temporary bodge to avoid cross-tree merge issues.
  242. * New drivers should register the wm8731 I2C device in the machine
  243. * setup code (under arch/arm for ARM systems).
  244. */
  245. static int wm8731_i2c_register(void)
  246. {
  247. struct i2c_board_info info;
  248. struct i2c_adapter *adapter;
  249. struct i2c_client *client;
  250. memset(&info, 0, sizeof(struct i2c_board_info));
  251. info.addr = 0x1b;
  252. strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
  253. adapter = i2c_get_adapter(0);
  254. if (!adapter) {
  255. printk(KERN_ERR "can't get i2c adapter 0\n");
  256. return -ENODEV;
  257. }
  258. client = i2c_new_device(adapter, &info);
  259. i2c_put_adapter(adapter);
  260. if (!client) {
  261. printk(KERN_ERR "can't add i2c device at 0x%x\n",
  262. (unsigned int)info.addr);
  263. return -ENODEV;
  264. }
  265. return 0;
  266. }
  267. /* poodle audio subsystem */
  268. static struct snd_soc_device poodle_snd_devdata = {
  269. .card = &snd_soc_poodle,
  270. .codec_dev = &soc_codec_dev_wm8731,
  271. };
  272. static struct platform_device *poodle_snd_device;
  273. static int __init poodle_init(void)
  274. {
  275. int ret;
  276. if (!machine_is_poodle())
  277. return -ENODEV;
  278. ret = wm8731_i2c_register();
  279. if (ret != 0)
  280. return ret;
  281. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  282. POODLE_LOCOMO_GPIO_AMP_ON, 0);
  283. /* should we mute HP at startup - burning power ?*/
  284. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  285. POODLE_LOCOMO_GPIO_MUTE_L, 0);
  286. locomo_gpio_set_dir(&poodle_locomo_device.dev,
  287. POODLE_LOCOMO_GPIO_MUTE_R, 0);
  288. poodle_snd_device = platform_device_alloc("soc-audio", -1);
  289. if (!poodle_snd_device)
  290. return -ENOMEM;
  291. platform_set_drvdata(poodle_snd_device, &poodle_snd_devdata);
  292. poodle_snd_devdata.dev = &poodle_snd_device->dev;
  293. ret = platform_device_add(poodle_snd_device);
  294. if (ret)
  295. platform_device_put(poodle_snd_device);
  296. return ret;
  297. }
  298. static void __exit poodle_exit(void)
  299. {
  300. platform_device_unregister(poodle_snd_device);
  301. }
  302. module_init(poodle_init);
  303. module_exit(poodle_exit);
  304. /* Module information */
  305. MODULE_AUTHOR("Richard Purdie");
  306. MODULE_DESCRIPTION("ALSA SoC Poodle");
  307. MODULE_LICENSE("GPL");