omap-mcbsp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. cpu_dai->dma_data = &omap_mcbsp_dai_dma_params[id][substream->stream];
  260. if (mcbsp_data->configured) {
  261. /* McBSP already configured by another stream */
  262. return 0;
  263. }
  264. format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  265. wpf = channels = params_channels(params);
  266. if (channels == 2 && format == SND_SOC_DAIFMT_I2S) {
  267. /* Use dual-phase frames */
  268. regs->rcr2 |= RPHASE;
  269. regs->xcr2 |= XPHASE;
  270. /* Set 1 word per (McBSP) frame for phase1 and phase2 */
  271. wpf--;
  272. regs->rcr2 |= RFRLEN2(wpf - 1);
  273. regs->xcr2 |= XFRLEN2(wpf - 1);
  274. }
  275. regs->rcr1 |= RFRLEN1(wpf - 1);
  276. regs->xcr1 |= XFRLEN1(wpf - 1);
  277. switch (params_format(params)) {
  278. case SNDRV_PCM_FORMAT_S16_LE:
  279. /* Set word lengths */
  280. wlen = 16;
  281. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
  282. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
  283. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
  284. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
  285. break;
  286. default:
  287. /* Unsupported PCM format */
  288. return -EINVAL;
  289. }
  290. /* In McBSP master modes, FRAME (i.e. sample rate) is generated
  291. * by _counting_ BCLKs. Calculate frame size in BCLKs */
  292. master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  293. if (master == SND_SOC_DAIFMT_CBS_CFS) {
  294. div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
  295. framesize = (mcbsp_data->in_freq / div) / params_rate(params);
  296. if (framesize < wlen * channels) {
  297. printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
  298. "channels\n", __func__);
  299. return -EINVAL;
  300. }
  301. } else
  302. framesize = wlen * channels;
  303. /* Set FS period and length in terms of bit clock periods */
  304. switch (format) {
  305. case SND_SOC_DAIFMT_I2S:
  306. regs->srgr2 |= FPER(framesize - 1);
  307. regs->srgr1 |= FWID((framesize >> 1) - 1);
  308. break;
  309. case SND_SOC_DAIFMT_DSP_A:
  310. case SND_SOC_DAIFMT_DSP_B:
  311. regs->srgr2 |= FPER(framesize - 1);
  312. regs->srgr1 |= FWID(0);
  313. break;
  314. }
  315. omap_mcbsp_config(bus_id, &mcbsp_data->regs);
  316. mcbsp_data->configured = 1;
  317. return 0;
  318. }
  319. /*
  320. * This must be called before _set_clkdiv and _set_sysclk since McBSP register
  321. * cache is initialized here
  322. */
  323. static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  324. unsigned int fmt)
  325. {
  326. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  327. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  328. unsigned int temp_fmt = fmt;
  329. if (mcbsp_data->configured)
  330. return 0;
  331. mcbsp_data->fmt = fmt;
  332. memset(regs, 0, sizeof(*regs));
  333. /* Generic McBSP register settings */
  334. regs->spcr2 |= XINTM(3) | FREE;
  335. regs->spcr1 |= RINTM(3);
  336. /* RFIG and XFIG are not defined in 34xx */
  337. if (!cpu_is_omap34xx()) {
  338. regs->rcr2 |= RFIG;
  339. regs->xcr2 |= XFIG;
  340. }
  341. if (cpu_is_omap2430() || cpu_is_omap34xx()) {
  342. regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
  343. regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
  344. }
  345. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  346. case SND_SOC_DAIFMT_I2S:
  347. /* 1-bit data delay */
  348. regs->rcr2 |= RDATDLY(1);
  349. regs->xcr2 |= XDATDLY(1);
  350. break;
  351. case SND_SOC_DAIFMT_DSP_A:
  352. /* 1-bit data delay */
  353. regs->rcr2 |= RDATDLY(1);
  354. regs->xcr2 |= XDATDLY(1);
  355. /* Invert FS polarity configuration */
  356. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  357. break;
  358. case SND_SOC_DAIFMT_DSP_B:
  359. /* 0-bit data delay */
  360. regs->rcr2 |= RDATDLY(0);
  361. regs->xcr2 |= XDATDLY(0);
  362. /* Invert FS polarity configuration */
  363. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  364. break;
  365. default:
  366. /* Unsupported data format */
  367. return -EINVAL;
  368. }
  369. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  370. case SND_SOC_DAIFMT_CBS_CFS:
  371. /* McBSP master. Set FS and bit clocks as outputs */
  372. regs->pcr0 |= FSXM | FSRM |
  373. CLKXM | CLKRM;
  374. /* Sample rate generator drives the FS */
  375. regs->srgr2 |= FSGM;
  376. break;
  377. case SND_SOC_DAIFMT_CBM_CFM:
  378. /* McBSP slave */
  379. break;
  380. default:
  381. /* Unsupported master/slave configuration */
  382. return -EINVAL;
  383. }
  384. /* Set bit clock (CLKX/CLKR) and FS polarities */
  385. switch (temp_fmt & SND_SOC_DAIFMT_INV_MASK) {
  386. case SND_SOC_DAIFMT_NB_NF:
  387. /*
  388. * Normal BCLK + FS.
  389. * FS active low. TX data driven on falling edge of bit clock
  390. * and RX data sampled on rising edge of bit clock.
  391. */
  392. regs->pcr0 |= FSXP | FSRP |
  393. CLKXP | CLKRP;
  394. break;
  395. case SND_SOC_DAIFMT_NB_IF:
  396. regs->pcr0 |= CLKXP | CLKRP;
  397. break;
  398. case SND_SOC_DAIFMT_IB_NF:
  399. regs->pcr0 |= FSXP | FSRP;
  400. break;
  401. case SND_SOC_DAIFMT_IB_IF:
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. return 0;
  407. }
  408. static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  409. int div_id, int div)
  410. {
  411. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  412. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  413. if (div_id != OMAP_MCBSP_CLKGDV)
  414. return -ENODEV;
  415. mcbsp_data->clk_div = div;
  416. regs->srgr1 |= CLKGDV(div - 1);
  417. return 0;
  418. }
  419. static int omap_mcbsp_dai_set_clks_src(struct omap_mcbsp_data *mcbsp_data,
  420. int clk_id)
  421. {
  422. int sel_bit;
  423. u16 reg, reg_devconf1 = OMAP243X_CONTROL_DEVCONF1;
  424. if (cpu_class_is_omap1()) {
  425. /* OMAP1's can use only external source clock */
  426. if (unlikely(clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK))
  427. return -EINVAL;
  428. else
  429. return 0;
  430. }
  431. if (cpu_is_omap2420() && mcbsp_data->bus_id > 1)
  432. return -EINVAL;
  433. if (cpu_is_omap343x())
  434. reg_devconf1 = OMAP343X_CONTROL_DEVCONF1;
  435. switch (mcbsp_data->bus_id) {
  436. case 0:
  437. reg = OMAP2_CONTROL_DEVCONF0;
  438. sel_bit = 2;
  439. break;
  440. case 1:
  441. reg = OMAP2_CONTROL_DEVCONF0;
  442. sel_bit = 6;
  443. break;
  444. case 2:
  445. reg = reg_devconf1;
  446. sel_bit = 0;
  447. break;
  448. case 3:
  449. reg = reg_devconf1;
  450. sel_bit = 2;
  451. break;
  452. case 4:
  453. reg = reg_devconf1;
  454. sel_bit = 4;
  455. break;
  456. default:
  457. return -EINVAL;
  458. }
  459. if (clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK)
  460. omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
  461. else
  462. omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
  463. return 0;
  464. }
  465. static int omap_mcbsp_dai_set_rcvr_src(struct omap_mcbsp_data *mcbsp_data,
  466. int clk_id)
  467. {
  468. int sel_bit, set = 0;
  469. u16 reg = OMAP2_CONTROL_DEVCONF0;
  470. if (cpu_class_is_omap1())
  471. return -EINVAL; /* TODO: Can this be implemented for OMAP1? */
  472. if (mcbsp_data->bus_id != 0)
  473. return -EINVAL;
  474. switch (clk_id) {
  475. case OMAP_MCBSP_CLKR_SRC_CLKX:
  476. set = 1;
  477. case OMAP_MCBSP_CLKR_SRC_CLKR:
  478. sel_bit = 3;
  479. break;
  480. case OMAP_MCBSP_FSR_SRC_FSX:
  481. set = 1;
  482. case OMAP_MCBSP_FSR_SRC_FSR:
  483. sel_bit = 4;
  484. break;
  485. default:
  486. return -EINVAL;
  487. }
  488. if (set)
  489. omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
  490. else
  491. omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
  492. return 0;
  493. }
  494. static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  495. int clk_id, unsigned int freq,
  496. int dir)
  497. {
  498. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  499. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  500. int err = 0;
  501. mcbsp_data->in_freq = freq;
  502. switch (clk_id) {
  503. case OMAP_MCBSP_SYSCLK_CLK:
  504. regs->srgr2 |= CLKSM;
  505. break;
  506. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  507. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  508. err = omap_mcbsp_dai_set_clks_src(mcbsp_data, clk_id);
  509. break;
  510. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  511. regs->srgr2 |= CLKSM;
  512. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  513. regs->pcr0 |= SCLKME;
  514. break;
  515. case OMAP_MCBSP_CLKR_SRC_CLKR:
  516. case OMAP_MCBSP_CLKR_SRC_CLKX:
  517. case OMAP_MCBSP_FSR_SRC_FSR:
  518. case OMAP_MCBSP_FSR_SRC_FSX:
  519. err = omap_mcbsp_dai_set_rcvr_src(mcbsp_data, clk_id);
  520. break;
  521. default:
  522. err = -ENODEV;
  523. }
  524. return err;
  525. }
  526. static struct snd_soc_dai_ops omap_mcbsp_dai_ops = {
  527. .startup = omap_mcbsp_dai_startup,
  528. .shutdown = omap_mcbsp_dai_shutdown,
  529. .trigger = omap_mcbsp_dai_trigger,
  530. .hw_params = omap_mcbsp_dai_hw_params,
  531. .set_fmt = omap_mcbsp_dai_set_dai_fmt,
  532. .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
  533. .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
  534. };
  535. #define OMAP_MCBSP_DAI_BUILDER(link_id) \
  536. { \
  537. .name = "omap-mcbsp-dai-"#link_id, \
  538. .id = (link_id), \
  539. .playback = { \
  540. .channels_min = 1, \
  541. .channels_max = 16, \
  542. .rates = OMAP_MCBSP_RATES, \
  543. .formats = SNDRV_PCM_FMTBIT_S16_LE, \
  544. }, \
  545. .capture = { \
  546. .channels_min = 1, \
  547. .channels_max = 16, \
  548. .rates = OMAP_MCBSP_RATES, \
  549. .formats = SNDRV_PCM_FMTBIT_S16_LE, \
  550. }, \
  551. .ops = &omap_mcbsp_dai_ops, \
  552. .private_data = &mcbsp_data[(link_id)].bus_id, \
  553. }
  554. struct snd_soc_dai omap_mcbsp_dai[] = {
  555. OMAP_MCBSP_DAI_BUILDER(0),
  556. OMAP_MCBSP_DAI_BUILDER(1),
  557. #if NUM_LINKS >= 3
  558. OMAP_MCBSP_DAI_BUILDER(2),
  559. #endif
  560. #if NUM_LINKS == 5
  561. OMAP_MCBSP_DAI_BUILDER(3),
  562. OMAP_MCBSP_DAI_BUILDER(4),
  563. #endif
  564. };
  565. EXPORT_SYMBOL_GPL(omap_mcbsp_dai);
  566. static int __init snd_omap_mcbsp_init(void)
  567. {
  568. return snd_soc_register_dais(omap_mcbsp_dai,
  569. ARRAY_SIZE(omap_mcbsp_dai));
  570. }
  571. module_init(snd_omap_mcbsp_init);
  572. static void __exit snd_omap_mcbsp_exit(void)
  573. {
  574. snd_soc_unregister_dais(omap_mcbsp_dai, ARRAY_SIZE(omap_mcbsp_dai));
  575. }
  576. module_exit(snd_omap_mcbsp_exit);
  577. MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
  578. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  579. MODULE_LICENSE("GPL");