PageRenderTime 169ms CodeModel.GetById 17ms RepoModel.GetById 2ms app.codeStats 0ms

/sound/soc/pxa/corgi.c

https://bitbucket.org/abioy/linux
C | 358 lines | 257 code | 58 blank | 43 comment | 17 complexity | 4f05b928e83da16e8721f0d5a95b0765 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * corgi.c -- SoC audio for Corgi
  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. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/timer.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/gpio.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 <mach/corgi.h>
  28. #include <mach/audio.h>
  29. #include "../codecs/wm8731.h"
  30. #include "pxa2xx-pcm.h"
  31. #include "pxa2xx-i2s.h"
  32. #define CORGI_HP 0
  33. #define CORGI_MIC 1
  34. #define CORGI_LINE 2
  35. #define CORGI_HEADSET 3
  36. #define CORGI_HP_OFF 4
  37. #define CORGI_SPK_ON 0
  38. #define CORGI_SPK_OFF 1
  39. /* audio clock in Hz - rounded from 12.235MHz */
  40. #define CORGI_AUDIO_CLOCK 12288000
  41. static int corgi_jack_func;
  42. static int corgi_spk_func;
  43. static void corgi_ext_control(struct snd_soc_codec *codec)
  44. {
  45. /* set up jack connection */
  46. switch (corgi_jack_func) {
  47. case CORGI_HP:
  48. /* set = unmute headphone */
  49. gpio_set_value(CORGI_GPIO_MUTE_L, 1);
  50. gpio_set_value(CORGI_GPIO_MUTE_R, 1);
  51. snd_soc_dapm_disable_pin(codec, "Mic Jack");
  52. snd_soc_dapm_disable_pin(codec, "Line Jack");
  53. snd_soc_dapm_enable_pin(codec, "Headphone Jack");
  54. snd_soc_dapm_disable_pin(codec, "Headset Jack");
  55. break;
  56. case CORGI_MIC:
  57. /* reset = mute headphone */
  58. gpio_set_value(CORGI_GPIO_MUTE_L, 0);
  59. gpio_set_value(CORGI_GPIO_MUTE_R, 0);
  60. snd_soc_dapm_enable_pin(codec, "Mic Jack");
  61. snd_soc_dapm_disable_pin(codec, "Line Jack");
  62. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  63. snd_soc_dapm_disable_pin(codec, "Headset Jack");
  64. break;
  65. case CORGI_LINE:
  66. gpio_set_value(CORGI_GPIO_MUTE_L, 0);
  67. gpio_set_value(CORGI_GPIO_MUTE_R, 0);
  68. snd_soc_dapm_disable_pin(codec, "Mic Jack");
  69. snd_soc_dapm_enable_pin(codec, "Line Jack");
  70. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  71. snd_soc_dapm_disable_pin(codec, "Headset Jack");
  72. break;
  73. case CORGI_HEADSET:
  74. gpio_set_value(CORGI_GPIO_MUTE_L, 0);
  75. gpio_set_value(CORGI_GPIO_MUTE_R, 1);
  76. snd_soc_dapm_enable_pin(codec, "Mic Jack");
  77. snd_soc_dapm_disable_pin(codec, "Line Jack");
  78. snd_soc_dapm_disable_pin(codec, "Headphone Jack");
  79. snd_soc_dapm_enable_pin(codec, "Headset Jack");
  80. break;
  81. }
  82. if (corgi_spk_func == CORGI_SPK_ON)
  83. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  84. else
  85. snd_soc_dapm_disable_pin(codec, "Ext Spk");
  86. /* signal a DAPM event */
  87. snd_soc_dapm_sync(codec);
  88. }
  89. static int corgi_startup(struct snd_pcm_substream *substream)
  90. {
  91. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  92. struct snd_soc_codec *codec = rtd->socdev->card->codec;
  93. /* check the jack status at stream startup */
  94. corgi_ext_control(codec);
  95. return 0;
  96. }
  97. /* we need to unmute the HP at shutdown as the mute burns power on corgi */
  98. static void corgi_shutdown(struct snd_pcm_substream *substream)
  99. {
  100. /* set = unmute headphone */
  101. gpio_set_value(CORGI_GPIO_MUTE_L, 1);
  102. gpio_set_value(CORGI_GPIO_MUTE_R, 1);
  103. }
  104. static int corgi_hw_params(struct snd_pcm_substream *substream,
  105. struct snd_pcm_hw_params *params)
  106. {
  107. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  109. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  110. unsigned int clk = 0;
  111. int ret = 0;
  112. switch (params_rate(params)) {
  113. case 8000:
  114. case 16000:
  115. case 48000:
  116. case 96000:
  117. clk = 12288000;
  118. break;
  119. case 11025:
  120. case 22050:
  121. case 44100:
  122. clk = 11289600;
  123. break;
  124. }
  125. /* set codec DAI configuration */
  126. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  127. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  128. if (ret < 0)
  129. return ret;
  130. /* set cpu DAI configuration */
  131. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  132. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  133. if (ret < 0)
  134. return ret;
  135. /* set the codec system clock for DAC and ADC */
  136. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
  137. SND_SOC_CLOCK_IN);
  138. if (ret < 0)
  139. return ret;
  140. /* set the I2S system clock as input (unused) */
  141. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  142. SND_SOC_CLOCK_IN);
  143. if (ret < 0)
  144. return ret;
  145. return 0;
  146. }
  147. static struct snd_soc_ops corgi_ops = {
  148. .startup = corgi_startup,
  149. .hw_params = corgi_hw_params,
  150. .shutdown = corgi_shutdown,
  151. };
  152. static int corgi_get_jack(struct snd_kcontrol *kcontrol,
  153. struct snd_ctl_elem_value *ucontrol)
  154. {
  155. ucontrol->value.integer.value[0] = corgi_jack_func;
  156. return 0;
  157. }
  158. static int corgi_set_jack(struct snd_kcontrol *kcontrol,
  159. struct snd_ctl_elem_value *ucontrol)
  160. {
  161. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  162. if (corgi_jack_func == ucontrol->value.integer.value[0])
  163. return 0;
  164. corgi_jack_func = ucontrol->value.integer.value[0];
  165. corgi_ext_control(codec);
  166. return 1;
  167. }
  168. static int corgi_get_spk(struct snd_kcontrol *kcontrol,
  169. struct snd_ctl_elem_value *ucontrol)
  170. {
  171. ucontrol->value.integer.value[0] = corgi_spk_func;
  172. return 0;
  173. }
  174. static int corgi_set_spk(struct snd_kcontrol *kcontrol,
  175. struct snd_ctl_elem_value *ucontrol)
  176. {
  177. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  178. if (corgi_spk_func == ucontrol->value.integer.value[0])
  179. return 0;
  180. corgi_spk_func = ucontrol->value.integer.value[0];
  181. corgi_ext_control(codec);
  182. return 1;
  183. }
  184. static int corgi_amp_event(struct snd_soc_dapm_widget *w,
  185. struct snd_kcontrol *k, int event)
  186. {
  187. gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
  188. return 0;
  189. }
  190. static int corgi_mic_event(struct snd_soc_dapm_widget *w,
  191. struct snd_kcontrol *k, int event)
  192. {
  193. gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
  194. return 0;
  195. }
  196. /* corgi machine dapm widgets */
  197. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  198. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  199. SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
  200. SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
  201. SND_SOC_DAPM_LINE("Line Jack", NULL),
  202. SND_SOC_DAPM_HP("Headset Jack", NULL),
  203. };
  204. /* Corgi machine audio map (connections to the codec pins) */
  205. static const struct snd_soc_dapm_route audio_map[] = {
  206. /* headset Jack - in = micin, out = LHPOUT*/
  207. {"Headset Jack", NULL, "LHPOUT"},
  208. /* headphone connected to LHPOUT1, RHPOUT1 */
  209. {"Headphone Jack", NULL, "LHPOUT"},
  210. {"Headphone Jack", NULL, "RHPOUT"},
  211. /* speaker connected to LOUT, ROUT */
  212. {"Ext Spk", NULL, "ROUT"},
  213. {"Ext Spk", NULL, "LOUT"},
  214. /* mic is connected to MICIN (via right channel of headphone jack) */
  215. {"MICIN", NULL, "Mic Jack"},
  216. /* Same as the above but no mic bias for line signals */
  217. {"MICIN", NULL, "Line Jack"},
  218. };
  219. static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
  220. "Off"};
  221. static const char *spk_function[] = {"On", "Off"};
  222. static const struct soc_enum corgi_enum[] = {
  223. SOC_ENUM_SINGLE_EXT(5, jack_function),
  224. SOC_ENUM_SINGLE_EXT(2, spk_function),
  225. };
  226. static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
  227. SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
  228. corgi_set_jack),
  229. SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
  230. corgi_set_spk),
  231. };
  232. /*
  233. * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
  234. */
  235. static int corgi_wm8731_init(struct snd_soc_codec *codec)
  236. {
  237. int err;
  238. snd_soc_dapm_nc_pin(codec, "LLINEIN");
  239. snd_soc_dapm_nc_pin(codec, "RLINEIN");
  240. /* Add corgi specific controls */
  241. err = snd_soc_add_controls(codec, wm8731_corgi_controls,
  242. ARRAY_SIZE(wm8731_corgi_controls));
  243. if (err < 0)
  244. return err;
  245. /* Add corgi specific widgets */
  246. snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
  247. ARRAY_SIZE(wm8731_dapm_widgets));
  248. /* Set up corgi specific audio path audio_map */
  249. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  250. snd_soc_dapm_sync(codec);
  251. return 0;
  252. }
  253. /* corgi digital audio interface glue - connects codec <--> CPU */
  254. static struct snd_soc_dai_link corgi_dai = {
  255. .name = "WM8731",
  256. .stream_name = "WM8731",
  257. .cpu_dai = &pxa_i2s_dai,
  258. .codec_dai = &wm8731_dai,
  259. .init = corgi_wm8731_init,
  260. .ops = &corgi_ops,
  261. };
  262. /* corgi audio machine driver */
  263. static struct snd_soc_card snd_soc_corgi = {
  264. .name = "Corgi",
  265. .platform = &pxa2xx_soc_platform,
  266. .dai_link = &corgi_dai,
  267. .num_links = 1,
  268. };
  269. /* corgi audio subsystem */
  270. static struct snd_soc_device corgi_snd_devdata = {
  271. .card = &snd_soc_corgi,
  272. .codec_dev = &soc_codec_dev_wm8731,
  273. };
  274. static struct platform_device *corgi_snd_device;
  275. static int __init corgi_init(void)
  276. {
  277. int ret;
  278. if (!(machine_is_corgi() || machine_is_shepherd() ||
  279. machine_is_husky()))
  280. return -ENODEV;
  281. corgi_snd_device = platform_device_alloc("soc-audio", -1);
  282. if (!corgi_snd_device)
  283. return -ENOMEM;
  284. platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
  285. corgi_snd_devdata.dev = &corgi_snd_device->dev;
  286. ret = platform_device_add(corgi_snd_device);
  287. if (ret)
  288. platform_device_put(corgi_snd_device);
  289. return ret;
  290. }
  291. static void __exit corgi_exit(void)
  292. {
  293. platform_device_unregister(corgi_snd_device);
  294. }
  295. module_init(corgi_init);
  296. module_exit(corgi_exit);
  297. /* Module information */
  298. MODULE_AUTHOR("Richard Purdie");
  299. MODULE_DESCRIPTION("ALSA SoC Corgi");
  300. MODULE_LICENSE("GPL");