tegra_wm8903.c 12 KB

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