tegra_wm8903.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. struct tegra_wm8903 {
  52. struct tegra_asoc_utils_data util_data;
  53. struct tegra_wm8903_platform_data *pdata;
  54. int gpio_requested;
  55. };
  56. static int tegra_wm8903_hw_params(struct snd_pcm_substream *substream,
  57. struct snd_pcm_hw_params *params)
  58. {
  59. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  60. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  61. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  62. struct snd_soc_codec *codec = rtd->codec;
  63. struct snd_soc_card *card = codec->card;
  64. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  65. int srate, mclk;
  66. int err;
  67. srate = params_rate(params);
  68. switch (srate) {
  69. case 64000:
  70. case 88200:
  71. case 96000:
  72. mclk = 128 * srate;
  73. break;
  74. default:
  75. mclk = 256 * srate;
  76. break;
  77. }
  78. /* FIXME: Codec only requires >= 3MHz if OSR==0 */
  79. while (mclk < 6000000)
  80. mclk *= 2;
  81. err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
  82. if (err < 0) {
  83. dev_err(card->dev, "Can't configure clocks\n");
  84. return err;
  85. }
  86. err = snd_soc_dai_set_fmt(codec_dai,
  87. SND_SOC_DAIFMT_I2S |
  88. SND_SOC_DAIFMT_NB_NF |
  89. SND_SOC_DAIFMT_CBS_CFS);
  90. if (err < 0) {
  91. dev_err(card->dev, "codec_dai fmt not set\n");
  92. return err;
  93. }
  94. err = snd_soc_dai_set_fmt(cpu_dai,
  95. SND_SOC_DAIFMT_I2S |
  96. SND_SOC_DAIFMT_NB_NF |
  97. SND_SOC_DAIFMT_CBS_CFS);
  98. if (err < 0) {
  99. dev_err(card->dev, "cpu_dai fmt not set\n");
  100. return err;
  101. }
  102. err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  103. SND_SOC_CLOCK_IN);
  104. if (err < 0) {
  105. dev_err(card->dev, "codec_dai clock not set\n");
  106. return err;
  107. }
  108. return 0;
  109. }
  110. static struct snd_soc_ops tegra_wm8903_ops = {
  111. .hw_params = tegra_wm8903_hw_params,
  112. };
  113. static struct snd_soc_jack tegra_wm8903_hp_jack;
  114. static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = {
  115. {
  116. .pin = "Headphone Jack",
  117. .mask = SND_JACK_HEADPHONE,
  118. },
  119. };
  120. static struct snd_soc_jack_gpio tegra_wm8903_hp_jack_gpio = {
  121. .name = "headphone detect",
  122. .report = SND_JACK_HEADPHONE,
  123. .debounce_time = 150,
  124. .invert = 1,
  125. };
  126. static struct snd_soc_jack tegra_wm8903_mic_jack;
  127. static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
  128. {
  129. .pin = "Mic Jack",
  130. .mask = SND_JACK_MICROPHONE,
  131. },
  132. };
  133. static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget *w,
  134. struct snd_kcontrol *k, int event)
  135. {
  136. struct snd_soc_dapm_context *dapm = w->dapm;
  137. struct snd_soc_card *card = dapm->card;
  138. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  139. struct tegra_wm8903_platform_data *pdata = machine->pdata;
  140. if (!(machine->gpio_requested & GPIO_SPKR_EN))
  141. return 0;
  142. gpio_set_value_cansleep(pdata->gpio_spkr_en,
  143. SND_SOC_DAPM_EVENT_ON(event));
  144. return 0;
  145. }
  146. static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget *w,
  147. struct snd_kcontrol *k, int event)
  148. {
  149. struct snd_soc_dapm_context *dapm = w->dapm;
  150. struct snd_soc_card *card = dapm->card;
  151. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  152. struct tegra_wm8903_platform_data *pdata = machine->pdata;
  153. if (!(machine->gpio_requested & GPIO_HP_MUTE))
  154. return 0;
  155. gpio_set_value_cansleep(pdata->gpio_hp_mute,
  156. !SND_SOC_DAPM_EVENT_ON(event));
  157. return 0;
  158. }
  159. static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets[] = {
  160. SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk),
  161. SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp),
  162. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  163. };
  164. static const struct snd_soc_dapm_route harmony_audio_map[] = {
  165. {"Headphone Jack", NULL, "HPOUTR"},
  166. {"Headphone Jack", NULL, "HPOUTL"},
  167. {"Int Spk", NULL, "ROP"},
  168. {"Int Spk", NULL, "RON"},
  169. {"Int Spk", NULL, "LOP"},
  170. {"Int Spk", NULL, "LON"},
  171. {"Mic Bias", NULL, "Mic Jack"},
  172. {"IN1L", NULL, "Mic Bias"},
  173. };
  174. static const struct snd_soc_dapm_route seaboard_audio_map[] = {
  175. {"Headphone Jack", NULL, "HPOUTR"},
  176. {"Headphone Jack", NULL, "HPOUTL"},
  177. {"Int Spk", NULL, "ROP"},
  178. {"Int Spk", NULL, "RON"},
  179. {"Int Spk", NULL, "LOP"},
  180. {"Int Spk", NULL, "LON"},
  181. {"Mic Bias", NULL, "Mic Jack"},
  182. {"IN1R", NULL, "Mic Bias"},
  183. };
  184. static const struct snd_soc_dapm_route kaen_audio_map[] = {
  185. {"Headphone Jack", NULL, "HPOUTR"},
  186. {"Headphone Jack", NULL, "HPOUTL"},
  187. {"Int Spk", NULL, "ROP"},
  188. {"Int Spk", NULL, "RON"},
  189. {"Int Spk", NULL, "LOP"},
  190. {"Int Spk", NULL, "LON"},
  191. {"Mic Bias", NULL, "Mic Jack"},
  192. {"IN2R", NULL, "Mic Bias"},
  193. };
  194. static const struct snd_soc_dapm_route aebl_audio_map[] = {
  195. {"Headphone Jack", NULL, "HPOUTR"},
  196. {"Headphone Jack", NULL, "HPOUTL"},
  197. {"Int Spk", NULL, "LINEOUTR"},
  198. {"Int Spk", NULL, "LINEOUTL"},
  199. {"Mic Bias", NULL, "Mic Jack"},
  200. {"IN1R", NULL, "Mic Bias"},
  201. };
  202. static const struct snd_kcontrol_new tegra_wm8903_controls[] = {
  203. SOC_DAPM_PIN_SWITCH("Int Spk"),
  204. };
  205. static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
  206. {
  207. struct snd_soc_codec *codec = rtd->codec;
  208. struct snd_soc_dapm_context *dapm = &codec->dapm;
  209. struct snd_soc_card *card = codec->card;
  210. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  211. struct tegra_wm8903_platform_data *pdata = machine->pdata;
  212. int ret;
  213. if (gpio_is_valid(pdata->gpio_spkr_en)) {
  214. ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
  215. if (ret) {
  216. dev_err(card->dev, "cannot get spkr_en gpio\n");
  217. return ret;
  218. }
  219. machine->gpio_requested |= GPIO_SPKR_EN;
  220. gpio_direction_output(pdata->gpio_spkr_en, 0);
  221. }
  222. if (gpio_is_valid(pdata->gpio_hp_mute)) {
  223. ret = gpio_request(pdata->gpio_hp_mute, "hp_mute");
  224. if (ret) {
  225. dev_err(card->dev, "cannot get hp_mute gpio\n");
  226. return ret;
  227. }
  228. machine->gpio_requested |= GPIO_HP_MUTE;
  229. gpio_direction_output(pdata->gpio_hp_mute, 1);
  230. }
  231. if (gpio_is_valid(pdata->gpio_int_mic_en)) {
  232. ret = gpio_request(pdata->gpio_int_mic_en, "int_mic_en");
  233. if (ret) {
  234. dev_err(card->dev, "cannot get int_mic_en gpio\n");
  235. return ret;
  236. }
  237. machine->gpio_requested |= GPIO_INT_MIC_EN;
  238. /* Disable int mic; enable signal is active-high */
  239. gpio_direction_output(pdata->gpio_int_mic_en, 0);
  240. }
  241. if (gpio_is_valid(pdata->gpio_ext_mic_en)) {
  242. ret = gpio_request(pdata->gpio_ext_mic_en, "ext_mic_en");
  243. if (ret) {
  244. dev_err(card->dev, "cannot get ext_mic_en gpio\n");
  245. return ret;
  246. }
  247. machine->gpio_requested |= GPIO_EXT_MIC_EN;
  248. /* Enable ext mic; enable signal is active-low */
  249. gpio_direction_output(pdata->gpio_ext_mic_en, 0);
  250. }
  251. if (gpio_is_valid(pdata->gpio_hp_det)) {
  252. tegra_wm8903_hp_jack_gpio.gpio = pdata->gpio_hp_det;
  253. snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
  254. &tegra_wm8903_hp_jack);
  255. snd_soc_jack_add_pins(&tegra_wm8903_hp_jack,
  256. ARRAY_SIZE(tegra_wm8903_hp_jack_pins),
  257. tegra_wm8903_hp_jack_pins);
  258. snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
  259. 1,
  260. &tegra_wm8903_hp_jack_gpio);
  261. }
  262. snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
  263. &tegra_wm8903_mic_jack);
  264. snd_soc_jack_add_pins(&tegra_wm8903_mic_jack,
  265. ARRAY_SIZE(tegra_wm8903_mic_jack_pins),
  266. tegra_wm8903_mic_jack_pins);
  267. wm8903_mic_detect(codec, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
  268. 0);
  269. snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
  270. /* FIXME: Calculate automatically based on DAPM routes? */
  271. if (!machine_is_harmony() && !machine_is_ventana())
  272. snd_soc_dapm_nc_pin(dapm, "IN1L");
  273. if (!machine_is_seaboard() && !machine_is_aebl())
  274. snd_soc_dapm_nc_pin(dapm, "IN1R");
  275. snd_soc_dapm_nc_pin(dapm, "IN2L");
  276. if (!machine_is_kaen())
  277. snd_soc_dapm_nc_pin(dapm, "IN2R");
  278. snd_soc_dapm_nc_pin(dapm, "IN3L");
  279. snd_soc_dapm_nc_pin(dapm, "IN3R");
  280. if (machine_is_aebl()) {
  281. snd_soc_dapm_nc_pin(dapm, "LON");
  282. snd_soc_dapm_nc_pin(dapm, "RON");
  283. snd_soc_dapm_nc_pin(dapm, "ROP");
  284. snd_soc_dapm_nc_pin(dapm, "LOP");
  285. } else {
  286. snd_soc_dapm_nc_pin(dapm, "LINEOUTR");
  287. snd_soc_dapm_nc_pin(dapm, "LINEOUTL");
  288. }
  289. snd_soc_dapm_sync(dapm);
  290. return 0;
  291. }
  292. static struct snd_soc_dai_link tegra_wm8903_dai = {
  293. .name = "WM8903",
  294. .stream_name = "WM8903 PCM",
  295. .codec_name = "wm8903.0-001a",
  296. .platform_name = "tegra-pcm-audio",
  297. .cpu_dai_name = "tegra-i2s.0",
  298. .codec_dai_name = "wm8903-hifi",
  299. .init = tegra_wm8903_init,
  300. .ops = &tegra_wm8903_ops,
  301. };
  302. static struct snd_soc_card snd_soc_tegra_wm8903 = {
  303. .name = "tegra-wm8903",
  304. .dai_link = &tegra_wm8903_dai,
  305. .num_links = 1,
  306. .controls = tegra_wm8903_controls,
  307. .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
  308. .dapm_widgets = tegra_wm8903_dapm_widgets,
  309. .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
  310. };
  311. static __devinit int tegra_wm8903_driver_probe(struct platform_device *pdev)
  312. {
  313. struct snd_soc_card *card = &snd_soc_tegra_wm8903;
  314. struct tegra_wm8903 *machine;
  315. struct tegra_wm8903_platform_data *pdata;
  316. int ret;
  317. pdata = pdev->dev.platform_data;
  318. if (!pdata) {
  319. dev_err(&pdev->dev, "No platform data supplied\n");
  320. return -EINVAL;
  321. }
  322. machine = kzalloc(sizeof(struct tegra_wm8903), GFP_KERNEL);
  323. if (!machine) {
  324. dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n");
  325. return -ENOMEM;
  326. }
  327. machine->pdata = pdata;
  328. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  329. if (ret)
  330. goto err_free_machine;
  331. card->dev = &pdev->dev;
  332. platform_set_drvdata(pdev, card);
  333. snd_soc_card_set_drvdata(card, machine);
  334. if (machine_is_harmony() || machine_is_ventana()) {
  335. card->dapm_routes = harmony_audio_map;
  336. card->num_dapm_routes = ARRAY_SIZE(harmony_audio_map);
  337. } else if (machine_is_seaboard()) {
  338. card->dapm_routes = seaboard_audio_map;
  339. card->num_dapm_routes = ARRAY_SIZE(seaboard_audio_map);
  340. } else if (machine_is_kaen()) {
  341. card->dapm_routes = kaen_audio_map;
  342. card->num_dapm_routes = ARRAY_SIZE(kaen_audio_map);
  343. } else {
  344. card->dapm_routes = aebl_audio_map;
  345. card->num_dapm_routes = ARRAY_SIZE(aebl_audio_map);
  346. }
  347. ret = snd_soc_register_card(card);
  348. if (ret) {
  349. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  350. ret);
  351. goto err_fini_utils;
  352. }
  353. return 0;
  354. err_fini_utils:
  355. tegra_asoc_utils_fini(&machine->util_data);
  356. err_free_machine:
  357. kfree(machine);
  358. return ret;
  359. }
  360. static int __devexit tegra_wm8903_driver_remove(struct platform_device *pdev)
  361. {
  362. struct snd_soc_card *card = platform_get_drvdata(pdev);
  363. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  364. struct tegra_wm8903_platform_data *pdata = machine->pdata;
  365. snd_soc_unregister_card(card);
  366. tegra_asoc_utils_fini(&machine->util_data);
  367. if (machine->gpio_requested & GPIO_EXT_MIC_EN)
  368. gpio_free(pdata->gpio_ext_mic_en);
  369. if (machine->gpio_requested & GPIO_INT_MIC_EN)
  370. gpio_free(pdata->gpio_int_mic_en);
  371. if (machine->gpio_requested & GPIO_HP_MUTE)
  372. gpio_free(pdata->gpio_hp_mute);
  373. if (machine->gpio_requested & GPIO_SPKR_EN)
  374. gpio_free(pdata->gpio_spkr_en);
  375. kfree(machine);
  376. return 0;
  377. }
  378. static struct platform_driver tegra_wm8903_driver = {
  379. .driver = {
  380. .name = DRV_NAME,
  381. .owner = THIS_MODULE,
  382. .pm = &snd_soc_pm_ops,
  383. },
  384. .probe = tegra_wm8903_driver_probe,
  385. .remove = __devexit_p(tegra_wm8903_driver_remove),
  386. };
  387. static int __init tegra_wm8903_modinit(void)
  388. {
  389. return platform_driver_register(&tegra_wm8903_driver);
  390. }
  391. module_init(tegra_wm8903_modinit);
  392. static void __exit tegra_wm8903_modexit(void)
  393. {
  394. platform_driver_unregister(&tegra_wm8903_driver);
  395. }
  396. module_exit(tegra_wm8903_modexit);
  397. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  398. MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
  399. MODULE_LICENSE("GPL");
  400. MODULE_ALIAS("platform:" DRV_NAME);