tegra_wm8903.c 10 KB

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