omap-mcbsp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 <jarkko.nikula@bitmer.com>
  7. * Peter Ujfalusi <peter.ujfalusi@ti.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/dma.h>
  33. #include <plat/mcbsp.h>
  34. #include "omap-mcbsp.h"
  35. #include "omap-pcm.h"
  36. #define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000)
  37. #define OMAP_MCBSP_SOC_SINGLE_S16_EXT(xname, xmin, xmax, \
  38. xhandler_get, xhandler_put) \
  39. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  40. .info = omap_mcbsp_st_info_volsw, \
  41. .get = xhandler_get, .put = xhandler_put, \
  42. .private_value = (unsigned long) &(struct soc_mixer_control) \
  43. {.min = xmin, .max = xmax} }
  44. struct omap_mcbsp_data {
  45. unsigned int bus_id;
  46. struct omap_mcbsp_reg_cfg regs;
  47. unsigned int fmt;
  48. /*
  49. * Flags indicating is the bus already activated and configured by
  50. * another substream
  51. */
  52. int active;
  53. int configured;
  54. unsigned int in_freq;
  55. int clk_div;
  56. int wlen;
  57. };
  58. static struct omap_mcbsp_data mcbsp_data[NUM_LINKS];
  59. /*
  60. * Stream DMA parameters. DMA request line and port address are set runtime
  61. * since they are different between OMAP1 and later OMAPs
  62. */
  63. static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2];
  64. static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream)
  65. {
  66. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  67. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  68. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  69. struct omap_pcm_dma_data *dma_data;
  70. int dma_op_mode = omap_mcbsp_get_dma_op_mode(mcbsp_data->bus_id);
  71. int words;
  72. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  73. /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
  74. if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
  75. /*
  76. * Configure McBSP threshold based on either:
  77. * packet_size, when the sDMA is in packet mode, or
  78. * based on the period size.
  79. */
  80. if (dma_data->packet_size)
  81. words = dma_data->packet_size;
  82. else
  83. words = snd_pcm_lib_period_bytes(substream) /
  84. (mcbsp_data->wlen / 8);
  85. else
  86. words = 1;
  87. /* Configure McBSP internal buffer usage */
  88. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  89. omap_mcbsp_set_tx_threshold(mcbsp_data->bus_id, words);
  90. else
  91. omap_mcbsp_set_rx_threshold(mcbsp_data->bus_id, words);
  92. }
  93. static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params,
  94. struct snd_pcm_hw_rule *rule)
  95. {
  96. struct snd_interval *buffer_size = hw_param_interval(params,
  97. SNDRV_PCM_HW_PARAM_BUFFER_SIZE);
  98. struct snd_interval *channels = hw_param_interval(params,
  99. SNDRV_PCM_HW_PARAM_CHANNELS);
  100. struct omap_mcbsp_data *mcbsp_data = rule->private;
  101. struct snd_interval frames;
  102. int size;
  103. snd_interval_any(&frames);
  104. size = omap_mcbsp_get_fifo_size(mcbsp_data->bus_id);
  105. frames.min = size / channels->min;
  106. frames.integer = 1;
  107. return snd_interval_refine(buffer_size, &frames);
  108. }
  109. static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
  110. struct snd_soc_dai *cpu_dai)
  111. {
  112. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  113. int bus_id = mcbsp_data->bus_id;
  114. int err = 0;
  115. if (!cpu_dai->active)
  116. err = omap_mcbsp_request(bus_id);
  117. /*
  118. * OMAP3 McBSP FIFO is word structured.
  119. * McBSP2 has 1024 + 256 = 1280 word long buffer,
  120. * McBSP1,3,4,5 has 128 word long buffer
  121. * This means that the size of the FIFO depends on the sample format.
  122. * For example on McBSP3:
  123. * 16bit samples: size is 128 * 2 = 256 bytes
  124. * 32bit samples: size is 128 * 4 = 512 bytes
  125. * It is simpler to place constraint for buffer and period based on
  126. * channels.
  127. * McBSP3 as example again (16 or 32 bit samples):
  128. * 1 channel (mono): size is 128 frames (128 words)
  129. * 2 channels (stereo): size is 128 / 2 = 64 frames (2 * 64 words)
  130. * 4 channels: size is 128 / 4 = 32 frames (4 * 32 words)
  131. */
  132. if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  133. /*
  134. * Rule for the buffer size. We should not allow
  135. * smaller buffer than the FIFO size to avoid underruns
  136. */
  137. snd_pcm_hw_rule_add(substream->runtime, 0,
  138. SNDRV_PCM_HW_PARAM_CHANNELS,
  139. omap_mcbsp_hwrule_min_buffersize,
  140. mcbsp_data,
  141. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
  142. /* Make sure, that the period size is always even */
  143. snd_pcm_hw_constraint_step(substream->runtime, 0,
  144. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
  145. }
  146. return err;
  147. }
  148. static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
  149. struct snd_soc_dai *cpu_dai)
  150. {
  151. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  152. if (!cpu_dai->active) {
  153. omap_mcbsp_free(mcbsp_data->bus_id);
  154. mcbsp_data->configured = 0;
  155. }
  156. }
  157. static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  158. struct snd_soc_dai *cpu_dai)
  159. {
  160. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  161. int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  162. switch (cmd) {
  163. case SNDRV_PCM_TRIGGER_START:
  164. case SNDRV_PCM_TRIGGER_RESUME:
  165. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  166. mcbsp_data->active++;
  167. omap_mcbsp_start(mcbsp_data->bus_id, play, !play);
  168. break;
  169. case SNDRV_PCM_TRIGGER_STOP:
  170. case SNDRV_PCM_TRIGGER_SUSPEND:
  171. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  172. omap_mcbsp_stop(mcbsp_data->bus_id, play, !play);
  173. mcbsp_data->active--;
  174. break;
  175. default:
  176. err = -EINVAL;
  177. }
  178. return err;
  179. }
  180. static snd_pcm_sframes_t omap_mcbsp_dai_delay(
  181. struct snd_pcm_substream *substream,
  182. struct snd_soc_dai *dai)
  183. {
  184. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  185. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  186. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  187. u16 fifo_use;
  188. snd_pcm_sframes_t delay;
  189. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  190. fifo_use = omap_mcbsp_get_tx_delay(mcbsp_data->bus_id);
  191. else
  192. fifo_use = omap_mcbsp_get_rx_delay(mcbsp_data->bus_id);
  193. /*
  194. * Divide the used locations with the channel count to get the
  195. * FIFO usage in samples (don't care about partial samples in the
  196. * buffer).
  197. */
  198. delay = fifo_use / substream->runtime->channels;
  199. return delay;
  200. }
  201. static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
  202. struct snd_pcm_hw_params *params,
  203. struct snd_soc_dai *cpu_dai)
  204. {
  205. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  206. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  207. struct omap_pcm_dma_data *dma_data;
  208. int dma, bus_id = mcbsp_data->bus_id;
  209. int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT;
  210. int pkt_size = 0;
  211. unsigned long port;
  212. unsigned int format, div, framesize, master;
  213. dma_data = &omap_mcbsp_dai_dma_params[cpu_dai->id][substream->stream];
  214. dma = omap_mcbsp_dma_ch_params(bus_id, substream->stream);
  215. port = omap_mcbsp_dma_reg_params(bus_id, substream->stream);
  216. switch (params_format(params)) {
  217. case SNDRV_PCM_FORMAT_S16_LE:
  218. dma_data->data_type = OMAP_DMA_DATA_TYPE_S16;
  219. wlen = 16;
  220. break;
  221. case SNDRV_PCM_FORMAT_S32_LE:
  222. dma_data->data_type = OMAP_DMA_DATA_TYPE_S32;
  223. wlen = 32;
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  229. dma_data->set_threshold = omap_mcbsp_set_threshold;
  230. /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
  231. if (omap_mcbsp_get_dma_op_mode(bus_id) ==
  232. MCBSP_DMA_MODE_THRESHOLD) {
  233. int period_words, max_thrsh;
  234. period_words = params_period_bytes(params) / (wlen / 8);
  235. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  236. max_thrsh = omap_mcbsp_get_max_tx_threshold(
  237. mcbsp_data->bus_id);
  238. else
  239. max_thrsh = omap_mcbsp_get_max_rx_threshold(
  240. mcbsp_data->bus_id);
  241. /*
  242. * If the period contains less or equal number of words,
  243. * we are using the original threshold mode setup:
  244. * McBSP threshold = sDMA frame size = period_size
  245. * Otherwise we switch to sDMA packet mode:
  246. * McBSP threshold = sDMA packet size
  247. * sDMA frame size = period size
  248. */
  249. if (period_words > max_thrsh) {
  250. int divider = 0;
  251. /*
  252. * Look for the biggest threshold value, which
  253. * divides the period size evenly.
  254. */
  255. divider = period_words / max_thrsh;
  256. if (period_words % max_thrsh)
  257. divider++;
  258. while (period_words % divider &&
  259. divider < period_words)
  260. divider++;
  261. if (divider == period_words)
  262. return -EINVAL;
  263. pkt_size = period_words / divider;
  264. sync_mode = OMAP_DMA_SYNC_PACKET;
  265. } else {
  266. sync_mode = OMAP_DMA_SYNC_FRAME;
  267. }
  268. }
  269. }
  270. dma_data->name = substream->stream ? "Audio Capture" : "Audio Playback";
  271. dma_data->dma_req = dma;
  272. dma_data->port_addr = port;
  273. dma_data->sync_mode = sync_mode;
  274. dma_data->packet_size = pkt_size;
  275. snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
  276. if (mcbsp_data->configured) {
  277. /* McBSP already configured by another stream */
  278. return 0;
  279. }
  280. regs->rcr2 &= ~(RPHASE | RFRLEN2(0x7f) | RWDLEN2(7));
  281. regs->xcr2 &= ~(RPHASE | XFRLEN2(0x7f) | XWDLEN2(7));
  282. regs->rcr1 &= ~(RFRLEN1(0x7f) | RWDLEN1(7));
  283. regs->xcr1 &= ~(XFRLEN1(0x7f) | XWDLEN1(7));
  284. format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  285. wpf = channels = params_channels(params);
  286. if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
  287. format == SND_SOC_DAIFMT_LEFT_J)) {
  288. /* Use dual-phase frames */
  289. regs->rcr2 |= RPHASE;
  290. regs->xcr2 |= XPHASE;
  291. /* Set 1 word per (McBSP) frame for phase1 and phase2 */
  292. wpf--;
  293. regs->rcr2 |= RFRLEN2(wpf - 1);
  294. regs->xcr2 |= XFRLEN2(wpf - 1);
  295. }
  296. regs->rcr1 |= RFRLEN1(wpf - 1);
  297. regs->xcr1 |= XFRLEN1(wpf - 1);
  298. switch (params_format(params)) {
  299. case SNDRV_PCM_FORMAT_S16_LE:
  300. /* Set word lengths */
  301. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
  302. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
  303. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
  304. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
  305. break;
  306. case SNDRV_PCM_FORMAT_S32_LE:
  307. /* Set word lengths */
  308. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_32);
  309. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_32);
  310. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_32);
  311. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_32);
  312. break;
  313. default:
  314. /* Unsupported PCM format */
  315. return -EINVAL;
  316. }
  317. /* In McBSP master modes, FRAME (i.e. sample rate) is generated
  318. * by _counting_ BCLKs. Calculate frame size in BCLKs */
  319. master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  320. if (master == SND_SOC_DAIFMT_CBS_CFS) {
  321. div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
  322. framesize = (mcbsp_data->in_freq / div) / params_rate(params);
  323. if (framesize < wlen * channels) {
  324. printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
  325. "channels\n", __func__);
  326. return -EINVAL;
  327. }
  328. } else
  329. framesize = wlen * channels;
  330. /* Set FS period and length in terms of bit clock periods */
  331. regs->srgr2 &= ~FPER(0xfff);
  332. regs->srgr1 &= ~FWID(0xff);
  333. switch (format) {
  334. case SND_SOC_DAIFMT_I2S:
  335. case SND_SOC_DAIFMT_LEFT_J:
  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->wlen = wlen;
  347. mcbsp_data->configured = 1;
  348. return 0;
  349. }
  350. /*
  351. * This must be called before _set_clkdiv and _set_sysclk since McBSP register
  352. * cache is initialized here
  353. */
  354. static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  355. unsigned int fmt)
  356. {
  357. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  358. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  359. bool inv_fs = false;
  360. if (mcbsp_data->configured)
  361. return 0;
  362. mcbsp_data->fmt = fmt;
  363. memset(regs, 0, sizeof(*regs));
  364. /* Generic McBSP register settings */
  365. regs->spcr2 |= XINTM(3) | FREE;
  366. regs->spcr1 |= RINTM(3);
  367. /* RFIG and XFIG are not defined in 34xx */
  368. if (!cpu_is_omap34xx() && !cpu_is_omap44xx()) {
  369. regs->rcr2 |= RFIG;
  370. regs->xcr2 |= XFIG;
  371. }
  372. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  373. regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
  374. regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
  375. }
  376. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  377. case SND_SOC_DAIFMT_I2S:
  378. /* 1-bit data delay */
  379. regs->rcr2 |= RDATDLY(1);
  380. regs->xcr2 |= XDATDLY(1);
  381. break;
  382. case SND_SOC_DAIFMT_LEFT_J:
  383. /* 0-bit data delay */
  384. regs->rcr2 |= RDATDLY(0);
  385. regs->xcr2 |= XDATDLY(0);
  386. regs->spcr1 |= RJUST(2);
  387. /* Invert FS polarity configuration */
  388. inv_fs = true;
  389. break;
  390. case SND_SOC_DAIFMT_DSP_A:
  391. /* 1-bit data delay */
  392. regs->rcr2 |= RDATDLY(1);
  393. regs->xcr2 |= XDATDLY(1);
  394. /* Invert FS polarity configuration */
  395. inv_fs = true;
  396. break;
  397. case SND_SOC_DAIFMT_DSP_B:
  398. /* 0-bit data delay */
  399. regs->rcr2 |= RDATDLY(0);
  400. regs->xcr2 |= XDATDLY(0);
  401. /* Invert FS polarity configuration */
  402. inv_fs = true;
  403. break;
  404. default:
  405. /* Unsupported data format */
  406. return -EINVAL;
  407. }
  408. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  409. case SND_SOC_DAIFMT_CBS_CFS:
  410. /* McBSP master. Set FS and bit clocks as outputs */
  411. regs->pcr0 |= FSXM | FSRM |
  412. CLKXM | CLKRM;
  413. /* Sample rate generator drives the FS */
  414. regs->srgr2 |= FSGM;
  415. break;
  416. case SND_SOC_DAIFMT_CBM_CFM:
  417. /* McBSP slave */
  418. break;
  419. default:
  420. /* Unsupported master/slave configuration */
  421. return -EINVAL;
  422. }
  423. /* Set bit clock (CLKX/CLKR) and FS polarities */
  424. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  425. case SND_SOC_DAIFMT_NB_NF:
  426. /*
  427. * Normal BCLK + FS.
  428. * FS active low. TX data driven on falling edge of bit clock
  429. * and RX data sampled on rising edge of bit clock.
  430. */
  431. regs->pcr0 |= FSXP | FSRP |
  432. CLKXP | CLKRP;
  433. break;
  434. case SND_SOC_DAIFMT_NB_IF:
  435. regs->pcr0 |= CLKXP | CLKRP;
  436. break;
  437. case SND_SOC_DAIFMT_IB_NF:
  438. regs->pcr0 |= FSXP | FSRP;
  439. break;
  440. case SND_SOC_DAIFMT_IB_IF:
  441. break;
  442. default:
  443. return -EINVAL;
  444. }
  445. if (inv_fs == true)
  446. regs->pcr0 ^= FSXP | FSRP;
  447. return 0;
  448. }
  449. static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  450. int div_id, int div)
  451. {
  452. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  453. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  454. if (div_id != OMAP_MCBSP_CLKGDV)
  455. return -ENODEV;
  456. mcbsp_data->clk_div = div;
  457. regs->srgr1 &= ~CLKGDV(0xff);
  458. regs->srgr1 |= CLKGDV(div - 1);
  459. return 0;
  460. }
  461. static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  462. int clk_id, unsigned int freq,
  463. int dir)
  464. {
  465. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  466. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  467. int err = 0;
  468. if (mcbsp_data->active) {
  469. if (freq == mcbsp_data->in_freq)
  470. return 0;
  471. else
  472. return -EBUSY;
  473. }
  474. /* The McBSP signal muxing functions are only available on McBSP1 */
  475. if (clk_id == OMAP_MCBSP_CLKR_SRC_CLKR ||
  476. clk_id == OMAP_MCBSP_CLKR_SRC_CLKX ||
  477. clk_id == OMAP_MCBSP_FSR_SRC_FSR ||
  478. clk_id == OMAP_MCBSP_FSR_SRC_FSX)
  479. if (cpu_class_is_omap1() || mcbsp_data->bus_id != 0)
  480. return -EINVAL;
  481. mcbsp_data->in_freq = freq;
  482. regs->srgr2 &= ~CLKSM;
  483. regs->pcr0 &= ~SCLKME;
  484. switch (clk_id) {
  485. case OMAP_MCBSP_SYSCLK_CLK:
  486. regs->srgr2 |= CLKSM;
  487. break;
  488. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  489. if (cpu_class_is_omap1()) {
  490. err = -EINVAL;
  491. break;
  492. }
  493. err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id,
  494. MCBSP_CLKS_PRCM_SRC);
  495. break;
  496. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  497. if (cpu_class_is_omap1()) {
  498. err = 0;
  499. break;
  500. }
  501. err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id,
  502. MCBSP_CLKS_PAD_SRC);
  503. break;
  504. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  505. regs->srgr2 |= CLKSM;
  506. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  507. regs->pcr0 |= SCLKME;
  508. break;
  509. case OMAP_MCBSP_CLKR_SRC_CLKR:
  510. if (cpu_class_is_omap1())
  511. break;
  512. omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKR);
  513. break;
  514. case OMAP_MCBSP_CLKR_SRC_CLKX:
  515. if (cpu_class_is_omap1())
  516. break;
  517. omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKX);
  518. break;
  519. case OMAP_MCBSP_FSR_SRC_FSR:
  520. if (cpu_class_is_omap1())
  521. break;
  522. omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSR);
  523. break;
  524. case OMAP_MCBSP_FSR_SRC_FSX:
  525. if (cpu_class_is_omap1())
  526. break;
  527. omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSX);
  528. break;
  529. default:
  530. err = -ENODEV;
  531. }
  532. return err;
  533. }
  534. static const struct snd_soc_dai_ops mcbsp_dai_ops = {
  535. .startup = omap_mcbsp_dai_startup,
  536. .shutdown = omap_mcbsp_dai_shutdown,
  537. .trigger = omap_mcbsp_dai_trigger,
  538. .delay = omap_mcbsp_dai_delay,
  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. static int mcbsp_dai_probe(struct snd_soc_dai *dai)
  545. {
  546. mcbsp_data[dai->id].bus_id = dai->id;
  547. snd_soc_dai_set_drvdata(dai, &mcbsp_data[dai->id].bus_id);
  548. return 0;
  549. }
  550. static struct snd_soc_dai_driver omap_mcbsp_dai = {
  551. .probe = mcbsp_dai_probe,
  552. .playback = {
  553. .channels_min = 1,
  554. .channels_max = 16,
  555. .rates = OMAP_MCBSP_RATES,
  556. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  557. },
  558. .capture = {
  559. .channels_min = 1,
  560. .channels_max = 16,
  561. .rates = OMAP_MCBSP_RATES,
  562. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  563. },
  564. .ops = &mcbsp_dai_ops,
  565. };
  566. static int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
  567. struct snd_ctl_elem_info *uinfo)
  568. {
  569. struct soc_mixer_control *mc =
  570. (struct soc_mixer_control *)kcontrol->private_value;
  571. int max = mc->max;
  572. int min = mc->min;
  573. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  574. uinfo->count = 1;
  575. uinfo->value.integer.min = min;
  576. uinfo->value.integer.max = max;
  577. return 0;
  578. }
  579. #define OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(id, channel) \
  580. static int \
  581. omap_mcbsp##id##_set_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  582. struct snd_ctl_elem_value *uc) \
  583. { \
  584. struct soc_mixer_control *mc = \
  585. (struct soc_mixer_control *)kc->private_value; \
  586. int max = mc->max; \
  587. int min = mc->min; \
  588. int val = uc->value.integer.value[0]; \
  589. \
  590. if (val < min || val > max) \
  591. return -EINVAL; \
  592. \
  593. /* OMAP McBSP implementation uses index values 0..4 */ \
  594. return omap_st_set_chgain((id)-1, channel, val); \
  595. }
  596. #define OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(id, channel) \
  597. static int \
  598. omap_mcbsp##id##_get_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  599. struct snd_ctl_elem_value *uc) \
  600. { \
  601. s16 chgain; \
  602. \
  603. if (omap_st_get_chgain((id)-1, channel, &chgain)) \
  604. return -EAGAIN; \
  605. \
  606. uc->value.integer.value[0] = chgain; \
  607. return 0; \
  608. }
  609. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 0)
  610. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 1)
  611. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 0)
  612. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 1)
  613. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 0)
  614. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 1)
  615. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 0)
  616. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 1)
  617. static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
  618. struct snd_ctl_elem_value *ucontrol)
  619. {
  620. struct soc_mixer_control *mc =
  621. (struct soc_mixer_control *)kcontrol->private_value;
  622. u8 value = ucontrol->value.integer.value[0];
  623. if (value == omap_st_is_enabled(mc->reg))
  624. return 0;
  625. if (value)
  626. omap_st_enable(mc->reg);
  627. else
  628. omap_st_disable(mc->reg);
  629. return 1;
  630. }
  631. static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
  632. struct snd_ctl_elem_value *ucontrol)
  633. {
  634. struct soc_mixer_control *mc =
  635. (struct soc_mixer_control *)kcontrol->private_value;
  636. ucontrol->value.integer.value[0] = omap_st_is_enabled(mc->reg);
  637. return 0;
  638. }
  639. static const struct snd_kcontrol_new omap_mcbsp2_st_controls[] = {
  640. SOC_SINGLE_EXT("McBSP2 Sidetone Switch", 1, 0, 1, 0,
  641. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
  642. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 0 Volume",
  643. -32768, 32767,
  644. omap_mcbsp2_get_st_ch0_volume,
  645. omap_mcbsp2_set_st_ch0_volume),
  646. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 1 Volume",
  647. -32768, 32767,
  648. omap_mcbsp2_get_st_ch1_volume,
  649. omap_mcbsp2_set_st_ch1_volume),
  650. };
  651. static const struct snd_kcontrol_new omap_mcbsp3_st_controls[] = {
  652. SOC_SINGLE_EXT("McBSP3 Sidetone Switch", 2, 0, 1, 0,
  653. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
  654. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 0 Volume",
  655. -32768, 32767,
  656. omap_mcbsp3_get_st_ch0_volume,
  657. omap_mcbsp3_set_st_ch0_volume),
  658. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 1 Volume",
  659. -32768, 32767,
  660. omap_mcbsp3_get_st_ch1_volume,
  661. omap_mcbsp3_set_st_ch1_volume),
  662. };
  663. int omap_mcbsp_st_add_controls(struct snd_soc_codec *codec, int mcbsp_id)
  664. {
  665. if (!cpu_is_omap34xx())
  666. return -ENODEV;
  667. switch (mcbsp_id) {
  668. case 1: /* McBSP 2 */
  669. return snd_soc_add_controls(codec, omap_mcbsp2_st_controls,
  670. ARRAY_SIZE(omap_mcbsp2_st_controls));
  671. case 2: /* McBSP 3 */
  672. return snd_soc_add_controls(codec, omap_mcbsp3_st_controls,
  673. ARRAY_SIZE(omap_mcbsp3_st_controls));
  674. default:
  675. break;
  676. }
  677. return -EINVAL;
  678. }
  679. EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
  680. static __devinit int asoc_mcbsp_probe(struct platform_device *pdev)
  681. {
  682. return snd_soc_register_dai(&pdev->dev, &omap_mcbsp_dai);
  683. }
  684. static int __devexit asoc_mcbsp_remove(struct platform_device *pdev)
  685. {
  686. snd_soc_unregister_dai(&pdev->dev);
  687. return 0;
  688. }
  689. static struct platform_driver asoc_mcbsp_driver = {
  690. .driver = {
  691. .name = "omap-mcbsp-dai",
  692. .owner = THIS_MODULE,
  693. },
  694. .probe = asoc_mcbsp_probe,
  695. .remove = __devexit_p(asoc_mcbsp_remove),
  696. };
  697. module_platform_driver(asoc_mcbsp_driver);
  698. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  699. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  700. MODULE_LICENSE("GPL");