tegra_wm8903.c 11 KB

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