harmony.c 9.9 KB

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