omap-mcbsp.c 22 KB

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