tegra_wm8903.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * tegra_wm8903.c - Tegra machine ASoC driver for boards using WM8903 codec.
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010-2011 - NVIDIA, Inc.
  6. *
  7. * Based on code copyright/by:
  8. *
  9. * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
  10. *
  11. * Copyright 2007 Wolfson Microelectronics PLC.
  12. * Author: Graeme Gregory
  13. * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  27. * 02110-1301 USA
  28. *
  29. */
  30. #include <asm/mach-types.h>
  31. #include <linux/module.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/slab.h>
  34. #include <linux/gpio.h>
  35. #include <mach/tegra_wm8903_pdata.h>
  36. #include <sound/core.h>
  37. #include <sound/jack.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/soc.h>
  41. #include "../codecs/wm8903.h"
  42. #include "tegra_das.h"
  43. #include "tegra_i2s.h"
  44. #include "tegra_pcm.h"
  45. #include "tegra_asoc_utils.h"
  46. #define DRV_NAME "tegra-snd-wm8903"
  47. #define GPIO_SPKR_EN BIT(0)
  48. #define GPIO_HP_MUTE BIT(1)
  49. #define GPIO_INT_MIC_EN BIT(2)
  50. #define GPIO_EXT_MIC_EN BIT(3)
  51. #define GPIO_HP_DET BIT(4)
  52. struct tegra_wm8903 {
  53. struct tegra_asoc_utils_data util_data;
  54. struct tegra_wm8903_platform_data *pdata;
  55. int gpio_requested;
  56. };
  57. static int tegra_wm8903_hw_params(struct snd_pcm_substream *substream,
  58. struct snd_pcm_hw_params *params)
  59. {
  60. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  61. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  62. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  63. struct snd_soc_codec *codec = rtd->codec;
  64. struct snd_soc_card *card = codec->card;
  65. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  66. int srate, mclk;
  67. int err;
  68. srate = params_rate(params);
  69. switch (srate) {
  70. case 64000:
  71. case 88200:
  72. case 96000:
  73. mclk = 128 * srate;
  74. break;
  75. default:
  76. mclk = 256 * srate;
  77. break;
  78. }
  79. /* FIXME: Codec only requires >= 3MHz if OSR==0 */
  80. while (mclk < 6000000)
  81. mclk *= 2;
  82. err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
  83. if (err < 0) {
  84. dev_err(card->dev, "Can't configure clocks\n");
  85. return err;
  86. }
  87. err = snd_soc_dai_set_fmt(codec_dai,
  88. SND_SOC_DAIFMT_I2S |
  89. SND_SOC_DAIFMT_NB_NF |
  90. SND_SOC_DAIFMT_CBS_CFS);
  91. if (err < 0) {
  92. dev_err(card->dev, "codec_dai fmt not set\n");
  93. return err;
  94. }
  95. err = snd_soc_dai_set_fmt(cpu_dai,
  96. SND_SOC_DAIFMT_I2S |
  97. SND_SOC_DAIFMT_NB_NF |
  98. SND_SOC_DAIFMT_CBS_CFS);
  99. if (err < 0) {
  100. dev_err(card->dev, "cpu_dai fmt not set\n");
  101. return err;
  102. }
  103. err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  104. SND_SOC_CLOCK_IN);
  105. if (err < 0) {
  106. dev_err(card->dev, "codec_dai clock not set\n");
  107. return err;
  108. }
  109. return 0;
  110. }
  111. static struct snd_soc_ops tegra_wm8903_ops = {
  112. .hw_params = tegra_wm8903_hw_params,
  113. };
  114. static struct snd_soc_jack tegra_wm8903_hp_jack;
  115. static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = {
  116. {
  117. .pin = "Headphone Jack",
  118. .mask = SND_JACK_HEADPHONE,
  119. },
  120. };
  121. static struct snd_soc_jack_gpio tegra_wm8903_hp_jack_gpio = {
  122. .name = "headphone detect",
  123. .report = SND_JACK_HEADPHONE,
  124. .debounce_time = 150,
  125. .invert = 1,
  126. };
  127. static struct snd_soc_jack tegra_wm8903_mic_jack;
  128. static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
  129. {
  130. .pin = "Mic Jack",
  131. .mask = SND_JACK_MICROPHONE,
  132. },
  133. };
  134. static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget *w,
  135. struct snd_kcontrol *k, int event)
  136. {
  137. struct snd_soc_dapm_context *dapm = w->dapm;
  138. struct snd_soc_card *card = dapm->card;
  139. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  140. struct tegra_wm8903_platform_data *pdata = machine->pdata;
  141. if (!(machine->gpio_requested & GPIO_SPKR_EN))
  142. return 0;
  143. gpio_set_value_cansleep(pdata->gpio_spkr_en,
  144. SND_SOC_DAPM_EVENT_ON(event));
  145. return 0;
  146. }
  147. static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget *w,
  148. struct snd_kcontrol *k, int event)
  149. {
  150. struct snd_soc_dapm_context *dapm = w->dapm;
  151. struct snd_soc_card *card = dapm->card;
  152. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  153. struct tegra_wm8903_platform_data *pdata = machine->pdata;
  154. if (!(machine->gpio_requested & GPIO_HP_MUTE))
  155. return 0;
  156. gpio_set_value_cansleep(pdata->gpio_hp_mute,
  157. !SND_SOC_DAPM_EVENT_ON(event));
  158. return 0;
  159. }
  160. static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets[] = {
  161. SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk),
  162. SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp),
  163. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  164. };
  165. static const struct snd_soc_dapm_route harmony_audio_map[] = {
  166. {"Headphone Jack", NULL, "HPOUTR"},
  167. {"Headphone Jack", NULL, "HPOUTL"},
  168. {"Int Spk", NULL, "ROP"},
  169. {"Int Spk", NULL, "RON"},
  170. {"Int Spk", NULL, "LOP"},
  171. {"Int Spk", NULL, "LON"},
  172. {"Mic Bias", NULL, "Mic Jack"},
  173. {"IN1L", NULL, "Mic Bias"},
  174. };
  175. static const struct snd_soc_dapm_route seaboard_audio_map[] = {
  176. {"Headphone Jack", NULL, "HPOUTR"},
  177. {"Headphone Jack", NULL, "HPOUTL"},
  178. {"Int Spk", NULL, "ROP"},
  179. {"Int Spk", NULL, "RON"},
  180. {"Int Spk", NULL, "LOP"},
  181. {"Int Spk", NULL, "LON"},
  182. {"Mic Bias", NULL, "Mic Jack"},
  183. {"IN1R", NULL, "Mic Bias"},
  184. };
  185. static const struct snd_soc_dapm_route kaen_audio_map[] = {
  186. {"Headphone Jack", NULL, "HPOUTR"},
  187. {"Headphone Jack", NULL, "HPOUTL"},
  188. {"Int Spk", NULL, "ROP"},
  189. {"Int Spk", NULL, "RON"},
  190. {"Int Spk", NULL, "LOP"},
  191. {"Int Spk", NULL, "LON"},
  192. {"Mic Bias", NULL, "Mic Jack"},
  193. {"IN2R", NULL, "Mic Bias"},
  194. };
  195. static const struct snd_soc_dapm_route aebl_audio_map[] = {
  196. {"Headphone Jack", NULL, "HPOUTR"},
  197. {"Headphone Jack", NULL, "HPOUTL"},
  198. {"Int Spk", NULL, "LINEOUTR"},
  199. {"Int Spk", NULL, "LINEOUTL"},
  200. {"Mic Bias", NULL, "Mic Jack"},
  201. {"IN1R", NULL, "Mic Bias"},
  202. };
  203. static const struct snd_kcontrol_new tegra_wm8903_controls[] = {
  204. SOC_DAPM_PIN_SWITCH("Int Spk"),
  205. };
  206. static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
  207. {
  208. struct snd_soc_codec *codec = rtd->codec;
  209. struct snd_soc_dapm_context *dapm = &codec->dapm;
  210. struct snd_soc_card *card = codec->card;
  211. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  212. struct tegra_wm8903_platform_data *pdata = machine->pdata;
  213. int ret;
  214. ret = tegra_das_connect_dap_to_dac(TEGRA_DAS_DAP_ID_1,
  215. TEGRA_DAS_DAP_SEL_DAC1);
  216. if (ret) {
  217. dev_err(card->dev, "Can't set up DAS DAP connection\n");
  218. return ret;
  219. }
  220. ret = tegra_das_connect_dac_to_dap(TEGRA_DAS_DAC_ID_1,
  221. TEGRA_DAS_DAC_SEL_DAP1);
  222. if (ret) {
  223. dev_err(card->dev, "Can't set up DAS DAC connection\n");
  224. return ret;
  225. }
  226. if (gpio_is_valid(pdata->gpio_spkr_en)) {
  227. ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
  228. if (ret) {
  229. dev_err(card->dev, "cannot get spkr_en gpio\n");
  230. return ret;
  231. }
  232. machine->gpio_requested |= GPIO_SPKR_EN;
  233. gpio_direction_output(pdata->gpio_spkr_en, 0);
  234. }
  235. if (gpio_is_valid(pdata->gpio_hp_mute)) {
  236. ret = gpio_request(pdata->gpio_hp_mute, "hp_mute");
  237. if (ret) {
  238. dev_err(card->dev, "cannot get hp_mute gpio\n");
  239. return ret;
  240. }
  241. machine->gpio_requested |= GPIO_HP_MUTE;
  242. gpio_direction_output(pdata->gpio_hp_mute, 1);
  243. }
  244. if (gpio_is_valid(pdata->gpio_int_mic_en)) {
  245. ret = gpio_request(pdata->gpio_int_mic_en, "int_mic_en");
  246. if (ret) {
  247. dev_err(card->dev, "cannot get int_mic_en gpio\n");
  248. return ret;
  249. }
  250. machine->gpio_requested |= GPIO_INT_MIC_EN;
  251. /* Disable int mic; enable signal is active-high */
  252. gpio_direction_output(pdata->gpio_int_mic_en, 0);
  253. }
  254. if (gpio_is_valid(pdata->gpio_ext_mic_en)) {
  255. ret = gpio_request(pdata->gpio_ext_mic_en, "ext_mic_en");
  256. if (ret) {
  257. dev_err(card->dev, "cannot get ext_mic_en gpio\n");
  258. return ret;
  259. }
  260. machine->gpio_requested |= GPIO_EXT_MIC_EN;
  261. /* Enable ext mic; enable signal is active-low */
  262. gpio_direction_output(pdata->gpio_ext_mic_en, 0);
  263. }
  264. if (gpio_is_valid(pdata->gpio_hp_det)) {
  265. tegra_wm8903_hp_jack_gpio.gpio = pdata->gpio_hp_det;
  266. snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
  267. &tegra_wm8903_hp_jack);
  268. snd_soc_jack_add_pins(&tegra_wm8903_hp_jack,
  269. ARRAY_SIZE(tegra_wm8903_hp_jack_pins),
  270. tegra_wm8903_hp_jack_pins);
  271. snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
  272. 1,
  273. &tegra_wm8903_hp_jack_gpio);
  274. machine->gpio_requested |= GPIO_HP_DET;
  275. }
  276. snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
  277. &tegra_wm8903_mic_jack);
  278. snd_soc_jack_add_pins(&tegra_wm8903_mic_jack,
  279. ARRAY_SIZE(tegra_wm8903_mic_jack_pins),
  280. tegra_wm8903_mic_jack_pins);
  281. wm8903_mic_detect(codec, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
  282. 0);
  283. snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
  284. /* FIXME: Calculate automatically based on DAPM routes? */
  285. if (!machine_is_harmony())
  286. snd_soc_dapm_nc_pin(dapm, "IN1L");
  287. if (!machine_is_seaboard() && !machine_is_aebl())
  288. snd_soc_dapm_nc_pin(dapm, "IN1R");
  289. snd_soc_dapm_nc_pin(dapm, "IN2L");
  290. if (!machine_is_kaen())
  291. snd_soc_dapm_nc_pin(dapm, "IN2R");
  292. snd_soc_dapm_nc_pin(dapm, "IN3L");
  293. snd_soc_dapm_nc_pin(dapm, "IN3R");
  294. if (machine_is_aebl()) {
  295. snd_soc_dapm_nc_pin(dapm, "LON");
  296. snd_soc_dapm_nc_pin(dapm, "RON");
  297. snd_soc_dapm_nc_pin(dapm, "ROP");
  298. snd_soc_dapm_nc_pin(dapm, "LOP");
  299. } else {
  300. snd_soc_dapm_nc_pin(dapm, "LINEOUTR");
  301. snd_soc_dapm_nc_pin(dapm, "LINEOUTL");
  302. }
  303. return 0;
  304. }
  305. static struct snd_soc_dai_link tegra_wm8903_dai = {
  306. .name = "WM8903",
  307. .stream_name = "WM8903 PCM",
  308. .codec_name = "wm8903.0-001a",
  309. .platform_name = "tegra-pcm-audio",
  310. .cpu_dai_name = "tegra-i2s.0",
  311. .codec_dai_name = "wm8903-hifi",
  312. .init = tegra_wm8903_init,
  313. .ops = &tegra_wm8903_ops,
  314. };
  315. static struct snd_soc_card snd_soc_tegra_wm8903 = {
  316. .name = "tegra-wm8903",
  317. .dai_link = &tegra_wm8903_dai,
  318. .num_links = 1,
  319. .controls = tegra_wm8903_controls,
  320. .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
  321. .dapm_widgets = tegra_wm8903_dapm_widgets,
  322. .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
  323. };
  324. static __devinit int tegra_wm8903_driver_probe(struct platform_device *pdev)
  325. {
  326. struct snd_soc_card *card = &snd_soc_tegra_wm8903;
  327. struct tegra_wm8903 *machine;
  328. struct tegra_wm8903_platform_data *pdata;
  329. int ret;
  330. pdata = pdev->dev.platform_data;
  331. if (!pdata) {
  332. dev_err(&pdev->dev, "No platform data supplied\n");
  333. return -EINVAL;
  334. }
  335. machine = kzalloc(sizeof(struct tegra_wm8903), GFP_KERNEL);
  336. if (!machine) {
  337. dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n");
  338. return -ENOMEM;
  339. }
  340. machine->pdata = pdata;
  341. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  342. if (ret)
  343. goto err_free_machine;
  344. card->dev = &pdev->dev;
  345. platform_set_drvdata(pdev, card);
  346. snd_soc_card_set_drvdata(card, machine);
  347. if (machine_is_harmony()) {
  348. card->dapm_routes = harmony_audio_map;
  349. card->num_dapm_routes = ARRAY_SIZE(harmony_audio_map);
  350. } else if (machine_is_seaboard()) {
  351. card->dapm_routes = seaboard_audio_map;
  352. card->num_dapm_routes = ARRAY_SIZE(seaboard_audio_map);
  353. } else if (machine_is_kaen()) {
  354. card->dapm_routes = kaen_audio_map;
  355. card->num_dapm_routes = ARRAY_SIZE(kaen_audio_map);
  356. } else {
  357. card->dapm_routes = aebl_audio_map;
  358. card->num_dapm_routes = ARRAY_SIZE(aebl_audio_map);
  359. }
  360. ret = snd_soc_register_card(card);
  361. if (ret) {
  362. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  363. ret);
  364. goto err_fini_utils;
  365. }
  366. return 0;
  367. err_fini_utils:
  368. tegra_asoc_utils_fini(&machine->util_data);
  369. err_free_machine:
  370. kfree(machine);
  371. return ret;
  372. }
  373. static int __devexit tegra_wm8903_driver_remove(struct platform_device *pdev)
  374. {
  375. struct snd_soc_card *card = platform_get_drvdata(pdev);
  376. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  377. struct tegra_wm8903_platform_data *pdata = machine->pdata;
  378. if (machine->gpio_requested & GPIO_HP_DET)
  379. snd_soc_jack_free_gpios(&tegra_wm8903_hp_jack,
  380. 1,
  381. &tegra_wm8903_hp_jack_gpio);
  382. if (machine->gpio_requested & GPIO_EXT_MIC_EN)
  383. gpio_free(pdata->gpio_ext_mic_en);
  384. if (machine->gpio_requested & GPIO_INT_MIC_EN)
  385. gpio_free(pdata->gpio_int_mic_en);
  386. if (machine->gpio_requested & GPIO_HP_MUTE)
  387. gpio_free(pdata->gpio_hp_mute);
  388. if (machine->gpio_requested & GPIO_SPKR_EN)
  389. gpio_free(pdata->gpio_spkr_en);
  390. machine->gpio_requested = 0;
  391. snd_soc_unregister_card(card);
  392. tegra_asoc_utils_fini(&machine->util_data);
  393. kfree(machine);
  394. return 0;
  395. }
  396. static struct platform_driver tegra_wm8903_driver = {
  397. .driver = {
  398. .name = DRV_NAME,
  399. .owner = THIS_MODULE,
  400. .pm = &snd_soc_pm_ops,
  401. },
  402. .probe = tegra_wm8903_driver_probe,
  403. .remove = __devexit_p(tegra_wm8903_driver_remove),
  404. };
  405. static int __init tegra_wm8903_modinit(void)
  406. {
  407. return platform_driver_register(&tegra_wm8903_driver);
  408. }
  409. module_init(tegra_wm8903_modinit);
  410. static void __exit tegra_wm8903_modexit(void)
  411. {
  412. platform_driver_unregister(&tegra_wm8903_driver);
  413. }
  414. module_exit(tegra_wm8903_modexit);
  415. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  416. MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
  417. MODULE_LICENSE("GPL");
  418. MODULE_ALIAS("platform:" DRV_NAME);