omap3pandora.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/soc.h>
  28. #include <sound/soc-dapm.h>
  29. #include <asm/mach-types.h>
  30. #include "omap-mcbsp.h"
  31. #include "omap-pcm.h"
  32. #include "../codecs/twl4030.h"
  33. #define OMAP3_PANDORA_DAC_POWER_GPIO 118
  34. #define OMAP3_PANDORA_AMP_POWER_GPIO 14
  35. #define PREFIX "ASoC omap3pandora: "
  36. static int omap3pandora_cmn_hw_params(struct snd_soc_dai *codec_dai,
  37. struct snd_soc_dai *cpu_dai, unsigned int fmt)
  38. {
  39. int ret;
  40. /* Set codec DAI configuration */
  41. ret = snd_soc_dai_set_fmt(codec_dai, fmt);
  42. if (ret < 0) {
  43. pr_err(PREFIX "can't set codec DAI configuration\n");
  44. return ret;
  45. }
  46. /* Set cpu DAI configuration */
  47. ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
  48. if (ret < 0) {
  49. pr_err(PREFIX "can't set cpu DAI configuration\n");
  50. return ret;
  51. }
  52. /* Set the codec system clock for DAC and ADC */
  53. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
  54. SND_SOC_CLOCK_IN);
  55. if (ret < 0) {
  56. pr_err(PREFIX "can't set codec system clock\n");
  57. return ret;
  58. }
  59. /* Set McBSP clock to external */
  60. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT, 0,
  61. SND_SOC_CLOCK_IN);
  62. if (ret < 0) {
  63. pr_err(PREFIX "can't set cpu system clock\n");
  64. return ret;
  65. }
  66. ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
  67. if (ret < 0) {
  68. pr_err(PREFIX "can't set SRG clock divider\n");
  69. return ret;
  70. }
  71. return 0;
  72. }
  73. static int omap3pandora_out_hw_params(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params)
  75. {
  76. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  77. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  78. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  79. return omap3pandora_cmn_hw_params(codec_dai, cpu_dai,
  80. SND_SOC_DAIFMT_I2S |
  81. SND_SOC_DAIFMT_IB_NF |
  82. SND_SOC_DAIFMT_CBS_CFS);
  83. }
  84. static int omap3pandora_in_hw_params(struct snd_pcm_substream *substream,
  85. struct snd_pcm_hw_params *params)
  86. {
  87. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  88. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  89. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  90. return omap3pandora_cmn_hw_params(codec_dai, cpu_dai,
  91. SND_SOC_DAIFMT_I2S |
  92. SND_SOC_DAIFMT_NB_NF |
  93. SND_SOC_DAIFMT_CBS_CFS);
  94. }
  95. static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
  96. struct snd_kcontrol *k, int event)
  97. {
  98. if (SND_SOC_DAPM_EVENT_ON(event)) {
  99. gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
  100. gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
  101. } else {
  102. gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
  103. mdelay(1);
  104. gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
  105. }
  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("PCM DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
  118. SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
  119. 0, 0, NULL, 0, omap3pandora_hp_event,
  120. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  121. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  122. SND_SOC_DAPM_LINE("Line Out", NULL),
  123. };
  124. static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
  125. SND_SOC_DAPM_MIC("Mic (internal)", NULL),
  126. SND_SOC_DAPM_MIC("Mic (external)", NULL),
  127. SND_SOC_DAPM_LINE("Line In", NULL),
  128. };
  129. static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
  130. {"Headphone Amplifier", NULL, "PCM DAC"},
  131. {"Line Out", NULL, "PCM DAC"},
  132. {"Headphone Jack", NULL, "Headphone Amplifier"},
  133. };
  134. static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
  135. {"AUXL", NULL, "Line In"},
  136. {"AUXR", NULL, "Line In"},
  137. {"MAINMIC", NULL, "Mic Bias 1"},
  138. {"Mic Bias 1", NULL, "Mic (internal)"},
  139. {"SUBMIC", NULL, "Mic Bias 2"},
  140. {"Mic Bias 2", NULL, "Mic (external)"},
  141. };
  142. static int omap3pandora_out_init(struct snd_soc_codec *codec)
  143. {
  144. int ret;
  145. /* All TWL4030 output pins are floating */
  146. snd_soc_dapm_nc_pin(codec, "OUTL");
  147. snd_soc_dapm_nc_pin(codec, "OUTR");
  148. snd_soc_dapm_nc_pin(codec, "EARPIECE");
  149. snd_soc_dapm_nc_pin(codec, "PREDRIVEL");
  150. snd_soc_dapm_nc_pin(codec, "PREDRIVER");
  151. snd_soc_dapm_nc_pin(codec, "HSOL");
  152. snd_soc_dapm_nc_pin(codec, "HSOR");
  153. snd_soc_dapm_nc_pin(codec, "CARKITL");
  154. snd_soc_dapm_nc_pin(codec, "CARKITR");
  155. snd_soc_dapm_nc_pin(codec, "HFL");
  156. snd_soc_dapm_nc_pin(codec, "HFR");
  157. snd_soc_dapm_nc_pin(codec, "VIBRA");
  158. ret = snd_soc_dapm_new_controls(codec, omap3pandora_out_dapm_widgets,
  159. ARRAY_SIZE(omap3pandora_out_dapm_widgets));
  160. if (ret < 0)
  161. return ret;
  162. snd_soc_dapm_add_routes(codec, omap3pandora_out_map,
  163. ARRAY_SIZE(omap3pandora_out_map));
  164. return snd_soc_dapm_sync(codec);
  165. }
  166. static int omap3pandora_in_init(struct snd_soc_codec *codec)
  167. {
  168. int ret;
  169. /* Not comnnected */
  170. snd_soc_dapm_nc_pin(codec, "HSMIC");
  171. snd_soc_dapm_nc_pin(codec, "CARKITMIC");
  172. snd_soc_dapm_nc_pin(codec, "DIGIMIC0");
  173. snd_soc_dapm_nc_pin(codec, "DIGIMIC1");
  174. ret = snd_soc_dapm_new_controls(codec, omap3pandora_in_dapm_widgets,
  175. ARRAY_SIZE(omap3pandora_in_dapm_widgets));
  176. if (ret < 0)
  177. return ret;
  178. snd_soc_dapm_add_routes(codec, omap3pandora_in_map,
  179. ARRAY_SIZE(omap3pandora_in_map));
  180. return snd_soc_dapm_sync(codec);
  181. }
  182. static struct snd_soc_ops omap3pandora_out_ops = {
  183. .hw_params = omap3pandora_out_hw_params,
  184. };
  185. static struct snd_soc_ops omap3pandora_in_ops = {
  186. .hw_params = omap3pandora_in_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 = &omap_mcbsp_dai[0],
  194. .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
  195. .ops = &omap3pandora_out_ops,
  196. .init = omap3pandora_out_init,
  197. }, {
  198. .name = "TWL4030",
  199. .stream_name = "Line/Mic In",
  200. .cpu_dai = &omap_mcbsp_dai[1],
  201. .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
  202. .ops = &omap3pandora_in_ops,
  203. .init = omap3pandora_in_init,
  204. }
  205. };
  206. /* SoC card */
  207. static struct snd_soc_card snd_soc_card_omap3pandora = {
  208. .name = "omap3pandora",
  209. .platform = &omap_soc_platform,
  210. .dai_link = omap3pandora_dai,
  211. .num_links = ARRAY_SIZE(omap3pandora_dai),
  212. };
  213. /* Audio subsystem */
  214. static struct snd_soc_device omap3pandora_snd_data = {
  215. .card = &snd_soc_card_omap3pandora,
  216. .codec_dev = &soc_codec_dev_twl4030,
  217. };
  218. static struct platform_device *omap3pandora_snd_device;
  219. static int __init omap3pandora_soc_init(void)
  220. {
  221. int ret;
  222. if (!machine_is_omap3_pandora())
  223. return -ENODEV;
  224. pr_info("OMAP3 Pandora SoC init\n");
  225. ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
  226. if (ret) {
  227. pr_err(PREFIX "Failed to get DAC power GPIO\n");
  228. return ret;
  229. }
  230. ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
  231. if (ret) {
  232. pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
  233. goto fail0;
  234. }
  235. ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
  236. if (ret) {
  237. pr_err(PREFIX "Failed to get amp power GPIO\n");
  238. goto fail0;
  239. }
  240. ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
  241. if (ret) {
  242. pr_err(PREFIX "Failed to set amp power GPIO direction\n");
  243. goto fail1;
  244. }
  245. omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
  246. if (omap3pandora_snd_device == NULL) {
  247. pr_err(PREFIX "Platform device allocation failed\n");
  248. ret = -ENOMEM;
  249. goto fail1;
  250. }
  251. platform_set_drvdata(omap3pandora_snd_device, &omap3pandora_snd_data);
  252. omap3pandora_snd_data.dev = &omap3pandora_snd_device->dev;
  253. *(unsigned int *)omap_mcbsp_dai[0].private_data = 1; /* McBSP2 */
  254. *(unsigned int *)omap_mcbsp_dai[1].private_data = 3; /* McBSP4 */
  255. ret = platform_device_add(omap3pandora_snd_device);
  256. if (ret) {
  257. pr_err(PREFIX "Unable to add platform device\n");
  258. goto fail2;
  259. }
  260. return 0;
  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. platform_device_unregister(omap3pandora_snd_device);
  273. gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
  274. gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
  275. }
  276. module_exit(omap3pandora_soc_exit);
  277. MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
  278. MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
  279. MODULE_LICENSE("GPL");