omap-mcbsp.c 21 KB

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