magician.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * SoC audio for HTC Magician
  3. *
  4. * Copyright (c) 2006 Philipp Zabel <philipp.zabel@gmail.com>
  5. *
  6. * based on spitz.c,
  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/timer.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/delay.h>
  21. #include <linux/gpio.h>
  22. #include <linux/i2c.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/uda1380.h>
  28. #include <mach/magician.h>
  29. #include <asm/mach-types.h>
  30. #include "../codecs/uda1380.h"
  31. #include "pxa2xx-i2s.h"
  32. #include "pxa-ssp.h"
  33. #define MAGICIAN_MIC 0
  34. #define MAGICIAN_MIC_EXT 1
  35. static int magician_hp_switch;
  36. static int magician_spk_switch = 1;
  37. static int magician_in_sel = MAGICIAN_MIC;
  38. static void magician_ext_control(struct snd_soc_dapm_context *dapm)
  39. {
  40. snd_soc_dapm_mutex_lock(dapm);
  41. if (magician_spk_switch)
  42. snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
  43. else
  44. snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
  45. if (magician_hp_switch)
  46. snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
  47. else
  48. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  49. switch (magician_in_sel) {
  50. case MAGICIAN_MIC:
  51. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Mic");
  52. snd_soc_dapm_enable_pin_unlocked(dapm, "Call Mic");
  53. break;
  54. case MAGICIAN_MIC_EXT:
  55. snd_soc_dapm_disable_pin_unlocked(dapm, "Call Mic");
  56. snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Mic");
  57. break;
  58. }
  59. snd_soc_dapm_sync_unlocked(dapm);
  60. snd_soc_dapm_mutex_unlock(dapm);
  61. }
  62. static int magician_startup(struct snd_pcm_substream *substream)
  63. {
  64. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  65. /* check the jack status at stream startup */
  66. magician_ext_control(&rtd->card->dapm);
  67. return 0;
  68. }
  69. /*
  70. * Magician uses SSP port for playback.
  71. */
  72. static int magician_playback_hw_params(struct snd_pcm_substream *substream,
  73. struct snd_pcm_hw_params *params)
  74. {
  75. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  76. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  77. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  78. unsigned int width;
  79. int ret = 0;
  80. /* set codec DAI configuration */
  81. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_MSB |
  82. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  83. if (ret < 0)
  84. return ret;
  85. /* set cpu DAI configuration */
  86. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_DSP_A |
  87. SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_CBS_CFS);
  88. if (ret < 0)
  89. return ret;
  90. width = snd_pcm_format_physical_width(params_format(params));
  91. ret = snd_soc_dai_set_tdm_slot(cpu_dai, 1, 0, 1, width);
  92. if (ret < 0)
  93. return ret;
  94. /* set audio clock as clock source */
  95. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0,
  96. SND_SOC_CLOCK_OUT);
  97. if (ret < 0)
  98. return ret;
  99. return 0;
  100. }
  101. /*
  102. * Magician uses I2S for capture.
  103. */
  104. static int magician_capture_hw_params(struct snd_pcm_substream *substream,
  105. struct snd_pcm_hw_params *params)
  106. {
  107. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  109. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  110. int ret = 0;
  111. /* set codec DAI configuration */
  112. ret = snd_soc_dai_set_fmt(codec_dai,
  113. SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  114. SND_SOC_DAIFMT_CBS_CFS);
  115. if (ret < 0)
  116. return ret;
  117. /* set cpu DAI configuration */
  118. ret = snd_soc_dai_set_fmt(cpu_dai,
  119. SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  120. SND_SOC_DAIFMT_CBS_CFS);
  121. if (ret < 0)
  122. return ret;
  123. /* set the I2S system clock as output */
  124. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  125. SND_SOC_CLOCK_OUT);
  126. if (ret < 0)
  127. return ret;
  128. return 0;
  129. }
  130. static const struct snd_soc_ops magician_capture_ops = {
  131. .startup = magician_startup,
  132. .hw_params = magician_capture_hw_params,
  133. };
  134. static const struct snd_soc_ops magician_playback_ops = {
  135. .startup = magician_startup,
  136. .hw_params = magician_playback_hw_params,
  137. };
  138. static int magician_get_hp(struct snd_kcontrol *kcontrol,
  139. struct snd_ctl_elem_value *ucontrol)
  140. {
  141. ucontrol->value.integer.value[0] = magician_hp_switch;
  142. return 0;
  143. }
  144. static int magician_set_hp(struct snd_kcontrol *kcontrol,
  145. struct snd_ctl_elem_value *ucontrol)
  146. {
  147. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  148. if (magician_hp_switch == ucontrol->value.integer.value[0])
  149. return 0;
  150. magician_hp_switch = ucontrol->value.integer.value[0];
  151. magician_ext_control(&card->dapm);
  152. return 1;
  153. }
  154. static int magician_get_spk(struct snd_kcontrol *kcontrol,
  155. struct snd_ctl_elem_value *ucontrol)
  156. {
  157. ucontrol->value.integer.value[0] = magician_spk_switch;
  158. return 0;
  159. }
  160. static int magician_set_spk(struct snd_kcontrol *kcontrol,
  161. struct snd_ctl_elem_value *ucontrol)
  162. {
  163. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  164. if (magician_spk_switch == ucontrol->value.integer.value[0])
  165. return 0;
  166. magician_spk_switch = ucontrol->value.integer.value[0];
  167. magician_ext_control(&card->dapm);
  168. return 1;
  169. }
  170. static int magician_get_input(struct snd_kcontrol *kcontrol,
  171. struct snd_ctl_elem_value *ucontrol)
  172. {
  173. ucontrol->value.enumerated.item[0] = magician_in_sel;
  174. return 0;
  175. }
  176. static int magician_set_input(struct snd_kcontrol *kcontrol,
  177. struct snd_ctl_elem_value *ucontrol)
  178. {
  179. if (magician_in_sel == ucontrol->value.enumerated.item[0])
  180. return 0;
  181. magician_in_sel = ucontrol->value.enumerated.item[0];
  182. switch (magician_in_sel) {
  183. case MAGICIAN_MIC:
  184. gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 1);
  185. break;
  186. case MAGICIAN_MIC_EXT:
  187. gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 0);
  188. }
  189. return 1;
  190. }
  191. static int magician_spk_power(struct snd_soc_dapm_widget *w,
  192. struct snd_kcontrol *k, int event)
  193. {
  194. gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, SND_SOC_DAPM_EVENT_ON(event));
  195. return 0;
  196. }
  197. static int magician_hp_power(struct snd_soc_dapm_widget *w,
  198. struct snd_kcontrol *k, int event)
  199. {
  200. gpio_set_value(EGPIO_MAGICIAN_EP_POWER, SND_SOC_DAPM_EVENT_ON(event));
  201. return 0;
  202. }
  203. static int magician_mic_bias(struct snd_soc_dapm_widget *w,
  204. struct snd_kcontrol *k, int event)
  205. {
  206. gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, SND_SOC_DAPM_EVENT_ON(event));
  207. return 0;
  208. }
  209. /* magician machine dapm widgets */
  210. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  211. SND_SOC_DAPM_HP("Headphone Jack", magician_hp_power),
  212. SND_SOC_DAPM_SPK("Speaker", magician_spk_power),
  213. SND_SOC_DAPM_MIC("Call Mic", magician_mic_bias),
  214. SND_SOC_DAPM_MIC("Headset Mic", magician_mic_bias),
  215. };
  216. /* magician machine audio_map */
  217. static const struct snd_soc_dapm_route audio_map[] = {
  218. /* Headphone connected to VOUTL, VOUTR */
  219. {"Headphone Jack", NULL, "VOUTL"},
  220. {"Headphone Jack", NULL, "VOUTR"},
  221. /* Speaker connected to VOUTL, VOUTR */
  222. {"Speaker", NULL, "VOUTL"},
  223. {"Speaker", NULL, "VOUTR"},
  224. /* Mics are connected to VINM */
  225. {"VINM", NULL, "Headset Mic"},
  226. {"VINM", NULL, "Call Mic"},
  227. };
  228. static const char * const input_select[] = {"Call Mic", "Headset Mic"};
  229. static const struct soc_enum magician_in_sel_enum =
  230. SOC_ENUM_SINGLE_EXT(2, input_select);
  231. static const struct snd_kcontrol_new uda1380_magician_controls[] = {
  232. SOC_SINGLE_BOOL_EXT("Headphone Switch",
  233. (unsigned long)&magician_hp_switch,
  234. magician_get_hp, magician_set_hp),
  235. SOC_SINGLE_BOOL_EXT("Speaker Switch",
  236. (unsigned long)&magician_spk_switch,
  237. magician_get_spk, magician_set_spk),
  238. SOC_ENUM_EXT("Input Select", magician_in_sel_enum,
  239. magician_get_input, magician_set_input),
  240. };
  241. /* magician digital audio interface glue - connects codec <--> CPU */
  242. static struct snd_soc_dai_link magician_dai[] = {
  243. {
  244. .name = "uda1380",
  245. .stream_name = "UDA1380 Playback",
  246. .cpu_dai_name = "pxa-ssp-dai.0",
  247. .codec_dai_name = "uda1380-hifi-playback",
  248. .platform_name = "pxa-pcm-audio",
  249. .codec_name = "uda1380-codec.0-0018",
  250. .ops = &magician_playback_ops,
  251. },
  252. {
  253. .name = "uda1380",
  254. .stream_name = "UDA1380 Capture",
  255. .cpu_dai_name = "pxa2xx-i2s",
  256. .codec_dai_name = "uda1380-hifi-capture",
  257. .platform_name = "pxa-pcm-audio",
  258. .codec_name = "uda1380-codec.0-0018",
  259. .ops = &magician_capture_ops,
  260. }
  261. };
  262. /* magician audio machine driver */
  263. static struct snd_soc_card snd_soc_card_magician = {
  264. .name = "Magician",
  265. .owner = THIS_MODULE,
  266. .dai_link = magician_dai,
  267. .num_links = ARRAY_SIZE(magician_dai),
  268. .controls = uda1380_magician_controls,
  269. .num_controls = ARRAY_SIZE(uda1380_magician_controls),
  270. .dapm_widgets = uda1380_dapm_widgets,
  271. .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
  272. .dapm_routes = audio_map,
  273. .num_dapm_routes = ARRAY_SIZE(audio_map),
  274. .fully_routed = true,
  275. };
  276. static struct platform_device *magician_snd_device;
  277. /*
  278. * FIXME: move into magician board file once merged into the pxa tree
  279. */
  280. static struct uda1380_platform_data uda1380_info = {
  281. .gpio_power = EGPIO_MAGICIAN_CODEC_POWER,
  282. .gpio_reset = EGPIO_MAGICIAN_CODEC_RESET,
  283. .dac_clk = UDA1380_DAC_CLK_WSPLL,
  284. };
  285. static struct i2c_board_info i2c_board_info[] = {
  286. {
  287. I2C_BOARD_INFO("uda1380", 0x18),
  288. .platform_data = &uda1380_info,
  289. },
  290. };
  291. static int __init magician_init(void)
  292. {
  293. int ret;
  294. struct i2c_adapter *adapter;
  295. struct i2c_client *client;
  296. if (!machine_is_magician())
  297. return -ENODEV;
  298. adapter = i2c_get_adapter(0);
  299. if (!adapter)
  300. return -ENODEV;
  301. client = i2c_new_device(adapter, i2c_board_info);
  302. i2c_put_adapter(adapter);
  303. if (!client)
  304. return -ENODEV;
  305. ret = gpio_request(EGPIO_MAGICIAN_SPK_POWER, "SPK_POWER");
  306. if (ret)
  307. goto err_request_spk;
  308. ret = gpio_request(EGPIO_MAGICIAN_EP_POWER, "EP_POWER");
  309. if (ret)
  310. goto err_request_ep;
  311. ret = gpio_request(EGPIO_MAGICIAN_MIC_POWER, "MIC_POWER");
  312. if (ret)
  313. goto err_request_mic;
  314. ret = gpio_request(EGPIO_MAGICIAN_IN_SEL0, "IN_SEL0");
  315. if (ret)
  316. goto err_request_in_sel0;
  317. ret = gpio_request(EGPIO_MAGICIAN_IN_SEL1, "IN_SEL1");
  318. if (ret)
  319. goto err_request_in_sel1;
  320. gpio_set_value(EGPIO_MAGICIAN_IN_SEL0, 0);
  321. magician_snd_device = platform_device_alloc("soc-audio", -1);
  322. if (!magician_snd_device) {
  323. ret = -ENOMEM;
  324. goto err_pdev;
  325. }
  326. platform_set_drvdata(magician_snd_device, &snd_soc_card_magician);
  327. ret = platform_device_add(magician_snd_device);
  328. if (ret) {
  329. platform_device_put(magician_snd_device);
  330. goto err_pdev;
  331. }
  332. return 0;
  333. err_pdev:
  334. gpio_free(EGPIO_MAGICIAN_IN_SEL1);
  335. err_request_in_sel1:
  336. gpio_free(EGPIO_MAGICIAN_IN_SEL0);
  337. err_request_in_sel0:
  338. gpio_free(EGPIO_MAGICIAN_MIC_POWER);
  339. err_request_mic:
  340. gpio_free(EGPIO_MAGICIAN_EP_POWER);
  341. err_request_ep:
  342. gpio_free(EGPIO_MAGICIAN_SPK_POWER);
  343. err_request_spk:
  344. return ret;
  345. }
  346. static void __exit magician_exit(void)
  347. {
  348. platform_device_unregister(magician_snd_device);
  349. gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, 0);
  350. gpio_set_value(EGPIO_MAGICIAN_EP_POWER, 0);
  351. gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, 0);
  352. gpio_free(EGPIO_MAGICIAN_IN_SEL1);
  353. gpio_free(EGPIO_MAGICIAN_IN_SEL0);
  354. gpio_free(EGPIO_MAGICIAN_MIC_POWER);
  355. gpio_free(EGPIO_MAGICIAN_EP_POWER);
  356. gpio_free(EGPIO_MAGICIAN_SPK_POWER);
  357. }
  358. module_init(magician_init);
  359. module_exit(magician_exit);
  360. MODULE_AUTHOR("Philipp Zabel");
  361. MODULE_DESCRIPTION("ALSA SoC Magician");
  362. MODULE_LICENSE("GPL");