omap3pandora.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * omap3pandora.c -- SoC audio for Pandora Handheld Console
  4. *
  5. * Author: Gražvydas Ignotas <notasas@gmail.com>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/gpio.h>
  10. #include <linux/delay.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/module.h>
  13. #include <sound/core.h>
  14. #include <sound/pcm.h>
  15. #include <sound/soc.h>
  16. #include <asm/mach-types.h>
  17. #include <linux/platform_data/asoc-ti-mcbsp.h>
  18. #include "omap-mcbsp.h"
  19. #define OMAP3_PANDORA_DAC_POWER_GPIO 118
  20. #define OMAP3_PANDORA_AMP_POWER_GPIO 14
  21. #define PREFIX "ASoC omap3pandora: "
  22. static struct regulator *omap3pandora_dac_reg;
  23. static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
  24. struct snd_pcm_hw_params *params)
  25. {
  26. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  27. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  28. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  29. int ret;
  30. /* Set the codec system clock for DAC and ADC */
  31. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
  32. SND_SOC_CLOCK_IN);
  33. if (ret < 0) {
  34. pr_err(PREFIX "can't set codec system clock\n");
  35. return ret;
  36. }
  37. /* Set McBSP clock to external */
  38. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
  39. 256 * params_rate(params),
  40. SND_SOC_CLOCK_IN);
  41. if (ret < 0) {
  42. pr_err(PREFIX "can't set cpu system clock\n");
  43. return ret;
  44. }
  45. ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
  46. if (ret < 0) {
  47. pr_err(PREFIX "can't set SRG clock divider\n");
  48. return ret;
  49. }
  50. return 0;
  51. }
  52. static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
  53. struct snd_kcontrol *k, int event)
  54. {
  55. int ret;
  56. /*
  57. * The PCM1773 DAC datasheet requires 1ms delay between switching
  58. * VCC power on/off and /PD pin high/low
  59. */
  60. if (SND_SOC_DAPM_EVENT_ON(event)) {
  61. ret = regulator_enable(omap3pandora_dac_reg);
  62. if (ret) {
  63. dev_err(w->dapm->dev, "Failed to power DAC: %d\n", ret);
  64. return ret;
  65. }
  66. mdelay(1);
  67. gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
  68. } else {
  69. gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
  70. mdelay(1);
  71. regulator_disable(omap3pandora_dac_reg);
  72. }
  73. return 0;
  74. }
  75. static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
  76. struct snd_kcontrol *k, int event)
  77. {
  78. if (SND_SOC_DAPM_EVENT_ON(event))
  79. gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
  80. else
  81. gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
  82. return 0;
  83. }
  84. /*
  85. * Audio paths on Pandora board:
  86. *
  87. * |O| ---> PCM DAC +-> AMP -> Headphone Jack
  88. * |M| A +--------> Line Out
  89. * |A| <~~clk~~+
  90. * |P| <--- TWL4030 <--------- Line In and MICs
  91. */
  92. static const struct snd_soc_dapm_widget omap3pandora_dapm_widgets[] = {
  93. SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
  94. 0, 0, omap3pandora_dac_event,
  95. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  96. SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
  97. 0, 0, NULL, 0, omap3pandora_hp_event,
  98. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  99. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  100. SND_SOC_DAPM_LINE("Line Out", NULL),
  101. SND_SOC_DAPM_MIC("Mic (internal)", NULL),
  102. SND_SOC_DAPM_MIC("Mic (external)", NULL),
  103. SND_SOC_DAPM_LINE("Line In", NULL),
  104. };
  105. static const struct snd_soc_dapm_route omap3pandora_map[] = {
  106. {"PCM DAC", NULL, "APLL Enable"},
  107. {"Headphone Amplifier", NULL, "PCM DAC"},
  108. {"Line Out", NULL, "PCM DAC"},
  109. {"Headphone Jack", NULL, "Headphone Amplifier"},
  110. {"AUXL", NULL, "Line In"},
  111. {"AUXR", NULL, "Line In"},
  112. {"MAINMIC", NULL, "Mic (internal)"},
  113. {"Mic (internal)", NULL, "Mic Bias 1"},
  114. {"SUBMIC", NULL, "Mic (external)"},
  115. {"Mic (external)", NULL, "Mic Bias 2"},
  116. };
  117. static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
  118. {
  119. struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
  120. /* All TWL4030 output pins are floating */
  121. snd_soc_dapm_nc_pin(dapm, "EARPIECE");
  122. snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
  123. snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
  124. snd_soc_dapm_nc_pin(dapm, "HSOL");
  125. snd_soc_dapm_nc_pin(dapm, "HSOR");
  126. snd_soc_dapm_nc_pin(dapm, "CARKITL");
  127. snd_soc_dapm_nc_pin(dapm, "CARKITR");
  128. snd_soc_dapm_nc_pin(dapm, "HFL");
  129. snd_soc_dapm_nc_pin(dapm, "HFR");
  130. snd_soc_dapm_nc_pin(dapm, "VIBRA");
  131. return 0;
  132. }
  133. static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
  134. {
  135. struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
  136. /* Not comnnected */
  137. snd_soc_dapm_nc_pin(dapm, "HSMIC");
  138. snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
  139. snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
  140. snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
  141. return 0;
  142. }
  143. static const struct snd_soc_ops omap3pandora_ops = {
  144. .hw_params = omap3pandora_hw_params,
  145. };
  146. /* Digital audio interface glue - connects codec <--> CPU */
  147. SND_SOC_DAILINK_DEFS(out,
  148. DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.2")),
  149. DAILINK_COMP_ARRAY(COMP_CODEC("twl4030-codec", "twl4030-hifi")),
  150. DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.2")));
  151. SND_SOC_DAILINK_DEFS(in,
  152. DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.4")),
  153. DAILINK_COMP_ARRAY(COMP_CODEC("twl4030-codec", "twl4030-hifi")),
  154. DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.4")));
  155. static struct snd_soc_dai_link omap3pandora_dai[] = {
  156. {
  157. .name = "PCM1773",
  158. .stream_name = "HiFi Out",
  159. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  160. SND_SOC_DAIFMT_CBS_CFS,
  161. .ops = &omap3pandora_ops,
  162. .init = omap3pandora_out_init,
  163. SND_SOC_DAILINK_REG(out),
  164. }, {
  165. .name = "TWL4030",
  166. .stream_name = "Line/Mic In",
  167. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  168. SND_SOC_DAIFMT_CBS_CFS,
  169. .ops = &omap3pandora_ops,
  170. .init = omap3pandora_in_init,
  171. SND_SOC_DAILINK_REG(in),
  172. }
  173. };
  174. /* SoC card */
  175. static struct snd_soc_card snd_soc_card_omap3pandora = {
  176. .name = "omap3pandora",
  177. .owner = THIS_MODULE,
  178. .dai_link = omap3pandora_dai,
  179. .num_links = ARRAY_SIZE(omap3pandora_dai),
  180. .dapm_widgets = omap3pandora_dapm_widgets,
  181. .num_dapm_widgets = ARRAY_SIZE(omap3pandora_dapm_widgets),
  182. .dapm_routes = omap3pandora_map,
  183. .num_dapm_routes = ARRAY_SIZE(omap3pandora_map),
  184. };
  185. static struct platform_device *omap3pandora_snd_device;
  186. static int __init omap3pandora_soc_init(void)
  187. {
  188. int ret;
  189. if (!machine_is_omap3_pandora())
  190. return -ENODEV;
  191. pr_info("OMAP3 Pandora SoC init\n");
  192. ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
  193. if (ret) {
  194. pr_err(PREFIX "Failed to get DAC power GPIO\n");
  195. return ret;
  196. }
  197. ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
  198. if (ret) {
  199. pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
  200. goto fail0;
  201. }
  202. ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
  203. if (ret) {
  204. pr_err(PREFIX "Failed to get amp power GPIO\n");
  205. goto fail0;
  206. }
  207. ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
  208. if (ret) {
  209. pr_err(PREFIX "Failed to set amp power GPIO direction\n");
  210. goto fail1;
  211. }
  212. omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
  213. if (omap3pandora_snd_device == NULL) {
  214. pr_err(PREFIX "Platform device allocation failed\n");
  215. ret = -ENOMEM;
  216. goto fail1;
  217. }
  218. platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
  219. ret = platform_device_add(omap3pandora_snd_device);
  220. if (ret) {
  221. pr_err(PREFIX "Unable to add platform device\n");
  222. goto fail2;
  223. }
  224. omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
  225. if (IS_ERR(omap3pandora_dac_reg)) {
  226. pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
  227. dev_name(&omap3pandora_snd_device->dev),
  228. PTR_ERR(omap3pandora_dac_reg));
  229. ret = PTR_ERR(omap3pandora_dac_reg);
  230. goto fail3;
  231. }
  232. return 0;
  233. fail3:
  234. platform_device_del(omap3pandora_snd_device);
  235. fail2:
  236. platform_device_put(omap3pandora_snd_device);
  237. fail1:
  238. gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
  239. fail0:
  240. gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
  241. return ret;
  242. }
  243. module_init(omap3pandora_soc_init);
  244. static void __exit omap3pandora_soc_exit(void)
  245. {
  246. regulator_put(omap3pandora_dac_reg);
  247. platform_device_unregister(omap3pandora_snd_device);
  248. gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
  249. gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
  250. }
  251. module_exit(omap3pandora_soc_exit);
  252. MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
  253. MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
  254. MODULE_LICENSE("GPL");