tegra_wm8903.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. if (gpio_is_valid(pdata->gpio_spkr_en)) {
  215. ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
  216. if (ret) {
  217. dev_err(card->dev, "cannot get spkr_en gpio\n");
  218. return ret;
  219. }
  220. machine->gpio_requested |= GPIO_SPKR_EN;
  221. gpio_direction_output(pdata->gpio_spkr_en, 0);
  222. }
  223. if (gpio_is_valid(pdata->gpio_hp_mute)) {
  224. ret = gpio_request(pdata->gpio_hp_mute, "hp_mute");
  225. if (ret) {
  226. dev_err(card->dev, "cannot get hp_mute gpio\n");
  227. return ret;
  228. }
  229. machine->gpio_requested |= GPIO_HP_MUTE;
  230. gpio_direction_output(pdata->gpio_hp_mute, 0);
  231. }
  232. if (gpio_is_valid(pdata->gpio_int_mic_en)) {
  233. ret = gpio_request(pdata->gpio_int_mic_en, "int_mic_en");
  234. if (ret) {
  235. dev_err(card->dev, "cannot get int_mic_en gpio\n");
  236. return ret;
  237. }
  238. machine->gpio_requested |= GPIO_INT_MIC_EN;
  239. /* Disable int mic; enable signal is active-high */
  240. gpio_direction_output(pdata->gpio_int_mic_en, 0);
  241. }
  242. if (gpio_is_valid(pdata->gpio_ext_mic_en)) {
  243. ret = gpio_request(pdata->gpio_ext_mic_en, "ext_mic_en");
  244. if (ret) {
  245. dev_err(card->dev, "cannot get ext_mic_en gpio\n");
  246. return ret;
  247. }
  248. machine->gpio_requested |= GPIO_EXT_MIC_EN;
  249. /* Enable ext mic; enable signal is active-low */
  250. gpio_direction_output(pdata->gpio_ext_mic_en, 0);
  251. }
  252. if (gpio_is_valid(pdata->gpio_hp_det)) {
  253. tegra_wm8903_hp_jack_gpio.gpio = pdata->gpio_hp_det;
  254. snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
  255. &tegra_wm8903_hp_jack);
  256. snd_soc_jack_add_pins(&tegra_wm8903_hp_jack,
  257. ARRAY_SIZE(tegra_wm8903_hp_jack_pins),
  258. tegra_wm8903_hp_jack_pins);
  259. snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
  260. 1,
  261. &tegra_wm8903_hp_jack_gpio);
  262. machine->gpio_requested |= GPIO_HP_DET;
  263. }
  264. snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
  265. &tegra_wm8903_mic_jack);
  266. snd_soc_jack_add_pins(&tegra_wm8903_mic_jack,
  267. ARRAY_SIZE(tegra_wm8903_mic_jack_pins),
  268. tegra_wm8903_mic_jack_pins);
  269. wm8903_mic_detect(codec, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
  270. 0);
  271. snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
  272. /* FIXME: Calculate automatically based on DAPM routes? */
  273. if (!machine_is_harmony() && !machine_is_ventana())
  274. snd_soc_dapm_nc_pin(dapm, "IN1L");
  275. if (!machine_is_seaboard() && !machine_is_aebl())
  276. snd_soc_dapm_nc_pin(dapm, "IN1R");
  277. snd_soc_dapm_nc_pin(dapm, "IN2L");
  278. if (!machine_is_kaen())
  279. snd_soc_dapm_nc_pin(dapm, "IN2R");
  280. snd_soc_dapm_nc_pin(dapm, "IN3L");
  281. snd_soc_dapm_nc_pin(dapm, "IN3R");
  282. if (machine_is_aebl()) {
  283. snd_soc_dapm_nc_pin(dapm, "LON");
  284. snd_soc_dapm_nc_pin(dapm, "RON");
  285. snd_soc_dapm_nc_pin(dapm, "ROP");
  286. snd_soc_dapm_nc_pin(dapm, "LOP");
  287. } else {
  288. snd_soc_dapm_nc_pin(dapm, "LINEOUTR");
  289. snd_soc_dapm_nc_pin(dapm, "LINEOUTL");
  290. }
  291. snd_soc_dapm_sync(dapm);
  292. return 0;
  293. }
  294. static struct snd_soc_dai_link tegra_wm8903_dai = {
  295. .name = "WM8903",
  296. .stream_name = "WM8903 PCM",
  297. .codec_name = "wm8903.0-001a",
  298. .platform_name = "tegra-pcm-audio",
  299. .cpu_dai_name = "tegra-i2s.0",
  300. .codec_dai_name = "wm8903-hifi",
  301. .init = tegra_wm8903_init,
  302. .ops = &tegra_wm8903_ops,
  303. };
  304. static struct snd_soc_card snd_soc_tegra_wm8903 = {
  305. .name = "tegra-wm8903",
  306. .dai_link = &tegra_wm8903_dai,
  307. .num_links = 1,
  308. .controls = tegra_wm8903_controls,
  309. .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
  310. .dapm_widgets = tegra_wm8903_dapm_widgets,
  311. .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
  312. };
  313. static __devinit int tegra_wm8903_driver_probe(struct platform_device *pdev)
  314. {
  315. struct snd_soc_card *card = &snd_soc_tegra_wm8903;
  316. struct tegra_wm8903 *machine;
  317. struct tegra_wm8903_platform_data *pdata;
  318. int ret;
  319. pdata = pdev->dev.platform_data;
  320. if (!pdata) {
  321. dev_err(&pdev->dev, "No platform data supplied\n");
  322. return -EINVAL;
  323. }
  324. machine = kzalloc(sizeof(struct tegra_wm8903), GFP_KERNEL);
  325. if (!machine) {
  326. dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n");
  327. return -ENOMEM;
  328. }
  329. machine->pdata = pdata;
  330. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  331. if (ret)
  332. goto err_free_machine;
  333. card->dev = &pdev->dev;
  334. platform_set_drvdata(pdev, card);
  335. snd_soc_card_set_drvdata(card, machine);
  336. if (machine_is_harmony() || machine_is_ventana()) {
  337. card->dapm_routes = harmony_audio_map;
  338. card->num_dapm_routes = ARRAY_SIZE(harmony_audio_map);
  339. } else if (machine_is_seaboard()) {
  340. card->dapm_routes = seaboard_audio_map;
  341. card->num_dapm_routes = ARRAY_SIZE(seaboard_audio_map);
  342. } else if (machine_is_kaen()) {
  343. card->dapm_routes = kaen_audio_map;
  344. card->num_dapm_routes = ARRAY_SIZE(kaen_audio_map);
  345. } else {
  346. card->dapm_routes = aebl_audio_map;
  347. card->num_dapm_routes = ARRAY_SIZE(aebl_audio_map);
  348. }
  349. ret = snd_soc_register_card(card);
  350. if (ret) {
  351. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  352. ret);
  353. goto err_fini_utils;
  354. }
  355. return 0;
  356. err_fini_utils:
  357. tegra_asoc_utils_fini(&machine->util_data);
  358. err_free_machine:
  359. kfree(machine);
  360. return ret;
  361. }
  362. static int __devexit tegra_wm8903_driver_remove(struct platform_device *pdev)
  363. {
  364. struct snd_soc_card *card = platform_get_drvdata(pdev);
  365. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  366. struct tegra_wm8903_platform_data *pdata = machine->pdata;
  367. if (machine->gpio_requested & GPIO_HP_DET)
  368. snd_soc_jack_free_gpios(&tegra_wm8903_hp_jack,
  369. 1,
  370. &tegra_wm8903_hp_jack_gpio);
  371. if (machine->gpio_requested & GPIO_EXT_MIC_EN)
  372. gpio_free(pdata->gpio_ext_mic_en);
  373. if (machine->gpio_requested & GPIO_INT_MIC_EN)
  374. gpio_free(pdata->gpio_int_mic_en);
  375. if (machine->gpio_requested & GPIO_HP_MUTE)
  376. gpio_free(pdata->gpio_hp_mute);
  377. if (machine->gpio_requested & GPIO_SPKR_EN)
  378. gpio_free(pdata->gpio_spkr_en);
  379. machine->gpio_requested = 0;
  380. snd_soc_unregister_card(card);
  381. tegra_asoc_utils_fini(&machine->util_data);
  382. kfree(machine);
  383. return 0;
  384. }
  385. static struct platform_driver tegra_wm8903_driver = {
  386. .driver = {
  387. .name = DRV_NAME,
  388. .owner = THIS_MODULE,
  389. .pm = &snd_soc_pm_ops,
  390. },
  391. .probe = tegra_wm8903_driver_probe,
  392. .remove = __devexit_p(tegra_wm8903_driver_remove),
  393. };
  394. static int __init tegra_wm8903_modinit(void)
  395. {
  396. return platform_driver_register(&tegra_wm8903_driver);
  397. }
  398. module_init(tegra_wm8903_modinit);
  399. static void __exit tegra_wm8903_modexit(void)
  400. {
  401. platform_driver_unregister(&tegra_wm8903_driver);
  402. }
  403. module_exit(tegra_wm8903_modexit);
  404. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  405. MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
  406. MODULE_LICENSE("GPL");
  407. MODULE_ALIAS("platform:" DRV_NAME);