omap-mcbsp.c 18 KB

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