tegra_wm8903.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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_card *card = rtd->card;
  147. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  148. if (gpio_is_valid(machine->gpio_hp_det)) {
  149. tegra_wm8903_hp_jack_gpio.gpio = machine->gpio_hp_det;
  150. snd_soc_card_jack_new(rtd->card, "Headphone Jack",
  151. SND_JACK_HEADPHONE, &tegra_wm8903_hp_jack,
  152. tegra_wm8903_hp_jack_pins,
  153. ARRAY_SIZE(tegra_wm8903_hp_jack_pins));
  154. snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
  155. 1,
  156. &tegra_wm8903_hp_jack_gpio);
  157. }
  158. snd_soc_card_jack_new(rtd->card, "Mic Jack", SND_JACK_MICROPHONE,
  159. &tegra_wm8903_mic_jack,
  160. tegra_wm8903_mic_jack_pins,
  161. ARRAY_SIZE(tegra_wm8903_mic_jack_pins));
  162. wm8903_mic_detect(codec, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
  163. 0);
  164. snd_soc_dapm_force_enable_pin(&card->dapm, "MICBIAS");
  165. return 0;
  166. }
  167. static int tegra_wm8903_remove(struct snd_soc_card *card)
  168. {
  169. struct snd_soc_pcm_runtime *rtd = &(card->rtd[0]);
  170. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  171. struct snd_soc_codec *codec = codec_dai->codec;
  172. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  173. if (gpio_is_valid(machine->gpio_hp_det)) {
  174. snd_soc_jack_free_gpios(&tegra_wm8903_hp_jack, 1,
  175. &tegra_wm8903_hp_jack_gpio);
  176. }
  177. wm8903_mic_detect(codec, NULL, 0, 0);
  178. return 0;
  179. }
  180. static struct snd_soc_dai_link tegra_wm8903_dai = {
  181. .name = "WM8903",
  182. .stream_name = "WM8903 PCM",
  183. .codec_dai_name = "wm8903-hifi",
  184. .init = tegra_wm8903_init,
  185. .ops = &tegra_wm8903_ops,
  186. .dai_fmt = SND_SOC_DAIFMT_I2S |
  187. SND_SOC_DAIFMT_NB_NF |
  188. SND_SOC_DAIFMT_CBS_CFS,
  189. };
  190. static struct snd_soc_card snd_soc_tegra_wm8903 = {
  191. .name = "tegra-wm8903",
  192. .owner = THIS_MODULE,
  193. .dai_link = &tegra_wm8903_dai,
  194. .num_links = 1,
  195. .remove = tegra_wm8903_remove,
  196. .controls = tegra_wm8903_controls,
  197. .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
  198. .dapm_widgets = tegra_wm8903_dapm_widgets,
  199. .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
  200. .fully_routed = true,
  201. };
  202. static int tegra_wm8903_driver_probe(struct platform_device *pdev)
  203. {
  204. struct device_node *np = pdev->dev.of_node;
  205. struct snd_soc_card *card = &snd_soc_tegra_wm8903;
  206. struct tegra_wm8903 *machine;
  207. int ret;
  208. machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903),
  209. GFP_KERNEL);
  210. if (!machine) {
  211. dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n");
  212. return -ENOMEM;
  213. }
  214. card->dev = &pdev->dev;
  215. platform_set_drvdata(pdev, card);
  216. snd_soc_card_set_drvdata(card, machine);
  217. machine->gpio_spkr_en = of_get_named_gpio(np, "nvidia,spkr-en-gpios",
  218. 0);
  219. if (machine->gpio_spkr_en == -EPROBE_DEFER)
  220. return -EPROBE_DEFER;
  221. if (gpio_is_valid(machine->gpio_spkr_en)) {
  222. ret = devm_gpio_request_one(&pdev->dev, machine->gpio_spkr_en,
  223. GPIOF_OUT_INIT_LOW, "spkr_en");
  224. if (ret) {
  225. dev_err(card->dev, "cannot get spkr_en gpio\n");
  226. return ret;
  227. }
  228. }
  229. machine->gpio_hp_mute = of_get_named_gpio(np, "nvidia,hp-mute-gpios",
  230. 0);
  231. if (machine->gpio_hp_mute == -EPROBE_DEFER)
  232. return -EPROBE_DEFER;
  233. if (gpio_is_valid(machine->gpio_hp_mute)) {
  234. ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_mute,
  235. GPIOF_OUT_INIT_HIGH, "hp_mute");
  236. if (ret) {
  237. dev_err(card->dev, "cannot get hp_mute gpio\n");
  238. return ret;
  239. }
  240. }
  241. machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
  242. if (machine->gpio_hp_det == -EPROBE_DEFER)
  243. return -EPROBE_DEFER;
  244. machine->gpio_int_mic_en = of_get_named_gpio(np,
  245. "nvidia,int-mic-en-gpios", 0);
  246. if (machine->gpio_int_mic_en == -EPROBE_DEFER)
  247. return -EPROBE_DEFER;
  248. if (gpio_is_valid(machine->gpio_int_mic_en)) {
  249. /* Disable int mic; enable signal is active-high */
  250. ret = devm_gpio_request_one(&pdev->dev,
  251. machine->gpio_int_mic_en,
  252. GPIOF_OUT_INIT_LOW, "int_mic_en");
  253. if (ret) {
  254. dev_err(card->dev, "cannot get int_mic_en gpio\n");
  255. return ret;
  256. }
  257. }
  258. machine->gpio_ext_mic_en = of_get_named_gpio(np,
  259. "nvidia,ext-mic-en-gpios", 0);
  260. if (machine->gpio_ext_mic_en == -EPROBE_DEFER)
  261. return -EPROBE_DEFER;
  262. if (gpio_is_valid(machine->gpio_ext_mic_en)) {
  263. /* Enable ext mic; enable signal is active-low */
  264. ret = devm_gpio_request_one(&pdev->dev,
  265. machine->gpio_ext_mic_en,
  266. GPIOF_OUT_INIT_LOW, "ext_mic_en");
  267. if (ret) {
  268. dev_err(card->dev, "cannot get ext_mic_en gpio\n");
  269. return ret;
  270. }
  271. }
  272. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  273. if (ret)
  274. goto err;
  275. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  276. if (ret)
  277. goto err;
  278. tegra_wm8903_dai.codec_of_node = of_parse_phandle(np,
  279. "nvidia,audio-codec", 0);
  280. if (!tegra_wm8903_dai.codec_of_node) {
  281. dev_err(&pdev->dev,
  282. "Property 'nvidia,audio-codec' missing or invalid\n");
  283. ret = -EINVAL;
  284. goto err;
  285. }
  286. tegra_wm8903_dai.cpu_of_node = of_parse_phandle(np,
  287. "nvidia,i2s-controller", 0);
  288. if (!tegra_wm8903_dai.cpu_of_node) {
  289. dev_err(&pdev->dev,
  290. "Property 'nvidia,i2s-controller' missing or invalid\n");
  291. ret = -EINVAL;
  292. goto err;
  293. }
  294. tegra_wm8903_dai.platform_of_node = tegra_wm8903_dai.cpu_of_node;
  295. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  296. if (ret)
  297. goto err;
  298. ret = snd_soc_register_card(card);
  299. if (ret) {
  300. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  301. ret);
  302. goto err_fini_utils;
  303. }
  304. return 0;
  305. err_fini_utils:
  306. tegra_asoc_utils_fini(&machine->util_data);
  307. err:
  308. return ret;
  309. }
  310. static int tegra_wm8903_driver_remove(struct platform_device *pdev)
  311. {
  312. struct snd_soc_card *card = platform_get_drvdata(pdev);
  313. struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  314. snd_soc_unregister_card(card);
  315. tegra_asoc_utils_fini(&machine->util_data);
  316. return 0;
  317. }
  318. static const struct of_device_id tegra_wm8903_of_match[] = {
  319. { .compatible = "nvidia,tegra-audio-wm8903", },
  320. {},
  321. };
  322. static struct platform_driver tegra_wm8903_driver = {
  323. .driver = {
  324. .name = DRV_NAME,
  325. .pm = &snd_soc_pm_ops,
  326. .of_match_table = tegra_wm8903_of_match,
  327. },
  328. .probe = tegra_wm8903_driver_probe,
  329. .remove = tegra_wm8903_driver_remove,
  330. };
  331. module_platform_driver(tegra_wm8903_driver);
  332. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  333. MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
  334. MODULE_LICENSE("GPL");
  335. MODULE_ALIAS("platform:" DRV_NAME);
  336. MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);