omap-mcbsp.c 23 KB

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