tegra_wm8903.c 14 KB

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