omap3pandora.c 8.8 KB

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