omap-mcbsp.c 22 KB

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