omap-mcbsp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 <jhnikula@gmail.com>
  7. * Peter Ujfalusi <peter.ujfalusi@nokia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/initval.h>
  31. #include <sound/soc.h>
  32. #include <plat/control.h>
  33. #include <plat/dma.h>
  34. #include <plat/mcbsp.h>
  35. #include "omap-mcbsp.h"
  36. #include "omap-pcm.h"
  37. #define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000)
  38. struct omap_mcbsp_data {
  39. unsigned int bus_id;
  40. struct omap_mcbsp_reg_cfg regs;
  41. unsigned int fmt;
  42. /*
  43. * Flags indicating is the bus already activated and configured by
  44. * another substream
  45. */
  46. int active;
  47. int configured;
  48. unsigned int in_freq;
  49. int clk_div;
  50. };
  51. #define to_mcbsp(priv) container_of((priv), struct omap_mcbsp_data, bus_id)
  52. static struct omap_mcbsp_data mcbsp_data[NUM_LINKS];
  53. /*
  54. * Stream DMA parameters. DMA request line and port address are set runtime
  55. * since they are different between OMAP1 and later OMAPs
  56. */
  57. static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2];
  58. #if defined(CONFIG_ARCH_OMAP15XX) || defined(CONFIG_ARCH_OMAP16XX)
  59. static const int omap1_dma_reqs[][2] = {
  60. { OMAP_DMA_MCBSP1_TX, OMAP_DMA_MCBSP1_RX },
  61. { OMAP_DMA_MCBSP2_TX, OMAP_DMA_MCBSP2_RX },
  62. { OMAP_DMA_MCBSP3_TX, OMAP_DMA_MCBSP3_RX },
  63. };
  64. static const unsigned long omap1_mcbsp_port[][2] = {
  65. { OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
  66. OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
  67. { OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
  68. OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
  69. { OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DXR1,
  70. OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DRR1 },
  71. };
  72. #else
  73. static const int omap1_dma_reqs[][2] = {};
  74. static const unsigned long omap1_mcbsp_port[][2] = {};
  75. #endif
  76. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  77. static const int omap24xx_dma_reqs[][2] = {
  78. { OMAP24XX_DMA_MCBSP1_TX, OMAP24XX_DMA_MCBSP1_RX },
  79. { OMAP24XX_DMA_MCBSP2_TX, OMAP24XX_DMA_MCBSP2_RX },
  80. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
  81. { OMAP24XX_DMA_MCBSP3_TX, OMAP24XX_DMA_MCBSP3_RX },
  82. { OMAP24XX_DMA_MCBSP4_TX, OMAP24XX_DMA_MCBSP4_RX },
  83. { OMAP24XX_DMA_MCBSP5_TX, OMAP24XX_DMA_MCBSP5_RX },
  84. #endif
  85. };
  86. #else
  87. static const int omap24xx_dma_reqs[][2] = {};
  88. #endif
  89. #if defined(CONFIG_ARCH_OMAP2420)
  90. static const unsigned long omap2420_mcbsp_port[][2] = {
  91. { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
  92. OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
  93. { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
  94. OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
  95. };
  96. #else
  97. static const unsigned long omap2420_mcbsp_port[][2] = {};
  98. #endif
  99. #if defined(CONFIG_ARCH_OMAP2430)
  100. static const unsigned long omap2430_mcbsp_port[][2] = {
  101. { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
  102. OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
  103. { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
  104. OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
  105. { OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
  106. OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
  107. { OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
  108. OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
  109. { OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
  110. OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
  111. };
  112. #else
  113. static const unsigned long omap2430_mcbsp_port[][2] = {};
  114. #endif
  115. #if defined(CONFIG_ARCH_OMAP34XX)
  116. static const unsigned long omap34xx_mcbsp_port[][2] = {
  117. { OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
  118. OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
  119. { OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
  120. OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
  121. { OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
  122. OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
  123. { OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
  124. OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
  125. { OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
  126. OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
  127. };
  128. #else
  129. static const unsigned long omap34xx_mcbsp_port[][2] = {};
  130. #endif
  131. static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream)
  132. {
  133. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  134. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  135. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  136. int dma_op_mode = omap_mcbsp_get_dma_op_mode(mcbsp_data->bus_id);
  137. int samples;
  138. /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
  139. if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
  140. samples = snd_pcm_lib_period_bytes(substream) >> 1;
  141. else
  142. samples = 1;
  143. /* Configure McBSP internal buffer usage */
  144. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  145. omap_mcbsp_set_tx_threshold(mcbsp_data->bus_id, samples - 1);
  146. else
  147. omap_mcbsp_set_rx_threshold(mcbsp_data->bus_id, samples - 1);
  148. }
  149. static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
  150. struct snd_soc_dai *dai)
  151. {
  152. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  153. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  154. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  155. int bus_id = mcbsp_data->bus_id;
  156. int err = 0;
  157. if (!cpu_dai->active)
  158. err = omap_mcbsp_request(bus_id);
  159. if (cpu_is_omap343x()) {
  160. int dma_op_mode = omap_mcbsp_get_dma_op_mode(bus_id);
  161. int max_period;
  162. /*
  163. * McBSP2 in OMAP3 has 1024 * 32-bit internal audio buffer.
  164. * Set constraint for minimum buffer size to the same than FIFO
  165. * size in order to avoid underruns in playback startup because
  166. * HW is keeping the DMA request active until FIFO is filled.
  167. */
  168. if (bus_id == 1)
  169. snd_pcm_hw_constraint_minmax(substream->runtime,
  170. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  171. 4096, UINT_MAX);
  172. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  173. max_period = omap_mcbsp_get_max_tx_threshold(bus_id);
  174. else
  175. max_period = omap_mcbsp_get_max_rx_threshold(bus_id);
  176. max_period++;
  177. max_period <<= 1;
  178. if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
  179. snd_pcm_hw_constraint_minmax(substream->runtime,
  180. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  181. 32, max_period);
  182. }
  183. return err;
  184. }
  185. static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
  186. struct snd_soc_dai *dai)
  187. {
  188. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  189. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  190. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  191. if (!cpu_dai->active) {
  192. omap_mcbsp_free(mcbsp_data->bus_id);
  193. mcbsp_data->configured = 0;
  194. }
  195. }
  196. static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  197. struct snd_soc_dai *dai)
  198. {
  199. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  200. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  201. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  202. int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  203. switch (cmd) {
  204. case SNDRV_PCM_TRIGGER_START:
  205. case SNDRV_PCM_TRIGGER_RESUME:
  206. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  207. mcbsp_data->active++;
  208. omap_mcbsp_start(mcbsp_data->bus_id, play, !play);
  209. break;
  210. case SNDRV_PCM_TRIGGER_STOP:
  211. case SNDRV_PCM_TRIGGER_SUSPEND:
  212. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  213. omap_mcbsp_stop(mcbsp_data->bus_id, play, !play);
  214. mcbsp_data->active--;
  215. break;
  216. default:
  217. err = -EINVAL;
  218. }
  219. return err;
  220. }
  221. static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
  222. struct snd_pcm_hw_params *params,
  223. struct snd_soc_dai *dai)
  224. {
  225. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  226. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  227. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  228. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  229. int dma, bus_id = mcbsp_data->bus_id, id = cpu_dai->id;
  230. int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT;
  231. unsigned long port;
  232. unsigned int format, div, framesize, master;
  233. if (cpu_class_is_omap1()) {
  234. dma = omap1_dma_reqs[bus_id][substream->stream];
  235. port = omap1_mcbsp_port[bus_id][substream->stream];
  236. } else if (cpu_is_omap2420()) {
  237. dma = omap24xx_dma_reqs[bus_id][substream->stream];
  238. port = omap2420_mcbsp_port[bus_id][substream->stream];
  239. } else if (cpu_is_omap2430()) {
  240. dma = omap24xx_dma_reqs[bus_id][substream->stream];
  241. port = omap2430_mcbsp_port[bus_id][substream->stream];
  242. } else if (cpu_is_omap343x()) {
  243. dma = omap24xx_dma_reqs[bus_id][substream->stream];
  244. port = omap34xx_mcbsp_port[bus_id][substream->stream];
  245. omap_mcbsp_dai_dma_params[id][substream->stream].set_threshold =
  246. omap_mcbsp_set_threshold;
  247. /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
  248. if (omap_mcbsp_get_dma_op_mode(bus_id) ==
  249. MCBSP_DMA_MODE_THRESHOLD)
  250. sync_mode = OMAP_DMA_SYNC_FRAME;
  251. } else {
  252. return -ENODEV;
  253. }
  254. omap_mcbsp_dai_dma_params[id][substream->stream].name =
  255. substream->stream ? "Audio Capture" : "Audio Playback";
  256. omap_mcbsp_dai_dma_params[id][substream->stream].dma_req = dma;
  257. omap_mcbsp_dai_dma_params[id][substream->stream].port_addr = port;
  258. omap_mcbsp_dai_dma_params[id][substream->stream].sync_mode = sync_mode;
  259. omap_mcbsp_dai_dma_params[id][substream->stream].data_type =
  260. OMAP_DMA_DATA_TYPE_S16;
  261. cpu_dai->dma_data = &omap_mcbsp_dai_dma_params[id][substream->stream];
  262. if (mcbsp_data->configured) {
  263. /* McBSP already configured by another stream */
  264. return 0;
  265. }
  266. format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  267. wpf = channels = params_channels(params);
  268. if (channels == 2 && format == SND_SOC_DAIFMT_I2S) {
  269. /* Use dual-phase frames */
  270. regs->rcr2 |= RPHASE;
  271. regs->xcr2 |= XPHASE;
  272. /* Set 1 word per (McBSP) frame for phase1 and phase2 */
  273. wpf--;
  274. regs->rcr2 |= RFRLEN2(wpf - 1);
  275. regs->xcr2 |= XFRLEN2(wpf - 1);
  276. }
  277. regs->rcr1 |= RFRLEN1(wpf - 1);
  278. regs->xcr1 |= XFRLEN1(wpf - 1);
  279. switch (params_format(params)) {
  280. case SNDRV_PCM_FORMAT_S16_LE:
  281. /* Set word lengths */
  282. wlen = 16;
  283. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
  284. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
  285. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
  286. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
  287. break;
  288. default:
  289. /* Unsupported PCM format */
  290. return -EINVAL;
  291. }
  292. /* In McBSP master modes, FRAME (i.e. sample rate) is generated
  293. * by _counting_ BCLKs. Calculate frame size in BCLKs */
  294. master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  295. if (master == SND_SOC_DAIFMT_CBS_CFS) {
  296. div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
  297. framesize = (mcbsp_data->in_freq / div) / params_rate(params);
  298. if (framesize < wlen * channels) {
  299. printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
  300. "channels\n", __func__);
  301. return -EINVAL;
  302. }
  303. } else
  304. framesize = wlen * channels;
  305. /* Set FS period and length in terms of bit clock periods */
  306. switch (format) {
  307. case SND_SOC_DAIFMT_I2S:
  308. regs->srgr2 |= FPER(framesize - 1);
  309. regs->srgr1 |= FWID((framesize >> 1) - 1);
  310. break;
  311. case SND_SOC_DAIFMT_DSP_A:
  312. case SND_SOC_DAIFMT_DSP_B:
  313. regs->srgr2 |= FPER(framesize - 1);
  314. regs->srgr1 |= FWID(0);
  315. break;
  316. }
  317. omap_mcbsp_config(bus_id, &mcbsp_data->regs);
  318. mcbsp_data->configured = 1;
  319. return 0;
  320. }
  321. /*
  322. * This must be called before _set_clkdiv and _set_sysclk since McBSP register
  323. * cache is initialized here
  324. */
  325. static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  326. unsigned int fmt)
  327. {
  328. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  329. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  330. unsigned int temp_fmt = fmt;
  331. if (mcbsp_data->configured)
  332. return 0;
  333. mcbsp_data->fmt = fmt;
  334. memset(regs, 0, sizeof(*regs));
  335. /* Generic McBSP register settings */
  336. regs->spcr2 |= XINTM(3) | FREE;
  337. regs->spcr1 |= RINTM(3);
  338. /* RFIG and XFIG are not defined in 34xx */
  339. if (!cpu_is_omap34xx()) {
  340. regs->rcr2 |= RFIG;
  341. regs->xcr2 |= XFIG;
  342. }
  343. if (cpu_is_omap2430() || cpu_is_omap34xx()) {
  344. regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
  345. regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
  346. }
  347. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  348. case SND_SOC_DAIFMT_I2S:
  349. /* 1-bit data delay */
  350. regs->rcr2 |= RDATDLY(1);
  351. regs->xcr2 |= XDATDLY(1);
  352. break;
  353. case SND_SOC_DAIFMT_DSP_A:
  354. /* 1-bit data delay */
  355. regs->rcr2 |= RDATDLY(1);
  356. regs->xcr2 |= XDATDLY(1);
  357. /* Invert FS polarity configuration */
  358. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  359. break;
  360. case SND_SOC_DAIFMT_DSP_B:
  361. /* 0-bit data delay */
  362. regs->rcr2 |= RDATDLY(0);
  363. regs->xcr2 |= XDATDLY(0);
  364. /* Invert FS polarity configuration */
  365. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  366. break;
  367. default:
  368. /* Unsupported data format */
  369. return -EINVAL;
  370. }
  371. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  372. case SND_SOC_DAIFMT_CBS_CFS:
  373. /* McBSP master. Set FS and bit clocks as outputs */
  374. regs->pcr0 |= FSXM | FSRM |
  375. CLKXM | CLKRM;
  376. /* Sample rate generator drives the FS */
  377. regs->srgr2 |= FSGM;
  378. break;
  379. case SND_SOC_DAIFMT_CBM_CFM:
  380. /* McBSP slave */
  381. break;
  382. default:
  383. /* Unsupported master/slave configuration */
  384. return -EINVAL;
  385. }
  386. /* Set bit clock (CLKX/CLKR) and FS polarities */
  387. switch (temp_fmt & SND_SOC_DAIFMT_INV_MASK) {
  388. case SND_SOC_DAIFMT_NB_NF:
  389. /*
  390. * Normal BCLK + FS.
  391. * FS active low. TX data driven on falling edge of bit clock
  392. * and RX data sampled on rising edge of bit clock.
  393. */
  394. regs->pcr0 |= FSXP | FSRP |
  395. CLKXP | CLKRP;
  396. break;
  397. case SND_SOC_DAIFMT_NB_IF:
  398. regs->pcr0 |= CLKXP | CLKRP;
  399. break;
  400. case SND_SOC_DAIFMT_IB_NF:
  401. regs->pcr0 |= FSXP | FSRP;
  402. break;
  403. case SND_SOC_DAIFMT_IB_IF:
  404. break;
  405. default:
  406. return -EINVAL;
  407. }
  408. return 0;
  409. }
  410. static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  411. int div_id, int div)
  412. {
  413. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  414. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  415. if (div_id != OMAP_MCBSP_CLKGDV)
  416. return -ENODEV;
  417. mcbsp_data->clk_div = div;
  418. regs->srgr1 |= CLKGDV(div - 1);
  419. return 0;
  420. }
  421. static int omap_mcbsp_dai_set_clks_src(struct omap_mcbsp_data *mcbsp_data,
  422. int clk_id)
  423. {
  424. int sel_bit;
  425. u16 reg, reg_devconf1 = OMAP243X_CONTROL_DEVCONF1;
  426. if (cpu_class_is_omap1()) {
  427. /* OMAP1's can use only external source clock */
  428. if (unlikely(clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK))
  429. return -EINVAL;
  430. else
  431. return 0;
  432. }
  433. if (cpu_is_omap2420() && mcbsp_data->bus_id > 1)
  434. return -EINVAL;
  435. if (cpu_is_omap343x())
  436. reg_devconf1 = OMAP343X_CONTROL_DEVCONF1;
  437. switch (mcbsp_data->bus_id) {
  438. case 0:
  439. reg = OMAP2_CONTROL_DEVCONF0;
  440. sel_bit = 2;
  441. break;
  442. case 1:
  443. reg = OMAP2_CONTROL_DEVCONF0;
  444. sel_bit = 6;
  445. break;
  446. case 2:
  447. reg = reg_devconf1;
  448. sel_bit = 0;
  449. break;
  450. case 3:
  451. reg = reg_devconf1;
  452. sel_bit = 2;
  453. break;
  454. case 4:
  455. reg = reg_devconf1;
  456. sel_bit = 4;
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. if (clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK)
  462. omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
  463. else
  464. omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
  465. return 0;
  466. }
  467. static int omap_mcbsp_dai_set_rcvr_src(struct omap_mcbsp_data *mcbsp_data,
  468. int clk_id)
  469. {
  470. int sel_bit, set = 0;
  471. u16 reg = OMAP2_CONTROL_DEVCONF0;
  472. if (cpu_class_is_omap1())
  473. return -EINVAL; /* TODO: Can this be implemented for OMAP1? */
  474. if (mcbsp_data->bus_id != 0)
  475. return -EINVAL;
  476. switch (clk_id) {
  477. case OMAP_MCBSP_CLKR_SRC_CLKX:
  478. set = 1;
  479. case OMAP_MCBSP_CLKR_SRC_CLKR:
  480. sel_bit = 3;
  481. break;
  482. case OMAP_MCBSP_FSR_SRC_FSX:
  483. set = 1;
  484. case OMAP_MCBSP_FSR_SRC_FSR:
  485. sel_bit = 4;
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. if (set)
  491. omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
  492. else
  493. omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
  494. return 0;
  495. }
  496. static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  497. int clk_id, unsigned int freq,
  498. int dir)
  499. {
  500. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  501. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  502. int err = 0;
  503. mcbsp_data->in_freq = freq;
  504. switch (clk_id) {
  505. case OMAP_MCBSP_SYSCLK_CLK:
  506. regs->srgr2 |= CLKSM;
  507. break;
  508. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  509. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  510. err = omap_mcbsp_dai_set_clks_src(mcbsp_data, clk_id);
  511. break;
  512. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  513. regs->srgr2 |= CLKSM;
  514. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  515. regs->pcr0 |= SCLKME;
  516. break;
  517. case OMAP_MCBSP_CLKR_SRC_CLKR:
  518. case OMAP_MCBSP_CLKR_SRC_CLKX:
  519. case OMAP_MCBSP_FSR_SRC_FSR:
  520. case OMAP_MCBSP_FSR_SRC_FSX:
  521. err = omap_mcbsp_dai_set_rcvr_src(mcbsp_data, clk_id);
  522. break;
  523. default:
  524. err = -ENODEV;
  525. }
  526. return err;
  527. }
  528. static struct snd_soc_dai_ops omap_mcbsp_dai_ops = {
  529. .startup = omap_mcbsp_dai_startup,
  530. .shutdown = omap_mcbsp_dai_shutdown,
  531. .trigger = omap_mcbsp_dai_trigger,
  532. .hw_params = omap_mcbsp_dai_hw_params,
  533. .set_fmt = omap_mcbsp_dai_set_dai_fmt,
  534. .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
  535. .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
  536. };
  537. #define OMAP_MCBSP_DAI_BUILDER(link_id) \
  538. { \
  539. .name = "omap-mcbsp-dai-"#link_id, \
  540. .id = (link_id), \
  541. .playback = { \
  542. .channels_min = 1, \
  543. .channels_max = 16, \
  544. .rates = OMAP_MCBSP_RATES, \
  545. .formats = SNDRV_PCM_FMTBIT_S16_LE, \
  546. }, \
  547. .capture = { \
  548. .channels_min = 1, \
  549. .channels_max = 16, \
  550. .rates = OMAP_MCBSP_RATES, \
  551. .formats = SNDRV_PCM_FMTBIT_S16_LE, \
  552. }, \
  553. .ops = &omap_mcbsp_dai_ops, \
  554. .private_data = &mcbsp_data[(link_id)].bus_id, \
  555. }
  556. struct snd_soc_dai omap_mcbsp_dai[] = {
  557. OMAP_MCBSP_DAI_BUILDER(0),
  558. OMAP_MCBSP_DAI_BUILDER(1),
  559. #if NUM_LINKS >= 3
  560. OMAP_MCBSP_DAI_BUILDER(2),
  561. #endif
  562. #if NUM_LINKS == 5
  563. OMAP_MCBSP_DAI_BUILDER(3),
  564. OMAP_MCBSP_DAI_BUILDER(4),
  565. #endif
  566. };
  567. EXPORT_SYMBOL_GPL(omap_mcbsp_dai);
  568. static int __init snd_omap_mcbsp_init(void)
  569. {
  570. return snd_soc_register_dais(omap_mcbsp_dai,
  571. ARRAY_SIZE(omap_mcbsp_dai));
  572. }
  573. module_init(snd_omap_mcbsp_init);
  574. static void __exit snd_omap_mcbsp_exit(void)
  575. {
  576. snd_soc_unregister_dais(omap_mcbsp_dai, ARRAY_SIZE(omap_mcbsp_dai));
  577. }
  578. module_exit(snd_omap_mcbsp_exit);
  579. MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
  580. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  581. MODULE_LICENSE("GPL");