omap-mcbsp.c 24 KB

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