tegra_max98090.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. };
  119. static const struct snd_kcontrol_new tegra_max98090_controls[] = {
  120. SOC_DAPM_PIN_SWITCH("Speakers"),
  121. };
  122. static int tegra_max98090_asoc_init(struct snd_soc_pcm_runtime *rtd)
  123. {
  124. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  125. struct snd_soc_codec *codec = codec_dai->codec;
  126. struct tegra_max98090 *machine = snd_soc_card_get_drvdata(rtd->card);
  127. if (gpio_is_valid(machine->gpio_hp_det)) {
  128. snd_soc_jack_new(codec, "Headphones", SND_JACK_HEADPHONE,
  129. &tegra_max98090_hp_jack);
  130. snd_soc_jack_add_pins(&tegra_max98090_hp_jack,
  131. ARRAY_SIZE(tegra_max98090_hp_jack_pins),
  132. tegra_max98090_hp_jack_pins);
  133. tegra_max98090_hp_jack_gpio.gpio = machine->gpio_hp_det;
  134. snd_soc_jack_add_gpios(&tegra_max98090_hp_jack,
  135. 1,
  136. &tegra_max98090_hp_jack_gpio);
  137. }
  138. if (gpio_is_valid(machine->gpio_mic_det)) {
  139. snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
  140. &tegra_max98090_mic_jack);
  141. snd_soc_jack_add_pins(&tegra_max98090_mic_jack,
  142. ARRAY_SIZE(tegra_max98090_mic_jack_pins),
  143. tegra_max98090_mic_jack_pins);
  144. tegra_max98090_mic_jack_gpio.gpio = machine->gpio_mic_det;
  145. snd_soc_jack_add_gpios(&tegra_max98090_mic_jack,
  146. 1,
  147. &tegra_max98090_mic_jack_gpio);
  148. }
  149. return 0;
  150. }
  151. static int tegra_max98090_card_remove(struct snd_soc_card *card)
  152. {
  153. struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
  154. if (gpio_is_valid(machine->gpio_hp_det)) {
  155. snd_soc_jack_free_gpios(&tegra_max98090_hp_jack, 1,
  156. &tegra_max98090_hp_jack_gpio);
  157. }
  158. if (gpio_is_valid(machine->gpio_mic_det)) {
  159. snd_soc_jack_free_gpios(&tegra_max98090_mic_jack, 1,
  160. &tegra_max98090_mic_jack_gpio);
  161. }
  162. return 0;
  163. }
  164. static struct snd_soc_dai_link tegra_max98090_dai = {
  165. .name = "max98090",
  166. .stream_name = "max98090 PCM",
  167. .codec_dai_name = "HiFi",
  168. .init = tegra_max98090_asoc_init,
  169. .ops = &tegra_max98090_ops,
  170. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  171. SND_SOC_DAIFMT_CBS_CFS,
  172. };
  173. static struct snd_soc_card snd_soc_tegra_max98090 = {
  174. .name = "tegra-max98090",
  175. .owner = THIS_MODULE,
  176. .remove = tegra_max98090_card_remove,
  177. .dai_link = &tegra_max98090_dai,
  178. .num_links = 1,
  179. .controls = tegra_max98090_controls,
  180. .num_controls = ARRAY_SIZE(tegra_max98090_controls),
  181. .dapm_widgets = tegra_max98090_dapm_widgets,
  182. .num_dapm_widgets = ARRAY_SIZE(tegra_max98090_dapm_widgets),
  183. .fully_routed = true,
  184. };
  185. static int tegra_max98090_probe(struct platform_device *pdev)
  186. {
  187. struct device_node *np = pdev->dev.of_node;
  188. struct snd_soc_card *card = &snd_soc_tegra_max98090;
  189. struct tegra_max98090 *machine;
  190. int ret;
  191. machine = devm_kzalloc(&pdev->dev,
  192. sizeof(struct tegra_max98090), GFP_KERNEL);
  193. if (!machine) {
  194. dev_err(&pdev->dev, "Can't allocate tegra_max98090\n");
  195. return -ENOMEM;
  196. }
  197. card->dev = &pdev->dev;
  198. platform_set_drvdata(pdev, card);
  199. snd_soc_card_set_drvdata(card, machine);
  200. machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
  201. if (machine->gpio_hp_det == -EPROBE_DEFER)
  202. return -EPROBE_DEFER;
  203. machine->gpio_mic_det =
  204. of_get_named_gpio(np, "nvidia,mic-det-gpios", 0);
  205. if (machine->gpio_mic_det == -EPROBE_DEFER)
  206. return -EPROBE_DEFER;
  207. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  208. if (ret)
  209. goto err;
  210. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  211. if (ret)
  212. goto err;
  213. tegra_max98090_dai.codec_of_node = of_parse_phandle(np,
  214. "nvidia,audio-codec", 0);
  215. if (!tegra_max98090_dai.codec_of_node) {
  216. dev_err(&pdev->dev,
  217. "Property 'nvidia,audio-codec' missing or invalid\n");
  218. ret = -EINVAL;
  219. goto err;
  220. }
  221. tegra_max98090_dai.cpu_of_node = of_parse_phandle(np,
  222. "nvidia,i2s-controller", 0);
  223. if (!tegra_max98090_dai.cpu_of_node) {
  224. dev_err(&pdev->dev,
  225. "Property 'nvidia,i2s-controller' missing or invalid\n");
  226. ret = -EINVAL;
  227. goto err;
  228. }
  229. tegra_max98090_dai.platform_of_node = tegra_max98090_dai.cpu_of_node;
  230. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  231. if (ret)
  232. goto err;
  233. ret = snd_soc_register_card(card);
  234. if (ret) {
  235. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  236. ret);
  237. goto err_fini_utils;
  238. }
  239. return 0;
  240. err_fini_utils:
  241. tegra_asoc_utils_fini(&machine->util_data);
  242. err:
  243. return ret;
  244. }
  245. static int tegra_max98090_remove(struct platform_device *pdev)
  246. {
  247. struct snd_soc_card *card = platform_get_drvdata(pdev);
  248. struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
  249. snd_soc_unregister_card(card);
  250. tegra_asoc_utils_fini(&machine->util_data);
  251. return 0;
  252. }
  253. static const struct of_device_id tegra_max98090_of_match[] = {
  254. { .compatible = "nvidia,tegra-audio-max98090", },
  255. {},
  256. };
  257. static struct platform_driver tegra_max98090_driver = {
  258. .driver = {
  259. .name = DRV_NAME,
  260. .owner = THIS_MODULE,
  261. .pm = &snd_soc_pm_ops,
  262. .of_match_table = tegra_max98090_of_match,
  263. },
  264. .probe = tegra_max98090_probe,
  265. .remove = tegra_max98090_remove,
  266. };
  267. module_platform_driver(tegra_max98090_driver);
  268. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  269. MODULE_DESCRIPTION("Tegra max98090 machine ASoC driver");
  270. MODULE_LICENSE("GPL v2");
  271. MODULE_ALIAS("platform:" DRV_NAME);
  272. MODULE_DEVICE_TABLE(of, tegra_max98090_of_match);