omap3pandora.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * omap3pandora.c -- SoC audio for Pandora Handheld Console
  3. *
  4. * Author: Gražvydas Ignotas <notasas@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms 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 that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/gpio.h>
  24. #include <linux/delay.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/module.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/soc.h>
  30. #include <asm/mach-types.h>
  31. #include <plat/mcbsp.h>
  32. #include "omap-mcbsp.h"
  33. #include "omap-pcm.h"
  34. #define OMAP3_PANDORA_DAC_POWER_GPIO 118
  35. #define OMAP3_PANDORA_AMP_POWER_GPIO 14
  36. #define PREFIX "ASoC omap3pandora: "
  37. static struct regulator *omap3pandora_dac_reg;
  38. static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
  39. struct snd_pcm_hw_params *params)
  40. {
  41. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  42. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  43. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  44. int ret;
  45. /* Set the codec system clock for DAC and ADC */
  46. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
  47. SND_SOC_CLOCK_IN);
  48. if (ret < 0) {
  49. pr_err(PREFIX "can't set codec system clock\n");
  50. return ret;
  51. }
  52. /* Set McBSP clock to external */
  53. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
  54. 256 * params_rate(params),
  55. SND_SOC_CLOCK_IN);
  56. if (ret < 0) {
  57. pr_err(PREFIX "can't set cpu system clock\n");
  58. return ret;
  59. }
  60. ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
  61. if (ret < 0) {
  62. pr_err(PREFIX "can't set SRG clock divider\n");
  63. return ret;
  64. }
  65. return 0;
  66. }
  67. static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
  68. struct snd_kcontrol *k, int event)
  69. {
  70. /*
  71. * The PCM1773 DAC datasheet requires 1ms delay between switching
  72. * VCC power on/off and /PD pin high/low
  73. */
  74. if (SND_SOC_DAPM_EVENT_ON(event)) {
  75. regulator_enable(omap3pandora_dac_reg);
  76. mdelay(1);
  77. gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
  78. } else {
  79. gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
  80. mdelay(1);
  81. regulator_disable(omap3pandora_dac_reg);
  82. }
  83. return 0;
  84. }
  85. static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
  86. struct snd_kcontrol *k, int event)
  87. {
  88. if (SND_SOC_DAPM_EVENT_ON(event))
  89. gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
  90. else
  91. gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
  92. return 0;
  93. }
  94. /*
  95. * Audio paths on Pandora board:
  96. *
  97. * |O| ---> PCM DAC +-> AMP -> Headphone Jack
  98. * |M| A +--------> Line Out
  99. * |A| <~~clk~~+
  100. * |P| <--- TWL4030 <--------- Line In and MICs
  101. */
  102. static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
  103. SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
  104. 0, 0, omap3pandora_dac_event,
  105. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  106. SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
  107. 0, 0, NULL, 0, omap3pandora_hp_event,
  108. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  109. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  110. SND_SOC_DAPM_LINE("Line Out", NULL),
  111. };
  112. static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
  113. SND_SOC_DAPM_MIC("Mic (internal)", NULL),
  114. SND_SOC_DAPM_MIC("Mic (external)", NULL),
  115. SND_SOC_DAPM_LINE("Line In", NULL),
  116. };
  117. static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
  118. {"PCM DAC", NULL, "APLL Enable"},
  119. {"Headphone Amplifier", NULL, "PCM DAC"},
  120. {"Line Out", NULL, "PCM DAC"},
  121. {"Headphone Jack", NULL, "Headphone Amplifier"},
  122. };
  123. static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
  124. {"AUXL", NULL, "Line In"},
  125. {"AUXR", NULL, "Line In"},
  126. {"MAINMIC", NULL, "Mic Bias 1"},
  127. {"Mic Bias 1", NULL, "Mic (internal)"},
  128. {"SUBMIC", NULL, "Mic Bias 2"},
  129. {"Mic Bias 2", NULL, "Mic (external)"},
  130. };
  131. static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
  132. {
  133. struct snd_soc_codec *codec = rtd->codec;
  134. struct snd_soc_dapm_context *dapm = &codec->dapm;
  135. int ret;
  136. /* All TWL4030 output pins are floating */
  137. snd_soc_dapm_nc_pin(dapm, "EARPIECE");
  138. snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
  139. snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
  140. snd_soc_dapm_nc_pin(dapm, "HSOL");
  141. snd_soc_dapm_nc_pin(dapm, "HSOR");
  142. snd_soc_dapm_nc_pin(dapm, "CARKITL");
  143. snd_soc_dapm_nc_pin(dapm, "CARKITR");
  144. snd_soc_dapm_nc_pin(dapm, "HFL");
  145. snd_soc_dapm_nc_pin(dapm, "HFR");
  146. snd_soc_dapm_nc_pin(dapm, "VIBRA");
  147. ret = snd_soc_dapm_new_controls(dapm, omap3pandora_out_dapm_widgets,
  148. ARRAY_SIZE(omap3pandora_out_dapm_widgets));
  149. if (ret < 0)
  150. return ret;
  151. return snd_soc_dapm_add_routes(dapm, omap3pandora_out_map,
  152. ARRAY_SIZE(omap3pandora_out_map));
  153. }
  154. static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
  155. {
  156. struct snd_soc_codec *codec = rtd->codec;
  157. struct snd_soc_dapm_context *dapm = &codec->dapm;
  158. int ret;
  159. /* Not comnnected */
  160. snd_soc_dapm_nc_pin(dapm, "HSMIC");
  161. snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
  162. snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
  163. snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
  164. ret = snd_soc_dapm_new_controls(dapm, omap3pandora_in_dapm_widgets,
  165. ARRAY_SIZE(omap3pandora_in_dapm_widgets));
  166. if (ret < 0)
  167. return ret;
  168. return snd_soc_dapm_add_routes(dapm, omap3pandora_in_map,
  169. ARRAY_SIZE(omap3pandora_in_map));
  170. }
  171. static struct snd_soc_ops omap3pandora_ops = {
  172. .hw_params = omap3pandora_hw_params,
  173. };
  174. /* Digital audio interface glue - connects codec <--> CPU */
  175. static struct snd_soc_dai_link omap3pandora_dai[] = {
  176. {
  177. .name = "PCM1773",
  178. .stream_name = "HiFi Out",
  179. .cpu_dai_name = "omap-mcbsp.2",
  180. .codec_dai_name = "twl4030-hifi",
  181. .platform_name = "omap-pcm-audio",
  182. .codec_name = "twl4030-codec",
  183. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  184. SND_SOC_DAIFMT_CBS_CFS,
  185. .ops = &omap3pandora_ops,
  186. .init = omap3pandora_out_init,
  187. }, {
  188. .name = "TWL4030",
  189. .stream_name = "Line/Mic In",
  190. .cpu_dai_name = "omap-mcbsp.4",
  191. .codec_dai_name = "twl4030-hifi",
  192. .platform_name = "omap-pcm-audio",
  193. .codec_name = "twl4030-codec",
  194. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  195. SND_SOC_DAIFMT_CBS_CFS,
  196. .ops = &omap3pandora_ops,
  197. .init = omap3pandora_in_init,
  198. }
  199. };
  200. /* SoC card */
  201. static struct snd_soc_card snd_soc_card_omap3pandora = {
  202. .name = "omap3pandora",
  203. .owner = THIS_MODULE,
  204. .dai_link = omap3pandora_dai,
  205. .num_links = ARRAY_SIZE(omap3pandora_dai),
  206. };
  207. static struct platform_device *omap3pandora_snd_device;
  208. static int __init omap3pandora_soc_init(void)
  209. {
  210. int ret;
  211. if (!machine_is_omap3_pandora())
  212. return -ENODEV;
  213. pr_info("OMAP3 Pandora SoC init\n");
  214. ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
  215. if (ret) {
  216. pr_err(PREFIX "Failed to get DAC power GPIO\n");
  217. return ret;
  218. }
  219. ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
  220. if (ret) {
  221. pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
  222. goto fail0;
  223. }
  224. ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
  225. if (ret) {
  226. pr_err(PREFIX "Failed to get amp power GPIO\n");
  227. goto fail0;
  228. }
  229. ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
  230. if (ret) {
  231. pr_err(PREFIX "Failed to set amp power GPIO direction\n");
  232. goto fail1;
  233. }
  234. omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
  235. if (omap3pandora_snd_device == NULL) {
  236. pr_err(PREFIX "Platform device allocation failed\n");
  237. ret = -ENOMEM;
  238. goto fail1;
  239. }
  240. platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
  241. ret = platform_device_add(omap3pandora_snd_device);
  242. if (ret) {
  243. pr_err(PREFIX "Unable to add platform device\n");
  244. goto fail2;
  245. }
  246. omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
  247. if (IS_ERR(omap3pandora_dac_reg)) {
  248. pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
  249. dev_name(&omap3pandora_snd_device->dev),
  250. PTR_ERR(omap3pandora_dac_reg));
  251. ret = PTR_ERR(omap3pandora_dac_reg);
  252. goto fail3;
  253. }
  254. return 0;
  255. fail3:
  256. platform_device_del(omap3pandora_snd_device);
  257. fail2:
  258. platform_device_put(omap3pandora_snd_device);
  259. fail1:
  260. gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
  261. fail0:
  262. gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
  263. return ret;
  264. }
  265. module_init(omap3pandora_soc_init);
  266. static void __exit omap3pandora_soc_exit(void)
  267. {
  268. regulator_put(omap3pandora_dac_reg);
  269. platform_device_unregister(omap3pandora_snd_device);
  270. gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
  271. gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
  272. }
  273. module_exit(omap3pandora_soc_exit);
  274. MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
  275. MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
  276. MODULE_LICENSE("GPL");