tegra_wm8903.c 11 KB

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