omap-mcbsp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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/cpu.h>
  36. #include <linux/platform_data/asoc-ti-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. snd_soc_dai_set_dma_data(cpu_dai, substream,
  139. &mcbsp->dma_data[substream->stream]);
  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;
  203. int pkt_size = 0;
  204. unsigned int format, div, framesize, master;
  205. dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream);
  206. channels = params_channels(params);
  207. switch (params_format(params)) {
  208. case SNDRV_PCM_FORMAT_S16_LE:
  209. wlen = 16;
  210. break;
  211. case SNDRV_PCM_FORMAT_S32_LE:
  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. if (mcbsp->configured) {
  250. /* McBSP already configured by another stream */
  251. return 0;
  252. }
  253. regs->rcr2 &= ~(RPHASE | RFRLEN2(0x7f) | RWDLEN2(7));
  254. regs->xcr2 &= ~(RPHASE | XFRLEN2(0x7f) | XWDLEN2(7));
  255. regs->rcr1 &= ~(RFRLEN1(0x7f) | RWDLEN1(7));
  256. regs->xcr1 &= ~(XFRLEN1(0x7f) | XWDLEN1(7));
  257. format = mcbsp->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  258. wpf = channels;
  259. if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
  260. format == SND_SOC_DAIFMT_LEFT_J)) {
  261. /* Use dual-phase frames */
  262. regs->rcr2 |= RPHASE;
  263. regs->xcr2 |= XPHASE;
  264. /* Set 1 word per (McBSP) frame for phase1 and phase2 */
  265. wpf--;
  266. regs->rcr2 |= RFRLEN2(wpf - 1);
  267. regs->xcr2 |= XFRLEN2(wpf - 1);
  268. }
  269. regs->rcr1 |= RFRLEN1(wpf - 1);
  270. regs->xcr1 |= XFRLEN1(wpf - 1);
  271. switch (params_format(params)) {
  272. case SNDRV_PCM_FORMAT_S16_LE:
  273. /* Set word lengths */
  274. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
  275. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
  276. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
  277. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
  278. break;
  279. case SNDRV_PCM_FORMAT_S32_LE:
  280. /* Set word lengths */
  281. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_32);
  282. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_32);
  283. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_32);
  284. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_32);
  285. break;
  286. default:
  287. /* Unsupported PCM format */
  288. return -EINVAL;
  289. }
  290. /* In McBSP master modes, FRAME (i.e. sample rate) is generated
  291. * by _counting_ BCLKs. Calculate frame size in BCLKs */
  292. master = mcbsp->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  293. if (master == SND_SOC_DAIFMT_CBS_CFS) {
  294. div = mcbsp->clk_div ? mcbsp->clk_div : 1;
  295. framesize = (mcbsp->in_freq / div) / params_rate(params);
  296. if (framesize < wlen * channels) {
  297. printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
  298. "channels\n", __func__);
  299. return -EINVAL;
  300. }
  301. } else
  302. framesize = wlen * channels;
  303. /* Set FS period and length in terms of bit clock periods */
  304. regs->srgr2 &= ~FPER(0xfff);
  305. regs->srgr1 &= ~FWID(0xff);
  306. switch (format) {
  307. case SND_SOC_DAIFMT_I2S:
  308. case SND_SOC_DAIFMT_LEFT_J:
  309. regs->srgr2 |= FPER(framesize - 1);
  310. regs->srgr1 |= FWID((framesize >> 1) - 1);
  311. break;
  312. case SND_SOC_DAIFMT_DSP_A:
  313. case SND_SOC_DAIFMT_DSP_B:
  314. regs->srgr2 |= FPER(framesize - 1);
  315. regs->srgr1 |= FWID(0);
  316. break;
  317. }
  318. omap_mcbsp_config(mcbsp, &mcbsp->cfg_regs);
  319. mcbsp->wlen = wlen;
  320. mcbsp->configured = 1;
  321. return 0;
  322. }
  323. /*
  324. * This must be called before _set_clkdiv and _set_sysclk since McBSP register
  325. * cache is initialized here
  326. */
  327. static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  328. unsigned int fmt)
  329. {
  330. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  331. struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
  332. bool inv_fs = false;
  333. if (mcbsp->configured)
  334. return 0;
  335. mcbsp->fmt = fmt;
  336. memset(regs, 0, sizeof(*regs));
  337. /* Generic McBSP register settings */
  338. regs->spcr2 |= XINTM(3) | FREE;
  339. regs->spcr1 |= RINTM(3);
  340. /* RFIG and XFIG are not defined in 2430 and on OMAP3+ */
  341. if (!mcbsp->pdata->has_ccr) {
  342. regs->rcr2 |= RFIG;
  343. regs->xcr2 |= XFIG;
  344. }
  345. /* Configure XCCR/RCCR only for revisions which have ccr registers */
  346. if (mcbsp->pdata->has_ccr) {
  347. regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
  348. regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
  349. }
  350. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  351. case SND_SOC_DAIFMT_I2S:
  352. /* 1-bit data delay */
  353. regs->rcr2 |= RDATDLY(1);
  354. regs->xcr2 |= XDATDLY(1);
  355. break;
  356. case SND_SOC_DAIFMT_LEFT_J:
  357. /* 0-bit data delay */
  358. regs->rcr2 |= RDATDLY(0);
  359. regs->xcr2 |= XDATDLY(0);
  360. regs->spcr1 |= RJUST(2);
  361. /* Invert FS polarity configuration */
  362. inv_fs = true;
  363. break;
  364. case SND_SOC_DAIFMT_DSP_A:
  365. /* 1-bit data delay */
  366. regs->rcr2 |= RDATDLY(1);
  367. regs->xcr2 |= XDATDLY(1);
  368. /* Invert FS polarity configuration */
  369. inv_fs = true;
  370. break;
  371. case SND_SOC_DAIFMT_DSP_B:
  372. /* 0-bit data delay */
  373. regs->rcr2 |= RDATDLY(0);
  374. regs->xcr2 |= XDATDLY(0);
  375. /* Invert FS polarity configuration */
  376. inv_fs = true;
  377. break;
  378. default:
  379. /* Unsupported data format */
  380. return -EINVAL;
  381. }
  382. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  383. case SND_SOC_DAIFMT_CBS_CFS:
  384. /* McBSP master. Set FS and bit clocks as outputs */
  385. regs->pcr0 |= FSXM | FSRM |
  386. CLKXM | CLKRM;
  387. /* Sample rate generator drives the FS */
  388. regs->srgr2 |= FSGM;
  389. break;
  390. case SND_SOC_DAIFMT_CBM_CFM:
  391. /* McBSP slave */
  392. break;
  393. default:
  394. /* Unsupported master/slave configuration */
  395. return -EINVAL;
  396. }
  397. /* Set bit clock (CLKX/CLKR) and FS polarities */
  398. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  399. case SND_SOC_DAIFMT_NB_NF:
  400. /*
  401. * Normal BCLK + FS.
  402. * FS active low. TX data driven on falling edge of bit clock
  403. * and RX data sampled on rising edge of bit clock.
  404. */
  405. regs->pcr0 |= FSXP | FSRP |
  406. CLKXP | CLKRP;
  407. break;
  408. case SND_SOC_DAIFMT_NB_IF:
  409. regs->pcr0 |= CLKXP | CLKRP;
  410. break;
  411. case SND_SOC_DAIFMT_IB_NF:
  412. regs->pcr0 |= FSXP | FSRP;
  413. break;
  414. case SND_SOC_DAIFMT_IB_IF:
  415. break;
  416. default:
  417. return -EINVAL;
  418. }
  419. if (inv_fs == true)
  420. regs->pcr0 ^= FSXP | FSRP;
  421. return 0;
  422. }
  423. static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  424. int div_id, int div)
  425. {
  426. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  427. struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
  428. if (div_id != OMAP_MCBSP_CLKGDV)
  429. return -ENODEV;
  430. mcbsp->clk_div = div;
  431. regs->srgr1 &= ~CLKGDV(0xff);
  432. regs->srgr1 |= CLKGDV(div - 1);
  433. return 0;
  434. }
  435. static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  436. int clk_id, unsigned int freq,
  437. int dir)
  438. {
  439. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  440. struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
  441. int err = 0;
  442. if (mcbsp->active) {
  443. if (freq == mcbsp->in_freq)
  444. return 0;
  445. else
  446. return -EBUSY;
  447. }
  448. mcbsp->in_freq = freq;
  449. regs->srgr2 &= ~CLKSM;
  450. regs->pcr0 &= ~SCLKME;
  451. switch (clk_id) {
  452. case OMAP_MCBSP_SYSCLK_CLK:
  453. regs->srgr2 |= CLKSM;
  454. break;
  455. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  456. if (cpu_class_is_omap1()) {
  457. err = -EINVAL;
  458. break;
  459. }
  460. err = omap2_mcbsp_set_clks_src(mcbsp,
  461. MCBSP_CLKS_PRCM_SRC);
  462. break;
  463. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  464. if (cpu_class_is_omap1()) {
  465. err = 0;
  466. break;
  467. }
  468. err = omap2_mcbsp_set_clks_src(mcbsp,
  469. MCBSP_CLKS_PAD_SRC);
  470. break;
  471. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  472. regs->srgr2 |= CLKSM;
  473. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  474. regs->pcr0 |= SCLKME;
  475. break;
  476. default:
  477. err = -ENODEV;
  478. }
  479. return err;
  480. }
  481. static const struct snd_soc_dai_ops mcbsp_dai_ops = {
  482. .startup = omap_mcbsp_dai_startup,
  483. .shutdown = omap_mcbsp_dai_shutdown,
  484. .trigger = omap_mcbsp_dai_trigger,
  485. .delay = omap_mcbsp_dai_delay,
  486. .hw_params = omap_mcbsp_dai_hw_params,
  487. .set_fmt = omap_mcbsp_dai_set_dai_fmt,
  488. .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
  489. .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
  490. };
  491. static int omap_mcbsp_probe(struct snd_soc_dai *dai)
  492. {
  493. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(dai);
  494. pm_runtime_enable(mcbsp->dev);
  495. return 0;
  496. }
  497. static int omap_mcbsp_remove(struct snd_soc_dai *dai)
  498. {
  499. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(dai);
  500. pm_runtime_disable(mcbsp->dev);
  501. return 0;
  502. }
  503. static struct snd_soc_dai_driver omap_mcbsp_dai = {
  504. .probe = omap_mcbsp_probe,
  505. .remove = omap_mcbsp_remove,
  506. .playback = {
  507. .channels_min = 1,
  508. .channels_max = 16,
  509. .rates = OMAP_MCBSP_RATES,
  510. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  511. },
  512. .capture = {
  513. .channels_min = 1,
  514. .channels_max = 16,
  515. .rates = OMAP_MCBSP_RATES,
  516. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  517. },
  518. .ops = &mcbsp_dai_ops,
  519. };
  520. static int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
  521. struct snd_ctl_elem_info *uinfo)
  522. {
  523. struct soc_mixer_control *mc =
  524. (struct soc_mixer_control *)kcontrol->private_value;
  525. int max = mc->max;
  526. int min = mc->min;
  527. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  528. uinfo->count = 1;
  529. uinfo->value.integer.min = min;
  530. uinfo->value.integer.max = max;
  531. return 0;
  532. }
  533. #define OMAP_MCBSP_ST_CHANNEL_VOLUME(channel) \
  534. static int \
  535. omap_mcbsp_set_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  536. struct snd_ctl_elem_value *uc) \
  537. { \
  538. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kc); \
  539. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); \
  540. struct soc_mixer_control *mc = \
  541. (struct soc_mixer_control *)kc->private_value; \
  542. int max = mc->max; \
  543. int min = mc->min; \
  544. int val = uc->value.integer.value[0]; \
  545. \
  546. if (val < min || val > max) \
  547. return -EINVAL; \
  548. \
  549. /* OMAP McBSP implementation uses index values 0..4 */ \
  550. return omap_st_set_chgain(mcbsp, channel, val); \
  551. } \
  552. \
  553. static int \
  554. omap_mcbsp_get_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. s16 chgain; \
  560. \
  561. if (omap_st_get_chgain(mcbsp, channel, &chgain)) \
  562. return -EAGAIN; \
  563. \
  564. uc->value.integer.value[0] = chgain; \
  565. return 0; \
  566. }
  567. OMAP_MCBSP_ST_CHANNEL_VOLUME(0)
  568. OMAP_MCBSP_ST_CHANNEL_VOLUME(1)
  569. static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
  570. struct snd_ctl_elem_value *ucontrol)
  571. {
  572. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  573. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  574. u8 value = ucontrol->value.integer.value[0];
  575. if (value == omap_st_is_enabled(mcbsp))
  576. return 0;
  577. if (value)
  578. omap_st_enable(mcbsp);
  579. else
  580. omap_st_disable(mcbsp);
  581. return 1;
  582. }
  583. static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
  584. struct snd_ctl_elem_value *ucontrol)
  585. {
  586. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  587. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  588. ucontrol->value.integer.value[0] = omap_st_is_enabled(mcbsp);
  589. return 0;
  590. }
  591. #define OMAP_MCBSP_ST_CONTROLS(port) \
  592. static const struct snd_kcontrol_new omap_mcbsp##port##_st_controls[] = { \
  593. SOC_SINGLE_EXT("McBSP" #port " Sidetone Switch", 1, 0, 1, 0, \
  594. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode), \
  595. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP" #port " Sidetone Channel 0 Volume", \
  596. -32768, 32767, \
  597. omap_mcbsp_get_st_ch0_volume, \
  598. omap_mcbsp_set_st_ch0_volume), \
  599. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP" #port " Sidetone Channel 1 Volume", \
  600. -32768, 32767, \
  601. omap_mcbsp_get_st_ch1_volume, \
  602. omap_mcbsp_set_st_ch1_volume), \
  603. }
  604. OMAP_MCBSP_ST_CONTROLS(2);
  605. OMAP_MCBSP_ST_CONTROLS(3);
  606. int omap_mcbsp_st_add_controls(struct snd_soc_pcm_runtime *rtd)
  607. {
  608. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  609. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  610. if (!mcbsp->st_data) {
  611. dev_warn(mcbsp->dev, "No sidetone data for port\n");
  612. return 0;
  613. }
  614. switch (mcbsp->id) {
  615. case 2: /* McBSP 2 */
  616. return snd_soc_add_dai_controls(cpu_dai,
  617. omap_mcbsp2_st_controls,
  618. ARRAY_SIZE(omap_mcbsp2_st_controls));
  619. case 3: /* McBSP 3 */
  620. return snd_soc_add_dai_controls(cpu_dai,
  621. omap_mcbsp3_st_controls,
  622. ARRAY_SIZE(omap_mcbsp3_st_controls));
  623. default:
  624. break;
  625. }
  626. return -EINVAL;
  627. }
  628. EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
  629. static struct omap_mcbsp_platform_data omap2420_pdata = {
  630. .reg_step = 4,
  631. .reg_size = 2,
  632. };
  633. static struct omap_mcbsp_platform_data omap2430_pdata = {
  634. .reg_step = 4,
  635. .reg_size = 4,
  636. .has_ccr = true,
  637. };
  638. static struct omap_mcbsp_platform_data omap3_pdata = {
  639. .reg_step = 4,
  640. .reg_size = 4,
  641. .has_ccr = true,
  642. .has_wakeup = true,
  643. };
  644. static struct omap_mcbsp_platform_data omap4_pdata = {
  645. .reg_step = 4,
  646. .reg_size = 4,
  647. .has_ccr = true,
  648. .has_wakeup = true,
  649. };
  650. static const struct of_device_id omap_mcbsp_of_match[] = {
  651. {
  652. .compatible = "ti,omap2420-mcbsp",
  653. .data = &omap2420_pdata,
  654. },
  655. {
  656. .compatible = "ti,omap2430-mcbsp",
  657. .data = &omap2430_pdata,
  658. },
  659. {
  660. .compatible = "ti,omap3-mcbsp",
  661. .data = &omap3_pdata,
  662. },
  663. {
  664. .compatible = "ti,omap4-mcbsp",
  665. .data = &omap4_pdata,
  666. },
  667. { },
  668. };
  669. MODULE_DEVICE_TABLE(of, omap_mcbsp_of_match);
  670. static __devinit int asoc_mcbsp_probe(struct platform_device *pdev)
  671. {
  672. struct omap_mcbsp_platform_data *pdata = dev_get_platdata(&pdev->dev);
  673. struct omap_mcbsp *mcbsp;
  674. const struct of_device_id *match;
  675. int ret;
  676. match = of_match_device(omap_mcbsp_of_match, &pdev->dev);
  677. if (match) {
  678. struct device_node *node = pdev->dev.of_node;
  679. int buffer_size;
  680. pdata = devm_kzalloc(&pdev->dev,
  681. sizeof(struct omap_mcbsp_platform_data),
  682. GFP_KERNEL);
  683. if (!pdata)
  684. return -ENOMEM;
  685. memcpy(pdata, match->data, sizeof(*pdata));
  686. if (!of_property_read_u32(node, "ti,buffer-size", &buffer_size))
  687. pdata->buffer_size = buffer_size;
  688. } else if (!pdata) {
  689. dev_err(&pdev->dev, "missing platform data.\n");
  690. return -EINVAL;
  691. }
  692. mcbsp = devm_kzalloc(&pdev->dev, sizeof(struct omap_mcbsp), GFP_KERNEL);
  693. if (!mcbsp)
  694. return -ENOMEM;
  695. mcbsp->id = pdev->id;
  696. mcbsp->pdata = pdata;
  697. mcbsp->dev = &pdev->dev;
  698. platform_set_drvdata(pdev, mcbsp);
  699. ret = omap_mcbsp_init(pdev);
  700. if (!ret)
  701. return snd_soc_register_dai(&pdev->dev, &omap_mcbsp_dai);
  702. return ret;
  703. }
  704. static int __devexit asoc_mcbsp_remove(struct platform_device *pdev)
  705. {
  706. struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
  707. snd_soc_unregister_dai(&pdev->dev);
  708. if (mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  709. mcbsp->pdata->ops->free(mcbsp->id);
  710. omap_mcbsp_sysfs_remove(mcbsp);
  711. clk_put(mcbsp->fclk);
  712. platform_set_drvdata(pdev, NULL);
  713. return 0;
  714. }
  715. static struct platform_driver asoc_mcbsp_driver = {
  716. .driver = {
  717. .name = "omap-mcbsp",
  718. .owner = THIS_MODULE,
  719. .of_match_table = omap_mcbsp_of_match,
  720. },
  721. .probe = asoc_mcbsp_probe,
  722. .remove = __devexit_p(asoc_mcbsp_remove),
  723. };
  724. module_platform_driver(asoc_mcbsp_driver);
  725. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  726. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  727. MODULE_LICENSE("GPL");
  728. MODULE_ALIAS("platform:omap-mcbsp");