tegra_wm8903.c 12 KB

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