harmony.c 8.9 KB

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