tegra_max98090.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Tegra machine ASoC driver for boards using a MAX90809 CODEC.
  3. *
  4. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Based on code copyright/by:
  19. *
  20. * Copyright (C) 2010-2012 - NVIDIA, Inc.
  21. * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
  22. * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
  23. * Copyright 2007 Wolfson Microelectronics PLC.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/gpio.h>
  29. #include <linux/of_gpio.h>
  30. #include <sound/core.h>
  31. #include <sound/jack.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include "tegra_asoc_utils.h"
  36. #define DRV_NAME "tegra-snd-max98090"
  37. struct tegra_max98090 {
  38. struct tegra_asoc_utils_data util_data;
  39. int gpio_hp_det;
  40. int gpio_mic_det;
  41. };
  42. static int tegra_max98090_asoc_hw_params(struct snd_pcm_substream *substream,
  43. struct snd_pcm_hw_params *params)
  44. {
  45. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  46. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  47. struct snd_soc_card *card = rtd->card;
  48. struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
  49. int srate, mclk;
  50. int err;
  51. srate = params_rate(params);
  52. switch (srate) {
  53. case 8000:
  54. case 16000:
  55. case 24000:
  56. case 32000:
  57. case 48000:
  58. case 64000:
  59. case 96000:
  60. mclk = 12288000;
  61. break;
  62. case 11025:
  63. case 22050:
  64. case 44100:
  65. case 88200:
  66. mclk = 11289600;
  67. break;
  68. default:
  69. mclk = 12000000;
  70. break;
  71. }
  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_max98090_ops = {
  86. .hw_params = tegra_max98090_asoc_hw_params,
  87. };
  88. static struct snd_soc_jack tegra_max98090_hp_jack;
  89. static struct snd_soc_jack_pin tegra_max98090_hp_jack_pins[] = {
  90. {
  91. .pin = "Headphones",
  92. .mask = SND_JACK_HEADPHONE,
  93. },
  94. };
  95. static struct snd_soc_jack_gpio tegra_max98090_hp_jack_gpio = {
  96. .name = "Headphone detection",
  97. .report = SND_JACK_HEADPHONE,
  98. .debounce_time = 150,
  99. .invert = 1,
  100. };
  101. static struct snd_soc_jack tegra_max98090_mic_jack;
  102. static struct snd_soc_jack_pin tegra_max98090_mic_jack_pins[] = {
  103. {
  104. .pin = "Mic Jack",
  105. .mask = SND_JACK_MICROPHONE,
  106. },
  107. };
  108. static struct snd_soc_jack_gpio tegra_max98090_mic_jack_gpio = {
  109. .name = "Mic detection",
  110. .report = SND_JACK_MICROPHONE,
  111. .debounce_time = 150,
  112. .invert = 1,
  113. };
  114. static const struct snd_soc_dapm_widget tegra_max98090_dapm_widgets[] = {
  115. SND_SOC_DAPM_HP("Headphones", NULL),
  116. SND_SOC_DAPM_SPK("Speakers", NULL),
  117. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  118. SND_SOC_DAPM_MIC("Int Mic", NULL),
  119. };
  120. static const struct snd_kcontrol_new tegra_max98090_controls[] = {
  121. SOC_DAPM_PIN_SWITCH("Headphones"),
  122. SOC_DAPM_PIN_SWITCH("Speakers"),
  123. SOC_DAPM_PIN_SWITCH("Mic Jack"),
  124. SOC_DAPM_PIN_SWITCH("Int Mic"),
  125. };
  126. static int tegra_max98090_asoc_init(struct snd_soc_pcm_runtime *rtd)
  127. {
  128. struct tegra_max98090 *machine = snd_soc_card_get_drvdata(rtd->card);
  129. if (gpio_is_valid(machine->gpio_hp_det)) {
  130. snd_soc_card_jack_new(rtd->card, "Headphones",
  131. SND_JACK_HEADPHONE,
  132. &tegra_max98090_hp_jack,
  133. tegra_max98090_hp_jack_pins,
  134. ARRAY_SIZE(tegra_max98090_hp_jack_pins));
  135. tegra_max98090_hp_jack_gpio.gpio = machine->gpio_hp_det;
  136. snd_soc_jack_add_gpios(&tegra_max98090_hp_jack,
  137. 1,
  138. &tegra_max98090_hp_jack_gpio);
  139. }
  140. if (gpio_is_valid(machine->gpio_mic_det)) {
  141. snd_soc_card_jack_new(rtd->card, "Mic Jack",
  142. SND_JACK_MICROPHONE,
  143. &tegra_max98090_mic_jack,
  144. tegra_max98090_mic_jack_pins,
  145. ARRAY_SIZE(tegra_max98090_mic_jack_pins));
  146. tegra_max98090_mic_jack_gpio.gpio = machine->gpio_mic_det;
  147. snd_soc_jack_add_gpios(&tegra_max98090_mic_jack,
  148. 1,
  149. &tegra_max98090_mic_jack_gpio);
  150. }
  151. return 0;
  152. }
  153. static int tegra_max98090_card_remove(struct snd_soc_card *card)
  154. {
  155. struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
  156. if (gpio_is_valid(machine->gpio_hp_det)) {
  157. snd_soc_jack_free_gpios(&tegra_max98090_hp_jack, 1,
  158. &tegra_max98090_hp_jack_gpio);
  159. }
  160. if (gpio_is_valid(machine->gpio_mic_det)) {
  161. snd_soc_jack_free_gpios(&tegra_max98090_mic_jack, 1,
  162. &tegra_max98090_mic_jack_gpio);
  163. }
  164. return 0;
  165. }
  166. static struct snd_soc_dai_link tegra_max98090_dai = {
  167. .name = "max98090",
  168. .stream_name = "max98090 PCM",
  169. .codec_dai_name = "HiFi",
  170. .init = tegra_max98090_asoc_init,
  171. .ops = &tegra_max98090_ops,
  172. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  173. SND_SOC_DAIFMT_CBS_CFS,
  174. };
  175. static struct snd_soc_card snd_soc_tegra_max98090 = {
  176. .name = "tegra-max98090",
  177. .owner = THIS_MODULE,
  178. .remove = tegra_max98090_card_remove,
  179. .dai_link = &tegra_max98090_dai,
  180. .num_links = 1,
  181. .controls = tegra_max98090_controls,
  182. .num_controls = ARRAY_SIZE(tegra_max98090_controls),
  183. .dapm_widgets = tegra_max98090_dapm_widgets,
  184. .num_dapm_widgets = ARRAY_SIZE(tegra_max98090_dapm_widgets),
  185. .fully_routed = true,
  186. };
  187. static int tegra_max98090_probe(struct platform_device *pdev)
  188. {
  189. struct device_node *np = pdev->dev.of_node;
  190. struct snd_soc_card *card = &snd_soc_tegra_max98090;
  191. struct tegra_max98090 *machine;
  192. int ret;
  193. machine = devm_kzalloc(&pdev->dev,
  194. sizeof(struct tegra_max98090), GFP_KERNEL);
  195. if (!machine) {
  196. dev_err(&pdev->dev, "Can't allocate tegra_max98090\n");
  197. return -ENOMEM;
  198. }
  199. card->dev = &pdev->dev;
  200. platform_set_drvdata(pdev, card);
  201. snd_soc_card_set_drvdata(card, machine);
  202. machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
  203. if (machine->gpio_hp_det == -EPROBE_DEFER)
  204. return -EPROBE_DEFER;
  205. machine->gpio_mic_det =
  206. of_get_named_gpio(np, "nvidia,mic-det-gpios", 0);
  207. if (machine->gpio_mic_det == -EPROBE_DEFER)
  208. return -EPROBE_DEFER;
  209. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  210. if (ret)
  211. goto err;
  212. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  213. if (ret)
  214. goto err;
  215. tegra_max98090_dai.codec_of_node = of_parse_phandle(np,
  216. "nvidia,audio-codec", 0);
  217. if (!tegra_max98090_dai.codec_of_node) {
  218. dev_err(&pdev->dev,
  219. "Property 'nvidia,audio-codec' missing or invalid\n");
  220. ret = -EINVAL;
  221. goto err;
  222. }
  223. tegra_max98090_dai.cpu_of_node = of_parse_phandle(np,
  224. "nvidia,i2s-controller", 0);
  225. if (!tegra_max98090_dai.cpu_of_node) {
  226. dev_err(&pdev->dev,
  227. "Property 'nvidia,i2s-controller' missing or invalid\n");
  228. ret = -EINVAL;
  229. goto err;
  230. }
  231. tegra_max98090_dai.platform_of_node = tegra_max98090_dai.cpu_of_node;
  232. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  233. if (ret)
  234. goto err;
  235. ret = snd_soc_register_card(card);
  236. if (ret) {
  237. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  238. ret);
  239. goto err_fini_utils;
  240. }
  241. return 0;
  242. err_fini_utils:
  243. tegra_asoc_utils_fini(&machine->util_data);
  244. err:
  245. return ret;
  246. }
  247. static int tegra_max98090_remove(struct platform_device *pdev)
  248. {
  249. struct snd_soc_card *card = platform_get_drvdata(pdev);
  250. struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
  251. snd_soc_unregister_card(card);
  252. tegra_asoc_utils_fini(&machine->util_data);
  253. return 0;
  254. }
  255. static const struct of_device_id tegra_max98090_of_match[] = {
  256. { .compatible = "nvidia,tegra-audio-max98090", },
  257. {},
  258. };
  259. static struct platform_driver tegra_max98090_driver = {
  260. .driver = {
  261. .name = DRV_NAME,
  262. .pm = &snd_soc_pm_ops,
  263. .of_match_table = tegra_max98090_of_match,
  264. },
  265. .probe = tegra_max98090_probe,
  266. .remove = tegra_max98090_remove,
  267. };
  268. module_platform_driver(tegra_max98090_driver);
  269. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  270. MODULE_DESCRIPTION("Tegra max98090 machine ASoC driver");
  271. MODULE_LICENSE("GPL v2");
  272. MODULE_ALIAS("platform:" DRV_NAME);
  273. MODULE_DEVICE_TABLE(of, tegra_max98090_of_match);