omap-mcbsp.c 22 KB

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