omap-mcbsp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * omap-mcbsp.c -- OMAP ALSA SoC DAI driver using McBSP port
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/device.h>
  26. #include <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/initval.h>
  30. #include <sound/soc.h>
  31. #include <asm/arch/control.h>
  32. #include <asm/arch/dma.h>
  33. #include <asm/arch/mcbsp.h>
  34. #include "omap-mcbsp.h"
  35. #include "omap-pcm.h"
  36. #define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_44100 | \
  37. SNDRV_PCM_RATE_48000 | \
  38. SNDRV_PCM_RATE_KNOT)
  39. struct omap_mcbsp_data {
  40. unsigned int bus_id;
  41. struct omap_mcbsp_reg_cfg regs;
  42. /*
  43. * Flags indicating is the bus already activated and configured by
  44. * another substream
  45. */
  46. int active;
  47. int configured;
  48. };
  49. #define to_mcbsp(priv) container_of((priv), struct omap_mcbsp_data, bus_id)
  50. static struct omap_mcbsp_data mcbsp_data[NUM_LINKS];
  51. /*
  52. * Stream DMA parameters. DMA request line and port address are set runtime
  53. * since they are different between OMAP1 and later OMAPs
  54. */
  55. static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2] = {
  56. {
  57. { .name = "I2S PCM Stereo out", },
  58. { .name = "I2S PCM Stereo in", },
  59. },
  60. };
  61. #if defined(CONFIG_ARCH_OMAP15XX) || defined(CONFIG_ARCH_OMAP16XX)
  62. static const int omap1_dma_reqs[][2] = {
  63. { OMAP_DMA_MCBSP1_TX, OMAP_DMA_MCBSP1_RX },
  64. { OMAP_DMA_MCBSP2_TX, OMAP_DMA_MCBSP2_RX },
  65. { OMAP_DMA_MCBSP3_TX, OMAP_DMA_MCBSP3_RX },
  66. };
  67. static const unsigned long omap1_mcbsp_port[][2] = {
  68. { OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
  69. OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
  70. { OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
  71. OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
  72. { OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DXR1,
  73. OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DRR1 },
  74. };
  75. #else
  76. static const int omap1_dma_reqs[][2] = {};
  77. static const unsigned long omap1_mcbsp_port[][2] = {};
  78. #endif
  79. #if defined(CONFIG_ARCH_OMAP2420)
  80. static const int omap2420_dma_reqs[][2] = {
  81. { OMAP24XX_DMA_MCBSP1_TX, OMAP24XX_DMA_MCBSP1_RX },
  82. { OMAP24XX_DMA_MCBSP2_TX, OMAP24XX_DMA_MCBSP2_RX },
  83. };
  84. static const unsigned long omap2420_mcbsp_port[][2] = {
  85. { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
  86. OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
  87. { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
  88. OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
  89. };
  90. #else
  91. static const int omap2420_dma_reqs[][2] = {};
  92. static const unsigned long omap2420_mcbsp_port[][2] = {};
  93. #endif
  94. static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream)
  95. {
  96. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  97. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  98. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  99. int err = 0;
  100. if (!cpu_dai->active)
  101. err = omap_mcbsp_request(mcbsp_data->bus_id);
  102. return err;
  103. }
  104. static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream)
  105. {
  106. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  107. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  108. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  109. if (!cpu_dai->active) {
  110. omap_mcbsp_free(mcbsp_data->bus_id);
  111. mcbsp_data->configured = 0;
  112. }
  113. }
  114. static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd)
  115. {
  116. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  117. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  118. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  119. int err = 0;
  120. switch (cmd) {
  121. case SNDRV_PCM_TRIGGER_START:
  122. case SNDRV_PCM_TRIGGER_RESUME:
  123. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  124. if (!mcbsp_data->active++)
  125. omap_mcbsp_start(mcbsp_data->bus_id);
  126. break;
  127. case SNDRV_PCM_TRIGGER_STOP:
  128. case SNDRV_PCM_TRIGGER_SUSPEND:
  129. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  130. if (!--mcbsp_data->active)
  131. omap_mcbsp_stop(mcbsp_data->bus_id);
  132. break;
  133. default:
  134. err = -EINVAL;
  135. }
  136. return err;
  137. }
  138. static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
  139. struct snd_pcm_hw_params *params)
  140. {
  141. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  142. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  143. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  144. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  145. int dma, bus_id = mcbsp_data->bus_id, id = cpu_dai->id;
  146. unsigned long port;
  147. if (cpu_class_is_omap1()) {
  148. dma = omap1_dma_reqs[bus_id][substream->stream];
  149. port = omap1_mcbsp_port[bus_id][substream->stream];
  150. } else if (cpu_is_omap2420()) {
  151. dma = omap2420_dma_reqs[bus_id][substream->stream];
  152. port = omap2420_mcbsp_port[bus_id][substream->stream];
  153. } else {
  154. /*
  155. * TODO: Add support for 2430 and 3430
  156. */
  157. return -ENODEV;
  158. }
  159. omap_mcbsp_dai_dma_params[id][substream->stream].dma_req = dma;
  160. omap_mcbsp_dai_dma_params[id][substream->stream].port_addr = port;
  161. cpu_dai->dma_data = &omap_mcbsp_dai_dma_params[id][substream->stream];
  162. if (mcbsp_data->configured) {
  163. /* McBSP already configured by another stream */
  164. return 0;
  165. }
  166. switch (params_channels(params)) {
  167. case 2:
  168. /* Set 1 word per (McBPSP) frame and use dual-phase frames */
  169. regs->rcr2 |= RFRLEN2(1 - 1) | RPHASE;
  170. regs->rcr1 |= RFRLEN1(1 - 1);
  171. regs->xcr2 |= XFRLEN2(1 - 1) | XPHASE;
  172. regs->xcr1 |= XFRLEN1(1 - 1);
  173. break;
  174. default:
  175. /* Unsupported number of channels */
  176. return -EINVAL;
  177. }
  178. switch (params_format(params)) {
  179. case SNDRV_PCM_FORMAT_S16_LE:
  180. /* Set word lengths */
  181. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
  182. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
  183. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
  184. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
  185. /* Set FS period and length in terms of bit clock periods */
  186. regs->srgr2 |= FPER(16 * 2 - 1);
  187. regs->srgr1 |= FWID(16 - 1);
  188. break;
  189. default:
  190. /* Unsupported PCM format */
  191. return -EINVAL;
  192. }
  193. omap_mcbsp_config(bus_id, &mcbsp_data->regs);
  194. mcbsp_data->configured = 1;
  195. return 0;
  196. }
  197. /*
  198. * This must be called before _set_clkdiv and _set_sysclk since McBSP register
  199. * cache is initialized here
  200. */
  201. static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  202. unsigned int fmt)
  203. {
  204. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  205. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  206. if (mcbsp_data->configured)
  207. return 0;
  208. memset(regs, 0, sizeof(*regs));
  209. /* Generic McBSP register settings */
  210. regs->spcr2 |= XINTM(3) | FREE;
  211. regs->spcr1 |= RINTM(3);
  212. regs->rcr2 |= RFIG;
  213. regs->xcr2 |= XFIG;
  214. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  215. case SND_SOC_DAIFMT_I2S:
  216. /* 1-bit data delay */
  217. regs->rcr2 |= RDATDLY(1);
  218. regs->xcr2 |= XDATDLY(1);
  219. break;
  220. default:
  221. /* Unsupported data format */
  222. return -EINVAL;
  223. }
  224. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  225. case SND_SOC_DAIFMT_CBS_CFS:
  226. /* McBSP master. Set FS and bit clocks as outputs */
  227. regs->pcr0 |= FSXM | FSRM |
  228. CLKXM | CLKRM;
  229. /* Sample rate generator drives the FS */
  230. regs->srgr2 |= FSGM;
  231. break;
  232. case SND_SOC_DAIFMT_CBM_CFM:
  233. /* McBSP slave */
  234. break;
  235. default:
  236. /* Unsupported master/slave configuration */
  237. return -EINVAL;
  238. }
  239. /* Set bit clock (CLKX/CLKR) and FS polarities */
  240. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  241. case SND_SOC_DAIFMT_NB_NF:
  242. /*
  243. * Normal BCLK + FS.
  244. * FS active low. TX data driven on falling edge of bit clock
  245. * and RX data sampled on rising edge of bit clock.
  246. */
  247. regs->pcr0 |= FSXP | FSRP |
  248. CLKXP | CLKRP;
  249. break;
  250. case SND_SOC_DAIFMT_NB_IF:
  251. regs->pcr0 |= CLKXP | CLKRP;
  252. break;
  253. case SND_SOC_DAIFMT_IB_NF:
  254. regs->pcr0 |= FSXP | FSRP;
  255. break;
  256. case SND_SOC_DAIFMT_IB_IF:
  257. break;
  258. default:
  259. return -EINVAL;
  260. }
  261. return 0;
  262. }
  263. static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  264. int div_id, int div)
  265. {
  266. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  267. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  268. if (div_id != OMAP_MCBSP_CLKGDV)
  269. return -ENODEV;
  270. regs->srgr1 |= CLKGDV(div - 1);
  271. return 0;
  272. }
  273. static int omap_mcbsp_dai_set_clks_src(struct omap_mcbsp_data *mcbsp_data,
  274. int clk_id)
  275. {
  276. int sel_bit;
  277. u16 reg;
  278. if (cpu_class_is_omap1()) {
  279. /* OMAP1's can use only external source clock */
  280. if (unlikely(clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK))
  281. return -EINVAL;
  282. else
  283. return 0;
  284. }
  285. switch (mcbsp_data->bus_id) {
  286. case 0:
  287. reg = OMAP2_CONTROL_DEVCONF0;
  288. sel_bit = 2;
  289. break;
  290. case 1:
  291. reg = OMAP2_CONTROL_DEVCONF0;
  292. sel_bit = 6;
  293. break;
  294. /* TODO: Support for ports 3 - 5 in OMAP2430 and OMAP34xx */
  295. default:
  296. return -EINVAL;
  297. }
  298. if (cpu_class_is_omap2()) {
  299. if (clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK) {
  300. omap_ctrl_writel(omap_ctrl_readl(reg) &
  301. ~(1 << sel_bit), reg);
  302. } else {
  303. omap_ctrl_writel(omap_ctrl_readl(reg) |
  304. (1 << sel_bit), reg);
  305. }
  306. }
  307. return 0;
  308. }
  309. static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  310. int clk_id, unsigned int freq,
  311. int dir)
  312. {
  313. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  314. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  315. int err = 0;
  316. switch (clk_id) {
  317. case OMAP_MCBSP_SYSCLK_CLK:
  318. regs->srgr2 |= CLKSM;
  319. break;
  320. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  321. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  322. err = omap_mcbsp_dai_set_clks_src(mcbsp_data, clk_id);
  323. break;
  324. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  325. regs->srgr2 |= CLKSM;
  326. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  327. regs->pcr0 |= SCLKME;
  328. break;
  329. default:
  330. err = -ENODEV;
  331. }
  332. return err;
  333. }
  334. struct snd_soc_dai omap_mcbsp_dai[NUM_LINKS] = {
  335. {
  336. .name = "omap-mcbsp-dai",
  337. .id = 0,
  338. .type = SND_SOC_DAI_I2S,
  339. .playback = {
  340. .channels_min = 2,
  341. .channels_max = 2,
  342. .rates = OMAP_MCBSP_RATES,
  343. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  344. },
  345. .capture = {
  346. .channels_min = 2,
  347. .channels_max = 2,
  348. .rates = OMAP_MCBSP_RATES,
  349. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  350. },
  351. .ops = {
  352. .startup = omap_mcbsp_dai_startup,
  353. .shutdown = omap_mcbsp_dai_shutdown,
  354. .trigger = omap_mcbsp_dai_trigger,
  355. .hw_params = omap_mcbsp_dai_hw_params,
  356. },
  357. .dai_ops = {
  358. .set_fmt = omap_mcbsp_dai_set_dai_fmt,
  359. .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
  360. .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
  361. },
  362. .private_data = &mcbsp_data[0].bus_id,
  363. },
  364. };
  365. EXPORT_SYMBOL_GPL(omap_mcbsp_dai);
  366. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
  367. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  368. MODULE_LICENSE("GPL");