omap-mcbsp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. #define OMAP_MCBSP_SOC_SINGLE_S16_EXT(xname, xmin, xmax, \
  39. xhandler_get, xhandler_put) \
  40. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  41. .info = omap_mcbsp_st_info_volsw, \
  42. .get = xhandler_get, .put = xhandler_put, \
  43. .private_value = (unsigned long) &(struct soc_mixer_control) \
  44. {.min = xmin, .max = xmax} }
  45. struct omap_mcbsp_data {
  46. unsigned int bus_id;
  47. struct omap_mcbsp_reg_cfg regs;
  48. unsigned int fmt;
  49. /*
  50. * Flags indicating is the bus already activated and configured by
  51. * another substream
  52. */
  53. int active;
  54. int configured;
  55. unsigned int in_freq;
  56. int clk_div;
  57. };
  58. #define to_mcbsp(priv) container_of((priv), struct omap_mcbsp_data, bus_id)
  59. static struct omap_mcbsp_data mcbsp_data[NUM_LINKS];
  60. /*
  61. * Stream DMA parameters. DMA request line and port address are set runtime
  62. * since they are different between OMAP1 and later OMAPs
  63. */
  64. static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2];
  65. #if defined(CONFIG_ARCH_OMAP15XX) || defined(CONFIG_ARCH_OMAP16XX)
  66. static const int omap1_dma_reqs[][2] = {
  67. { OMAP_DMA_MCBSP1_TX, OMAP_DMA_MCBSP1_RX },
  68. { OMAP_DMA_MCBSP2_TX, OMAP_DMA_MCBSP2_RX },
  69. { OMAP_DMA_MCBSP3_TX, OMAP_DMA_MCBSP3_RX },
  70. };
  71. static const unsigned long omap1_mcbsp_port[][2] = {
  72. { OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
  73. OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
  74. { OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
  75. OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
  76. { OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DXR1,
  77. OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DRR1 },
  78. };
  79. #else
  80. static const int omap1_dma_reqs[][2] = {};
  81. static const unsigned long omap1_mcbsp_port[][2] = {};
  82. #endif
  83. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  84. static const int omap24xx_dma_reqs[][2] = {
  85. { OMAP24XX_DMA_MCBSP1_TX, OMAP24XX_DMA_MCBSP1_RX },
  86. { OMAP24XX_DMA_MCBSP2_TX, OMAP24XX_DMA_MCBSP2_RX },
  87. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3)
  88. { OMAP24XX_DMA_MCBSP3_TX, OMAP24XX_DMA_MCBSP3_RX },
  89. { OMAP24XX_DMA_MCBSP4_TX, OMAP24XX_DMA_MCBSP4_RX },
  90. { OMAP24XX_DMA_MCBSP5_TX, OMAP24XX_DMA_MCBSP5_RX },
  91. #endif
  92. };
  93. #else
  94. static const int omap24xx_dma_reqs[][2] = {};
  95. #endif
  96. #if defined(CONFIG_ARCH_OMAP2420)
  97. static const unsigned long omap2420_mcbsp_port[][2] = {
  98. { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
  99. OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
  100. { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
  101. OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
  102. };
  103. #else
  104. static const unsigned long omap2420_mcbsp_port[][2] = {};
  105. #endif
  106. #if defined(CONFIG_ARCH_OMAP2430)
  107. static const unsigned long omap2430_mcbsp_port[][2] = {
  108. { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
  109. OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
  110. { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
  111. OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
  112. { OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
  113. OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
  114. { OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
  115. OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
  116. { OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
  117. OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
  118. };
  119. #else
  120. static const unsigned long omap2430_mcbsp_port[][2] = {};
  121. #endif
  122. #if defined(CONFIG_ARCH_OMAP3)
  123. static const unsigned long omap34xx_mcbsp_port[][2] = {
  124. { OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
  125. OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
  126. { OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
  127. OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
  128. { OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
  129. OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
  130. { OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
  131. OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
  132. { OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
  133. OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
  134. };
  135. #else
  136. static const unsigned long omap34xx_mcbsp_port[][2] = {};
  137. #endif
  138. static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream)
  139. {
  140. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  141. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  142. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  143. int dma_op_mode = omap_mcbsp_get_dma_op_mode(mcbsp_data->bus_id);
  144. int samples;
  145. /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
  146. if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
  147. samples = snd_pcm_lib_period_bytes(substream) >> 1;
  148. else
  149. samples = 1;
  150. /* Configure McBSP internal buffer usage */
  151. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  152. omap_mcbsp_set_tx_threshold(mcbsp_data->bus_id, samples - 1);
  153. else
  154. omap_mcbsp_set_rx_threshold(mcbsp_data->bus_id, samples - 1);
  155. }
  156. static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
  157. struct snd_soc_dai *dai)
  158. {
  159. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  160. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  161. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  162. int bus_id = mcbsp_data->bus_id;
  163. int err = 0;
  164. if (!cpu_dai->active)
  165. err = omap_mcbsp_request(bus_id);
  166. if (cpu_is_omap343x()) {
  167. int dma_op_mode = omap_mcbsp_get_dma_op_mode(bus_id);
  168. int max_period;
  169. /*
  170. * McBSP2 in OMAP3 has 1024 * 32-bit internal audio buffer.
  171. * Set constraint for minimum buffer size to the same than FIFO
  172. * size in order to avoid underruns in playback startup because
  173. * HW is keeping the DMA request active until FIFO is filled.
  174. */
  175. if (bus_id == 1)
  176. snd_pcm_hw_constraint_minmax(substream->runtime,
  177. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  178. 4096, UINT_MAX);
  179. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  180. max_period = omap_mcbsp_get_max_tx_threshold(bus_id);
  181. else
  182. max_period = omap_mcbsp_get_max_rx_threshold(bus_id);
  183. max_period++;
  184. max_period <<= 1;
  185. if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
  186. snd_pcm_hw_constraint_minmax(substream->runtime,
  187. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  188. 32, max_period);
  189. }
  190. return err;
  191. }
  192. static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
  193. struct snd_soc_dai *dai)
  194. {
  195. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  196. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  197. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  198. if (!cpu_dai->active) {
  199. omap_mcbsp_free(mcbsp_data->bus_id);
  200. mcbsp_data->configured = 0;
  201. }
  202. }
  203. static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  204. struct snd_soc_dai *dai)
  205. {
  206. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  207. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  208. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  209. int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  210. switch (cmd) {
  211. case SNDRV_PCM_TRIGGER_START:
  212. case SNDRV_PCM_TRIGGER_RESUME:
  213. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  214. mcbsp_data->active++;
  215. omap_mcbsp_start(mcbsp_data->bus_id, play, !play);
  216. break;
  217. case SNDRV_PCM_TRIGGER_STOP:
  218. case SNDRV_PCM_TRIGGER_SUSPEND:
  219. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  220. omap_mcbsp_stop(mcbsp_data->bus_id, play, !play);
  221. mcbsp_data->active--;
  222. break;
  223. default:
  224. err = -EINVAL;
  225. }
  226. return err;
  227. }
  228. static snd_pcm_sframes_t omap_mcbsp_dai_delay(
  229. struct snd_pcm_substream *substream,
  230. struct snd_soc_dai *dai)
  231. {
  232. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  233. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  234. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  235. u16 fifo_use;
  236. snd_pcm_sframes_t delay;
  237. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  238. fifo_use = omap_mcbsp_get_tx_delay(mcbsp_data->bus_id);
  239. else
  240. fifo_use = omap_mcbsp_get_rx_delay(mcbsp_data->bus_id);
  241. /*
  242. * Divide the used locations with the channel count to get the
  243. * FIFO usage in samples (don't care about partial samples in the
  244. * buffer).
  245. */
  246. delay = fifo_use / substream->runtime->channels;
  247. return delay;
  248. }
  249. static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
  250. struct snd_pcm_hw_params *params,
  251. struct snd_soc_dai *dai)
  252. {
  253. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  254. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  255. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  256. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  257. int dma, bus_id = mcbsp_data->bus_id, id = cpu_dai->id;
  258. int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT;
  259. unsigned long port;
  260. unsigned int format, div, framesize, master;
  261. if (cpu_class_is_omap1()) {
  262. dma = omap1_dma_reqs[bus_id][substream->stream];
  263. port = omap1_mcbsp_port[bus_id][substream->stream];
  264. } else if (cpu_is_omap2420()) {
  265. dma = omap24xx_dma_reqs[bus_id][substream->stream];
  266. port = omap2420_mcbsp_port[bus_id][substream->stream];
  267. } else if (cpu_is_omap2430()) {
  268. dma = omap24xx_dma_reqs[bus_id][substream->stream];
  269. port = omap2430_mcbsp_port[bus_id][substream->stream];
  270. } else if (cpu_is_omap343x()) {
  271. dma = omap24xx_dma_reqs[bus_id][substream->stream];
  272. port = omap34xx_mcbsp_port[bus_id][substream->stream];
  273. omap_mcbsp_dai_dma_params[id][substream->stream].set_threshold =
  274. omap_mcbsp_set_threshold;
  275. /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
  276. if (omap_mcbsp_get_dma_op_mode(bus_id) ==
  277. MCBSP_DMA_MODE_THRESHOLD)
  278. sync_mode = OMAP_DMA_SYNC_FRAME;
  279. } else {
  280. return -ENODEV;
  281. }
  282. omap_mcbsp_dai_dma_params[id][substream->stream].name =
  283. substream->stream ? "Audio Capture" : "Audio Playback";
  284. omap_mcbsp_dai_dma_params[id][substream->stream].dma_req = dma;
  285. omap_mcbsp_dai_dma_params[id][substream->stream].port_addr = port;
  286. omap_mcbsp_dai_dma_params[id][substream->stream].sync_mode = sync_mode;
  287. switch (params_format(params)) {
  288. case SNDRV_PCM_FORMAT_S16_LE:
  289. omap_mcbsp_dai_dma_params[id][substream->stream].data_type =
  290. OMAP_DMA_DATA_TYPE_S16;
  291. break;
  292. case SNDRV_PCM_FORMAT_S32_LE:
  293. omap_mcbsp_dai_dma_params[id][substream->stream].data_type =
  294. OMAP_DMA_DATA_TYPE_S32;
  295. break;
  296. default:
  297. return -EINVAL;
  298. }
  299. snd_soc_dai_set_dma_data(cpu_dai, substream,
  300. &omap_mcbsp_dai_dma_params[id][substream->stream]);
  301. if (mcbsp_data->configured) {
  302. /* McBSP already configured by another stream */
  303. return 0;
  304. }
  305. format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  306. wpf = channels = params_channels(params);
  307. if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
  308. format == SND_SOC_DAIFMT_LEFT_J)) {
  309. /* Use dual-phase frames */
  310. regs->rcr2 |= RPHASE;
  311. regs->xcr2 |= XPHASE;
  312. /* Set 1 word per (McBSP) frame for phase1 and phase2 */
  313. wpf--;
  314. regs->rcr2 |= RFRLEN2(wpf - 1);
  315. regs->xcr2 |= XFRLEN2(wpf - 1);
  316. }
  317. regs->rcr1 |= RFRLEN1(wpf - 1);
  318. regs->xcr1 |= XFRLEN1(wpf - 1);
  319. switch (params_format(params)) {
  320. case SNDRV_PCM_FORMAT_S16_LE:
  321. /* Set word lengths */
  322. wlen = 16;
  323. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
  324. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
  325. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
  326. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
  327. break;
  328. case SNDRV_PCM_FORMAT_S32_LE:
  329. /* Set word lengths */
  330. wlen = 32;
  331. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_32);
  332. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_32);
  333. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_32);
  334. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_32);
  335. break;
  336. default:
  337. /* Unsupported PCM format */
  338. return -EINVAL;
  339. }
  340. /* In McBSP master modes, FRAME (i.e. sample rate) is generated
  341. * by _counting_ BCLKs. Calculate frame size in BCLKs */
  342. master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  343. if (master == SND_SOC_DAIFMT_CBS_CFS) {
  344. div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
  345. framesize = (mcbsp_data->in_freq / div) / params_rate(params);
  346. if (framesize < wlen * channels) {
  347. printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
  348. "channels\n", __func__);
  349. return -EINVAL;
  350. }
  351. } else
  352. framesize = wlen * channels;
  353. /* Set FS period and length in terms of bit clock periods */
  354. switch (format) {
  355. case SND_SOC_DAIFMT_I2S:
  356. case SND_SOC_DAIFMT_LEFT_J:
  357. regs->srgr2 |= FPER(framesize - 1);
  358. regs->srgr1 |= FWID((framesize >> 1) - 1);
  359. break;
  360. case SND_SOC_DAIFMT_DSP_A:
  361. case SND_SOC_DAIFMT_DSP_B:
  362. regs->srgr2 |= FPER(framesize - 1);
  363. regs->srgr1 |= FWID(0);
  364. break;
  365. }
  366. omap_mcbsp_config(bus_id, &mcbsp_data->regs);
  367. mcbsp_data->configured = 1;
  368. return 0;
  369. }
  370. /*
  371. * This must be called before _set_clkdiv and _set_sysclk since McBSP register
  372. * cache is initialized here
  373. */
  374. static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  375. unsigned int fmt)
  376. {
  377. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  378. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  379. unsigned int temp_fmt = fmt;
  380. if (mcbsp_data->configured)
  381. return 0;
  382. mcbsp_data->fmt = fmt;
  383. memset(regs, 0, sizeof(*regs));
  384. /* Generic McBSP register settings */
  385. regs->spcr2 |= XINTM(3) | FREE;
  386. regs->spcr1 |= RINTM(3);
  387. /* RFIG and XFIG are not defined in 34xx */
  388. if (!cpu_is_omap34xx()) {
  389. regs->rcr2 |= RFIG;
  390. regs->xcr2 |= XFIG;
  391. }
  392. if (cpu_is_omap2430() || cpu_is_omap34xx()) {
  393. regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
  394. regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
  395. }
  396. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  397. case SND_SOC_DAIFMT_I2S:
  398. /* 1-bit data delay */
  399. regs->rcr2 |= RDATDLY(1);
  400. regs->xcr2 |= XDATDLY(1);
  401. break;
  402. case SND_SOC_DAIFMT_LEFT_J:
  403. /* 0-bit data delay */
  404. regs->rcr2 |= RDATDLY(0);
  405. regs->xcr2 |= XDATDLY(0);
  406. regs->spcr1 |= RJUST(2);
  407. /* Invert FS polarity configuration */
  408. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  409. break;
  410. case SND_SOC_DAIFMT_DSP_A:
  411. /* 1-bit data delay */
  412. regs->rcr2 |= RDATDLY(1);
  413. regs->xcr2 |= XDATDLY(1);
  414. /* Invert FS polarity configuration */
  415. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  416. break;
  417. case SND_SOC_DAIFMT_DSP_B:
  418. /* 0-bit data delay */
  419. regs->rcr2 |= RDATDLY(0);
  420. regs->xcr2 |= XDATDLY(0);
  421. /* Invert FS polarity configuration */
  422. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  423. break;
  424. default:
  425. /* Unsupported data format */
  426. return -EINVAL;
  427. }
  428. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  429. case SND_SOC_DAIFMT_CBS_CFS:
  430. /* McBSP master. Set FS and bit clocks as outputs */
  431. regs->pcr0 |= FSXM | FSRM |
  432. CLKXM | CLKRM;
  433. /* Sample rate generator drives the FS */
  434. regs->srgr2 |= FSGM;
  435. break;
  436. case SND_SOC_DAIFMT_CBM_CFM:
  437. /* McBSP slave */
  438. break;
  439. default:
  440. /* Unsupported master/slave configuration */
  441. return -EINVAL;
  442. }
  443. /* Set bit clock (CLKX/CLKR) and FS polarities */
  444. switch (temp_fmt & SND_SOC_DAIFMT_INV_MASK) {
  445. case SND_SOC_DAIFMT_NB_NF:
  446. /*
  447. * Normal BCLK + FS.
  448. * FS active low. TX data driven on falling edge of bit clock
  449. * and RX data sampled on rising edge of bit clock.
  450. */
  451. regs->pcr0 |= FSXP | FSRP |
  452. CLKXP | CLKRP;
  453. break;
  454. case SND_SOC_DAIFMT_NB_IF:
  455. regs->pcr0 |= CLKXP | CLKRP;
  456. break;
  457. case SND_SOC_DAIFMT_IB_NF:
  458. regs->pcr0 |= FSXP | FSRP;
  459. break;
  460. case SND_SOC_DAIFMT_IB_IF:
  461. break;
  462. default:
  463. return -EINVAL;
  464. }
  465. return 0;
  466. }
  467. static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  468. int div_id, int div)
  469. {
  470. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  471. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  472. if (div_id != OMAP_MCBSP_CLKGDV)
  473. return -ENODEV;
  474. mcbsp_data->clk_div = div;
  475. regs->srgr1 |= CLKGDV(div - 1);
  476. return 0;
  477. }
  478. static int omap_mcbsp_dai_set_clks_src(struct omap_mcbsp_data *mcbsp_data,
  479. int clk_id)
  480. {
  481. int sel_bit;
  482. u16 reg, reg_devconf1 = OMAP243X_CONTROL_DEVCONF1;
  483. if (cpu_class_is_omap1()) {
  484. /* OMAP1's can use only external source clock */
  485. if (unlikely(clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK))
  486. return -EINVAL;
  487. else
  488. return 0;
  489. }
  490. if (cpu_is_omap2420() && mcbsp_data->bus_id > 1)
  491. return -EINVAL;
  492. if (cpu_is_omap343x())
  493. reg_devconf1 = OMAP343X_CONTROL_DEVCONF1;
  494. switch (mcbsp_data->bus_id) {
  495. case 0:
  496. reg = OMAP2_CONTROL_DEVCONF0;
  497. sel_bit = 2;
  498. break;
  499. case 1:
  500. reg = OMAP2_CONTROL_DEVCONF0;
  501. sel_bit = 6;
  502. break;
  503. case 2:
  504. reg = reg_devconf1;
  505. sel_bit = 0;
  506. break;
  507. case 3:
  508. reg = reg_devconf1;
  509. sel_bit = 2;
  510. break;
  511. case 4:
  512. reg = reg_devconf1;
  513. sel_bit = 4;
  514. break;
  515. default:
  516. return -EINVAL;
  517. }
  518. if (clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK)
  519. omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
  520. else
  521. omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
  522. return 0;
  523. }
  524. static int omap_mcbsp_dai_set_rcvr_src(struct omap_mcbsp_data *mcbsp_data,
  525. int clk_id)
  526. {
  527. int sel_bit, set = 0;
  528. u16 reg = OMAP2_CONTROL_DEVCONF0;
  529. if (cpu_class_is_omap1())
  530. return -EINVAL; /* TODO: Can this be implemented for OMAP1? */
  531. if (mcbsp_data->bus_id != 0)
  532. return -EINVAL;
  533. switch (clk_id) {
  534. case OMAP_MCBSP_CLKR_SRC_CLKX:
  535. set = 1;
  536. case OMAP_MCBSP_CLKR_SRC_CLKR:
  537. sel_bit = 3;
  538. break;
  539. case OMAP_MCBSP_FSR_SRC_FSX:
  540. set = 1;
  541. case OMAP_MCBSP_FSR_SRC_FSR:
  542. sel_bit = 4;
  543. break;
  544. default:
  545. return -EINVAL;
  546. }
  547. if (set)
  548. omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
  549. else
  550. omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
  551. return 0;
  552. }
  553. static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  554. int clk_id, unsigned int freq,
  555. int dir)
  556. {
  557. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  558. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  559. int err = 0;
  560. mcbsp_data->in_freq = freq;
  561. switch (clk_id) {
  562. case OMAP_MCBSP_SYSCLK_CLK:
  563. regs->srgr2 |= CLKSM;
  564. break;
  565. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  566. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  567. err = omap_mcbsp_dai_set_clks_src(mcbsp_data, clk_id);
  568. break;
  569. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  570. regs->srgr2 |= CLKSM;
  571. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  572. regs->pcr0 |= SCLKME;
  573. break;
  574. case OMAP_MCBSP_CLKR_SRC_CLKR:
  575. case OMAP_MCBSP_CLKR_SRC_CLKX:
  576. case OMAP_MCBSP_FSR_SRC_FSR:
  577. case OMAP_MCBSP_FSR_SRC_FSX:
  578. err = omap_mcbsp_dai_set_rcvr_src(mcbsp_data, clk_id);
  579. break;
  580. default:
  581. err = -ENODEV;
  582. }
  583. return err;
  584. }
  585. static struct snd_soc_dai_ops omap_mcbsp_dai_ops = {
  586. .startup = omap_mcbsp_dai_startup,
  587. .shutdown = omap_mcbsp_dai_shutdown,
  588. .trigger = omap_mcbsp_dai_trigger,
  589. .delay = omap_mcbsp_dai_delay,
  590. .hw_params = omap_mcbsp_dai_hw_params,
  591. .set_fmt = omap_mcbsp_dai_set_dai_fmt,
  592. .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
  593. .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
  594. };
  595. #define OMAP_MCBSP_DAI_BUILDER(link_id) \
  596. { \
  597. .name = "omap-mcbsp-dai-"#link_id, \
  598. .id = (link_id), \
  599. .playback = { \
  600. .channels_min = 1, \
  601. .channels_max = 16, \
  602. .rates = OMAP_MCBSP_RATES, \
  603. .formats = SNDRV_PCM_FMTBIT_S16_LE | \
  604. SNDRV_PCM_FMTBIT_S32_LE, \
  605. }, \
  606. .capture = { \
  607. .channels_min = 1, \
  608. .channels_max = 16, \
  609. .rates = OMAP_MCBSP_RATES, \
  610. .formats = SNDRV_PCM_FMTBIT_S16_LE | \
  611. SNDRV_PCM_FMTBIT_S32_LE, \
  612. }, \
  613. .ops = &omap_mcbsp_dai_ops, \
  614. .private_data = &mcbsp_data[(link_id)].bus_id, \
  615. }
  616. struct snd_soc_dai omap_mcbsp_dai[] = {
  617. OMAP_MCBSP_DAI_BUILDER(0),
  618. OMAP_MCBSP_DAI_BUILDER(1),
  619. #if NUM_LINKS >= 3
  620. OMAP_MCBSP_DAI_BUILDER(2),
  621. #endif
  622. #if NUM_LINKS == 5
  623. OMAP_MCBSP_DAI_BUILDER(3),
  624. OMAP_MCBSP_DAI_BUILDER(4),
  625. #endif
  626. };
  627. EXPORT_SYMBOL_GPL(omap_mcbsp_dai);
  628. int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
  629. struct snd_ctl_elem_info *uinfo)
  630. {
  631. struct soc_mixer_control *mc =
  632. (struct soc_mixer_control *)kcontrol->private_value;
  633. int max = mc->max;
  634. int min = mc->min;
  635. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  636. uinfo->count = 1;
  637. uinfo->value.integer.min = min;
  638. uinfo->value.integer.max = max;
  639. return 0;
  640. }
  641. #define OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(id, channel) \
  642. static int \
  643. omap_mcbsp##id##_set_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  644. struct snd_ctl_elem_value *uc) \
  645. { \
  646. struct soc_mixer_control *mc = \
  647. (struct soc_mixer_control *)kc->private_value; \
  648. int max = mc->max; \
  649. int min = mc->min; \
  650. int val = uc->value.integer.value[0]; \
  651. \
  652. if (val < min || val > max) \
  653. return -EINVAL; \
  654. \
  655. /* OMAP McBSP implementation uses index values 0..4 */ \
  656. return omap_st_set_chgain((id)-1, channel, val); \
  657. }
  658. #define OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(id, channel) \
  659. static int \
  660. omap_mcbsp##id##_get_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  661. struct snd_ctl_elem_value *uc) \
  662. { \
  663. s16 chgain; \
  664. \
  665. if (omap_st_get_chgain((id)-1, channel, &chgain)) \
  666. return -EAGAIN; \
  667. \
  668. uc->value.integer.value[0] = chgain; \
  669. return 0; \
  670. }
  671. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 0)
  672. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 1)
  673. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 0)
  674. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 1)
  675. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 0)
  676. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 1)
  677. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 0)
  678. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 1)
  679. static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
  680. struct snd_ctl_elem_value *ucontrol)
  681. {
  682. struct soc_mixer_control *mc =
  683. (struct soc_mixer_control *)kcontrol->private_value;
  684. u8 value = ucontrol->value.integer.value[0];
  685. if (value == omap_st_is_enabled(mc->reg))
  686. return 0;
  687. if (value)
  688. omap_st_enable(mc->reg);
  689. else
  690. omap_st_disable(mc->reg);
  691. return 1;
  692. }
  693. static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
  694. struct snd_ctl_elem_value *ucontrol)
  695. {
  696. struct soc_mixer_control *mc =
  697. (struct soc_mixer_control *)kcontrol->private_value;
  698. ucontrol->value.integer.value[0] = omap_st_is_enabled(mc->reg);
  699. return 0;
  700. }
  701. static const struct snd_kcontrol_new omap_mcbsp2_st_controls[] = {
  702. SOC_SINGLE_EXT("McBSP2 Sidetone Switch", 1, 0, 1, 0,
  703. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
  704. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 0 Volume",
  705. -32768, 32767,
  706. omap_mcbsp2_get_st_ch0_volume,
  707. omap_mcbsp2_set_st_ch0_volume),
  708. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 1 Volume",
  709. -32768, 32767,
  710. omap_mcbsp2_get_st_ch1_volume,
  711. omap_mcbsp2_set_st_ch1_volume),
  712. };
  713. static const struct snd_kcontrol_new omap_mcbsp3_st_controls[] = {
  714. SOC_SINGLE_EXT("McBSP3 Sidetone Switch", 2, 0, 1, 0,
  715. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
  716. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 0 Volume",
  717. -32768, 32767,
  718. omap_mcbsp3_get_st_ch0_volume,
  719. omap_mcbsp3_set_st_ch0_volume),
  720. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 1 Volume",
  721. -32768, 32767,
  722. omap_mcbsp3_get_st_ch1_volume,
  723. omap_mcbsp3_set_st_ch1_volume),
  724. };
  725. int omap_mcbsp_st_add_controls(struct snd_soc_codec *codec, int mcbsp_id)
  726. {
  727. if (!cpu_is_omap34xx())
  728. return -ENODEV;
  729. switch (mcbsp_id) {
  730. case 1: /* McBSP 2 */
  731. return snd_soc_add_controls(codec, omap_mcbsp2_st_controls,
  732. ARRAY_SIZE(omap_mcbsp2_st_controls));
  733. case 2: /* McBSP 3 */
  734. return snd_soc_add_controls(codec, omap_mcbsp3_st_controls,
  735. ARRAY_SIZE(omap_mcbsp3_st_controls));
  736. default:
  737. break;
  738. }
  739. return -EINVAL;
  740. }
  741. EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
  742. static int __init snd_omap_mcbsp_init(void)
  743. {
  744. return snd_soc_register_dais(omap_mcbsp_dai,
  745. ARRAY_SIZE(omap_mcbsp_dai));
  746. }
  747. module_init(snd_omap_mcbsp_init);
  748. static void __exit snd_omap_mcbsp_exit(void)
  749. {
  750. snd_soc_unregister_dais(omap_mcbsp_dai, ARRAY_SIZE(omap_mcbsp_dai));
  751. }
  752. module_exit(snd_omap_mcbsp_exit);
  753. MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
  754. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  755. MODULE_LICENSE("GPL");