omap-mcbsp.c 23 KB

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