spitz.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * spitz.c -- SoC audio for Sharp SL-Cxx00 models Spitz, Borzoi and Akita
  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/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 <asm/mach-types.h>
  26. #include <mach/spitz.h>
  27. #include "../codecs/wm8750.h"
  28. #include "pxa2xx-i2s.h"
  29. #define SPITZ_HP 0
  30. #define SPITZ_MIC 1
  31. #define SPITZ_LINE 2
  32. #define SPITZ_HEADSET 3
  33. #define SPITZ_HP_OFF 4
  34. #define SPITZ_SPK_ON 0
  35. #define SPITZ_SPK_OFF 1
  36. /* audio clock in Hz - rounded from 12.235MHz */
  37. #define SPITZ_AUDIO_CLOCK 12288000
  38. static int spitz_jack_func;
  39. static int spitz_spk_func;
  40. static void spitz_ext_control(struct snd_soc_codec *codec)
  41. {
  42. struct snd_soc_dapm_context *dapm = &codec->dapm;
  43. if (spitz_spk_func == SPITZ_SPK_ON)
  44. snd_soc_dapm_enable_pin(dapm, "Ext Spk");
  45. else
  46. snd_soc_dapm_disable_pin(dapm, "Ext Spk");
  47. /* set up jack connection */
  48. switch (spitz_jack_func) {
  49. case SPITZ_HP:
  50. /* enable and unmute hp jack, disable mic bias */
  51. snd_soc_dapm_disable_pin(dapm, "Headset Jack");
  52. snd_soc_dapm_disable_pin(dapm, "Mic Jack");
  53. snd_soc_dapm_disable_pin(dapm, "Line Jack");
  54. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  55. gpio_set_value(SPITZ_GPIO_MUTE_L, 1);
  56. gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
  57. break;
  58. case SPITZ_MIC:
  59. /* enable mic jack and bias, mute hp */
  60. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  61. snd_soc_dapm_disable_pin(dapm, "Headset Jack");
  62. snd_soc_dapm_disable_pin(dapm, "Line Jack");
  63. snd_soc_dapm_enable_pin(dapm, "Mic Jack");
  64. gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
  65. gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
  66. break;
  67. case SPITZ_LINE:
  68. /* enable line jack, disable mic bias and mute hp */
  69. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  70. snd_soc_dapm_disable_pin(dapm, "Headset Jack");
  71. snd_soc_dapm_disable_pin(dapm, "Mic Jack");
  72. snd_soc_dapm_enable_pin(dapm, "Line Jack");
  73. gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
  74. gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
  75. break;
  76. case SPITZ_HEADSET:
  77. /* enable and unmute headset jack enable mic bias, mute L hp */
  78. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  79. snd_soc_dapm_enable_pin(dapm, "Mic Jack");
  80. snd_soc_dapm_disable_pin(dapm, "Line Jack");
  81. snd_soc_dapm_enable_pin(dapm, "Headset Jack");
  82. gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
  83. gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
  84. break;
  85. case SPITZ_HP_OFF:
  86. /* jack removed, everything off */
  87. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  88. snd_soc_dapm_disable_pin(dapm, "Headset Jack");
  89. snd_soc_dapm_disable_pin(dapm, "Mic Jack");
  90. snd_soc_dapm_disable_pin(dapm, "Line Jack");
  91. gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
  92. gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
  93. break;
  94. }
  95. snd_soc_dapm_sync(dapm);
  96. }
  97. static int spitz_startup(struct snd_pcm_substream *substream)
  98. {
  99. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100. struct snd_soc_codec *codec = rtd->codec;
  101. mutex_lock(&codec->mutex);
  102. /* check the jack status at stream startup */
  103. spitz_ext_control(codec);
  104. mutex_unlock(&codec->mutex);
  105. return 0;
  106. }
  107. static int spitz_hw_params(struct snd_pcm_substream *substream,
  108. struct snd_pcm_hw_params *params)
  109. {
  110. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  111. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  112. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  113. unsigned int clk = 0;
  114. int ret = 0;
  115. switch (params_rate(params)) {
  116. case 8000:
  117. case 16000:
  118. case 48000:
  119. case 96000:
  120. clk = 12288000;
  121. break;
  122. case 11025:
  123. case 22050:
  124. case 44100:
  125. clk = 11289600;
  126. break;
  127. }
  128. /* set codec DAI configuration */
  129. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  130. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  131. if (ret < 0)
  132. return ret;
  133. /* set cpu DAI configuration */
  134. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  135. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  136. if (ret < 0)
  137. return ret;
  138. /* set the codec system clock for DAC and ADC */
  139. ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
  140. SND_SOC_CLOCK_IN);
  141. if (ret < 0)
  142. return ret;
  143. /* set the I2S system clock as input (unused) */
  144. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  145. SND_SOC_CLOCK_IN);
  146. if (ret < 0)
  147. return ret;
  148. return 0;
  149. }
  150. static struct snd_soc_ops spitz_ops = {
  151. .startup = spitz_startup,
  152. .hw_params = spitz_hw_params,
  153. };
  154. static int spitz_get_jack(struct snd_kcontrol *kcontrol,
  155. struct snd_ctl_elem_value *ucontrol)
  156. {
  157. ucontrol->value.integer.value[0] = spitz_jack_func;
  158. return 0;
  159. }
  160. static int spitz_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 (spitz_jack_func == ucontrol->value.integer.value[0])
  165. return 0;
  166. spitz_jack_func = ucontrol->value.integer.value[0];
  167. spitz_ext_control(codec);
  168. return 1;
  169. }
  170. static int spitz_get_spk(struct snd_kcontrol *kcontrol,
  171. struct snd_ctl_elem_value *ucontrol)
  172. {
  173. ucontrol->value.integer.value[0] = spitz_spk_func;
  174. return 0;
  175. }
  176. static int spitz_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 (spitz_spk_func == ucontrol->value.integer.value[0])
  181. return 0;
  182. spitz_spk_func = ucontrol->value.integer.value[0];
  183. spitz_ext_control(codec);
  184. return 1;
  185. }
  186. static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
  187. struct snd_kcontrol *k, int event)
  188. {
  189. if (machine_is_borzoi() || machine_is_spitz())
  190. gpio_set_value(SPITZ_GPIO_MIC_BIAS,
  191. SND_SOC_DAPM_EVENT_ON(event));
  192. if (machine_is_akita())
  193. gpio_set_value(AKITA_GPIO_MIC_BIAS,
  194. SND_SOC_DAPM_EVENT_ON(event));
  195. return 0;
  196. }
  197. /* spitz machine dapm widgets */
  198. static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
  199. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  200. SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
  201. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  202. SND_SOC_DAPM_LINE("Line Jack", NULL),
  203. /* headset is a mic and mono headphone */
  204. SND_SOC_DAPM_HP("Headset Jack", NULL),
  205. };
  206. /* Spitz machine audio_map */
  207. static const struct snd_soc_dapm_route audio_map[] = {
  208. /* headphone connected to LOUT1, ROUT1 */
  209. {"Headphone Jack", NULL, "LOUT1"},
  210. {"Headphone Jack", NULL, "ROUT1"},
  211. /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
  212. {"Headset Jack", NULL, "ROUT1"},
  213. /* ext speaker connected to LOUT2, ROUT2 */
  214. {"Ext Spk", NULL , "ROUT2"},
  215. {"Ext Spk", NULL , "LOUT2"},
  216. /* mic is connected to input 1 - with bias */
  217. {"LINPUT1", NULL, "Mic Bias"},
  218. {"Mic Bias", NULL, "Mic Jack"},
  219. /* line is connected to input 1 - no bias */
  220. {"LINPUT1", NULL, "Line Jack"},
  221. };
  222. static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
  223. "Off"};
  224. static const char *spk_function[] = {"On", "Off"};
  225. static const struct soc_enum spitz_enum[] = {
  226. SOC_ENUM_SINGLE_EXT(5, jack_function),
  227. SOC_ENUM_SINGLE_EXT(2, spk_function),
  228. };
  229. static const struct snd_kcontrol_new wm8750_spitz_controls[] = {
  230. SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
  231. spitz_set_jack),
  232. SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
  233. spitz_set_spk),
  234. };
  235. /*
  236. * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device
  237. */
  238. static int spitz_wm8750_init(struct snd_soc_pcm_runtime *rtd)
  239. {
  240. struct snd_soc_codec *codec = rtd->codec;
  241. struct snd_soc_dapm_context *dapm = &codec->dapm;
  242. int err;
  243. /* NC codec pins */
  244. snd_soc_dapm_nc_pin(dapm, "RINPUT1");
  245. snd_soc_dapm_nc_pin(dapm, "LINPUT2");
  246. snd_soc_dapm_nc_pin(dapm, "RINPUT2");
  247. snd_soc_dapm_nc_pin(dapm, "LINPUT3");
  248. snd_soc_dapm_nc_pin(dapm, "RINPUT3");
  249. snd_soc_dapm_nc_pin(dapm, "OUT3");
  250. snd_soc_dapm_nc_pin(dapm, "MONO1");
  251. /* Add spitz specific controls */
  252. err = snd_soc_add_controls(codec, wm8750_spitz_controls,
  253. ARRAY_SIZE(wm8750_spitz_controls));
  254. if (err < 0)
  255. return err;
  256. /* Add spitz specific widgets */
  257. snd_soc_dapm_new_controls(dapm, wm8750_dapm_widgets,
  258. ARRAY_SIZE(wm8750_dapm_widgets));
  259. /* Set up spitz specific audio paths */
  260. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  261. snd_soc_dapm_sync(dapm);
  262. return 0;
  263. }
  264. /* spitz digital audio interface glue - connects codec <--> CPU */
  265. static struct snd_soc_dai_link spitz_dai = {
  266. .name = "wm8750",
  267. .stream_name = "WM8750",
  268. .cpu_dai_name = "pxa2xx-i2s",
  269. .codec_dai_name = "wm8750-hifi",
  270. .platform_name = "pxa-pcm-audio",
  271. .codec_name = "wm8750-codec.0-001b",
  272. .init = spitz_wm8750_init,
  273. .ops = &spitz_ops,
  274. };
  275. /* spitz audio machine driver */
  276. static struct snd_soc_card snd_soc_spitz = {
  277. .name = "Spitz",
  278. .dai_link = &spitz_dai,
  279. .num_links = 1,
  280. };
  281. static struct platform_device *spitz_snd_device;
  282. static int __init spitz_init(void)
  283. {
  284. int ret;
  285. if (!(machine_is_spitz() || machine_is_borzoi() || machine_is_akita()))
  286. return -ENODEV;
  287. spitz_snd_device = platform_device_alloc("soc-audio", -1);
  288. if (!spitz_snd_device)
  289. return -ENOMEM;
  290. platform_set_drvdata(spitz_snd_device, &snd_soc_spitz);
  291. ret = platform_device_add(spitz_snd_device);
  292. if (ret)
  293. platform_device_put(spitz_snd_device);
  294. return ret;
  295. }
  296. static void __exit spitz_exit(void)
  297. {
  298. platform_device_unregister(spitz_snd_device);
  299. }
  300. module_init(spitz_init);
  301. module_exit(spitz_exit);
  302. MODULE_AUTHOR("Richard Purdie");
  303. MODULE_DESCRIPTION("ALSA SoC Spitz");
  304. MODULE_LICENSE("GPL");