tegra_wm8903.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. struct device_node *np = card->dev->of_node;
  212. int ret;
  213. if (card->dev->platform_data) {
  214. memcpy(pdata, card->dev->platform_data, sizeof(*pdata));
  215. } else if (np) {
  216. /*
  217. * This part must be in init() rather than probe() in order to
  218. * guarantee that the WM8903 has been probed, and hence its
  219. * GPIO controller registered, which is a pre-condition for
  220. * of_get_named_gpio() to be able to map the phandles in the
  221. * properties to the controller node. Given this, all
  222. * pdata handling is in init() for consistency.
  223. */
  224. pdata->gpio_spkr_en = of_get_named_gpio(np,
  225. "nvidia,spkr-en-gpios", 0);
  226. pdata->gpio_hp_mute = of_get_named_gpio(np,
  227. "nvidia,hp-mute-gpios", 0);
  228. pdata->gpio_hp_det = of_get_named_gpio(np,
  229. "nvidia,hp-det-gpios", 0);
  230. pdata->gpio_int_mic_en = of_get_named_gpio(np,
  231. "nvidia,int-mic-en-gpios", 0);
  232. pdata->gpio_ext_mic_en = of_get_named_gpio(np,
  233. "nvidia,ext-mic-en-gpios", 0);
  234. } else {
  235. dev_err(card->dev, "No platform data supplied\n");
  236. return -EINVAL;
  237. }
  238. if (gpio_is_valid(pdata->gpio_spkr_en)) {
  239. ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
  240. if (ret) {
  241. dev_err(card->dev, "cannot get spkr_en gpio\n");
  242. return ret;
  243. }
  244. machine->gpio_requested |= GPIO_SPKR_EN;
  245. gpio_direction_output(pdata->gpio_spkr_en, 0);
  246. }
  247. if (gpio_is_valid(pdata->gpio_hp_mute)) {
  248. ret = gpio_request(pdata->gpio_hp_mute, "hp_mute");
  249. if (ret) {
  250. dev_err(card->dev, "cannot get hp_mute gpio\n");
  251. return ret;
  252. }
  253. machine->gpio_requested |= GPIO_HP_MUTE;
  254. gpio_direction_output(pdata->gpio_hp_mute, 1);
  255. }
  256. if (gpio_is_valid(pdata->gpio_int_mic_en)) {
  257. ret = gpio_request(pdata->gpio_int_mic_en, "int_mic_en");
  258. if (ret) {
  259. dev_err(card->dev, "cannot get int_mic_en gpio\n");
  260. return ret;
  261. }
  262. machine->gpio_requested |= GPIO_INT_MIC_EN;
  263. /* Disable int mic; enable signal is active-high */
  264. gpio_direction_output(pdata->gpio_int_mic_en, 0);
  265. }
  266. if (gpio_is_valid(pdata->gpio_ext_mic_en)) {
  267. ret = gpio_request(pdata->gpio_ext_mic_en, "ext_mic_en");
  268. if (ret) {
  269. dev_err(card->dev, "cannot get ext_mic_en gpio\n");
  270. return ret;
  271. }
  272. machine->gpio_requested |= GPIO_EXT_MIC_EN;
  273. /* Enable ext mic; enable signal is active-low */
  274. gpio_direction_output(pdata->gpio_ext_mic_en, 0);
  275. }
  276. if (gpio_is_valid(pdata->gpio_hp_det)) {
  277. tegra_wm8903_hp_jack_gpio.gpio = pdata->gpio_hp_det;
  278. snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
  279. &tegra_wm8903_hp_jack);
  280. snd_soc_jack_add_pins(&tegra_wm8903_hp_jack,
  281. ARRAY_SIZE(tegra_wm8903_hp_jack_pins),
  282. tegra_wm8903_hp_jack_pins);
  283. snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
  284. 1,
  285. &tegra_wm8903_hp_jack_gpio);
  286. machine->gpio_requested |= GPIO_HP_DET;
  287. }
  288. snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
  289. &tegra_wm8903_mic_jack);
  290. snd_soc_jack_add_pins(&tegra_wm8903_mic_jack,
  291. ARRAY_SIZE(tegra_wm8903_mic_jack_pins),
  292. tegra_wm8903_mic_jack_pins);
  293. wm8903_mic_detect(codec, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
  294. 0);
  295. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
  296. return 0;
  297. }
  298. static struct snd_soc_dai_link tegra_wm8903_dai = {
  299. .name = "WM8903",
  300. .stream_name = "WM8903 PCM",
  301. .codec_name = "wm8903.0-001a",
  302. .platform_name = "tegra20-i2s.0",
  303. .cpu_dai_name = "tegra20-i2s.0",
  304. .codec_dai_name = "wm8903-hifi",
  305. .init = tegra_wm8903_init,
  306. .ops = &tegra_wm8903_ops,
  307. };
  308. static struct snd_soc_card snd_soc_tegra_wm8903 = {
  309. .name = "tegra-wm8903",
  310. .owner = THIS_MODULE,
  311. .dai_link = &tegra_wm8903_dai,
  312. .num_links = 1,
  313. .controls = tegra_wm8903_controls,
  314. .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
  315. .dapm_widgets = tegra_wm8903_dapm_widgets,
  316. .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
  317. .fully_routed = true,
  318. };
  319. static __devinit int tegra_wm8903_driver_probe(struct platform_device *pdev)
  320. {
  321. struct snd_soc_card *card = &snd_soc_tegra_wm8903;
  322. struct tegra_wm8903 *machine;
  323. int ret;
  324. if (!pdev->dev.platform_data && !pdev->dev.of_node) {
  325. dev_err(&pdev->dev, "No platform data supplied\n");
  326. return -EINVAL;
  327. }
  328. machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903),
  329. GFP_KERNEL);
  330. if (!machine) {
  331. dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n");
  332. ret = -ENOMEM;
  333. goto err;
  334. }
  335. card->dev = &pdev->dev;
  336. platform_set_drvdata(pdev, card);
  337. snd_soc_card_set_drvdata(card, machine);
  338. if (pdev->dev.of_node) {
  339. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  340. if (ret)
  341. goto err;
  342. ret = snd_soc_of_parse_audio_routing(card,
  343. "nvidia,audio-routing");
  344. if (ret)
  345. goto err;
  346. tegra_wm8903_dai.codec_name = NULL;
  347. tegra_wm8903_dai.codec_of_node = of_parse_phandle(
  348. pdev->dev.of_node, "nvidia,audio-codec", 0);
  349. if (!tegra_wm8903_dai.codec_of_node) {
  350. dev_err(&pdev->dev,
  351. "Property 'nvidia,audio-codec' missing or invalid\n");
  352. ret = -EINVAL;
  353. goto err;
  354. }
  355. tegra_wm8903_dai.cpu_dai_name = NULL;
  356. tegra_wm8903_dai.cpu_dai_of_node = of_parse_phandle(
  357. pdev->dev.of_node, "nvidia,i2s-controller", 0);
  358. if (!tegra_wm8903_dai.cpu_dai_of_node) {
  359. dev_err(&pdev->dev,
  360. "Property 'nvidia,i2s-controller' missing or invalid\n");
  361. ret = -EINVAL;
  362. goto err;
  363. }
  364. tegra_wm8903_dai.platform_name = NULL;
  365. tegra_wm8903_dai.platform_of_node =
  366. tegra_wm8903_dai.cpu_dai_of_node;
  367. } else {
  368. if (machine_is_harmony()) {
  369. card->dapm_routes = harmony_audio_map;
  370. card->num_dapm_routes = ARRAY_SIZE(harmony_audio_map);
  371. } else if (machine_is_seaboard()) {
  372. card->dapm_routes = seaboard_audio_map;
  373. card->num_dapm_routes = ARRAY_SIZE(seaboard_audio_map);
  374. } else if (machine_is_kaen()) {
  375. card->dapm_routes = kaen_audio_map;
  376. card->num_dapm_routes = ARRAY_SIZE(kaen_audio_map);
  377. } else {
  378. card->dapm_routes = aebl_audio_map;
  379. card->num_dapm_routes = ARRAY_SIZE(aebl_audio_map);
  380. }
  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);