spitz.c 11 KB

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