corgi.c 10 KB

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