harmony.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * harmony.c - Harmony machine ASoC driver
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010-2011 - 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 <asm/mach-types.h>
  31. #include <linux/module.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/slab.h>
  34. #include <linux/gpio.h>
  35. #include <mach/harmony_audio.h>
  36. #include <sound/core.h>
  37. #include <sound/pcm.h>
  38. #include <sound/pcm_params.h>
  39. #include <sound/soc.h>
  40. #include "tegra_das.h"
  41. #include "tegra_i2s.h"
  42. #include "tegra_pcm.h"
  43. #include "tegra_asoc_utils.h"
  44. #define DRV_NAME "tegra-snd-harmony"
  45. struct tegra_harmony {
  46. struct tegra_asoc_utils_data util_data;
  47. struct harmony_audio_platform_data *pdata;
  48. int gpio_spkr_en_requested;
  49. };
  50. static int harmony_asoc_hw_params(struct snd_pcm_substream *substream,
  51. struct snd_pcm_hw_params *params)
  52. {
  53. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  54. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  55. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  56. struct snd_soc_codec *codec = rtd->codec;
  57. struct snd_soc_card *card = codec->card;
  58. struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
  59. int srate, mclk, mclk_change;
  60. int err;
  61. srate = params_rate(params);
  62. switch (srate) {
  63. case 64000:
  64. case 88200:
  65. case 96000:
  66. mclk = 128 * srate;
  67. break;
  68. default:
  69. mclk = 256 * srate;
  70. break;
  71. }
  72. /* FIXME: Codec only requires >= 3MHz if OSR==0 */
  73. while (mclk < 6000000)
  74. mclk *= 2;
  75. err = tegra_asoc_utils_set_rate(&harmony->util_data, srate, mclk,
  76. &mclk_change);
  77. if (err < 0) {
  78. dev_err(card->dev, "Can't configure clocks\n");
  79. return err;
  80. }
  81. err = snd_soc_dai_set_fmt(codec_dai,
  82. SND_SOC_DAIFMT_I2S |
  83. SND_SOC_DAIFMT_NB_NF |
  84. SND_SOC_DAIFMT_CBS_CFS);
  85. if (err < 0) {
  86. dev_err(card->dev, "codec_dai fmt not set\n");
  87. return err;
  88. }
  89. err = snd_soc_dai_set_fmt(cpu_dai,
  90. SND_SOC_DAIFMT_I2S |
  91. SND_SOC_DAIFMT_NB_NF |
  92. SND_SOC_DAIFMT_CBS_CFS);
  93. if (err < 0) {
  94. dev_err(card->dev, "cpu_dai fmt not set\n");
  95. return err;
  96. }
  97. if (mclk_change) {
  98. err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  99. SND_SOC_CLOCK_IN);
  100. if (err < 0) {
  101. dev_err(card->dev, "codec_dai clock not set\n");
  102. return err;
  103. }
  104. }
  105. return 0;
  106. }
  107. static struct snd_soc_ops harmony_asoc_ops = {
  108. .hw_params = harmony_asoc_hw_params,
  109. };
  110. static int harmony_event_int_spk(struct snd_soc_dapm_widget *w,
  111. struct snd_kcontrol *k, int event)
  112. {
  113. struct snd_soc_codec *codec = w->codec;
  114. struct snd_soc_card *card = codec->card;
  115. struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
  116. struct harmony_audio_platform_data *pdata = harmony->pdata;
  117. gpio_set_value_cansleep(pdata->gpio_spkr_en,
  118. SND_SOC_DAPM_EVENT_ON(event));
  119. return 0;
  120. }
  121. static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
  122. SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
  123. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  124. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  125. };
  126. static const struct snd_soc_dapm_route harmony_audio_map[] = {
  127. {"Headphone Jack", NULL, "HPOUTR"},
  128. {"Headphone Jack", NULL, "HPOUTL"},
  129. {"Int Spk", NULL, "ROP"},
  130. {"Int Spk", NULL, "RON"},
  131. {"Int Spk", NULL, "LOP"},
  132. {"Int Spk", NULL, "LON"},
  133. {"Mic Bias", NULL, "Mic Jack"},
  134. {"IN1L", NULL, "Mic Bias"},
  135. };
  136. static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
  137. {
  138. struct snd_soc_codec *codec = rtd->codec;
  139. struct snd_soc_dapm_context *dapm = &codec->dapm;
  140. struct snd_soc_card *card = codec->card;
  141. struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
  142. struct harmony_audio_platform_data *pdata = harmony->pdata;
  143. int ret;
  144. ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
  145. if (ret) {
  146. dev_err(card->dev, "cannot get spkr_en gpio\n");
  147. return ret;
  148. }
  149. harmony->gpio_spkr_en_requested = 1;
  150. gpio_direction_output(pdata->gpio_spkr_en, 0);
  151. snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
  152. ARRAY_SIZE(harmony_dapm_widgets));
  153. snd_soc_dapm_add_routes(dapm, harmony_audio_map,
  154. ARRAY_SIZE(harmony_audio_map));
  155. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  156. snd_soc_dapm_enable_pin(dapm, "Int Spk");
  157. snd_soc_dapm_enable_pin(dapm, "Mic Jack");
  158. snd_soc_dapm_sync(dapm);
  159. return 0;
  160. }
  161. static struct snd_soc_dai_link harmony_wm8903_dai = {
  162. .name = "WM8903",
  163. .stream_name = "WM8903 PCM",
  164. .codec_name = "wm8903-codec.0-001a",
  165. .platform_name = "tegra-pcm-audio",
  166. .cpu_dai_name = "tegra-i2s.0",
  167. .codec_dai_name = "wm8903-hifi",
  168. .init = harmony_asoc_init,
  169. .ops = &harmony_asoc_ops,
  170. };
  171. static struct snd_soc_card snd_soc_harmony = {
  172. .name = "tegra-harmony",
  173. .dai_link = &harmony_wm8903_dai,
  174. .num_links = 1,
  175. };
  176. static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
  177. {
  178. struct snd_soc_card *card = &snd_soc_harmony;
  179. struct tegra_harmony *harmony;
  180. struct harmony_audio_platform_data *pdata;
  181. int ret;
  182. if (!machine_is_harmony()) {
  183. dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
  184. return -ENODEV;
  185. }
  186. pdata = pdev->dev.platform_data;
  187. if (!pdata) {
  188. dev_err(&pdev->dev, "no platform data supplied\n");
  189. return -EINVAL;
  190. }
  191. harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
  192. if (!harmony) {
  193. dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
  194. return -ENOMEM;
  195. }
  196. harmony->pdata = pdata;
  197. ret = tegra_asoc_utils_init(&harmony->util_data, &pdev->dev);
  198. if (ret)
  199. goto err_free_harmony;
  200. card->dev = &pdev->dev;
  201. platform_set_drvdata(pdev, card);
  202. snd_soc_card_set_drvdata(card, harmony);
  203. ret = snd_soc_register_card(card);
  204. if (ret) {
  205. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  206. ret);
  207. goto err_clear_drvdata;
  208. }
  209. return 0;
  210. err_clear_drvdata:
  211. snd_soc_card_set_drvdata(card, NULL);
  212. platform_set_drvdata(pdev, NULL);
  213. card->dev = NULL;
  214. tegra_asoc_utils_fini(&harmony->util_data);
  215. err_free_harmony:
  216. kfree(harmony);
  217. return ret;
  218. }
  219. static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
  220. {
  221. struct snd_soc_card *card = platform_get_drvdata(pdev);
  222. struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
  223. struct harmony_audio_platform_data *pdata = harmony->pdata;
  224. snd_soc_unregister_card(card);
  225. snd_soc_card_set_drvdata(card, NULL);
  226. platform_set_drvdata(pdev, NULL);
  227. card->dev = NULL;
  228. tegra_asoc_utils_fini(&harmony->util_data);
  229. if (harmony->gpio_spkr_en_requested)
  230. gpio_free(pdata->gpio_spkr_en);
  231. kfree(harmony);
  232. return 0;
  233. }
  234. static struct platform_driver tegra_snd_harmony_driver = {
  235. .driver = {
  236. .name = DRV_NAME,
  237. .owner = THIS_MODULE,
  238. },
  239. .probe = tegra_snd_harmony_probe,
  240. .remove = __devexit_p(tegra_snd_harmony_remove),
  241. };
  242. static int __init snd_tegra_harmony_init(void)
  243. {
  244. return platform_driver_register(&tegra_snd_harmony_driver);
  245. }
  246. module_init(snd_tegra_harmony_init);
  247. static void __exit snd_tegra_harmony_exit(void)
  248. {
  249. platform_driver_unregister(&tegra_snd_harmony_driver);
  250. }
  251. module_exit(snd_tegra_harmony_exit);
  252. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  253. MODULE_DESCRIPTION("Harmony machine ASoC driver");
  254. MODULE_LICENSE("GPL");