omap-mcbsp.c 26 KB

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