omap-mcbsp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. omap_mcbsp_dai_dma_params[id][substream->stream].data_type =
  288. OMAP_DMA_DATA_TYPE_S16;
  289. snd_soc_dai_set_dma_data(cpu_dai, substream,
  290. &omap_mcbsp_dai_dma_params[id][substream->stream]);
  291. if (mcbsp_data->configured) {
  292. /* McBSP already configured by another stream */
  293. return 0;
  294. }
  295. format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  296. wpf = channels = params_channels(params);
  297. if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
  298. format == SND_SOC_DAIFMT_LEFT_J)) {
  299. /* Use dual-phase frames */
  300. regs->rcr2 |= RPHASE;
  301. regs->xcr2 |= XPHASE;
  302. /* Set 1 word per (McBSP) frame for phase1 and phase2 */
  303. wpf--;
  304. regs->rcr2 |= RFRLEN2(wpf - 1);
  305. regs->xcr2 |= XFRLEN2(wpf - 1);
  306. }
  307. regs->rcr1 |= RFRLEN1(wpf - 1);
  308. regs->xcr1 |= XFRLEN1(wpf - 1);
  309. switch (params_format(params)) {
  310. case SNDRV_PCM_FORMAT_S16_LE:
  311. /* Set word lengths */
  312. wlen = 16;
  313. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
  314. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
  315. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
  316. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
  317. break;
  318. default:
  319. /* Unsupported PCM format */
  320. return -EINVAL;
  321. }
  322. /* In McBSP master modes, FRAME (i.e. sample rate) is generated
  323. * by _counting_ BCLKs. Calculate frame size in BCLKs */
  324. master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  325. if (master == SND_SOC_DAIFMT_CBS_CFS) {
  326. div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
  327. framesize = (mcbsp_data->in_freq / div) / params_rate(params);
  328. if (framesize < wlen * channels) {
  329. printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
  330. "channels\n", __func__);
  331. return -EINVAL;
  332. }
  333. } else
  334. framesize = wlen * channels;
  335. /* Set FS period and length in terms of bit clock periods */
  336. switch (format) {
  337. case SND_SOC_DAIFMT_I2S:
  338. case SND_SOC_DAIFMT_LEFT_J:
  339. regs->srgr2 |= FPER(framesize - 1);
  340. regs->srgr1 |= FWID((framesize >> 1) - 1);
  341. break;
  342. case SND_SOC_DAIFMT_DSP_A:
  343. case SND_SOC_DAIFMT_DSP_B:
  344. regs->srgr2 |= FPER(framesize - 1);
  345. regs->srgr1 |= FWID(0);
  346. break;
  347. }
  348. omap_mcbsp_config(bus_id, &mcbsp_data->regs);
  349. mcbsp_data->configured = 1;
  350. return 0;
  351. }
  352. /*
  353. * This must be called before _set_clkdiv and _set_sysclk since McBSP register
  354. * cache is initialized here
  355. */
  356. static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  357. unsigned int fmt)
  358. {
  359. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  360. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  361. unsigned int temp_fmt = fmt;
  362. if (mcbsp_data->configured)
  363. return 0;
  364. mcbsp_data->fmt = fmt;
  365. memset(regs, 0, sizeof(*regs));
  366. /* Generic McBSP register settings */
  367. regs->spcr2 |= XINTM(3) | FREE;
  368. regs->spcr1 |= RINTM(3);
  369. /* RFIG and XFIG are not defined in 34xx */
  370. if (!cpu_is_omap34xx()) {
  371. regs->rcr2 |= RFIG;
  372. regs->xcr2 |= XFIG;
  373. }
  374. if (cpu_is_omap2430() || cpu_is_omap34xx()) {
  375. regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
  376. regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
  377. }
  378. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  379. case SND_SOC_DAIFMT_I2S:
  380. /* 1-bit data delay */
  381. regs->rcr2 |= RDATDLY(1);
  382. regs->xcr2 |= XDATDLY(1);
  383. break;
  384. case SND_SOC_DAIFMT_LEFT_J:
  385. /* 0-bit data delay */
  386. regs->rcr2 |= RDATDLY(0);
  387. regs->xcr2 |= XDATDLY(0);
  388. regs->spcr1 |= RJUST(2);
  389. /* Invert FS polarity configuration */
  390. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  391. break;
  392. case SND_SOC_DAIFMT_DSP_A:
  393. /* 1-bit data delay */
  394. regs->rcr2 |= RDATDLY(1);
  395. regs->xcr2 |= XDATDLY(1);
  396. /* Invert FS polarity configuration */
  397. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  398. break;
  399. case SND_SOC_DAIFMT_DSP_B:
  400. /* 0-bit data delay */
  401. regs->rcr2 |= RDATDLY(0);
  402. regs->xcr2 |= XDATDLY(0);
  403. /* Invert FS polarity configuration */
  404. temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
  405. break;
  406. default:
  407. /* Unsupported data format */
  408. return -EINVAL;
  409. }
  410. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  411. case SND_SOC_DAIFMT_CBS_CFS:
  412. /* McBSP master. Set FS and bit clocks as outputs */
  413. regs->pcr0 |= FSXM | FSRM |
  414. CLKXM | CLKRM;
  415. /* Sample rate generator drives the FS */
  416. regs->srgr2 |= FSGM;
  417. break;
  418. case SND_SOC_DAIFMT_CBM_CFM:
  419. /* McBSP slave */
  420. break;
  421. default:
  422. /* Unsupported master/slave configuration */
  423. return -EINVAL;
  424. }
  425. /* Set bit clock (CLKX/CLKR) and FS polarities */
  426. switch (temp_fmt & SND_SOC_DAIFMT_INV_MASK) {
  427. case SND_SOC_DAIFMT_NB_NF:
  428. /*
  429. * Normal BCLK + FS.
  430. * FS active low. TX data driven on falling edge of bit clock
  431. * and RX data sampled on rising edge of bit clock.
  432. */
  433. regs->pcr0 |= FSXP | FSRP |
  434. CLKXP | CLKRP;
  435. break;
  436. case SND_SOC_DAIFMT_NB_IF:
  437. regs->pcr0 |= CLKXP | CLKRP;
  438. break;
  439. case SND_SOC_DAIFMT_IB_NF:
  440. regs->pcr0 |= FSXP | FSRP;
  441. break;
  442. case SND_SOC_DAIFMT_IB_IF:
  443. break;
  444. default:
  445. return -EINVAL;
  446. }
  447. return 0;
  448. }
  449. static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  450. int div_id, int div)
  451. {
  452. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  453. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  454. if (div_id != OMAP_MCBSP_CLKGDV)
  455. return -ENODEV;
  456. mcbsp_data->clk_div = div;
  457. regs->srgr1 |= CLKGDV(div - 1);
  458. return 0;
  459. }
  460. static int omap_mcbsp_dai_set_clks_src(struct omap_mcbsp_data *mcbsp_data,
  461. int clk_id)
  462. {
  463. int sel_bit;
  464. u16 reg, reg_devconf1 = OMAP243X_CONTROL_DEVCONF1;
  465. if (cpu_class_is_omap1()) {
  466. /* OMAP1's can use only external source clock */
  467. if (unlikely(clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK))
  468. return -EINVAL;
  469. else
  470. return 0;
  471. }
  472. if (cpu_is_omap2420() && mcbsp_data->bus_id > 1)
  473. return -EINVAL;
  474. if (cpu_is_omap343x())
  475. reg_devconf1 = OMAP343X_CONTROL_DEVCONF1;
  476. switch (mcbsp_data->bus_id) {
  477. case 0:
  478. reg = OMAP2_CONTROL_DEVCONF0;
  479. sel_bit = 2;
  480. break;
  481. case 1:
  482. reg = OMAP2_CONTROL_DEVCONF0;
  483. sel_bit = 6;
  484. break;
  485. case 2:
  486. reg = reg_devconf1;
  487. sel_bit = 0;
  488. break;
  489. case 3:
  490. reg = reg_devconf1;
  491. sel_bit = 2;
  492. break;
  493. case 4:
  494. reg = reg_devconf1;
  495. sel_bit = 4;
  496. break;
  497. default:
  498. return -EINVAL;
  499. }
  500. if (clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK)
  501. omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
  502. else
  503. omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
  504. return 0;
  505. }
  506. static int omap_mcbsp_dai_set_rcvr_src(struct omap_mcbsp_data *mcbsp_data,
  507. int clk_id)
  508. {
  509. int sel_bit, set = 0;
  510. u16 reg = OMAP2_CONTROL_DEVCONF0;
  511. if (cpu_class_is_omap1())
  512. return -EINVAL; /* TODO: Can this be implemented for OMAP1? */
  513. if (mcbsp_data->bus_id != 0)
  514. return -EINVAL;
  515. switch (clk_id) {
  516. case OMAP_MCBSP_CLKR_SRC_CLKX:
  517. set = 1;
  518. case OMAP_MCBSP_CLKR_SRC_CLKR:
  519. sel_bit = 3;
  520. break;
  521. case OMAP_MCBSP_FSR_SRC_FSX:
  522. set = 1;
  523. case OMAP_MCBSP_FSR_SRC_FSR:
  524. sel_bit = 4;
  525. break;
  526. default:
  527. return -EINVAL;
  528. }
  529. if (set)
  530. omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
  531. else
  532. omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
  533. return 0;
  534. }
  535. static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  536. int clk_id, unsigned int freq,
  537. int dir)
  538. {
  539. struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
  540. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  541. int err = 0;
  542. mcbsp_data->in_freq = freq;
  543. switch (clk_id) {
  544. case OMAP_MCBSP_SYSCLK_CLK:
  545. regs->srgr2 |= CLKSM;
  546. break;
  547. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  548. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  549. err = omap_mcbsp_dai_set_clks_src(mcbsp_data, clk_id);
  550. break;
  551. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  552. regs->srgr2 |= CLKSM;
  553. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  554. regs->pcr0 |= SCLKME;
  555. break;
  556. case OMAP_MCBSP_CLKR_SRC_CLKR:
  557. case OMAP_MCBSP_CLKR_SRC_CLKX:
  558. case OMAP_MCBSP_FSR_SRC_FSR:
  559. case OMAP_MCBSP_FSR_SRC_FSX:
  560. err = omap_mcbsp_dai_set_rcvr_src(mcbsp_data, clk_id);
  561. break;
  562. default:
  563. err = -ENODEV;
  564. }
  565. return err;
  566. }
  567. static struct snd_soc_dai_ops omap_mcbsp_dai_ops = {
  568. .startup = omap_mcbsp_dai_startup,
  569. .shutdown = omap_mcbsp_dai_shutdown,
  570. .trigger = omap_mcbsp_dai_trigger,
  571. .delay = omap_mcbsp_dai_delay,
  572. .hw_params = omap_mcbsp_dai_hw_params,
  573. .set_fmt = omap_mcbsp_dai_set_dai_fmt,
  574. .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
  575. .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
  576. };
  577. #define OMAP_MCBSP_DAI_BUILDER(link_id) \
  578. { \
  579. .name = "omap-mcbsp-dai-"#link_id, \
  580. .id = (link_id), \
  581. .playback = { \
  582. .channels_min = 1, \
  583. .channels_max = 16, \
  584. .rates = OMAP_MCBSP_RATES, \
  585. .formats = SNDRV_PCM_FMTBIT_S16_LE, \
  586. }, \
  587. .capture = { \
  588. .channels_min = 1, \
  589. .channels_max = 16, \
  590. .rates = OMAP_MCBSP_RATES, \
  591. .formats = SNDRV_PCM_FMTBIT_S16_LE, \
  592. }, \
  593. .ops = &omap_mcbsp_dai_ops, \
  594. .private_data = &mcbsp_data[(link_id)].bus_id, \
  595. }
  596. struct snd_soc_dai omap_mcbsp_dai[] = {
  597. OMAP_MCBSP_DAI_BUILDER(0),
  598. OMAP_MCBSP_DAI_BUILDER(1),
  599. #if NUM_LINKS >= 3
  600. OMAP_MCBSP_DAI_BUILDER(2),
  601. #endif
  602. #if NUM_LINKS == 5
  603. OMAP_MCBSP_DAI_BUILDER(3),
  604. OMAP_MCBSP_DAI_BUILDER(4),
  605. #endif
  606. };
  607. EXPORT_SYMBOL_GPL(omap_mcbsp_dai);
  608. int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
  609. struct snd_ctl_elem_info *uinfo)
  610. {
  611. struct soc_mixer_control *mc =
  612. (struct soc_mixer_control *)kcontrol->private_value;
  613. int max = mc->max;
  614. int min = mc->min;
  615. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  616. uinfo->count = 1;
  617. uinfo->value.integer.min = min;
  618. uinfo->value.integer.max = max;
  619. return 0;
  620. }
  621. #define OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(id, channel) \
  622. static int \
  623. omap_mcbsp##id##_set_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  624. struct snd_ctl_elem_value *uc) \
  625. { \
  626. struct soc_mixer_control *mc = \
  627. (struct soc_mixer_control *)kc->private_value; \
  628. int max = mc->max; \
  629. int min = mc->min; \
  630. int val = uc->value.integer.value[0]; \
  631. \
  632. if (val < min || val > max) \
  633. return -EINVAL; \
  634. \
  635. /* OMAP McBSP implementation uses index values 0..4 */ \
  636. return omap_st_set_chgain((id)-1, channel, val); \
  637. }
  638. #define OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(id, channel) \
  639. static int \
  640. omap_mcbsp##id##_get_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  641. struct snd_ctl_elem_value *uc) \
  642. { \
  643. s16 chgain; \
  644. \
  645. if (omap_st_get_chgain((id)-1, channel, &chgain)) \
  646. return -EAGAIN; \
  647. \
  648. uc->value.integer.value[0] = chgain; \
  649. return 0; \
  650. }
  651. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 0)
  652. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 1)
  653. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 0)
  654. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 1)
  655. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 0)
  656. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 1)
  657. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 0)
  658. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 1)
  659. static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
  660. struct snd_ctl_elem_value *ucontrol)
  661. {
  662. struct soc_mixer_control *mc =
  663. (struct soc_mixer_control *)kcontrol->private_value;
  664. u8 value = ucontrol->value.integer.value[0];
  665. if (value == omap_st_is_enabled(mc->reg))
  666. return 0;
  667. if (value)
  668. omap_st_enable(mc->reg);
  669. else
  670. omap_st_disable(mc->reg);
  671. return 1;
  672. }
  673. static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
  674. struct snd_ctl_elem_value *ucontrol)
  675. {
  676. struct soc_mixer_control *mc =
  677. (struct soc_mixer_control *)kcontrol->private_value;
  678. ucontrol->value.integer.value[0] = omap_st_is_enabled(mc->reg);
  679. return 0;
  680. }
  681. static const struct snd_kcontrol_new omap_mcbsp2_st_controls[] = {
  682. SOC_SINGLE_EXT("McBSP2 Sidetone Switch", 1, 0, 1, 0,
  683. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
  684. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 0 Volume",
  685. -32768, 32767,
  686. omap_mcbsp2_get_st_ch0_volume,
  687. omap_mcbsp2_set_st_ch0_volume),
  688. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 1 Volume",
  689. -32768, 32767,
  690. omap_mcbsp2_get_st_ch1_volume,
  691. omap_mcbsp2_set_st_ch1_volume),
  692. };
  693. static const struct snd_kcontrol_new omap_mcbsp3_st_controls[] = {
  694. SOC_SINGLE_EXT("McBSP3 Sidetone Switch", 2, 0, 1, 0,
  695. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
  696. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 0 Volume",
  697. -32768, 32767,
  698. omap_mcbsp3_get_st_ch0_volume,
  699. omap_mcbsp3_set_st_ch0_volume),
  700. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 1 Volume",
  701. -32768, 32767,
  702. omap_mcbsp3_get_st_ch1_volume,
  703. omap_mcbsp3_set_st_ch1_volume),
  704. };
  705. int omap_mcbsp_st_add_controls(struct snd_soc_codec *codec, int mcbsp_id)
  706. {
  707. if (!cpu_is_omap34xx())
  708. return -ENODEV;
  709. switch (mcbsp_id) {
  710. case 1: /* McBSP 2 */
  711. return snd_soc_add_controls(codec, omap_mcbsp2_st_controls,
  712. ARRAY_SIZE(omap_mcbsp2_st_controls));
  713. case 2: /* McBSP 3 */
  714. return snd_soc_add_controls(codec, omap_mcbsp3_st_controls,
  715. ARRAY_SIZE(omap_mcbsp3_st_controls));
  716. default:
  717. break;
  718. }
  719. return -EINVAL;
  720. }
  721. EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
  722. static int __init snd_omap_mcbsp_init(void)
  723. {
  724. return snd_soc_register_dais(omap_mcbsp_dai,
  725. ARRAY_SIZE(omap_mcbsp_dai));
  726. }
  727. module_init(snd_omap_mcbsp_init);
  728. static void __exit snd_omap_mcbsp_exit(void)
  729. {
  730. snd_soc_unregister_dais(omap_mcbsp_dai, ARRAY_SIZE(omap_mcbsp_dai));
  731. }
  732. module_exit(snd_omap_mcbsp_exit);
  733. MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
  734. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  735. MODULE_LICENSE("GPL");