omap-mcbsp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 <sound/dmaengine_pcm.h>
  36. #include <sound/omap-pcm.h>
  37. #include <linux/platform_data/asoc-ti-mcbsp.h>
  38. #include "mcbsp.h"
  39. #include "omap-mcbsp.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. unsigned int packet_size)
  62. {
  63. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  64. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  65. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  66. int words;
  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 (packet_size)
  74. words = packet_size;
  75. else
  76. words = 1;
  77. /* Configure McBSP internal buffer usage */
  78. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  79. omap_mcbsp_set_tx_threshold(mcbsp, words);
  80. else
  81. omap_mcbsp_set_rx_threshold(mcbsp, words);
  82. }
  83. static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params,
  84. struct snd_pcm_hw_rule *rule)
  85. {
  86. struct snd_interval *buffer_size = hw_param_interval(params,
  87. SNDRV_PCM_HW_PARAM_BUFFER_SIZE);
  88. struct snd_interval *channels = hw_param_interval(params,
  89. SNDRV_PCM_HW_PARAM_CHANNELS);
  90. struct omap_mcbsp *mcbsp = rule->private;
  91. struct snd_interval frames;
  92. int size;
  93. snd_interval_any(&frames);
  94. size = mcbsp->pdata->buffer_size;
  95. frames.min = size / channels->min;
  96. frames.integer = 1;
  97. return snd_interval_refine(buffer_size, &frames);
  98. }
  99. static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
  100. struct snd_soc_dai *cpu_dai)
  101. {
  102. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  103. int err = 0;
  104. if (!cpu_dai->active)
  105. err = omap_mcbsp_request(mcbsp);
  106. /*
  107. * OMAP3 McBSP FIFO is word structured.
  108. * McBSP2 has 1024 + 256 = 1280 word long buffer,
  109. * McBSP1,3,4,5 has 128 word long buffer
  110. * This means that the size of the FIFO depends on the sample format.
  111. * For example on McBSP3:
  112. * 16bit samples: size is 128 * 2 = 256 bytes
  113. * 32bit samples: size is 128 * 4 = 512 bytes
  114. * It is simpler to place constraint for buffer and period based on
  115. * channels.
  116. * McBSP3 as example again (16 or 32 bit samples):
  117. * 1 channel (mono): size is 128 frames (128 words)
  118. * 2 channels (stereo): size is 128 / 2 = 64 frames (2 * 64 words)
  119. * 4 channels: size is 128 / 4 = 32 frames (4 * 32 words)
  120. */
  121. if (mcbsp->pdata->buffer_size) {
  122. /*
  123. * Rule for the buffer size. We should not allow
  124. * smaller buffer than the FIFO size to avoid underruns.
  125. * This applies only for the playback stream.
  126. */
  127. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  128. snd_pcm_hw_rule_add(substream->runtime, 0,
  129. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  130. omap_mcbsp_hwrule_min_buffersize,
  131. mcbsp,
  132. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  133. /* Make sure, that the period size is always even */
  134. snd_pcm_hw_constraint_step(substream->runtime, 0,
  135. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
  136. }
  137. return err;
  138. }
  139. static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
  140. struct snd_soc_dai *cpu_dai)
  141. {
  142. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  143. if (!cpu_dai->active) {
  144. omap_mcbsp_free(mcbsp);
  145. mcbsp->configured = 0;
  146. }
  147. }
  148. static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  149. struct snd_soc_dai *cpu_dai)
  150. {
  151. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  152. int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  153. switch (cmd) {
  154. case SNDRV_PCM_TRIGGER_START:
  155. case SNDRV_PCM_TRIGGER_RESUME:
  156. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  157. mcbsp->active++;
  158. omap_mcbsp_start(mcbsp, play, !play);
  159. break;
  160. case SNDRV_PCM_TRIGGER_STOP:
  161. case SNDRV_PCM_TRIGGER_SUSPEND:
  162. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  163. omap_mcbsp_stop(mcbsp, play, !play);
  164. mcbsp->active--;
  165. break;
  166. default:
  167. err = -EINVAL;
  168. }
  169. return err;
  170. }
  171. static snd_pcm_sframes_t omap_mcbsp_dai_delay(
  172. struct snd_pcm_substream *substream,
  173. struct snd_soc_dai *dai)
  174. {
  175. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  176. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  177. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  178. u16 fifo_use;
  179. snd_pcm_sframes_t delay;
  180. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  181. fifo_use = omap_mcbsp_get_tx_delay(mcbsp);
  182. else
  183. fifo_use = omap_mcbsp_get_rx_delay(mcbsp);
  184. /*
  185. * Divide the used locations with the channel count to get the
  186. * FIFO usage in samples (don't care about partial samples in the
  187. * buffer).
  188. */
  189. delay = fifo_use / substream->runtime->channels;
  190. return delay;
  191. }
  192. static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
  193. struct snd_pcm_hw_params *params,
  194. struct snd_soc_dai *cpu_dai)
  195. {
  196. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  197. struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
  198. struct snd_dmaengine_dai_dma_data *dma_data;
  199. int wlen, channels, wpf;
  200. int pkt_size = 0;
  201. unsigned int format, div, framesize, master;
  202. dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream);
  203. channels = params_channels(params);
  204. switch (params_format(params)) {
  205. case SNDRV_PCM_FORMAT_S16_LE:
  206. wlen = 16;
  207. break;
  208. case SNDRV_PCM_FORMAT_S32_LE:
  209. wlen = 32;
  210. break;
  211. default:
  212. return -EINVAL;
  213. }
  214. if (mcbsp->pdata->buffer_size) {
  215. if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) {
  216. int period_words, max_thrsh;
  217. int divider = 0;
  218. period_words = params_period_bytes(params) / (wlen / 8);
  219. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  220. max_thrsh = mcbsp->max_tx_thres;
  221. else
  222. max_thrsh = mcbsp->max_rx_thres;
  223. /*
  224. * Use sDMA packet mode if McBSP is in threshold mode:
  225. * If period words less than the FIFO size the packet
  226. * size is set to the number of period words, otherwise
  227. * Look for the biggest threshold value which divides
  228. * the period size evenly.
  229. */
  230. divider = period_words / max_thrsh;
  231. if (period_words % max_thrsh)
  232. divider++;
  233. while (period_words % divider &&
  234. divider < period_words)
  235. divider++;
  236. if (divider == period_words)
  237. return -EINVAL;
  238. pkt_size = period_words / divider;
  239. } else if (channels > 1) {
  240. /* Use packet mode for non mono streams */
  241. pkt_size = channels;
  242. }
  243. omap_mcbsp_set_threshold(substream, pkt_size);
  244. }
  245. dma_data->maxburst = pkt_size;
  246. if (mcbsp->configured) {
  247. /* McBSP already configured by another stream */
  248. return 0;
  249. }
  250. regs->rcr2 &= ~(RPHASE | RFRLEN2(0x7f) | RWDLEN2(7));
  251. regs->xcr2 &= ~(RPHASE | XFRLEN2(0x7f) | XWDLEN2(7));
  252. regs->rcr1 &= ~(RFRLEN1(0x7f) | RWDLEN1(7));
  253. regs->xcr1 &= ~(XFRLEN1(0x7f) | XWDLEN1(7));
  254. format = mcbsp->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  255. wpf = channels;
  256. if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
  257. format == SND_SOC_DAIFMT_LEFT_J)) {
  258. /* Use dual-phase frames */
  259. regs->rcr2 |= RPHASE;
  260. regs->xcr2 |= XPHASE;
  261. /* Set 1 word per (McBSP) frame for phase1 and phase2 */
  262. wpf--;
  263. regs->rcr2 |= RFRLEN2(wpf - 1);
  264. regs->xcr2 |= XFRLEN2(wpf - 1);
  265. }
  266. regs->rcr1 |= RFRLEN1(wpf - 1);
  267. regs->xcr1 |= XFRLEN1(wpf - 1);
  268. switch (params_format(params)) {
  269. case SNDRV_PCM_FORMAT_S16_LE:
  270. /* Set word lengths */
  271. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
  272. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
  273. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
  274. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
  275. break;
  276. case SNDRV_PCM_FORMAT_S32_LE:
  277. /* Set word lengths */
  278. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_32);
  279. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_32);
  280. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_32);
  281. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_32);
  282. break;
  283. default:
  284. /* Unsupported PCM format */
  285. return -EINVAL;
  286. }
  287. /* In McBSP master modes, FRAME (i.e. sample rate) is generated
  288. * by _counting_ BCLKs. Calculate frame size in BCLKs */
  289. master = mcbsp->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  290. if (master == SND_SOC_DAIFMT_CBS_CFS) {
  291. div = mcbsp->clk_div ? mcbsp->clk_div : 1;
  292. framesize = (mcbsp->in_freq / div) / params_rate(params);
  293. if (framesize < wlen * channels) {
  294. printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
  295. "channels\n", __func__);
  296. return -EINVAL;
  297. }
  298. } else
  299. framesize = wlen * channels;
  300. /* Set FS period and length in terms of bit clock periods */
  301. regs->srgr2 &= ~FPER(0xfff);
  302. regs->srgr1 &= ~FWID(0xff);
  303. switch (format) {
  304. case SND_SOC_DAIFMT_I2S:
  305. case SND_SOC_DAIFMT_LEFT_J:
  306. regs->srgr2 |= FPER(framesize - 1);
  307. regs->srgr1 |= FWID((framesize >> 1) - 1);
  308. break;
  309. case SND_SOC_DAIFMT_DSP_A:
  310. case SND_SOC_DAIFMT_DSP_B:
  311. regs->srgr2 |= FPER(framesize - 1);
  312. regs->srgr1 |= FWID(0);
  313. break;
  314. }
  315. omap_mcbsp_config(mcbsp, &mcbsp->cfg_regs);
  316. mcbsp->wlen = wlen;
  317. mcbsp->configured = 1;
  318. return 0;
  319. }
  320. /*
  321. * This must be called before _set_clkdiv and _set_sysclk since McBSP register
  322. * cache is initialized here
  323. */
  324. static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  325. unsigned int fmt)
  326. {
  327. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  328. struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
  329. bool inv_fs = false;
  330. if (mcbsp->configured)
  331. return 0;
  332. mcbsp->fmt = fmt;
  333. memset(regs, 0, sizeof(*regs));
  334. /* Generic McBSP register settings */
  335. regs->spcr2 |= XINTM(3) | FREE;
  336. regs->spcr1 |= RINTM(3);
  337. /* RFIG and XFIG are not defined in 2430 and on OMAP3+ */
  338. if (!mcbsp->pdata->has_ccr) {
  339. regs->rcr2 |= RFIG;
  340. regs->xcr2 |= XFIG;
  341. }
  342. /* Configure XCCR/RCCR only for revisions which have ccr registers */
  343. if (mcbsp->pdata->has_ccr) {
  344. regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
  345. regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
  346. }
  347. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  348. case SND_SOC_DAIFMT_I2S:
  349. /* 1-bit data delay */
  350. regs->rcr2 |= RDATDLY(1);
  351. regs->xcr2 |= XDATDLY(1);
  352. break;
  353. case SND_SOC_DAIFMT_LEFT_J:
  354. /* 0-bit data delay */
  355. regs->rcr2 |= RDATDLY(0);
  356. regs->xcr2 |= XDATDLY(0);
  357. regs->spcr1 |= RJUST(2);
  358. /* Invert FS polarity configuration */
  359. inv_fs = true;
  360. break;
  361. case SND_SOC_DAIFMT_DSP_A:
  362. /* 1-bit data delay */
  363. regs->rcr2 |= RDATDLY(1);
  364. regs->xcr2 |= XDATDLY(1);
  365. /* Invert FS polarity configuration */
  366. inv_fs = true;
  367. break;
  368. case SND_SOC_DAIFMT_DSP_B:
  369. /* 0-bit data delay */
  370. regs->rcr2 |= RDATDLY(0);
  371. regs->xcr2 |= XDATDLY(0);
  372. /* Invert FS polarity configuration */
  373. inv_fs = true;
  374. break;
  375. default:
  376. /* Unsupported data format */
  377. return -EINVAL;
  378. }
  379. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  380. case SND_SOC_DAIFMT_CBS_CFS:
  381. /* McBSP master. Set FS and bit clocks as outputs */
  382. regs->pcr0 |= FSXM | FSRM |
  383. CLKXM | CLKRM;
  384. /* Sample rate generator drives the FS */
  385. regs->srgr2 |= FSGM;
  386. break;
  387. case SND_SOC_DAIFMT_CBM_CFS:
  388. /* McBSP slave. FS clock as output */
  389. regs->srgr2 |= FSGM;
  390. regs->pcr0 |= FSXM | FSRM;
  391. break;
  392. case SND_SOC_DAIFMT_CBM_CFM:
  393. /* McBSP slave */
  394. break;
  395. default:
  396. /* Unsupported master/slave configuration */
  397. return -EINVAL;
  398. }
  399. /* Set bit clock (CLKX/CLKR) and FS polarities */
  400. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  401. case SND_SOC_DAIFMT_NB_NF:
  402. /*
  403. * Normal BCLK + FS.
  404. * FS active low. TX data driven on falling edge of bit clock
  405. * and RX data sampled on rising edge of bit clock.
  406. */
  407. regs->pcr0 |= FSXP | FSRP |
  408. CLKXP | CLKRP;
  409. break;
  410. case SND_SOC_DAIFMT_NB_IF:
  411. regs->pcr0 |= CLKXP | CLKRP;
  412. break;
  413. case SND_SOC_DAIFMT_IB_NF:
  414. regs->pcr0 |= FSXP | FSRP;
  415. break;
  416. case SND_SOC_DAIFMT_IB_IF:
  417. break;
  418. default:
  419. return -EINVAL;
  420. }
  421. if (inv_fs == true)
  422. regs->pcr0 ^= FSXP | FSRP;
  423. return 0;
  424. }
  425. static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  426. int div_id, int div)
  427. {
  428. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  429. struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
  430. if (div_id != OMAP_MCBSP_CLKGDV)
  431. return -ENODEV;
  432. mcbsp->clk_div = div;
  433. regs->srgr1 &= ~CLKGDV(0xff);
  434. regs->srgr1 |= CLKGDV(div - 1);
  435. return 0;
  436. }
  437. static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  438. int clk_id, unsigned int freq,
  439. int dir)
  440. {
  441. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  442. struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
  443. int err = 0;
  444. if (mcbsp->active) {
  445. if (freq == mcbsp->in_freq)
  446. return 0;
  447. else
  448. return -EBUSY;
  449. }
  450. mcbsp->in_freq = freq;
  451. regs->srgr2 &= ~CLKSM;
  452. regs->pcr0 &= ~SCLKME;
  453. switch (clk_id) {
  454. case OMAP_MCBSP_SYSCLK_CLK:
  455. regs->srgr2 |= CLKSM;
  456. break;
  457. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  458. if (mcbsp_omap1()) {
  459. err = -EINVAL;
  460. break;
  461. }
  462. err = omap2_mcbsp_set_clks_src(mcbsp,
  463. MCBSP_CLKS_PRCM_SRC);
  464. break;
  465. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  466. if (mcbsp_omap1()) {
  467. err = 0;
  468. break;
  469. }
  470. err = omap2_mcbsp_set_clks_src(mcbsp,
  471. MCBSP_CLKS_PAD_SRC);
  472. break;
  473. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  474. regs->srgr2 |= CLKSM;
  475. regs->pcr0 |= SCLKME;
  476. /*
  477. * If McBSP is master but yet the CLKX/CLKR pin drives the SRG,
  478. * disable output on those pins. This enables to inject the
  479. * reference clock through CLKX/CLKR. For this to work
  480. * set_dai_sysclk() _needs_ to be called after set_dai_fmt().
  481. */
  482. regs->pcr0 &= ~CLKXM;
  483. break;
  484. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  485. regs->pcr0 |= SCLKME;
  486. /* Disable ouput on CLKR pin in master mode */
  487. regs->pcr0 &= ~CLKRM;
  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. snd_soc_dai_init_dma_data(dai,
  509. &mcbsp->dma_data[SNDRV_PCM_STREAM_PLAYBACK],
  510. &mcbsp->dma_data[SNDRV_PCM_STREAM_CAPTURE]);
  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 const struct snd_soc_component_driver omap_mcbsp_component = {
  537. .name = "omap-mcbsp",
  538. };
  539. static int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
  540. struct snd_ctl_elem_info *uinfo)
  541. {
  542. struct soc_mixer_control *mc =
  543. (struct soc_mixer_control *)kcontrol->private_value;
  544. int max = mc->max;
  545. int min = mc->min;
  546. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  547. uinfo->count = 1;
  548. uinfo->value.integer.min = min;
  549. uinfo->value.integer.max = max;
  550. return 0;
  551. }
  552. #define OMAP_MCBSP_ST_CHANNEL_VOLUME(channel) \
  553. static int \
  554. omap_mcbsp_set_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  555. struct snd_ctl_elem_value *uc) \
  556. { \
  557. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kc); \
  558. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); \
  559. struct soc_mixer_control *mc = \
  560. (struct soc_mixer_control *)kc->private_value; \
  561. int max = mc->max; \
  562. int min = mc->min; \
  563. int val = uc->value.integer.value[0]; \
  564. \
  565. if (val < min || val > max) \
  566. return -EINVAL; \
  567. \
  568. /* OMAP McBSP implementation uses index values 0..4 */ \
  569. return omap_st_set_chgain(mcbsp, channel, val); \
  570. } \
  571. \
  572. static int \
  573. omap_mcbsp_get_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. s16 chgain; \
  579. \
  580. if (omap_st_get_chgain(mcbsp, channel, &chgain)) \
  581. return -EAGAIN; \
  582. \
  583. uc->value.integer.value[0] = chgain; \
  584. return 0; \
  585. }
  586. OMAP_MCBSP_ST_CHANNEL_VOLUME(0)
  587. OMAP_MCBSP_ST_CHANNEL_VOLUME(1)
  588. static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
  589. struct snd_ctl_elem_value *ucontrol)
  590. {
  591. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  592. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  593. u8 value = ucontrol->value.integer.value[0];
  594. if (value == omap_st_is_enabled(mcbsp))
  595. return 0;
  596. if (value)
  597. omap_st_enable(mcbsp);
  598. else
  599. omap_st_disable(mcbsp);
  600. return 1;
  601. }
  602. static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
  603. struct snd_ctl_elem_value *ucontrol)
  604. {
  605. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  606. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  607. ucontrol->value.integer.value[0] = omap_st_is_enabled(mcbsp);
  608. return 0;
  609. }
  610. #define OMAP_MCBSP_ST_CONTROLS(port) \
  611. static const struct snd_kcontrol_new omap_mcbsp##port##_st_controls[] = { \
  612. SOC_SINGLE_EXT("McBSP" #port " Sidetone Switch", 1, 0, 1, 0, \
  613. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode), \
  614. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP" #port " Sidetone Channel 0 Volume", \
  615. -32768, 32767, \
  616. omap_mcbsp_get_st_ch0_volume, \
  617. omap_mcbsp_set_st_ch0_volume), \
  618. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP" #port " Sidetone Channel 1 Volume", \
  619. -32768, 32767, \
  620. omap_mcbsp_get_st_ch1_volume, \
  621. omap_mcbsp_set_st_ch1_volume), \
  622. }
  623. OMAP_MCBSP_ST_CONTROLS(2);
  624. OMAP_MCBSP_ST_CONTROLS(3);
  625. int omap_mcbsp_st_add_controls(struct snd_soc_pcm_runtime *rtd, int port_id)
  626. {
  627. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  628. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  629. if (!mcbsp->st_data) {
  630. dev_warn(mcbsp->dev, "No sidetone data for port\n");
  631. return 0;
  632. }
  633. switch (port_id) {
  634. case 2: /* McBSP 2 */
  635. return snd_soc_add_dai_controls(cpu_dai,
  636. omap_mcbsp2_st_controls,
  637. ARRAY_SIZE(omap_mcbsp2_st_controls));
  638. case 3: /* McBSP 3 */
  639. return snd_soc_add_dai_controls(cpu_dai,
  640. omap_mcbsp3_st_controls,
  641. ARRAY_SIZE(omap_mcbsp3_st_controls));
  642. default:
  643. dev_err(mcbsp->dev, "Port %d not supported\n", port_id);
  644. break;
  645. }
  646. return -EINVAL;
  647. }
  648. EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
  649. static struct omap_mcbsp_platform_data omap2420_pdata = {
  650. .reg_step = 4,
  651. .reg_size = 2,
  652. };
  653. static struct omap_mcbsp_platform_data omap2430_pdata = {
  654. .reg_step = 4,
  655. .reg_size = 4,
  656. .has_ccr = true,
  657. };
  658. static struct omap_mcbsp_platform_data omap3_pdata = {
  659. .reg_step = 4,
  660. .reg_size = 4,
  661. .has_ccr = true,
  662. .has_wakeup = true,
  663. };
  664. static struct omap_mcbsp_platform_data omap4_pdata = {
  665. .reg_step = 4,
  666. .reg_size = 4,
  667. .has_ccr = true,
  668. .has_wakeup = true,
  669. };
  670. static const struct of_device_id omap_mcbsp_of_match[] = {
  671. {
  672. .compatible = "ti,omap2420-mcbsp",
  673. .data = &omap2420_pdata,
  674. },
  675. {
  676. .compatible = "ti,omap2430-mcbsp",
  677. .data = &omap2430_pdata,
  678. },
  679. {
  680. .compatible = "ti,omap3-mcbsp",
  681. .data = &omap3_pdata,
  682. },
  683. {
  684. .compatible = "ti,omap4-mcbsp",
  685. .data = &omap4_pdata,
  686. },
  687. { },
  688. };
  689. MODULE_DEVICE_TABLE(of, omap_mcbsp_of_match);
  690. static int asoc_mcbsp_probe(struct platform_device *pdev)
  691. {
  692. struct omap_mcbsp_platform_data *pdata = dev_get_platdata(&pdev->dev);
  693. struct omap_mcbsp *mcbsp;
  694. const struct of_device_id *match;
  695. int ret;
  696. match = of_match_device(omap_mcbsp_of_match, &pdev->dev);
  697. if (match) {
  698. struct device_node *node = pdev->dev.of_node;
  699. int buffer_size;
  700. pdata = devm_kzalloc(&pdev->dev,
  701. sizeof(struct omap_mcbsp_platform_data),
  702. GFP_KERNEL);
  703. if (!pdata)
  704. return -ENOMEM;
  705. memcpy(pdata, match->data, sizeof(*pdata));
  706. if (!of_property_read_u32(node, "ti,buffer-size", &buffer_size))
  707. pdata->buffer_size = buffer_size;
  708. } else if (!pdata) {
  709. dev_err(&pdev->dev, "missing platform data.\n");
  710. return -EINVAL;
  711. }
  712. mcbsp = devm_kzalloc(&pdev->dev, sizeof(struct omap_mcbsp), GFP_KERNEL);
  713. if (!mcbsp)
  714. return -ENOMEM;
  715. mcbsp->id = pdev->id;
  716. mcbsp->pdata = pdata;
  717. mcbsp->dev = &pdev->dev;
  718. platform_set_drvdata(pdev, mcbsp);
  719. ret = omap_mcbsp_init(pdev);
  720. if (ret)
  721. return ret;
  722. ret = devm_snd_soc_register_component(&pdev->dev,
  723. &omap_mcbsp_component,
  724. &omap_mcbsp_dai, 1);
  725. if (ret)
  726. return ret;
  727. return omap_pcm_platform_register(&pdev->dev);
  728. }
  729. static int asoc_mcbsp_remove(struct platform_device *pdev)
  730. {
  731. struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
  732. if (mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  733. mcbsp->pdata->ops->free(mcbsp->id);
  734. omap_mcbsp_sysfs_remove(mcbsp);
  735. clk_put(mcbsp->fclk);
  736. return 0;
  737. }
  738. static struct platform_driver asoc_mcbsp_driver = {
  739. .driver = {
  740. .name = "omap-mcbsp",
  741. .of_match_table = omap_mcbsp_of_match,
  742. },
  743. .probe = asoc_mcbsp_probe,
  744. .remove = asoc_mcbsp_remove,
  745. };
  746. module_platform_driver(asoc_mcbsp_driver);
  747. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  748. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  749. MODULE_LICENSE("GPL");
  750. MODULE_ALIAS("platform:omap-mcbsp");