omap3pandora.c 9.1 KB

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