omap3pandora.c 9.8 KB

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