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