omap-mcbsp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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_CFM:
  389. /* McBSP slave */
  390. break;
  391. default:
  392. /* Unsupported master/slave configuration */
  393. return -EINVAL;
  394. }
  395. /* Set bit clock (CLKX/CLKR) and FS polarities */
  396. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  397. case SND_SOC_DAIFMT_NB_NF:
  398. /*
  399. * Normal BCLK + FS.
  400. * FS active low. TX data driven on falling edge of bit clock
  401. * and RX data sampled on rising edge of bit clock.
  402. */
  403. regs->pcr0 |= FSXP | FSRP |
  404. CLKXP | CLKRP;
  405. break;
  406. case SND_SOC_DAIFMT_NB_IF:
  407. regs->pcr0 |= CLKXP | CLKRP;
  408. break;
  409. case SND_SOC_DAIFMT_IB_NF:
  410. regs->pcr0 |= FSXP | FSRP;
  411. break;
  412. case SND_SOC_DAIFMT_IB_IF:
  413. break;
  414. default:
  415. return -EINVAL;
  416. }
  417. if (inv_fs == true)
  418. regs->pcr0 ^= FSXP | FSRP;
  419. return 0;
  420. }
  421. static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  422. int div_id, int div)
  423. {
  424. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  425. struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
  426. if (div_id != OMAP_MCBSP_CLKGDV)
  427. return -ENODEV;
  428. mcbsp->clk_div = div;
  429. regs->srgr1 &= ~CLKGDV(0xff);
  430. regs->srgr1 |= CLKGDV(div - 1);
  431. return 0;
  432. }
  433. static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  434. int clk_id, unsigned int freq,
  435. int dir)
  436. {
  437. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
  438. struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
  439. int err = 0;
  440. if (mcbsp->active) {
  441. if (freq == mcbsp->in_freq)
  442. return 0;
  443. else
  444. return -EBUSY;
  445. }
  446. mcbsp->in_freq = freq;
  447. regs->srgr2 &= ~CLKSM;
  448. regs->pcr0 &= ~SCLKME;
  449. switch (clk_id) {
  450. case OMAP_MCBSP_SYSCLK_CLK:
  451. regs->srgr2 |= CLKSM;
  452. break;
  453. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  454. if (mcbsp_omap1()) {
  455. err = -EINVAL;
  456. break;
  457. }
  458. err = omap2_mcbsp_set_clks_src(mcbsp,
  459. MCBSP_CLKS_PRCM_SRC);
  460. break;
  461. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  462. if (mcbsp_omap1()) {
  463. err = 0;
  464. break;
  465. }
  466. err = omap2_mcbsp_set_clks_src(mcbsp,
  467. MCBSP_CLKS_PAD_SRC);
  468. break;
  469. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  470. regs->srgr2 |= CLKSM;
  471. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  472. regs->pcr0 |= SCLKME;
  473. break;
  474. default:
  475. err = -ENODEV;
  476. }
  477. return err;
  478. }
  479. static const struct snd_soc_dai_ops mcbsp_dai_ops = {
  480. .startup = omap_mcbsp_dai_startup,
  481. .shutdown = omap_mcbsp_dai_shutdown,
  482. .trigger = omap_mcbsp_dai_trigger,
  483. .delay = omap_mcbsp_dai_delay,
  484. .hw_params = omap_mcbsp_dai_hw_params,
  485. .set_fmt = omap_mcbsp_dai_set_dai_fmt,
  486. .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
  487. .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
  488. };
  489. static int omap_mcbsp_probe(struct snd_soc_dai *dai)
  490. {
  491. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(dai);
  492. pm_runtime_enable(mcbsp->dev);
  493. return 0;
  494. }
  495. static int omap_mcbsp_remove(struct snd_soc_dai *dai)
  496. {
  497. struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(dai);
  498. pm_runtime_disable(mcbsp->dev);
  499. return 0;
  500. }
  501. static struct snd_soc_dai_driver omap_mcbsp_dai = {
  502. .probe = omap_mcbsp_probe,
  503. .remove = omap_mcbsp_remove,
  504. .playback = {
  505. .channels_min = 1,
  506. .channels_max = 16,
  507. .rates = OMAP_MCBSP_RATES,
  508. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  509. },
  510. .capture = {
  511. .channels_min = 1,
  512. .channels_max = 16,
  513. .rates = OMAP_MCBSP_RATES,
  514. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  515. },
  516. .ops = &mcbsp_dai_ops,
  517. };
  518. static const struct snd_soc_component_driver omap_mcbsp_component = {
  519. .name = "omap-mcbsp",
  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 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_component(&pdev->dev, &omap_mcbsp_component,
  703. &omap_mcbsp_dai, 1);
  704. return ret;
  705. }
  706. static int asoc_mcbsp_remove(struct platform_device *pdev)
  707. {
  708. struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
  709. snd_soc_unregister_component(&pdev->dev);
  710. if (mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  711. mcbsp->pdata->ops->free(mcbsp->id);
  712. omap_mcbsp_sysfs_remove(mcbsp);
  713. clk_put(mcbsp->fclk);
  714. platform_set_drvdata(pdev, NULL);
  715. return 0;
  716. }
  717. static struct platform_driver asoc_mcbsp_driver = {
  718. .driver = {
  719. .name = "omap-mcbsp",
  720. .owner = THIS_MODULE,
  721. .of_match_table = omap_mcbsp_of_match,
  722. },
  723. .probe = asoc_mcbsp_probe,
  724. .remove = asoc_mcbsp_remove,
  725. };
  726. module_platform_driver(asoc_mcbsp_driver);
  727. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  728. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  729. MODULE_LICENSE("GPL");
  730. MODULE_ALIAS("platform:omap-mcbsp");