magician.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 acps, acds, width;
  79. unsigned int div4 = PXA_SSP_CLK_SCDB_4;
  80. int ret = 0;
  81. width = snd_pcm_format_physical_width(params_format(params));
  82. /*
  83. * rate = SSPSCLK / (2 * width(16 or 32))
  84. * SSPSCLK = (ACPS / ACDS) / SSPSCLKDIV(div4 or div1)
  85. */
  86. switch (params_rate(params)) {
  87. case 8000:
  88. /* off by a factor of 2: bug in the PXA27x audio clock? */
  89. acps = 32842000;
  90. switch (width) {
  91. case 16:
  92. /* 513156 Hz ~= _2_ * 8000 Hz * 32 (+0.23%) */
  93. acds = PXA_SSP_CLK_AUDIO_DIV_16;
  94. break;
  95. default: /* 32 */
  96. /* 1026312 Hz ~= _2_ * 8000 Hz * 64 (+0.23%) */
  97. acds = PXA_SSP_CLK_AUDIO_DIV_8;
  98. }
  99. break;
  100. case 11025:
  101. acps = 5622000;
  102. switch (width) {
  103. case 16:
  104. /* 351375 Hz ~= 11025 Hz * 32 (-0.41%) */
  105. acds = PXA_SSP_CLK_AUDIO_DIV_4;
  106. break;
  107. default: /* 32 */
  108. /* 702750 Hz ~= 11025 Hz * 64 (-0.41%) */
  109. acds = PXA_SSP_CLK_AUDIO_DIV_2;
  110. }
  111. break;
  112. case 22050:
  113. acps = 5622000;
  114. switch (width) {
  115. case 16:
  116. /* 702750 Hz ~= 22050 Hz * 32 (-0.41%) */
  117. acds = PXA_SSP_CLK_AUDIO_DIV_2;
  118. break;
  119. default: /* 32 */
  120. /* 1405500 Hz ~= 22050 Hz * 64 (-0.41%) */
  121. acds = PXA_SSP_CLK_AUDIO_DIV_1;
  122. }
  123. break;
  124. case 44100:
  125. acps = 5622000;
  126. switch (width) {
  127. case 16:
  128. /* 1405500 Hz ~= 44100 Hz * 32 (-0.41%) */
  129. acds = PXA_SSP_CLK_AUDIO_DIV_2;
  130. break;
  131. default: /* 32 */
  132. /* 2811000 Hz ~= 44100 Hz * 64 (-0.41%) */
  133. acds = PXA_SSP_CLK_AUDIO_DIV_1;
  134. }
  135. break;
  136. case 48000:
  137. acps = 12235000;
  138. switch (width) {
  139. case 16:
  140. /* 1529375 Hz ~= 48000 Hz * 32 (-0.44%) */
  141. acds = PXA_SSP_CLK_AUDIO_DIV_2;
  142. break;
  143. default: /* 32 */
  144. /* 3058750 Hz ~= 48000 Hz * 64 (-0.44%) */
  145. acds = PXA_SSP_CLK_AUDIO_DIV_1;
  146. }
  147. break;
  148. case 96000:
  149. default:
  150. acps = 12235000;
  151. switch (width) {
  152. case 16:
  153. /* 3058750 Hz ~= 96000 Hz * 32 (-0.44%) */
  154. acds = PXA_SSP_CLK_AUDIO_DIV_1;
  155. break;
  156. default: /* 32 */
  157. /* 6117500 Hz ~= 96000 Hz * 64 (-0.44%) */
  158. acds = PXA_SSP_CLK_AUDIO_DIV_2;
  159. div4 = PXA_SSP_CLK_SCDB_1;
  160. break;
  161. }
  162. break;
  163. }
  164. /* set codec DAI configuration */
  165. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_MSB |
  166. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  167. if (ret < 0)
  168. return ret;
  169. /* set cpu DAI configuration */
  170. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_DSP_A |
  171. SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_CBS_CFS);
  172. if (ret < 0)
  173. return ret;
  174. ret = snd_soc_dai_set_tdm_slot(cpu_dai, 1, 0, 1, width);
  175. if (ret < 0)
  176. return ret;
  177. /* set audio clock as clock source */
  178. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0,
  179. SND_SOC_CLOCK_OUT);
  180. if (ret < 0)
  181. return ret;
  182. /* set the SSP audio system clock ACDS divider */
  183. ret = snd_soc_dai_set_clkdiv(cpu_dai,
  184. PXA_SSP_AUDIO_DIV_ACDS, acds);
  185. if (ret < 0)
  186. return ret;
  187. /* set the SSP audio system clock SCDB divider4 */
  188. ret = snd_soc_dai_set_clkdiv(cpu_dai,
  189. PXA_SSP_AUDIO_DIV_SCDB, div4);
  190. if (ret < 0)
  191. return ret;
  192. /* set SSP audio pll clock */
  193. ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, acps);
  194. if (ret < 0)
  195. return ret;
  196. return 0;
  197. }
  198. /*
  199. * Magician uses I2S for capture.
  200. */
  201. static int magician_capture_hw_params(struct snd_pcm_substream *substream,
  202. struct snd_pcm_hw_params *params)
  203. {
  204. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  205. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  206. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  207. int ret = 0;
  208. /* set codec DAI configuration */
  209. ret = snd_soc_dai_set_fmt(codec_dai,
  210. SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  211. SND_SOC_DAIFMT_CBS_CFS);
  212. if (ret < 0)
  213. return ret;
  214. /* set cpu DAI configuration */
  215. ret = snd_soc_dai_set_fmt(cpu_dai,
  216. SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  217. SND_SOC_DAIFMT_CBS_CFS);
  218. if (ret < 0)
  219. return ret;
  220. /* set the I2S system clock as output */
  221. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  222. SND_SOC_CLOCK_OUT);
  223. if (ret < 0)
  224. return ret;
  225. return 0;
  226. }
  227. static struct snd_soc_ops magician_capture_ops = {
  228. .startup = magician_startup,
  229. .hw_params = magician_capture_hw_params,
  230. };
  231. static struct snd_soc_ops magician_playback_ops = {
  232. .startup = magician_startup,
  233. .hw_params = magician_playback_hw_params,
  234. };
  235. static int magician_get_hp(struct snd_kcontrol *kcontrol,
  236. struct snd_ctl_elem_value *ucontrol)
  237. {
  238. ucontrol->value.integer.value[0] = magician_hp_switch;
  239. return 0;
  240. }
  241. static int magician_set_hp(struct snd_kcontrol *kcontrol,
  242. struct snd_ctl_elem_value *ucontrol)
  243. {
  244. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  245. if (magician_hp_switch == ucontrol->value.integer.value[0])
  246. return 0;
  247. magician_hp_switch = ucontrol->value.integer.value[0];
  248. magician_ext_control(&card->dapm);
  249. return 1;
  250. }
  251. static int magician_get_spk(struct snd_kcontrol *kcontrol,
  252. struct snd_ctl_elem_value *ucontrol)
  253. {
  254. ucontrol->value.integer.value[0] = magician_spk_switch;
  255. return 0;
  256. }
  257. static int magician_set_spk(struct snd_kcontrol *kcontrol,
  258. struct snd_ctl_elem_value *ucontrol)
  259. {
  260. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  261. if (magician_spk_switch == ucontrol->value.integer.value[0])
  262. return 0;
  263. magician_spk_switch = ucontrol->value.integer.value[0];
  264. magician_ext_control(&card->dapm);
  265. return 1;
  266. }
  267. static int magician_get_input(struct snd_kcontrol *kcontrol,
  268. struct snd_ctl_elem_value *ucontrol)
  269. {
  270. ucontrol->value.enumerated.item[0] = magician_in_sel;
  271. return 0;
  272. }
  273. static int magician_set_input(struct snd_kcontrol *kcontrol,
  274. struct snd_ctl_elem_value *ucontrol)
  275. {
  276. if (magician_in_sel == ucontrol->value.enumerated.item[0])
  277. return 0;
  278. magician_in_sel = ucontrol->value.enumerated.item[0];
  279. switch (magician_in_sel) {
  280. case MAGICIAN_MIC:
  281. gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 1);
  282. break;
  283. case MAGICIAN_MIC_EXT:
  284. gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 0);
  285. }
  286. return 1;
  287. }
  288. static int magician_spk_power(struct snd_soc_dapm_widget *w,
  289. struct snd_kcontrol *k, int event)
  290. {
  291. gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, SND_SOC_DAPM_EVENT_ON(event));
  292. return 0;
  293. }
  294. static int magician_hp_power(struct snd_soc_dapm_widget *w,
  295. struct snd_kcontrol *k, int event)
  296. {
  297. gpio_set_value(EGPIO_MAGICIAN_EP_POWER, SND_SOC_DAPM_EVENT_ON(event));
  298. return 0;
  299. }
  300. static int magician_mic_bias(struct snd_soc_dapm_widget *w,
  301. struct snd_kcontrol *k, int event)
  302. {
  303. gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, SND_SOC_DAPM_EVENT_ON(event));
  304. return 0;
  305. }
  306. /* magician machine dapm widgets */
  307. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  308. SND_SOC_DAPM_HP("Headphone Jack", magician_hp_power),
  309. SND_SOC_DAPM_SPK("Speaker", magician_spk_power),
  310. SND_SOC_DAPM_MIC("Call Mic", magician_mic_bias),
  311. SND_SOC_DAPM_MIC("Headset Mic", magician_mic_bias),
  312. };
  313. /* magician machine audio_map */
  314. static const struct snd_soc_dapm_route audio_map[] = {
  315. /* Headphone connected to VOUTL, VOUTR */
  316. {"Headphone Jack", NULL, "VOUTL"},
  317. {"Headphone Jack", NULL, "VOUTR"},
  318. /* Speaker connected to VOUTL, VOUTR */
  319. {"Speaker", NULL, "VOUTL"},
  320. {"Speaker", NULL, "VOUTR"},
  321. /* Mics are connected to VINM */
  322. {"VINM", NULL, "Headset Mic"},
  323. {"VINM", NULL, "Call Mic"},
  324. };
  325. static const char *input_select[] = {"Call Mic", "Headset Mic"};
  326. static const struct soc_enum magician_in_sel_enum =
  327. SOC_ENUM_SINGLE_EXT(2, input_select);
  328. static const struct snd_kcontrol_new uda1380_magician_controls[] = {
  329. SOC_SINGLE_BOOL_EXT("Headphone Switch",
  330. (unsigned long)&magician_hp_switch,
  331. magician_get_hp, magician_set_hp),
  332. SOC_SINGLE_BOOL_EXT("Speaker Switch",
  333. (unsigned long)&magician_spk_switch,
  334. magician_get_spk, magician_set_spk),
  335. SOC_ENUM_EXT("Input Select", magician_in_sel_enum,
  336. magician_get_input, magician_set_input),
  337. };
  338. /* magician digital audio interface glue - connects codec <--> CPU */
  339. static struct snd_soc_dai_link magician_dai[] = {
  340. {
  341. .name = "uda1380",
  342. .stream_name = "UDA1380 Playback",
  343. .cpu_dai_name = "pxa-ssp-dai.0",
  344. .codec_dai_name = "uda1380-hifi-playback",
  345. .platform_name = "pxa-pcm-audio",
  346. .codec_name = "uda1380-codec.0-0018",
  347. .ops = &magician_playback_ops,
  348. },
  349. {
  350. .name = "uda1380",
  351. .stream_name = "UDA1380 Capture",
  352. .cpu_dai_name = "pxa2xx-i2s",
  353. .codec_dai_name = "uda1380-hifi-capture",
  354. .platform_name = "pxa-pcm-audio",
  355. .codec_name = "uda1380-codec.0-0018",
  356. .ops = &magician_capture_ops,
  357. }
  358. };
  359. /* magician audio machine driver */
  360. static struct snd_soc_card snd_soc_card_magician = {
  361. .name = "Magician",
  362. .owner = THIS_MODULE,
  363. .dai_link = magician_dai,
  364. .num_links = ARRAY_SIZE(magician_dai),
  365. .controls = uda1380_magician_controls,
  366. .num_controls = ARRAY_SIZE(uda1380_magician_controls),
  367. .dapm_widgets = uda1380_dapm_widgets,
  368. .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
  369. .dapm_routes = audio_map,
  370. .num_dapm_routes = ARRAY_SIZE(audio_map),
  371. .fully_routed = true,
  372. };
  373. static struct platform_device *magician_snd_device;
  374. /*
  375. * FIXME: move into magician board file once merged into the pxa tree
  376. */
  377. static struct uda1380_platform_data uda1380_info = {
  378. .gpio_power = EGPIO_MAGICIAN_CODEC_POWER,
  379. .gpio_reset = EGPIO_MAGICIAN_CODEC_RESET,
  380. .dac_clk = UDA1380_DAC_CLK_WSPLL,
  381. };
  382. static struct i2c_board_info i2c_board_info[] = {
  383. {
  384. I2C_BOARD_INFO("uda1380", 0x18),
  385. .platform_data = &uda1380_info,
  386. },
  387. };
  388. static int __init magician_init(void)
  389. {
  390. int ret;
  391. struct i2c_adapter *adapter;
  392. struct i2c_client *client;
  393. if (!machine_is_magician())
  394. return -ENODEV;
  395. adapter = i2c_get_adapter(0);
  396. if (!adapter)
  397. return -ENODEV;
  398. client = i2c_new_device(adapter, i2c_board_info);
  399. i2c_put_adapter(adapter);
  400. if (!client)
  401. return -ENODEV;
  402. ret = gpio_request(EGPIO_MAGICIAN_SPK_POWER, "SPK_POWER");
  403. if (ret)
  404. goto err_request_spk;
  405. ret = gpio_request(EGPIO_MAGICIAN_EP_POWER, "EP_POWER");
  406. if (ret)
  407. goto err_request_ep;
  408. ret = gpio_request(EGPIO_MAGICIAN_MIC_POWER, "MIC_POWER");
  409. if (ret)
  410. goto err_request_mic;
  411. ret = gpio_request(EGPIO_MAGICIAN_IN_SEL0, "IN_SEL0");
  412. if (ret)
  413. goto err_request_in_sel0;
  414. ret = gpio_request(EGPIO_MAGICIAN_IN_SEL1, "IN_SEL1");
  415. if (ret)
  416. goto err_request_in_sel1;
  417. gpio_set_value(EGPIO_MAGICIAN_IN_SEL0, 0);
  418. magician_snd_device = platform_device_alloc("soc-audio", -1);
  419. if (!magician_snd_device) {
  420. ret = -ENOMEM;
  421. goto err_pdev;
  422. }
  423. platform_set_drvdata(magician_snd_device, &snd_soc_card_magician);
  424. ret = platform_device_add(magician_snd_device);
  425. if (ret) {
  426. platform_device_put(magician_snd_device);
  427. goto err_pdev;
  428. }
  429. return 0;
  430. err_pdev:
  431. gpio_free(EGPIO_MAGICIAN_IN_SEL1);
  432. err_request_in_sel1:
  433. gpio_free(EGPIO_MAGICIAN_IN_SEL0);
  434. err_request_in_sel0:
  435. gpio_free(EGPIO_MAGICIAN_MIC_POWER);
  436. err_request_mic:
  437. gpio_free(EGPIO_MAGICIAN_EP_POWER);
  438. err_request_ep:
  439. gpio_free(EGPIO_MAGICIAN_SPK_POWER);
  440. err_request_spk:
  441. return ret;
  442. }
  443. static void __exit magician_exit(void)
  444. {
  445. platform_device_unregister(magician_snd_device);
  446. gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, 0);
  447. gpio_set_value(EGPIO_MAGICIAN_EP_POWER, 0);
  448. gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, 0);
  449. gpio_free(EGPIO_MAGICIAN_IN_SEL1);
  450. gpio_free(EGPIO_MAGICIAN_IN_SEL0);
  451. gpio_free(EGPIO_MAGICIAN_MIC_POWER);
  452. gpio_free(EGPIO_MAGICIAN_EP_POWER);
  453. gpio_free(EGPIO_MAGICIAN_SPK_POWER);
  454. }
  455. module_init(magician_init);
  456. module_exit(magician_exit);
  457. MODULE_AUTHOR("Philipp Zabel");
  458. MODULE_DESCRIPTION("ALSA SoC Magician");
  459. MODULE_LICENSE("GPL");