omap-mcbsp.c 22 KB

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