omap3pandora.c 8.9 KB

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