tegra_wm8903.c 14 KB

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