tegra_wm8903.c 13 KB

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