omap-mcbsp.c 23 KB

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