omap-mcbsp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/initval.h>
  31. #include <sound/soc.h>
  32. #include <plat/dma.h>
  33. #include <plat/mcbsp.h>
  34. #include "mcbsp.h"
  35. #include "omap-mcbsp.h"
  36. #include "omap-pcm.h"
  37. #define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000)
  38. #define OMAP_MCBSP_SOC_SINGLE_S16_EXT(xname, xmin, xmax, \
  39. xhandler_get, xhandler_put) \
  40. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  41. .info = omap_mcbsp_st_info_volsw, \
  42. .get = xhandler_get, .put = xhandler_put, \
  43. .private_value = (unsigned long) &(struct soc_mixer_control) \
  44. {.min = xmin, .max = xmax} }
  45. enum {
  46. OMAP_MCBSP_WORD_8 = 0,
  47. OMAP_MCBSP_WORD_12,
  48. OMAP_MCBSP_WORD_16,
  49. OMAP_MCBSP_WORD_20,
  50. OMAP_MCBSP_WORD_24,
  51. OMAP_MCBSP_WORD_32,
  52. };
  53. struct omap_mcbsp_data {
  54. unsigned int bus_id;
  55. struct omap_mcbsp_reg_cfg regs;
  56. unsigned int fmt;
  57. /*
  58. * Flags indicating is the bus already activated and configured by
  59. * another substream
  60. */
  61. int active;
  62. int configured;
  63. unsigned int in_freq;
  64. int clk_div;
  65. int wlen;
  66. };
  67. static struct omap_mcbsp_data mcbsp_data[NUM_LINKS];
  68. /*
  69. * Stream DMA parameters. DMA request line and port address are set runtime
  70. * since they are different between OMAP1 and later OMAPs
  71. */
  72. static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2];
  73. static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream)
  74. {
  75. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  76. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  77. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  78. struct omap_pcm_dma_data *dma_data;
  79. int dma_op_mode = omap_mcbsp_get_dma_op_mode(mcbsp_data->bus_id);
  80. int words;
  81. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  82. /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
  83. if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
  84. /*
  85. * Configure McBSP threshold based on either:
  86. * packet_size, when the sDMA is in packet mode, or
  87. * based on the period size.
  88. */
  89. if (dma_data->packet_size)
  90. words = dma_data->packet_size;
  91. else
  92. words = snd_pcm_lib_period_bytes(substream) /
  93. (mcbsp_data->wlen / 8);
  94. else
  95. words = 1;
  96. /* Configure McBSP internal buffer usage */
  97. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  98. omap_mcbsp_set_tx_threshold(mcbsp_data->bus_id, words);
  99. else
  100. omap_mcbsp_set_rx_threshold(mcbsp_data->bus_id, words);
  101. }
  102. static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params,
  103. struct snd_pcm_hw_rule *rule)
  104. {
  105. struct snd_interval *buffer_size = hw_param_interval(params,
  106. SNDRV_PCM_HW_PARAM_BUFFER_SIZE);
  107. struct snd_interval *channels = hw_param_interval(params,
  108. SNDRV_PCM_HW_PARAM_CHANNELS);
  109. struct omap_mcbsp_data *mcbsp_data = rule->private;
  110. struct snd_interval frames;
  111. int size;
  112. snd_interval_any(&frames);
  113. size = omap_mcbsp_get_fifo_size(mcbsp_data->bus_id);
  114. frames.min = size / channels->min;
  115. frames.integer = 1;
  116. return snd_interval_refine(buffer_size, &frames);
  117. }
  118. static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
  119. struct snd_soc_dai *cpu_dai)
  120. {
  121. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  122. int bus_id = mcbsp_data->bus_id;
  123. int err = 0;
  124. if (!cpu_dai->active)
  125. err = omap_mcbsp_request(bus_id);
  126. /*
  127. * OMAP3 McBSP FIFO is word structured.
  128. * McBSP2 has 1024 + 256 = 1280 word long buffer,
  129. * McBSP1,3,4,5 has 128 word long buffer
  130. * This means that the size of the FIFO depends on the sample format.
  131. * For example on McBSP3:
  132. * 16bit samples: size is 128 * 2 = 256 bytes
  133. * 32bit samples: size is 128 * 4 = 512 bytes
  134. * It is simpler to place constraint for buffer and period based on
  135. * channels.
  136. * McBSP3 as example again (16 or 32 bit samples):
  137. * 1 channel (mono): size is 128 frames (128 words)
  138. * 2 channels (stereo): size is 128 / 2 = 64 frames (2 * 64 words)
  139. * 4 channels: size is 128 / 4 = 32 frames (4 * 32 words)
  140. */
  141. if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  142. /*
  143. * Rule for the buffer size. We should not allow
  144. * smaller buffer than the FIFO size to avoid underruns
  145. */
  146. snd_pcm_hw_rule_add(substream->runtime, 0,
  147. SNDRV_PCM_HW_PARAM_CHANNELS,
  148. omap_mcbsp_hwrule_min_buffersize,
  149. mcbsp_data,
  150. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
  151. /* Make sure, that the period size is always even */
  152. snd_pcm_hw_constraint_step(substream->runtime, 0,
  153. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
  154. }
  155. return err;
  156. }
  157. static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
  158. struct snd_soc_dai *cpu_dai)
  159. {
  160. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  161. if (!cpu_dai->active) {
  162. omap_mcbsp_free(mcbsp_data->bus_id);
  163. mcbsp_data->configured = 0;
  164. }
  165. }
  166. static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  167. struct snd_soc_dai *cpu_dai)
  168. {
  169. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  170. int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  171. switch (cmd) {
  172. case SNDRV_PCM_TRIGGER_START:
  173. case SNDRV_PCM_TRIGGER_RESUME:
  174. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  175. mcbsp_data->active++;
  176. omap_mcbsp_start(mcbsp_data->bus_id, play, !play);
  177. break;
  178. case SNDRV_PCM_TRIGGER_STOP:
  179. case SNDRV_PCM_TRIGGER_SUSPEND:
  180. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  181. omap_mcbsp_stop(mcbsp_data->bus_id, play, !play);
  182. mcbsp_data->active--;
  183. break;
  184. default:
  185. err = -EINVAL;
  186. }
  187. return err;
  188. }
  189. static snd_pcm_sframes_t omap_mcbsp_dai_delay(
  190. struct snd_pcm_substream *substream,
  191. struct snd_soc_dai *dai)
  192. {
  193. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  194. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  195. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  196. u16 fifo_use;
  197. snd_pcm_sframes_t delay;
  198. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  199. fifo_use = omap_mcbsp_get_tx_delay(mcbsp_data->bus_id);
  200. else
  201. fifo_use = omap_mcbsp_get_rx_delay(mcbsp_data->bus_id);
  202. /*
  203. * Divide the used locations with the channel count to get the
  204. * FIFO usage in samples (don't care about partial samples in the
  205. * buffer).
  206. */
  207. delay = fifo_use / substream->runtime->channels;
  208. return delay;
  209. }
  210. static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
  211. struct snd_pcm_hw_params *params,
  212. struct snd_soc_dai *cpu_dai)
  213. {
  214. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  215. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  216. struct omap_pcm_dma_data *dma_data;
  217. int dma, bus_id = mcbsp_data->bus_id;
  218. int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT;
  219. int pkt_size = 0;
  220. unsigned long port;
  221. unsigned int format, div, framesize, master;
  222. dma_data = &omap_mcbsp_dai_dma_params[cpu_dai->id][substream->stream];
  223. dma = omap_mcbsp_dma_ch_params(bus_id, substream->stream);
  224. port = omap_mcbsp_dma_reg_params(bus_id, substream->stream);
  225. switch (params_format(params)) {
  226. case SNDRV_PCM_FORMAT_S16_LE:
  227. dma_data->data_type = OMAP_DMA_DATA_TYPE_S16;
  228. wlen = 16;
  229. break;
  230. case SNDRV_PCM_FORMAT_S32_LE:
  231. dma_data->data_type = OMAP_DMA_DATA_TYPE_S32;
  232. wlen = 32;
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  238. dma_data->set_threshold = omap_mcbsp_set_threshold;
  239. /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
  240. if (omap_mcbsp_get_dma_op_mode(bus_id) ==
  241. MCBSP_DMA_MODE_THRESHOLD) {
  242. int period_words, max_thrsh;
  243. period_words = params_period_bytes(params) / (wlen / 8);
  244. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  245. max_thrsh = omap_mcbsp_get_max_tx_threshold(
  246. mcbsp_data->bus_id);
  247. else
  248. max_thrsh = omap_mcbsp_get_max_rx_threshold(
  249. mcbsp_data->bus_id);
  250. /*
  251. * If the period contains less or equal number of words,
  252. * we are using the original threshold mode setup:
  253. * McBSP threshold = sDMA frame size = period_size
  254. * Otherwise we switch to sDMA packet mode:
  255. * McBSP threshold = sDMA packet size
  256. * sDMA frame size = period size
  257. */
  258. if (period_words > max_thrsh) {
  259. int divider = 0;
  260. /*
  261. * Look for the biggest threshold value, which
  262. * divides the period size evenly.
  263. */
  264. divider = period_words / max_thrsh;
  265. if (period_words % max_thrsh)
  266. divider++;
  267. while (period_words % divider &&
  268. divider < period_words)
  269. divider++;
  270. if (divider == period_words)
  271. return -EINVAL;
  272. pkt_size = period_words / divider;
  273. sync_mode = OMAP_DMA_SYNC_PACKET;
  274. } else {
  275. sync_mode = OMAP_DMA_SYNC_FRAME;
  276. }
  277. }
  278. }
  279. dma_data->name = substream->stream ? "Audio Capture" : "Audio Playback";
  280. dma_data->dma_req = dma;
  281. dma_data->port_addr = port;
  282. dma_data->sync_mode = sync_mode;
  283. dma_data->packet_size = pkt_size;
  284. snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
  285. if (mcbsp_data->configured) {
  286. /* McBSP already configured by another stream */
  287. return 0;
  288. }
  289. regs->rcr2 &= ~(RPHASE | RFRLEN2(0x7f) | RWDLEN2(7));
  290. regs->xcr2 &= ~(RPHASE | XFRLEN2(0x7f) | XWDLEN2(7));
  291. regs->rcr1 &= ~(RFRLEN1(0x7f) | RWDLEN1(7));
  292. regs->xcr1 &= ~(XFRLEN1(0x7f) | XWDLEN1(7));
  293. format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  294. wpf = channels = params_channels(params);
  295. if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
  296. format == SND_SOC_DAIFMT_LEFT_J)) {
  297. /* Use dual-phase frames */
  298. regs->rcr2 |= RPHASE;
  299. regs->xcr2 |= XPHASE;
  300. /* Set 1 word per (McBSP) frame for phase1 and phase2 */
  301. wpf--;
  302. regs->rcr2 |= RFRLEN2(wpf - 1);
  303. regs->xcr2 |= XFRLEN2(wpf - 1);
  304. }
  305. regs->rcr1 |= RFRLEN1(wpf - 1);
  306. regs->xcr1 |= XFRLEN1(wpf - 1);
  307. switch (params_format(params)) {
  308. case SNDRV_PCM_FORMAT_S16_LE:
  309. /* Set word lengths */
  310. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
  311. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
  312. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
  313. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
  314. break;
  315. case SNDRV_PCM_FORMAT_S32_LE:
  316. /* Set word lengths */
  317. regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_32);
  318. regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_32);
  319. regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_32);
  320. regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_32);
  321. break;
  322. default:
  323. /* Unsupported PCM format */
  324. return -EINVAL;
  325. }
  326. /* In McBSP master modes, FRAME (i.e. sample rate) is generated
  327. * by _counting_ BCLKs. Calculate frame size in BCLKs */
  328. master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  329. if (master == SND_SOC_DAIFMT_CBS_CFS) {
  330. div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
  331. framesize = (mcbsp_data->in_freq / div) / params_rate(params);
  332. if (framesize < wlen * channels) {
  333. printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
  334. "channels\n", __func__);
  335. return -EINVAL;
  336. }
  337. } else
  338. framesize = wlen * channels;
  339. /* Set FS period and length in terms of bit clock periods */
  340. regs->srgr2 &= ~FPER(0xfff);
  341. regs->srgr1 &= ~FWID(0xff);
  342. switch (format) {
  343. case SND_SOC_DAIFMT_I2S:
  344. case SND_SOC_DAIFMT_LEFT_J:
  345. regs->srgr2 |= FPER(framesize - 1);
  346. regs->srgr1 |= FWID((framesize >> 1) - 1);
  347. break;
  348. case SND_SOC_DAIFMT_DSP_A:
  349. case SND_SOC_DAIFMT_DSP_B:
  350. regs->srgr2 |= FPER(framesize - 1);
  351. regs->srgr1 |= FWID(0);
  352. break;
  353. }
  354. omap_mcbsp_config(bus_id, &mcbsp_data->regs);
  355. mcbsp_data->wlen = wlen;
  356. mcbsp_data->configured = 1;
  357. return 0;
  358. }
  359. /*
  360. * This must be called before _set_clkdiv and _set_sysclk since McBSP register
  361. * cache is initialized here
  362. */
  363. static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  364. unsigned int fmt)
  365. {
  366. struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  367. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  368. bool inv_fs = false;
  369. if (mcbsp_data->configured)
  370. return 0;
  371. mcbsp_data->fmt = fmt;
  372. memset(regs, 0, sizeof(*regs));
  373. /* Generic McBSP register settings */
  374. regs->spcr2 |= XINTM(3) | FREE;
  375. regs->spcr1 |= RINTM(3);
  376. /* RFIG and XFIG are not defined in 34xx */
  377. if (!cpu_is_omap34xx() && !cpu_is_omap44xx()) {
  378. regs->rcr2 |= RFIG;
  379. regs->xcr2 |= XFIG;
  380. }
  381. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  382. regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
  383. regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
  384. }
  385. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  386. case SND_SOC_DAIFMT_I2S:
  387. /* 1-bit data delay */
  388. regs->rcr2 |= RDATDLY(1);
  389. regs->xcr2 |= XDATDLY(1);
  390. break;
  391. case SND_SOC_DAIFMT_LEFT_J:
  392. /* 0-bit data delay */
  393. regs->rcr2 |= RDATDLY(0);
  394. regs->xcr2 |= XDATDLY(0);
  395. regs->spcr1 |= RJUST(2);
  396. /* Invert FS polarity configuration */
  397. inv_fs = true;
  398. break;
  399. case SND_SOC_DAIFMT_DSP_A:
  400. /* 1-bit data delay */
  401. regs->rcr2 |= RDATDLY(1);
  402. regs->xcr2 |= XDATDLY(1);
  403. /* Invert FS polarity configuration */
  404. inv_fs = true;
  405. break;
  406. case SND_SOC_DAIFMT_DSP_B:
  407. /* 0-bit data delay */
  408. regs->rcr2 |= RDATDLY(0);
  409. regs->xcr2 |= XDATDLY(0);
  410. /* Invert FS polarity configuration */
  411. inv_fs = true;
  412. break;
  413. default:
  414. /* Unsupported data format */
  415. return -EINVAL;
  416. }
  417. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  418. case SND_SOC_DAIFMT_CBS_CFS:
  419. /* McBSP master. Set FS and bit clocks as outputs */
  420. regs->pcr0 |= FSXM | FSRM |
  421. CLKXM | CLKRM;
  422. /* Sample rate generator drives the FS */
  423. regs->srgr2 |= FSGM;
  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_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  462. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  463. if (div_id != OMAP_MCBSP_CLKGDV)
  464. return -ENODEV;
  465. mcbsp_data->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_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
  475. struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
  476. int err = 0;
  477. if (mcbsp_data->active) {
  478. if (freq == mcbsp_data->in_freq)
  479. return 0;
  480. else
  481. return -EBUSY;
  482. }
  483. /* The McBSP signal muxing functions are only available on McBSP1 */
  484. if (clk_id == OMAP_MCBSP_CLKR_SRC_CLKR ||
  485. clk_id == OMAP_MCBSP_CLKR_SRC_CLKX ||
  486. clk_id == OMAP_MCBSP_FSR_SRC_FSR ||
  487. clk_id == OMAP_MCBSP_FSR_SRC_FSX)
  488. if (cpu_class_is_omap1() || mcbsp_data->bus_id != 0)
  489. return -EINVAL;
  490. mcbsp_data->in_freq = freq;
  491. regs->srgr2 &= ~CLKSM;
  492. regs->pcr0 &= ~SCLKME;
  493. switch (clk_id) {
  494. case OMAP_MCBSP_SYSCLK_CLK:
  495. regs->srgr2 |= CLKSM;
  496. break;
  497. case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
  498. if (cpu_class_is_omap1()) {
  499. err = -EINVAL;
  500. break;
  501. }
  502. err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id,
  503. MCBSP_CLKS_PRCM_SRC);
  504. break;
  505. case OMAP_MCBSP_SYSCLK_CLKS_EXT:
  506. if (cpu_class_is_omap1()) {
  507. err = 0;
  508. break;
  509. }
  510. err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id,
  511. MCBSP_CLKS_PAD_SRC);
  512. break;
  513. case OMAP_MCBSP_SYSCLK_CLKX_EXT:
  514. regs->srgr2 |= CLKSM;
  515. case OMAP_MCBSP_SYSCLK_CLKR_EXT:
  516. regs->pcr0 |= SCLKME;
  517. break;
  518. case OMAP_MCBSP_CLKR_SRC_CLKR:
  519. if (cpu_class_is_omap1())
  520. break;
  521. omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKR);
  522. break;
  523. case OMAP_MCBSP_CLKR_SRC_CLKX:
  524. if (cpu_class_is_omap1())
  525. break;
  526. omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKX);
  527. break;
  528. case OMAP_MCBSP_FSR_SRC_FSR:
  529. if (cpu_class_is_omap1())
  530. break;
  531. omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSR);
  532. break;
  533. case OMAP_MCBSP_FSR_SRC_FSX:
  534. if (cpu_class_is_omap1())
  535. break;
  536. omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSX);
  537. break;
  538. default:
  539. err = -ENODEV;
  540. }
  541. return err;
  542. }
  543. static const struct snd_soc_dai_ops mcbsp_dai_ops = {
  544. .startup = omap_mcbsp_dai_startup,
  545. .shutdown = omap_mcbsp_dai_shutdown,
  546. .trigger = omap_mcbsp_dai_trigger,
  547. .delay = omap_mcbsp_dai_delay,
  548. .hw_params = omap_mcbsp_dai_hw_params,
  549. .set_fmt = omap_mcbsp_dai_set_dai_fmt,
  550. .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
  551. .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
  552. };
  553. static int mcbsp_dai_probe(struct snd_soc_dai *dai)
  554. {
  555. mcbsp_data[dai->id].bus_id = dai->id;
  556. snd_soc_dai_set_drvdata(dai, &mcbsp_data[dai->id].bus_id);
  557. return 0;
  558. }
  559. static struct snd_soc_dai_driver omap_mcbsp_dai = {
  560. .probe = mcbsp_dai_probe,
  561. .playback = {
  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. .capture = {
  568. .channels_min = 1,
  569. .channels_max = 16,
  570. .rates = OMAP_MCBSP_RATES,
  571. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  572. },
  573. .ops = &mcbsp_dai_ops,
  574. };
  575. static int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
  576. struct snd_ctl_elem_info *uinfo)
  577. {
  578. struct soc_mixer_control *mc =
  579. (struct soc_mixer_control *)kcontrol->private_value;
  580. int max = mc->max;
  581. int min = mc->min;
  582. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  583. uinfo->count = 1;
  584. uinfo->value.integer.min = min;
  585. uinfo->value.integer.max = max;
  586. return 0;
  587. }
  588. #define OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(id, channel) \
  589. static int \
  590. omap_mcbsp##id##_set_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  591. struct snd_ctl_elem_value *uc) \
  592. { \
  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((id)-1, channel, val); \
  604. }
  605. #define OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(id, channel) \
  606. static int \
  607. omap_mcbsp##id##_get_st_ch##channel##_volume(struct snd_kcontrol *kc, \
  608. struct snd_ctl_elem_value *uc) \
  609. { \
  610. s16 chgain; \
  611. \
  612. if (omap_st_get_chgain((id)-1, channel, &chgain)) \
  613. return -EAGAIN; \
  614. \
  615. uc->value.integer.value[0] = chgain; \
  616. return 0; \
  617. }
  618. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 0)
  619. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 1)
  620. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 0)
  621. OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 1)
  622. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 0)
  623. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 1)
  624. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 0)
  625. OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 1)
  626. static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
  627. struct snd_ctl_elem_value *ucontrol)
  628. {
  629. struct soc_mixer_control *mc =
  630. (struct soc_mixer_control *)kcontrol->private_value;
  631. u8 value = ucontrol->value.integer.value[0];
  632. if (value == omap_st_is_enabled(mc->reg))
  633. return 0;
  634. if (value)
  635. omap_st_enable(mc->reg);
  636. else
  637. omap_st_disable(mc->reg);
  638. return 1;
  639. }
  640. static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
  641. struct snd_ctl_elem_value *ucontrol)
  642. {
  643. struct soc_mixer_control *mc =
  644. (struct soc_mixer_control *)kcontrol->private_value;
  645. ucontrol->value.integer.value[0] = omap_st_is_enabled(mc->reg);
  646. return 0;
  647. }
  648. static const struct snd_kcontrol_new omap_mcbsp2_st_controls[] = {
  649. SOC_SINGLE_EXT("McBSP2 Sidetone Switch", 1, 0, 1, 0,
  650. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
  651. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 0 Volume",
  652. -32768, 32767,
  653. omap_mcbsp2_get_st_ch0_volume,
  654. omap_mcbsp2_set_st_ch0_volume),
  655. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 1 Volume",
  656. -32768, 32767,
  657. omap_mcbsp2_get_st_ch1_volume,
  658. omap_mcbsp2_set_st_ch1_volume),
  659. };
  660. static const struct snd_kcontrol_new omap_mcbsp3_st_controls[] = {
  661. SOC_SINGLE_EXT("McBSP3 Sidetone Switch", 2, 0, 1, 0,
  662. omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
  663. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 0 Volume",
  664. -32768, 32767,
  665. omap_mcbsp3_get_st_ch0_volume,
  666. omap_mcbsp3_set_st_ch0_volume),
  667. OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 1 Volume",
  668. -32768, 32767,
  669. omap_mcbsp3_get_st_ch1_volume,
  670. omap_mcbsp3_set_st_ch1_volume),
  671. };
  672. int omap_mcbsp_st_add_controls(struct snd_soc_dai *dai)
  673. {
  674. if (!cpu_is_omap34xx())
  675. return -ENODEV;
  676. switch (dai->id) {
  677. case 1: /* McBSP 2 */
  678. return snd_soc_add_dai_controls(dai, omap_mcbsp2_st_controls,
  679. ARRAY_SIZE(omap_mcbsp2_st_controls));
  680. case 2: /* McBSP 3 */
  681. return snd_soc_add_dai_controls(dai, omap_mcbsp3_st_controls,
  682. ARRAY_SIZE(omap_mcbsp3_st_controls));
  683. default:
  684. break;
  685. }
  686. return -EINVAL;
  687. }
  688. EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
  689. static __devinit int asoc_mcbsp_probe(struct platform_device *pdev)
  690. {
  691. return snd_soc_register_dai(&pdev->dev, &omap_mcbsp_dai);
  692. }
  693. static int __devexit asoc_mcbsp_remove(struct platform_device *pdev)
  694. {
  695. snd_soc_unregister_dai(&pdev->dev);
  696. return 0;
  697. }
  698. static struct platform_driver asoc_mcbsp_driver = {
  699. .driver = {
  700. .name = "omap-mcbsp-dai",
  701. .owner = THIS_MODULE,
  702. },
  703. .probe = asoc_mcbsp_probe,
  704. .remove = __devexit_p(asoc_mcbsp_remove),
  705. };
  706. module_platform_driver(asoc_mcbsp_driver);
  707. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
  708. MODULE_DESCRIPTION("OMAP I2S SoC Interface");
  709. MODULE_LICENSE("GPL");