omap3pandora.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/soc.h>
  29. #include <sound/soc-dapm.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 fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  45. SND_SOC_DAIFMT_CBS_CFS;
  46. int ret;
  47. /* Set codec DAI configuration */
  48. ret = snd_soc_dai_set_fmt(codec_dai, fmt);
  49. if (ret < 0) {
  50. pr_err(PREFIX "can't set codec DAI configuration\n");
  51. return ret;
  52. }
  53. /* Set cpu DAI configuration */
  54. ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
  55. if (ret < 0) {
  56. pr_err(PREFIX "can't set cpu DAI configuration\n");
  57. return ret;
  58. }
  59. /* Set the codec system clock for DAC and ADC */
  60. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
  61. SND_SOC_CLOCK_IN);
  62. if (ret < 0) {
  63. pr_err(PREFIX "can't set codec system clock\n");
  64. return ret;
  65. }
  66. /* Set McBSP clock to external */
  67. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
  68. 256 * params_rate(params),
  69. SND_SOC_CLOCK_IN);
  70. if (ret < 0) {
  71. pr_err(PREFIX "can't set cpu system clock\n");
  72. return ret;
  73. }
  74. ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
  75. if (ret < 0) {
  76. pr_err(PREFIX "can't set SRG clock divider\n");
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
  82. struct snd_kcontrol *k, int event)
  83. {
  84. /*
  85. * The PCM1773 DAC datasheet requires 1ms delay between switching
  86. * VCC power on/off and /PD pin high/low
  87. */
  88. if (SND_SOC_DAPM_EVENT_ON(event)) {
  89. regulator_enable(omap3pandora_dac_reg);
  90. mdelay(1);
  91. gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
  92. } else {
  93. gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
  94. mdelay(1);
  95. regulator_disable(omap3pandora_dac_reg);
  96. }
  97. return 0;
  98. }
  99. static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
  100. struct snd_kcontrol *k, int event)
  101. {
  102. if (SND_SOC_DAPM_EVENT_ON(event))
  103. gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
  104. else
  105. gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
  106. return 0;
  107. }
  108. /*
  109. * Audio paths on Pandora board:
  110. *
  111. * |O| ---> PCM DAC +-> AMP -> Headphone Jack
  112. * |M| A +--------> Line Out
  113. * |A| <~~clk~~+
  114. * |P| <--- TWL4030 <--------- Line In and MICs
  115. */
  116. static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
  117. SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
  118. 0, 0, omap3pandora_dac_event,
  119. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  120. SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
  121. 0, 0, NULL, 0, omap3pandora_hp_event,
  122. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  123. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  124. SND_SOC_DAPM_LINE("Line Out", NULL),
  125. };
  126. static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
  127. SND_SOC_DAPM_MIC("Mic (internal)", NULL),
  128. SND_SOC_DAPM_MIC("Mic (external)", NULL),
  129. SND_SOC_DAPM_LINE("Line In", NULL),
  130. };
  131. static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
  132. {"PCM DAC", NULL, "APLL Enable"},
  133. {"Headphone Amplifier", NULL, "PCM DAC"},
  134. {"Line Out", NULL, "PCM DAC"},
  135. {"Headphone Jack", NULL, "Headphone Amplifier"},
  136. };
  137. static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
  138. {"AUXL", NULL, "Line In"},
  139. {"AUXR", NULL, "Line In"},
  140. {"MAINMIC", NULL, "Mic Bias 1"},
  141. {"Mic Bias 1", NULL, "Mic (internal)"},
  142. {"SUBMIC", NULL, "Mic Bias 2"},
  143. {"Mic Bias 2", NULL, "Mic (external)"},
  144. };
  145. static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
  146. {
  147. struct snd_soc_codec *codec = rtd->codec;
  148. int ret;
  149. /* All TWL4030 output pins are floating */
  150. snd_soc_dapm_nc_pin(codec, "EARPIECE");
  151. snd_soc_dapm_nc_pin(codec, "PREDRIVEL");
  152. snd_soc_dapm_nc_pin(codec, "PREDRIVER");
  153. snd_soc_dapm_nc_pin(codec, "HSOL");
  154. snd_soc_dapm_nc_pin(codec, "HSOR");
  155. snd_soc_dapm_nc_pin(codec, "CARKITL");
  156. snd_soc_dapm_nc_pin(codec, "CARKITR");
  157. snd_soc_dapm_nc_pin(codec, "HFL");
  158. snd_soc_dapm_nc_pin(codec, "HFR");
  159. snd_soc_dapm_nc_pin(codec, "VIBRA");
  160. ret = snd_soc_dapm_new_controls(codec, omap3pandora_out_dapm_widgets,
  161. ARRAY_SIZE(omap3pandora_out_dapm_widgets));
  162. if (ret < 0)
  163. return ret;
  164. snd_soc_dapm_add_routes(codec, omap3pandora_out_map,
  165. ARRAY_SIZE(omap3pandora_out_map));
  166. return snd_soc_dapm_sync(codec);
  167. }
  168. static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
  169. {
  170. struct snd_soc_codec *codec = rtd->codec;
  171. int ret;
  172. /* Not comnnected */
  173. snd_soc_dapm_nc_pin(codec, "HSMIC");
  174. snd_soc_dapm_nc_pin(codec, "CARKITMIC");
  175. snd_soc_dapm_nc_pin(codec, "DIGIMIC0");
  176. snd_soc_dapm_nc_pin(codec, "DIGIMIC1");
  177. ret = snd_soc_dapm_new_controls(codec, omap3pandora_in_dapm_widgets,
  178. ARRAY_SIZE(omap3pandora_in_dapm_widgets));
  179. if (ret < 0)
  180. return ret;
  181. snd_soc_dapm_add_routes(codec, omap3pandora_in_map,
  182. ARRAY_SIZE(omap3pandora_in_map));
  183. return snd_soc_dapm_sync(codec);
  184. }
  185. static struct snd_soc_ops omap3pandora_ops = {
  186. .hw_params = omap3pandora_hw_params,
  187. };
  188. /* Digital audio interface glue - connects codec <--> CPU */
  189. static struct snd_soc_dai_link omap3pandora_dai[] = {
  190. {
  191. .name = "PCM1773",
  192. .stream_name = "HiFi Out",
  193. .cpu_dai_name = "omap-mcbsp-dai.1",
  194. .codec_dai_name = "twl4030-hifi",
  195. .platform_name = "omap-pcm-audio",
  196. .codec_name = "twl4030-codec",
  197. .ops = &omap3pandora_ops,
  198. .init = omap3pandora_out_init,
  199. }, {
  200. .name = "TWL4030",
  201. .stream_name = "Line/Mic In",
  202. .cpu_dai_name = "omap-mcbsp-dai.3",
  203. .codec_dai_name = "twl4030-hifi",
  204. .platform_name = "omap-pcm-audio",
  205. .codec_name = "twl4030-codec",
  206. .ops = &omap3pandora_ops,
  207. .init = omap3pandora_in_init,
  208. }
  209. };
  210. /* SoC card */
  211. static struct snd_soc_card snd_soc_card_omap3pandora = {
  212. .name = "omap3pandora",
  213. .dai_link = omap3pandora_dai,
  214. .num_links = ARRAY_SIZE(omap3pandora_dai),
  215. };
  216. static struct platform_device *omap3pandora_snd_device;
  217. static int __init omap3pandora_soc_init(void)
  218. {
  219. int ret;
  220. if (!machine_is_omap3_pandora())
  221. return -ENODEV;
  222. pr_info("OMAP3 Pandora SoC init\n");
  223. ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
  224. if (ret) {
  225. pr_err(PREFIX "Failed to get DAC power GPIO\n");
  226. return ret;
  227. }
  228. ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
  229. if (ret) {
  230. pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
  231. goto fail0;
  232. }
  233. ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
  234. if (ret) {
  235. pr_err(PREFIX "Failed to get amp power GPIO\n");
  236. goto fail0;
  237. }
  238. ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
  239. if (ret) {
  240. pr_err(PREFIX "Failed to set amp power GPIO direction\n");
  241. goto fail1;
  242. }
  243. omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
  244. if (omap3pandora_snd_device == NULL) {
  245. pr_err(PREFIX "Platform device allocation failed\n");
  246. ret = -ENOMEM;
  247. goto fail1;
  248. }
  249. platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
  250. ret = platform_device_add(omap3pandora_snd_device);
  251. if (ret) {
  252. pr_err(PREFIX "Unable to add platform device\n");
  253. goto fail2;
  254. }
  255. omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
  256. if (IS_ERR(omap3pandora_dac_reg)) {
  257. pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
  258. dev_name(&omap3pandora_snd_device->dev),
  259. PTR_ERR(omap3pandora_dac_reg));
  260. ret = PTR_ERR(omap3pandora_dac_reg);
  261. goto fail3;
  262. }
  263. return 0;
  264. fail3:
  265. platform_device_del(omap3pandora_snd_device);
  266. fail2:
  267. platform_device_put(omap3pandora_snd_device);
  268. fail1:
  269. gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
  270. fail0:
  271. gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
  272. return ret;
  273. }
  274. module_init(omap3pandora_soc_init);
  275. static void __exit omap3pandora_soc_exit(void)
  276. {
  277. regulator_put(omap3pandora_dac_reg);
  278. platform_device_unregister(omap3pandora_snd_device);
  279. gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
  280. gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
  281. }
  282. module_exit(omap3pandora_soc_exit);
  283. MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
  284. MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
  285. MODULE_LICENSE("GPL");