omap-mcbsp.c 22 KB

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