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/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 "../codecs/wm8750.h"
  36. #include "pxa2xx-pcm.h"
  37. #include "pxa2xx-i2s.h"
  38. #define SPITZ_HP 0
  39. #define SPITZ_MIC 1
  40. #define SPITZ_LINE 2
  41. #define SPITZ_HEADSET 3
  42. #define SPITZ_HP_OFF 4
  43. #define SPITZ_SPK_ON 0
  44. #define SPITZ_SPK_OFF 1
  45. /* audio clock in Hz - rounded from 12.235MHz */
  46. #define SPITZ_AUDIO_CLOCK 12288000
  47. static int spitz_jack_func;
  48. static int spitz_spk_func;
  49. static void spitz_ext_control(struct snd_soc_codec *codec)
  50. {
  51. if (spitz_spk_func == SPITZ_SPK_ON)
  52. snd_soc_dapm_set_endpoint(codec, "Ext Spk", 1);
  53. else
  54. snd_soc_dapm_set_endpoint(codec, "Ext Spk", 0);
  55. /* set up jack connection */
  56. switch (spitz_jack_func) {
  57. case SPITZ_HP:
  58. /* enable and unmute hp jack, disable mic bias */
  59. snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0);
  60. snd_soc_dapm_set_endpoint(codec, "Mic Jack", 0);
  61. snd_soc_dapm_set_endpoint(codec, "Line Jack", 0);
  62. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 1);
  63. set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
  64. set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
  65. break;
  66. case SPITZ_MIC:
  67. /* enable mic jack and bias, mute hp */
  68. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
  69. snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0);
  70. snd_soc_dapm_set_endpoint(codec, "Line Jack", 0);
  71. snd_soc_dapm_set_endpoint(codec, "Mic Jack", 1);
  72. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
  73. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
  74. break;
  75. case SPITZ_LINE:
  76. /* enable line jack, disable mic bias and mute hp */
  77. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
  78. snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0);
  79. snd_soc_dapm_set_endpoint(codec, "Mic Jack", 0);
  80. snd_soc_dapm_set_endpoint(codec, "Line Jack", 1);
  81. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
  82. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
  83. break;
  84. case SPITZ_HEADSET:
  85. /* enable and unmute headset jack enable mic bias, mute L hp */
  86. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
  87. snd_soc_dapm_set_endpoint(codec, "Mic Jack", 1);
  88. snd_soc_dapm_set_endpoint(codec, "Line Jack", 0);
  89. snd_soc_dapm_set_endpoint(codec, "Headset Jack", 1);
  90. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
  91. set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
  92. break;
  93. case SPITZ_HP_OFF:
  94. /* jack removed, everything off */
  95. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
  96. snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0);
  97. snd_soc_dapm_set_endpoint(codec, "Mic Jack", 0);
  98. snd_soc_dapm_set_endpoint(codec, "Line Jack", 0);
  99. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
  100. reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
  101. break;
  102. }
  103. snd_soc_dapm_sync_endpoints(codec);
  104. }
  105. static int spitz_startup(struct snd_pcm_substream *substream)
  106. {
  107. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108. struct snd_soc_codec *codec = rtd->socdev->codec;
  109. /* check the jack status at stream startup */
  110. spitz_ext_control(codec);
  111. return 0;
  112. }
  113. static int spitz_hw_params(struct snd_pcm_substream *substream,
  114. struct snd_pcm_hw_params *params)
  115. {
  116. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  117. struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
  118. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  119. unsigned int clk = 0;
  120. int ret = 0;
  121. switch (params_rate(params)) {
  122. case 8000:
  123. case 16000:
  124. case 48000:
  125. case 96000:
  126. clk = 12288000;
  127. break;
  128. case 11025:
  129. case 22050:
  130. case 44100:
  131. clk = 11289600;
  132. break;
  133. }
  134. /* set codec DAI configuration */
  135. ret = codec_dai->dai_ops.set_fmt(codec_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 cpu DAI configuration */
  140. ret = cpu_dai->dai_ops.set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  141. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  142. if (ret < 0)
  143. return ret;
  144. /* set the codec system clock for DAC and ADC */
  145. ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8750_SYSCLK, clk,
  146. SND_SOC_CLOCK_IN);
  147. if (ret < 0)
  148. return ret;
  149. /* set the I2S system clock as input (unused) */
  150. ret = cpu_dai->dai_ops.set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  151. SND_SOC_CLOCK_IN);
  152. if (ret < 0)
  153. return ret;
  154. return 0;
  155. }
  156. static struct snd_soc_ops spitz_ops = {
  157. .startup = spitz_startup,
  158. .hw_params = spitz_hw_params,
  159. };
  160. static int spitz_get_jack(struct snd_kcontrol *kcontrol,
  161. struct snd_ctl_elem_value *ucontrol)
  162. {
  163. ucontrol->value.integer.value[0] = spitz_jack_func;
  164. return 0;
  165. }
  166. static int spitz_set_jack(struct snd_kcontrol *kcontrol,
  167. struct snd_ctl_elem_value *ucontrol)
  168. {
  169. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  170. if (spitz_jack_func == ucontrol->value.integer.value[0])
  171. return 0;
  172. spitz_jack_func = ucontrol->value.integer.value[0];
  173. spitz_ext_control(codec);
  174. return 1;
  175. }
  176. static int spitz_get_spk(struct snd_kcontrol *kcontrol,
  177. struct snd_ctl_elem_value *ucontrol)
  178. {
  179. ucontrol->value.integer.value[0] = spitz_spk_func;
  180. return 0;
  181. }
  182. static int spitz_set_spk(struct snd_kcontrol *kcontrol,
  183. struct snd_ctl_elem_value *ucontrol)
  184. {
  185. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  186. if (spitz_spk_func == ucontrol->value.integer.value[0])
  187. return 0;
  188. spitz_spk_func = ucontrol->value.integer.value[0];
  189. spitz_ext_control(codec);
  190. return 1;
  191. }
  192. static int spitz_mic_bias(struct snd_soc_dapm_widget *w, 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");