spitz.c 9.9 KB

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