spitz.c 11 KB

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