omap-mcbsp.c 22 KB

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