tegra_wm8903.c 14 KB

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