omap-mcbsp.c 22 KB

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