spitz.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <sound/soc-dapm.h>
  26. #include <asm/mach-types.h>
  27. #include <mach/spitz.h>
  28. #include "../codecs/wm8750.h"
  29. #include "pxa2xx-pcm.h"
  30. #include "pxa2xx-i2s.h"
  31. #define SPITZ_HP 0
  32. #define SPITZ_MIC 1
  33. #define SPITZ_LINE 2
  34. #define SPITZ_HEADSET 3
  35. #define SPITZ_HP_OFF 4
  36. #define SPITZ_SPK_ON 0
  37. #define SPITZ_SPK_OFF 1
  38. /* audio clock in Hz - rounded from 12.235MHz */
  39. #define SPITZ_AUDIO_CLOCK 12288000
  40. static int spitz_jack_func;
  41. static int spitz_spk_func;
  42. static void spitz_ext_control(struct snd_soc_codec *codec)
  43. {
  44. if (spitz_spk_func == SPITZ_SPK_ON)
  45. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  46. else
  47. snd_soc_dapm_disable_pin(codec, "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(codec, "Headset Jack");
  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. 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(codec, "Headphone Jack");
  62. snd_soc_dapm_disable_pin(codec, "Headset Jack");
  63. snd_soc_dapm_disable_pin(codec, "Line Jack");
  64. snd_soc_dapm_enable_pin(codec, "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(codec, "Headphone Jack");
  71. snd_soc_dapm_disable_pin(codec, "Headset Jack");
  72. snd_soc_dapm_disable_pin(codec, "Mic Jack");
  73. snd_soc_dapm_enable_pin(codec, "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(codec, "Headphone Jack");
  80. snd_soc_dapm_enable_pin(codec, "Mic Jack");
  81. snd_soc_dapm_disable_pin(codec, "Line Jack");
  82. snd_soc_dapm_enable_pin(codec, "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(codec, "Headphone Jack");
  89. snd_soc_dapm_disable_pin(codec, "Headset Jack");
  90. snd_soc_dapm_disable_pin(codec, "Mic Jack");
  91. snd_soc_dapm_disable_pin(codec, "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(codec);
  97. }
  98. static int spitz_startup(struct snd_pcm_substream *substream)
  99. {
  100. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  101. struct snd_soc_codec *codec = rtd->socdev->card->codec;
  102. /* check the jack status at stream startup */
  103. spitz_ext_control(codec);
  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->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, WM8750_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 spitz_ops = {
  150. .startup = spitz_startup,
  151. .hw_params = spitz_hw_params,
  152. };
  153. static int spitz_get_jack(struct snd_kcontrol *kcontrol,
  154. struct snd_ctl_elem_value *ucontrol)
  155. {
  156. ucontrol->value.integer.value[0] = spitz_jack_func;
  157. return 0;
  158. }
  159. static int spitz_set_jack(struct snd_kcontrol *kcontrol,
  160. struct snd_ctl_elem_value *ucontrol)
  161. {
  162. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  163. if (spitz_jack_func == ucontrol->value.integer.value[0])
  164. return 0;
  165. spitz_jack_func = ucontrol->value.integer.value[0];
  166. spitz_ext_control(codec);
  167. return 1;
  168. }
  169. static int spitz_get_spk(struct snd_kcontrol *kcontrol,
  170. struct snd_ctl_elem_value *ucontrol)
  171. {
  172. ucontrol->value.integer.value[0] = spitz_spk_func;
  173. return 0;
  174. }
  175. static int spitz_set_spk(struct snd_kcontrol *kcontrol,
  176. struct snd_ctl_elem_value *ucontrol)
  177. {
  178. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  179. if (spitz_spk_func == ucontrol->value.integer.value[0])
  180. return 0;
  181. spitz_spk_func = ucontrol->value.integer.value[0];
  182. spitz_ext_control(codec);
  183. return 1;
  184. }
  185. static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
  186. struct snd_kcontrol *k, int event)
  187. {
  188. if (machine_is_borzoi() || machine_is_spitz())
  189. gpio_set_value(SPITZ_GPIO_MIC_BIAS,
  190. SND_SOC_DAPM_EVENT_ON(event));
  191. if (machine_is_akita())
  192. gpio_set_value(AKITA_GPIO_MIC_BIAS,
  193. SND_SOC_DAPM_EVENT_ON(event));
  194. return 0;
  195. }
  196. /* spitz machine dapm widgets */
  197. static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
  198. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  199. SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
  200. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  201. SND_SOC_DAPM_LINE("Line Jack", NULL),
  202. /* headset is a mic and mono headphone */
  203. SND_SOC_DAPM_HP("Headset Jack", NULL),
  204. };
  205. /* Spitz machine audio_map */
  206. static const struct snd_soc_dapm_route audio_map[] = {
  207. /* headphone connected to LOUT1, ROUT1 */
  208. {"Headphone Jack", NULL, "LOUT1"},
  209. {"Headphone Jack", NULL, "ROUT1"},
  210. /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
  211. {"Headset Jack", NULL, "ROUT1"},
  212. /* ext speaker connected to LOUT2, ROUT2 */
  213. {"Ext Spk", NULL , "ROUT2"},
  214. {"Ext Spk", NULL , "LOUT2"},
  215. /* mic is connected to input 1 - with bias */
  216. {"LINPUT1", NULL, "Mic Bias"},
  217. {"Mic Bias", NULL, "Mic Jack"},
  218. /* line is connected to input 1 - no bias */
  219. {"LINPUT1", 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 spitz_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 wm8750_spitz_controls[] = {
  229. SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
  230. spitz_set_jack),
  231. SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
  232. spitz_set_spk),
  233. };
  234. /*
  235. * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device
  236. */
  237. static int spitz_wm8750_init(struct snd_soc_codec *codec)
  238. {
  239. int err;
  240. /* NC codec pins */
  241. snd_soc_dapm_nc_pin(codec, "RINPUT1");
  242. snd_soc_dapm_nc_pin(codec, "LINPUT2");
  243. snd_soc_dapm_nc_pin(codec, "RINPUT2");
  244. snd_soc_dapm_nc_pin(codec, "LINPUT3");
  245. snd_soc_dapm_nc_pin(codec, "RINPUT3");
  246. snd_soc_dapm_nc_pin(codec, "OUT3");
  247. snd_soc_dapm_nc_pin(codec, "MONO1");
  248. /* Add spitz specific controls */
  249. err = snd_soc_add_controls(codec, wm8750_spitz_controls,
  250. ARRAY_SIZE(wm8750_spitz_controls));
  251. if (err < 0)
  252. return err;
  253. /* Add spitz specific widgets */
  254. snd_soc_dapm_new_controls(codec, wm8750_dapm_widgets,
  255. ARRAY_SIZE(wm8750_dapm_widgets));
  256. /* Set up spitz specific audio paths */
  257. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  258. snd_soc_dapm_sync(codec);
  259. return 0;
  260. }
  261. /* spitz digital audio interface glue - connects codec <--> CPU */
  262. static struct snd_soc_dai_link spitz_dai = {
  263. .name = "wm8750",
  264. .stream_name = "WM8750",
  265. .cpu_dai = &pxa_i2s_dai,
  266. .codec_dai = &wm8750_dai,
  267. .init = spitz_wm8750_init,
  268. .ops = &spitz_ops,
  269. };
  270. /* spitz audio machine driver */
  271. static struct snd_soc_card snd_soc_spitz = {
  272. .name = "Spitz",
  273. .platform = &pxa2xx_soc_platform,
  274. .dai_link = &spitz_dai,
  275. .num_links = 1,
  276. };
  277. /* spitz audio private data */
  278. static struct wm8750_setup_data spitz_wm8750_setup = {
  279. .i2c_bus = 0,
  280. .i2c_address = 0x1b,
  281. };
  282. /* spitz audio subsystem */
  283. static struct snd_soc_device spitz_snd_devdata = {
  284. .card = &snd_soc_spitz,
  285. .codec_dev = &soc_codec_dev_wm8750,
  286. .codec_data = &spitz_wm8750_setup,
  287. };
  288. static struct platform_device *spitz_snd_device;
  289. static int __init spitz_init(void)
  290. {
  291. int ret;
  292. if (!(machine_is_spitz() || machine_is_borzoi() || machine_is_akita()))
  293. return -ENODEV;
  294. spitz_snd_device = platform_device_alloc("soc-audio", -1);
  295. if (!spitz_snd_device)
  296. return -ENOMEM;
  297. platform_set_drvdata(spitz_snd_device, &spitz_snd_devdata);
  298. spitz_snd_devdata.dev = &spitz_snd_device->dev;
  299. ret = platform_device_add(spitz_snd_device);
  300. if (ret)
  301. platform_device_put(spitz_snd_device);
  302. return ret;
  303. }
  304. static void __exit spitz_exit(void)
  305. {
  306. platform_device_unregister(spitz_snd_device);
  307. }
  308. module_init(spitz_init);
  309. module_exit(spitz_exit);
  310. MODULE_AUTHOR("Richard Purdie");
  311. MODULE_DESCRIPTION("ALSA SoC Spitz");
  312. MODULE_LICENSE("GPL");