davinci-i2s.c 22 KB

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