davinci-i2s.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
  3. *
  4. * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
  5. * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/initval.h>
  22. #include <sound/soc.h>
  23. #include <mach/asp.h>
  24. #include "davinci-pcm.h"
  25. #include "davinci-i2s.h"
  26. /*
  27. * NOTE: terminology here is confusing.
  28. *
  29. * - This driver supports the "Audio Serial Port" (ASP),
  30. * found on dm6446, dm355, and other DaVinci chips.
  31. *
  32. * - But it labels it a "Multi-channel Buffered Serial Port"
  33. * (McBSP) as on older chips like the dm642 ... which was
  34. * backward-compatible, possibly explaining that confusion.
  35. *
  36. * - OMAP chips have a controller called McBSP, which is
  37. * incompatible with the DaVinci flavor of McBSP.
  38. *
  39. * - Newer DaVinci chips have a controller called McASP,
  40. * incompatible with ASP and with either McBSP.
  41. *
  42. * In short: this uses ASP to implement I2S, not McBSP.
  43. * And it won't be the only DaVinci implemention of I2S.
  44. */
  45. #define DAVINCI_MCBSP_DRR_REG 0x00
  46. #define DAVINCI_MCBSP_DXR_REG 0x04
  47. #define DAVINCI_MCBSP_SPCR_REG 0x08
  48. #define DAVINCI_MCBSP_RCR_REG 0x0c
  49. #define DAVINCI_MCBSP_XCR_REG 0x10
  50. #define DAVINCI_MCBSP_SRGR_REG 0x14
  51. #define DAVINCI_MCBSP_PCR_REG 0x24
  52. #define DAVINCI_MCBSP_SPCR_RRST (1 << 0)
  53. #define DAVINCI_MCBSP_SPCR_RINTM(v) ((v) << 4)
  54. #define DAVINCI_MCBSP_SPCR_XRST (1 << 16)
  55. #define DAVINCI_MCBSP_SPCR_XINTM(v) ((v) << 20)
  56. #define DAVINCI_MCBSP_SPCR_GRST (1 << 22)
  57. #define DAVINCI_MCBSP_SPCR_FRST (1 << 23)
  58. #define DAVINCI_MCBSP_SPCR_FREE (1 << 25)
  59. #define DAVINCI_MCBSP_RCR_RWDLEN1(v) ((v) << 5)
  60. #define DAVINCI_MCBSP_RCR_RFRLEN1(v) ((v) << 8)
  61. #define DAVINCI_MCBSP_RCR_RDATDLY(v) ((v) << 16)
  62. #define DAVINCI_MCBSP_RCR_RFIG (1 << 18)
  63. #define DAVINCI_MCBSP_RCR_RWDLEN2(v) ((v) << 21)
  64. #define DAVINCI_MCBSP_RCR_RFRLEN2(v) ((v) << 24)
  65. #define DAVINCI_MCBSP_RCR_RPHASE BIT(31)
  66. #define DAVINCI_MCBSP_XCR_XWDLEN1(v) ((v) << 5)
  67. #define DAVINCI_MCBSP_XCR_XFRLEN1(v) ((v) << 8)
  68. #define DAVINCI_MCBSP_XCR_XDATDLY(v) ((v) << 16)
  69. #define DAVINCI_MCBSP_XCR_XFIG (1 << 18)
  70. #define DAVINCI_MCBSP_XCR_XWDLEN2(v) ((v) << 21)
  71. #define DAVINCI_MCBSP_XCR_XFRLEN2(v) ((v) << 24)
  72. #define DAVINCI_MCBSP_XCR_XPHASE BIT(31)
  73. #define DAVINCI_MCBSP_SRGR_FWID(v) ((v) << 8)
  74. #define DAVINCI_MCBSP_SRGR_FPER(v) ((v) << 16)
  75. #define DAVINCI_MCBSP_SRGR_FSGM (1 << 28)
  76. #define DAVINCI_MCBSP_SRGR_CLKSM BIT(29)
  77. #define DAVINCI_MCBSP_PCR_CLKRP (1 << 0)
  78. #define DAVINCI_MCBSP_PCR_CLKXP (1 << 1)
  79. #define DAVINCI_MCBSP_PCR_FSRP (1 << 2)
  80. #define DAVINCI_MCBSP_PCR_FSXP (1 << 3)
  81. #define DAVINCI_MCBSP_PCR_SCLKME (1 << 7)
  82. #define DAVINCI_MCBSP_PCR_CLKRM (1 << 8)
  83. #define DAVINCI_MCBSP_PCR_CLKXM (1 << 9)
  84. #define DAVINCI_MCBSP_PCR_FSRM (1 << 10)
  85. #define DAVINCI_MCBSP_PCR_FSXM (1 << 11)
  86. enum {
  87. DAVINCI_MCBSP_WORD_8 = 0,
  88. DAVINCI_MCBSP_WORD_12,
  89. DAVINCI_MCBSP_WORD_16,
  90. DAVINCI_MCBSP_WORD_20,
  91. DAVINCI_MCBSP_WORD_24,
  92. DAVINCI_MCBSP_WORD_32,
  93. };
  94. static const unsigned char data_type[SNDRV_PCM_FORMAT_S32_LE + 1] = {
  95. [SNDRV_PCM_FORMAT_S8] = 1,
  96. [SNDRV_PCM_FORMAT_S16_LE] = 2,
  97. [SNDRV_PCM_FORMAT_S32_LE] = 4,
  98. };
  99. static const unsigned char asp_word_length[SNDRV_PCM_FORMAT_S32_LE + 1] = {
  100. [SNDRV_PCM_FORMAT_S8] = DAVINCI_MCBSP_WORD_8,
  101. [SNDRV_PCM_FORMAT_S16_LE] = DAVINCI_MCBSP_WORD_16,
  102. [SNDRV_PCM_FORMAT_S32_LE] = DAVINCI_MCBSP_WORD_32,
  103. };
  104. static const unsigned char double_fmt[SNDRV_PCM_FORMAT_S32_LE + 1] = {
  105. [SNDRV_PCM_FORMAT_S8] = SNDRV_PCM_FORMAT_S16_LE,
  106. [SNDRV_PCM_FORMAT_S16_LE] = SNDRV_PCM_FORMAT_S32_LE,
  107. };
  108. struct davinci_mcbsp_dev {
  109. struct device *dev;
  110. struct davinci_pcm_dma_params dma_params[2];
  111. void __iomem *base;
  112. #define MOD_DSP_A 0
  113. #define MOD_DSP_B 1
  114. int mode;
  115. u32 pcr;
  116. struct clk *clk;
  117. /*
  118. * Combining both channels into 1 element will at least double the
  119. * amount of time between servicing the dma channel, increase
  120. * effiency, and reduce the chance of overrun/underrun. But,
  121. * it will result in the left & right channels being swapped.
  122. *
  123. * If relabeling the left and right channels is not possible,
  124. * you may want to let the codec know to swap them back.
  125. *
  126. * It may allow x10 the amount of time to service dma requests,
  127. * if the codec is master and is using an unnecessarily fast bit clock
  128. * (ie. tlvaic23b), independent of the sample rate. So, having an
  129. * entire frame at once means it can be serviced at the sample rate
  130. * instead of the bit clock rate.
  131. *
  132. * In the now unlikely case that an underrun still
  133. * occurs, both the left and right samples will be repeated
  134. * so that no pops are heard, and the left and right channels
  135. * won't end up being swapped because of the underrun.
  136. */
  137. unsigned enable_channel_combine:1;
  138. unsigned int fmt;
  139. int clk_div;
  140. int clk_input_pin;
  141. bool i2s_accurate_sck;
  142. };
  143. static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev,
  144. int reg, u32 val)
  145. {
  146. __raw_writel(val, dev->base + reg);
  147. }
  148. static inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg)
  149. {
  150. return __raw_readl(dev->base + reg);
  151. }
  152. static void toggle_clock(struct davinci_mcbsp_dev *dev, int playback)
  153. {
  154. u32 m = playback ? DAVINCI_MCBSP_PCR_CLKXP : DAVINCI_MCBSP_PCR_CLKRP;
  155. /* The clock needs to toggle to complete reset.
  156. * So, fake it by toggling the clk polarity.
  157. */
  158. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr ^ m);
  159. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr);
  160. }
  161. static void davinci_mcbsp_start(struct davinci_mcbsp_dev *dev,
  162. struct snd_pcm_substream *substream)
  163. {
  164. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  165. struct snd_soc_platform *platform = rtd->platform;
  166. int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  167. u32 spcr;
  168. u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST;
  169. spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
  170. if (spcr & mask) {
  171. /* start off disabled */
  172. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG,
  173. spcr & ~mask);
  174. toggle_clock(dev, playback);
  175. }
  176. if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM |
  177. DAVINCI_MCBSP_PCR_CLKXM | DAVINCI_MCBSP_PCR_CLKRM)) {
  178. /* Start the sample generator */
  179. spcr |= DAVINCI_MCBSP_SPCR_GRST;
  180. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
  181. }
  182. if (playback) {
  183. /* Stop the DMA to avoid data loss */
  184. /* while the transmitter is out of reset to handle XSYNCERR */
  185. if (platform->driver->ops->trigger) {
  186. int ret = platform->driver->ops->trigger(substream,
  187. SNDRV_PCM_TRIGGER_STOP);
  188. if (ret < 0)
  189. printk(KERN_DEBUG "Playback DMA stop failed\n");
  190. }
  191. /* Enable the transmitter */
  192. spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
  193. spcr |= DAVINCI_MCBSP_SPCR_XRST;
  194. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
  195. /* wait for any unexpected frame sync error to occur */
  196. udelay(100);
  197. /* Disable the transmitter to clear any outstanding XSYNCERR */
  198. spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
  199. spcr &= ~DAVINCI_MCBSP_SPCR_XRST;
  200. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
  201. toggle_clock(dev, playback);
  202. /* Restart the DMA */
  203. if (platform->driver->ops->trigger) {
  204. int ret = platform->driver->ops->trigger(substream,
  205. SNDRV_PCM_TRIGGER_START);
  206. if (ret < 0)
  207. printk(KERN_DEBUG "Playback DMA start failed\n");
  208. }
  209. }
  210. /* Enable transmitter or receiver */
  211. spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
  212. spcr |= mask;
  213. if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM)) {
  214. /* Start frame sync */
  215. spcr |= DAVINCI_MCBSP_SPCR_FRST;
  216. }
  217. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
  218. }
  219. static void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback)
  220. {
  221. u32 spcr;
  222. /* Reset transmitter/receiver and sample rate/frame sync generators */
  223. spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
  224. spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST);
  225. spcr &= playback ? ~DAVINCI_MCBSP_SPCR_XRST : ~DAVINCI_MCBSP_SPCR_RRST;
  226. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
  227. toggle_clock(dev, playback);
  228. }
  229. #define DEFAULT_BITPERSAMPLE 16
  230. static int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  231. unsigned int fmt)
  232. {
  233. struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  234. unsigned int pcr;
  235. unsigned int srgr;
  236. bool inv_fs = false;
  237. /* Attention srgr is updated by hw_params! */
  238. srgr = DAVINCI_MCBSP_SRGR_FSGM |
  239. DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) |
  240. DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1);
  241. dev->fmt = fmt;
  242. /* set master/slave audio interface */
  243. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  244. case SND_SOC_DAIFMT_CBS_CFS:
  245. /* cpu is master */
  246. pcr = DAVINCI_MCBSP_PCR_FSXM |
  247. DAVINCI_MCBSP_PCR_FSRM |
  248. DAVINCI_MCBSP_PCR_CLKXM |
  249. DAVINCI_MCBSP_PCR_CLKRM;
  250. break;
  251. case SND_SOC_DAIFMT_CBM_CFS:
  252. pcr = DAVINCI_MCBSP_PCR_FSRM | DAVINCI_MCBSP_PCR_FSXM;
  253. /*
  254. * Selection of the clock input pin that is the
  255. * input for the Sample Rate Generator.
  256. * McBSP FSR and FSX are driven by the Sample Rate
  257. * Generator.
  258. */
  259. switch (dev->clk_input_pin) {
  260. case MCBSP_CLKS:
  261. pcr |= DAVINCI_MCBSP_PCR_CLKXM |
  262. DAVINCI_MCBSP_PCR_CLKRM;
  263. break;
  264. case MCBSP_CLKR:
  265. pcr |= DAVINCI_MCBSP_PCR_SCLKME;
  266. break;
  267. default:
  268. dev_err(dev->dev, "bad clk_input_pin\n");
  269. return -EINVAL;
  270. }
  271. break;
  272. case SND_SOC_DAIFMT_CBM_CFM:
  273. /* codec is master */
  274. pcr = 0;
  275. break;
  276. default:
  277. printk(KERN_ERR "%s:bad master\n", __func__);
  278. return -EINVAL;
  279. }
  280. /* interface format */
  281. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  282. case SND_SOC_DAIFMT_I2S:
  283. /* Davinci doesn't support TRUE I2S, but some codecs will have
  284. * the left and right channels contiguous. This allows
  285. * dsp_a mode to be used with an inverted normal frame clk.
  286. * If your codec is master and does not have contiguous
  287. * channels, then you will have sound on only one channel.
  288. * Try using a different mode, or codec as slave.
  289. *
  290. * The TLV320AIC33 is an example of a codec where this works.
  291. * It has a variable bit clock frequency allowing it to have
  292. * valid data on every bit clock.
  293. *
  294. * The TLV320AIC23 is an example of a codec where this does not
  295. * work. It has a fixed bit clock frequency with progressively
  296. * more empty bit clock slots between channels as the sample
  297. * rate is lowered.
  298. */
  299. inv_fs = true;
  300. case SND_SOC_DAIFMT_DSP_A:
  301. dev->mode = MOD_DSP_A;
  302. break;
  303. case SND_SOC_DAIFMT_DSP_B:
  304. dev->mode = MOD_DSP_B;
  305. break;
  306. default:
  307. printk(KERN_ERR "%s:bad format\n", __func__);
  308. return -EINVAL;
  309. }
  310. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  311. case SND_SOC_DAIFMT_NB_NF:
  312. /* CLKRP Receive clock polarity,
  313. * 1 - sampled on rising edge of CLKR
  314. * valid on rising edge
  315. * CLKXP Transmit clock polarity,
  316. * 1 - clocked on falling edge of CLKX
  317. * valid on rising edge
  318. * FSRP Receive frame sync pol, 0 - active high
  319. * FSXP Transmit frame sync pol, 0 - active high
  320. */
  321. pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP);
  322. break;
  323. case SND_SOC_DAIFMT_IB_IF:
  324. /* CLKRP Receive clock polarity,
  325. * 0 - sampled on falling edge of CLKR
  326. * valid on falling edge
  327. * CLKXP Transmit clock polarity,
  328. * 0 - clocked on rising edge of CLKX
  329. * valid on falling edge
  330. * FSRP Receive frame sync pol, 1 - active low
  331. * FSXP Transmit frame sync pol, 1 - active low
  332. */
  333. pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
  334. break;
  335. case SND_SOC_DAIFMT_NB_IF:
  336. /* CLKRP Receive clock polarity,
  337. * 1 - sampled on rising edge of CLKR
  338. * valid on rising edge
  339. * CLKXP Transmit clock polarity,
  340. * 1 - clocked on falling edge of CLKX
  341. * valid on rising edge
  342. * FSRP Receive frame sync pol, 1 - active low
  343. * FSXP Transmit frame sync pol, 1 - active low
  344. */
  345. pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP |
  346. DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
  347. break;
  348. case SND_SOC_DAIFMT_IB_NF:
  349. /* CLKRP Receive clock polarity,
  350. * 0 - sampled on falling edge of CLKR
  351. * valid on falling edge
  352. * CLKXP Transmit clock polarity,
  353. * 0 - clocked on rising edge of CLKX
  354. * valid on falling edge
  355. * FSRP Receive frame sync pol, 0 - active high
  356. * FSXP Transmit frame sync pol, 0 - active high
  357. */
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. if (inv_fs == true)
  363. pcr ^= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP);
  364. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
  365. dev->pcr = pcr;
  366. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr);
  367. return 0;
  368. }
  369. static int davinci_i2s_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
  370. int div_id, int div)
  371. {
  372. struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  373. if (div_id != DAVINCI_MCBSP_CLKGDV)
  374. return -ENODEV;
  375. dev->clk_div = div;
  376. return 0;
  377. }
  378. static int davinci_i2s_hw_params(struct snd_pcm_substream *substream,
  379. struct snd_pcm_hw_params *params,
  380. struct snd_soc_dai *dai)
  381. {
  382. struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
  383. struct davinci_pcm_dma_params *dma_params =
  384. &dev->dma_params[substream->stream];
  385. struct snd_interval *i = NULL;
  386. int mcbsp_word_length, master;
  387. unsigned int rcr, xcr, srgr, clk_div, freq, framesize;
  388. u32 spcr;
  389. snd_pcm_format_t fmt;
  390. unsigned element_cnt = 1;
  391. /* general line settings */
  392. spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG);
  393. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  394. spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
  395. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
  396. } else {
  397. spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE;
  398. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr);
  399. }
  400. master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  401. fmt = params_format(params);
  402. mcbsp_word_length = asp_word_length[fmt];
  403. switch (master) {
  404. case SND_SOC_DAIFMT_CBS_CFS:
  405. freq = clk_get_rate(dev->clk);
  406. srgr = DAVINCI_MCBSP_SRGR_FSGM |
  407. DAVINCI_MCBSP_SRGR_CLKSM;
  408. srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length *
  409. 8 - 1);
  410. if (dev->i2s_accurate_sck) {
  411. clk_div = 256;
  412. do {
  413. framesize = (freq / (--clk_div)) /
  414. params->rate_num *
  415. params->rate_den;
  416. } while (((framesize < 33) || (framesize > 4095)) &&
  417. (clk_div));
  418. clk_div--;
  419. srgr |= DAVINCI_MCBSP_SRGR_FPER(framesize - 1);
  420. } else {
  421. /* symmetric waveforms */
  422. clk_div = freq / (mcbsp_word_length * 16) /
  423. params->rate_num * params->rate_den;
  424. srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length *
  425. 16 - 1);
  426. }
  427. clk_div &= 0xFF;
  428. srgr |= clk_div;
  429. break;
  430. case SND_SOC_DAIFMT_CBM_CFS:
  431. srgr = DAVINCI_MCBSP_SRGR_FSGM;
  432. clk_div = dev->clk_div - 1;
  433. srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length * 8 - 1);
  434. srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length * 16 - 1);
  435. clk_div &= 0xFF;
  436. srgr |= clk_div;
  437. break;
  438. case SND_SOC_DAIFMT_CBM_CFM:
  439. /* Clock and frame sync given from external sources */
  440. i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  441. srgr = DAVINCI_MCBSP_SRGR_FSGM;
  442. srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1);
  443. pr_debug("%s - %d FWID set: re-read srgr = %X\n",
  444. __func__, __LINE__, snd_interval_value(i) - 1);
  445. i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS);
  446. srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1);
  447. break;
  448. default:
  449. return -EINVAL;
  450. }
  451. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr);
  452. rcr = DAVINCI_MCBSP_RCR_RFIG;
  453. xcr = DAVINCI_MCBSP_XCR_XFIG;
  454. if (dev->mode == MOD_DSP_B) {
  455. rcr |= DAVINCI_MCBSP_RCR_RDATDLY(0);
  456. xcr |= DAVINCI_MCBSP_XCR_XDATDLY(0);
  457. } else {
  458. rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1);
  459. xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1);
  460. }
  461. /* Determine xfer data type */
  462. fmt = params_format(params);
  463. if ((fmt > SNDRV_PCM_FORMAT_S32_LE) || !data_type[fmt]) {
  464. printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n");
  465. return -EINVAL;
  466. }
  467. if (params_channels(params) == 2) {
  468. element_cnt = 2;
  469. if (double_fmt[fmt] && dev->enable_channel_combine) {
  470. element_cnt = 1;
  471. fmt = double_fmt[fmt];
  472. }
  473. switch (master) {
  474. case SND_SOC_DAIFMT_CBS_CFS:
  475. case SND_SOC_DAIFMT_CBS_CFM:
  476. rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(0);
  477. xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(0);
  478. rcr |= DAVINCI_MCBSP_RCR_RPHASE;
  479. xcr |= DAVINCI_MCBSP_XCR_XPHASE;
  480. break;
  481. case SND_SOC_DAIFMT_CBM_CFM:
  482. case SND_SOC_DAIFMT_CBM_CFS:
  483. rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(element_cnt - 1);
  484. xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(element_cnt - 1);
  485. break;
  486. default:
  487. return -EINVAL;
  488. }
  489. }
  490. dma_params->acnt = dma_params->data_type = data_type[fmt];
  491. dma_params->fifo_level = 0;
  492. mcbsp_word_length = asp_word_length[fmt];
  493. switch (master) {
  494. case SND_SOC_DAIFMT_CBS_CFS:
  495. case SND_SOC_DAIFMT_CBS_CFM:
  496. rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(0);
  497. xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(0);
  498. break;
  499. case SND_SOC_DAIFMT_CBM_CFM:
  500. case SND_SOC_DAIFMT_CBM_CFS:
  501. rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(element_cnt - 1);
  502. xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(element_cnt - 1);
  503. break;
  504. default:
  505. return -EINVAL;
  506. }
  507. rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) |
  508. DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length);
  509. xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) |
  510. DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length);
  511. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  512. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr);
  513. else
  514. davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr);
  515. pr_debug("%s - %d srgr=%X\n", __func__, __LINE__, srgr);
  516. pr_debug("%s - %d xcr=%X\n", __func__, __LINE__, xcr);
  517. pr_debug("%s - %d rcr=%X\n", __func__, __LINE__, rcr);
  518. return 0;
  519. }
  520. static int davinci_i2s_prepare(struct snd_pcm_substream *substream,
  521. struct snd_soc_dai *dai)
  522. {
  523. struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
  524. int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  525. davinci_mcbsp_stop(dev, playback);
  526. return 0;
  527. }
  528. static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  529. struct snd_soc_dai *dai)
  530. {
  531. struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
  532. int ret = 0;
  533. int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  534. switch (cmd) {
  535. case SNDRV_PCM_TRIGGER_START:
  536. case SNDRV_PCM_TRIGGER_RESUME:
  537. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  538. davinci_mcbsp_start(dev, substream);
  539. break;
  540. case SNDRV_PCM_TRIGGER_STOP:
  541. case SNDRV_PCM_TRIGGER_SUSPEND:
  542. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  543. davinci_mcbsp_stop(dev, playback);
  544. break;
  545. default:
  546. ret = -EINVAL;
  547. }
  548. return ret;
  549. }
  550. static int davinci_i2s_startup(struct snd_pcm_substream *substream,
  551. struct snd_soc_dai *dai)
  552. {
  553. struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
  554. snd_soc_dai_set_dma_data(dai, substream, dev->dma_params);
  555. return 0;
  556. }
  557. static void davinci_i2s_shutdown(struct snd_pcm_substream *substream,
  558. struct snd_soc_dai *dai)
  559. {
  560. struct davinci_mcbsp_dev *dev = snd_soc_dai_get_drvdata(dai);
  561. int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  562. davinci_mcbsp_stop(dev, playback);
  563. }
  564. #define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000
  565. static const struct snd_soc_dai_ops davinci_i2s_dai_ops = {
  566. .startup = davinci_i2s_startup,
  567. .shutdown = davinci_i2s_shutdown,
  568. .prepare = davinci_i2s_prepare,
  569. .trigger = davinci_i2s_trigger,
  570. .hw_params = davinci_i2s_hw_params,
  571. .set_fmt = davinci_i2s_set_dai_fmt,
  572. .set_clkdiv = davinci_i2s_dai_set_clkdiv,
  573. };
  574. static struct snd_soc_dai_driver davinci_i2s_dai = {
  575. .playback = {
  576. .channels_min = 2,
  577. .channels_max = 2,
  578. .rates = DAVINCI_I2S_RATES,
  579. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  580. .capture = {
  581. .channels_min = 2,
  582. .channels_max = 2,
  583. .rates = DAVINCI_I2S_RATES,
  584. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  585. .ops = &davinci_i2s_dai_ops,
  586. };
  587. static int davinci_i2s_probe(struct platform_device *pdev)
  588. {
  589. struct snd_platform_data *pdata = pdev->dev.platform_data;
  590. struct davinci_mcbsp_dev *dev;
  591. struct resource *mem, *ioarea, *res;
  592. enum dma_event_q asp_chan_q = EVENTQ_0;
  593. enum dma_event_q ram_chan_q = EVENTQ_1;
  594. int ret;
  595. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  596. if (!mem) {
  597. dev_err(&pdev->dev, "no mem resource?\n");
  598. return -ENODEV;
  599. }
  600. ioarea = devm_request_mem_region(&pdev->dev, mem->start,
  601. resource_size(mem),
  602. pdev->name);
  603. if (!ioarea) {
  604. dev_err(&pdev->dev, "McBSP region already claimed\n");
  605. return -EBUSY;
  606. }
  607. dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcbsp_dev),
  608. GFP_KERNEL);
  609. if (!dev)
  610. return -ENOMEM;
  611. if (pdata) {
  612. dev->enable_channel_combine = pdata->enable_channel_combine;
  613. dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].sram_size =
  614. pdata->sram_size_playback;
  615. dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].sram_size =
  616. pdata->sram_size_capture;
  617. dev->clk_input_pin = pdata->clk_input_pin;
  618. dev->i2s_accurate_sck = pdata->i2s_accurate_sck;
  619. asp_chan_q = pdata->asp_chan_q;
  620. ram_chan_q = pdata->ram_chan_q;
  621. }
  622. dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].asp_chan_q = asp_chan_q;
  623. dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].ram_chan_q = ram_chan_q;
  624. dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].asp_chan_q = asp_chan_q;
  625. dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].ram_chan_q = ram_chan_q;
  626. dev->clk = clk_get(&pdev->dev, NULL);
  627. if (IS_ERR(dev->clk))
  628. return -ENODEV;
  629. clk_enable(dev->clk);
  630. dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
  631. if (!dev->base) {
  632. dev_err(&pdev->dev, "ioremap failed\n");
  633. ret = -ENOMEM;
  634. goto err_release_clk;
  635. }
  636. dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr =
  637. (dma_addr_t)(mem->start + DAVINCI_MCBSP_DXR_REG);
  638. dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr =
  639. (dma_addr_t)(mem->start + DAVINCI_MCBSP_DRR_REG);
  640. /* first TX, then RX */
  641. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  642. if (!res) {
  643. dev_err(&pdev->dev, "no DMA resource\n");
  644. ret = -ENXIO;
  645. goto err_release_clk;
  646. }
  647. dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel = res->start;
  648. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  649. if (!res) {
  650. dev_err(&pdev->dev, "no DMA resource\n");
  651. ret = -ENXIO;
  652. goto err_release_clk;
  653. }
  654. dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel = res->start;
  655. dev->dev = &pdev->dev;
  656. dev_set_drvdata(&pdev->dev, dev);
  657. ret = snd_soc_register_dai(&pdev->dev, &davinci_i2s_dai);
  658. if (ret != 0)
  659. goto err_release_clk;
  660. return 0;
  661. err_release_clk:
  662. clk_disable(dev->clk);
  663. clk_put(dev->clk);
  664. return ret;
  665. }
  666. static int davinci_i2s_remove(struct platform_device *pdev)
  667. {
  668. struct davinci_mcbsp_dev *dev = dev_get_drvdata(&pdev->dev);
  669. snd_soc_unregister_dai(&pdev->dev);
  670. clk_disable(dev->clk);
  671. clk_put(dev->clk);
  672. dev->clk = NULL;
  673. return 0;
  674. }
  675. static struct platform_driver davinci_mcbsp_driver = {
  676. .probe = davinci_i2s_probe,
  677. .remove = davinci_i2s_remove,
  678. .driver = {
  679. .name = "davinci-mcbsp",
  680. .owner = THIS_MODULE,
  681. },
  682. };
  683. module_platform_driver(davinci_mcbsp_driver);
  684. MODULE_AUTHOR("Vladimir Barinov");
  685. MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface");
  686. MODULE_LICENSE("GPL");