spitz.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 int spitz_mic_gpio;
  41. static void spitz_ext_control(struct snd_soc_dapm_context *dapm)
  42. {
  43. snd_soc_dapm_mutex_lock(dapm);
  44. if (spitz_spk_func == SPITZ_SPK_ON)
  45. snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
  46. else
  47. snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
  48. /* set up jack connection */
  49. switch (spitz_jack_func) {
  50. case SPITZ_HP:
  51. /* enable and unmute hp jack, disable mic bias */
  52. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  53. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
  54. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  55. snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
  56. gpio_set_value(SPITZ_GPIO_MUTE_L, 1);
  57. gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
  58. break;
  59. case SPITZ_MIC:
  60. /* enable mic jack and bias, mute hp */
  61. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  62. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  63. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  64. snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
  65. gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
  66. gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
  67. break;
  68. case SPITZ_LINE:
  69. /* enable line jack, disable mic bias and mute hp */
  70. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  71. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  72. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
  73. snd_soc_dapm_enable_pin_unlocked(dapm, "Line Jack");
  74. gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
  75. gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
  76. break;
  77. case SPITZ_HEADSET:
  78. /* enable and unmute headset jack enable mic bias, mute L hp */
  79. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  80. snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
  81. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  82. snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
  83. gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
  84. gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
  85. break;
  86. case SPITZ_HP_OFF:
  87. /* jack removed, everything off */
  88. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  89. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  90. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
  91. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  92. gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
  93. gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
  94. break;
  95. }
  96. snd_soc_dapm_sync_unlocked(dapm);
  97. snd_soc_dapm_mutex_unlock(dapm);
  98. }
  99. static int spitz_startup(struct snd_pcm_substream *substream)
  100. {
  101. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  102. /* check the jack status at stream startup */
  103. spitz_ext_control(&rtd->card->dapm);
  104. return 0;
  105. }
  106. static int spitz_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->codec_dai;
  111. struct snd_soc_dai *cpu_dai = rtd->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 the codec system clock for DAC and ADC */
  128. ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
  129. SND_SOC_CLOCK_IN);
  130. if (ret < 0)
  131. return ret;
  132. /* set the I2S system clock as input (unused) */
  133. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  134. SND_SOC_CLOCK_IN);
  135. if (ret < 0)
  136. return ret;
  137. return 0;
  138. }
  139. static struct snd_soc_ops spitz_ops = {
  140. .startup = spitz_startup,
  141. .hw_params = spitz_hw_params,
  142. };
  143. static int spitz_get_jack(struct snd_kcontrol *kcontrol,
  144. struct snd_ctl_elem_value *ucontrol)
  145. {
  146. ucontrol->value.enumerated.item[0] = spitz_jack_func;
  147. return 0;
  148. }
  149. static int spitz_set_jack(struct snd_kcontrol *kcontrol,
  150. struct snd_ctl_elem_value *ucontrol)
  151. {
  152. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  153. if (spitz_jack_func == ucontrol->value.enumerated.item[0])
  154. return 0;
  155. spitz_jack_func = ucontrol->value.enumerated.item[0];
  156. spitz_ext_control(&card->dapm);
  157. return 1;
  158. }
  159. static int spitz_get_spk(struct snd_kcontrol *kcontrol,
  160. struct snd_ctl_elem_value *ucontrol)
  161. {
  162. ucontrol->value.enumerated.item[0] = spitz_spk_func;
  163. return 0;
  164. }
  165. static int spitz_set_spk(struct snd_kcontrol *kcontrol,
  166. struct snd_ctl_elem_value *ucontrol)
  167. {
  168. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  169. if (spitz_spk_func == ucontrol->value.enumerated.item[0])
  170. return 0;
  171. spitz_spk_func = ucontrol->value.enumerated.item[0];
  172. spitz_ext_control(&card->dapm);
  173. return 1;
  174. }
  175. static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
  176. struct snd_kcontrol *k, int event)
  177. {
  178. gpio_set_value_cansleep(spitz_mic_gpio, SND_SOC_DAPM_EVENT_ON(event));
  179. return 0;
  180. }
  181. /* spitz machine dapm widgets */
  182. static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
  183. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  184. SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
  185. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  186. SND_SOC_DAPM_LINE("Line Jack", NULL),
  187. /* headset is a mic and mono headphone */
  188. SND_SOC_DAPM_HP("Headset Jack", NULL),
  189. };
  190. /* Spitz machine audio_map */
  191. static const struct snd_soc_dapm_route spitz_audio_map[] = {
  192. /* headphone connected to LOUT1, ROUT1 */
  193. {"Headphone Jack", NULL, "LOUT1"},
  194. {"Headphone Jack", NULL, "ROUT1"},
  195. /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
  196. {"Headset Jack", NULL, "ROUT1"},
  197. /* ext speaker connected to LOUT2, ROUT2 */
  198. {"Ext Spk", NULL , "ROUT2"},
  199. {"Ext Spk", NULL , "LOUT2"},
  200. /* mic is connected to input 1 - with bias */
  201. {"LINPUT1", NULL, "Mic Bias"},
  202. {"Mic Bias", NULL, "Mic Jack"},
  203. /* line is connected to input 1 - no bias */
  204. {"LINPUT1", NULL, "Line Jack"},
  205. };
  206. static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
  207. "Off"};
  208. static const char *spk_function[] = {"On", "Off"};
  209. static const struct soc_enum spitz_enum[] = {
  210. SOC_ENUM_SINGLE_EXT(5, jack_function),
  211. SOC_ENUM_SINGLE_EXT(2, spk_function),
  212. };
  213. static const struct snd_kcontrol_new wm8750_spitz_controls[] = {
  214. SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
  215. spitz_set_jack),
  216. SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
  217. spitz_set_spk),
  218. };
  219. /* spitz digital audio interface glue - connects codec <--> CPU */
  220. static struct snd_soc_dai_link spitz_dai = {
  221. .name = "wm8750",
  222. .stream_name = "WM8750",
  223. .cpu_dai_name = "pxa2xx-i2s",
  224. .codec_dai_name = "wm8750-hifi",
  225. .platform_name = "pxa-pcm-audio",
  226. .codec_name = "wm8750.0-001b",
  227. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  228. SND_SOC_DAIFMT_CBS_CFS,
  229. .ops = &spitz_ops,
  230. };
  231. /* spitz audio machine driver */
  232. static struct snd_soc_card snd_soc_spitz = {
  233. .name = "Spitz",
  234. .owner = THIS_MODULE,
  235. .dai_link = &spitz_dai,
  236. .num_links = 1,
  237. .controls = wm8750_spitz_controls,
  238. .num_controls = ARRAY_SIZE(wm8750_spitz_controls),
  239. .dapm_widgets = wm8750_dapm_widgets,
  240. .num_dapm_widgets = ARRAY_SIZE(wm8750_dapm_widgets),
  241. .dapm_routes = spitz_audio_map,
  242. .num_dapm_routes = ARRAY_SIZE(spitz_audio_map),
  243. .fully_routed = true,
  244. };
  245. static int spitz_probe(struct platform_device *pdev)
  246. {
  247. struct snd_soc_card *card = &snd_soc_spitz;
  248. int ret;
  249. if (machine_is_akita())
  250. spitz_mic_gpio = AKITA_GPIO_MIC_BIAS;
  251. else
  252. spitz_mic_gpio = SPITZ_GPIO_MIC_BIAS;
  253. ret = gpio_request(spitz_mic_gpio, "MIC GPIO");
  254. if (ret)
  255. goto err1;
  256. ret = gpio_direction_output(spitz_mic_gpio, 0);
  257. if (ret)
  258. goto err2;
  259. card->dev = &pdev->dev;
  260. ret = devm_snd_soc_register_card(&pdev->dev, card);
  261. if (ret) {
  262. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  263. ret);
  264. goto err2;
  265. }
  266. return 0;
  267. err2:
  268. gpio_free(spitz_mic_gpio);
  269. err1:
  270. return ret;
  271. }
  272. static int spitz_remove(struct platform_device *pdev)
  273. {
  274. gpio_free(spitz_mic_gpio);
  275. return 0;
  276. }
  277. static struct platform_driver spitz_driver = {
  278. .driver = {
  279. .name = "spitz-audio",
  280. .pm = &snd_soc_pm_ops,
  281. },
  282. .probe = spitz_probe,
  283. .remove = spitz_remove,
  284. };
  285. module_platform_driver(spitz_driver);
  286. MODULE_AUTHOR("Richard Purdie");
  287. MODULE_DESCRIPTION("ALSA SoC Spitz");
  288. MODULE_LICENSE("GPL");
  289. MODULE_ALIAS("platform:spitz-audio");