davinci-mcasp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * ALSA SoC McASP Audio Layer for TI DAVINCI processor
  3. *
  4. * Multi-channel Audio Serial Port Driver
  5. *
  6. * Author: Nirmal Pandey <n-pandey@ti.com>,
  7. * Suresh Rajashekara <suresh.r@ti.com>
  8. * Steve Chen <schen@.mvista.com>
  9. *
  10. * Copyright: (C) 2009 MontaVista Software, Inc., <source@mvista.com>
  11. * Copyright: (C) 2009 Texas Instruments, India
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/io.h>
  23. #include <linux/clk.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/initval.h>
  28. #include <sound/soc.h>
  29. #include "davinci-pcm.h"
  30. #include "davinci-mcasp.h"
  31. /*
  32. * McASP register definitions
  33. */
  34. #define DAVINCI_MCASP_PID_REG 0x00
  35. #define DAVINCI_MCASP_PWREMUMGT_REG 0x04
  36. #define DAVINCI_MCASP_PFUNC_REG 0x10
  37. #define DAVINCI_MCASP_PDIR_REG 0x14
  38. #define DAVINCI_MCASP_PDOUT_REG 0x18
  39. #define DAVINCI_MCASP_PDSET_REG 0x1c
  40. #define DAVINCI_MCASP_PDCLR_REG 0x20
  41. #define DAVINCI_MCASP_TLGC_REG 0x30
  42. #define DAVINCI_MCASP_TLMR_REG 0x34
  43. #define DAVINCI_MCASP_GBLCTL_REG 0x44
  44. #define DAVINCI_MCASP_AMUTE_REG 0x48
  45. #define DAVINCI_MCASP_LBCTL_REG 0x4c
  46. #define DAVINCI_MCASP_TXDITCTL_REG 0x50
  47. #define DAVINCI_MCASP_GBLCTLR_REG 0x60
  48. #define DAVINCI_MCASP_RXMASK_REG 0x64
  49. #define DAVINCI_MCASP_RXFMT_REG 0x68
  50. #define DAVINCI_MCASP_RXFMCTL_REG 0x6c
  51. #define DAVINCI_MCASP_ACLKRCTL_REG 0x70
  52. #define DAVINCI_MCASP_AHCLKRCTL_REG 0x74
  53. #define DAVINCI_MCASP_RXTDM_REG 0x78
  54. #define DAVINCI_MCASP_EVTCTLR_REG 0x7c
  55. #define DAVINCI_MCASP_RXSTAT_REG 0x80
  56. #define DAVINCI_MCASP_RXTDMSLOT_REG 0x84
  57. #define DAVINCI_MCASP_RXCLKCHK_REG 0x88
  58. #define DAVINCI_MCASP_REVTCTL_REG 0x8c
  59. #define DAVINCI_MCASP_GBLCTLX_REG 0xa0
  60. #define DAVINCI_MCASP_TXMASK_REG 0xa4
  61. #define DAVINCI_MCASP_TXFMT_REG 0xa8
  62. #define DAVINCI_MCASP_TXFMCTL_REG 0xac
  63. #define DAVINCI_MCASP_ACLKXCTL_REG 0xb0
  64. #define DAVINCI_MCASP_AHCLKXCTL_REG 0xb4
  65. #define DAVINCI_MCASP_TXTDM_REG 0xb8
  66. #define DAVINCI_MCASP_EVTCTLX_REG 0xbc
  67. #define DAVINCI_MCASP_TXSTAT_REG 0xc0
  68. #define DAVINCI_MCASP_TXTDMSLOT_REG 0xc4
  69. #define DAVINCI_MCASP_TXCLKCHK_REG 0xc8
  70. #define DAVINCI_MCASP_XEVTCTL_REG 0xcc
  71. /* Left(even TDM Slot) Channel Status Register File */
  72. #define DAVINCI_MCASP_DITCSRA_REG 0x100
  73. /* Right(odd TDM slot) Channel Status Register File */
  74. #define DAVINCI_MCASP_DITCSRB_REG 0x118
  75. /* Left(even TDM slot) User Data Register File */
  76. #define DAVINCI_MCASP_DITUDRA_REG 0x130
  77. /* Right(odd TDM Slot) User Data Register File */
  78. #define DAVINCI_MCASP_DITUDRB_REG 0x148
  79. /* Serializer n Control Register */
  80. #define DAVINCI_MCASP_XRSRCTL_BASE_REG 0x180
  81. #define DAVINCI_MCASP_XRSRCTL_REG(n) (DAVINCI_MCASP_XRSRCTL_BASE_REG + \
  82. (n << 2))
  83. /* Transmit Buffer for Serializer n */
  84. #define DAVINCI_MCASP_TXBUF_REG 0x200
  85. /* Receive Buffer for Serializer n */
  86. #define DAVINCI_MCASP_RXBUF_REG 0x280
  87. /* McASP FIFO Registers */
  88. #define DAVINCI_MCASP_WFIFOCTL (0x1010)
  89. #define DAVINCI_MCASP_WFIFOSTS (0x1014)
  90. #define DAVINCI_MCASP_RFIFOCTL (0x1018)
  91. #define DAVINCI_MCASP_RFIFOSTS (0x101C)
  92. /*
  93. * DAVINCI_MCASP_PWREMUMGT_REG - Power Down and Emulation Management
  94. * Register Bits
  95. */
  96. #define MCASP_FREE BIT(0)
  97. #define MCASP_SOFT BIT(1)
  98. /*
  99. * DAVINCI_MCASP_PFUNC_REG - Pin Function / GPIO Enable Register Bits
  100. */
  101. #define AXR(n) (1<<n)
  102. #define PFUNC_AMUTE BIT(25)
  103. #define ACLKX BIT(26)
  104. #define AHCLKX BIT(27)
  105. #define AFSX BIT(28)
  106. #define ACLKR BIT(29)
  107. #define AHCLKR BIT(30)
  108. #define AFSR BIT(31)
  109. /*
  110. * DAVINCI_MCASP_PDIR_REG - Pin Direction Register Bits
  111. */
  112. #define AXR(n) (1<<n)
  113. #define PDIR_AMUTE BIT(25)
  114. #define ACLKX BIT(26)
  115. #define AHCLKX BIT(27)
  116. #define AFSX BIT(28)
  117. #define ACLKR BIT(29)
  118. #define AHCLKR BIT(30)
  119. #define AFSR BIT(31)
  120. /*
  121. * DAVINCI_MCASP_TXDITCTL_REG - Transmit DIT Control Register Bits
  122. */
  123. #define DITEN BIT(0) /* Transmit DIT mode enable/disable */
  124. #define VA BIT(2)
  125. #define VB BIT(3)
  126. /*
  127. * DAVINCI_MCASP_TXFMT_REG - Transmit Bitstream Format Register Bits
  128. */
  129. #define TXROT(val) (val)
  130. #define TXSEL BIT(3)
  131. #define TXSSZ(val) (val<<4)
  132. #define TXPBIT(val) (val<<8)
  133. #define TXPAD(val) (val<<13)
  134. #define TXORD BIT(15)
  135. #define FSXDLY(val) (val<<16)
  136. /*
  137. * DAVINCI_MCASP_RXFMT_REG - Receive Bitstream Format Register Bits
  138. */
  139. #define RXROT(val) (val)
  140. #define RXSEL BIT(3)
  141. #define RXSSZ(val) (val<<4)
  142. #define RXPBIT(val) (val<<8)
  143. #define RXPAD(val) (val<<13)
  144. #define RXORD BIT(15)
  145. #define FSRDLY(val) (val<<16)
  146. /*
  147. * DAVINCI_MCASP_TXFMCTL_REG - Transmit Frame Control Register Bits
  148. */
  149. #define FSXPOL BIT(0)
  150. #define AFSXE BIT(1)
  151. #define FSXDUR BIT(4)
  152. #define FSXMOD(val) (val<<7)
  153. /*
  154. * DAVINCI_MCASP_RXFMCTL_REG - Receive Frame Control Register Bits
  155. */
  156. #define FSRPOL BIT(0)
  157. #define AFSRE BIT(1)
  158. #define FSRDUR BIT(4)
  159. #define FSRMOD(val) (val<<7)
  160. /*
  161. * DAVINCI_MCASP_ACLKXCTL_REG - Transmit Clock Control Register Bits
  162. */
  163. #define ACLKXDIV(val) (val)
  164. #define ACLKXE BIT(5)
  165. #define TX_ASYNC BIT(6)
  166. #define ACLKXPOL BIT(7)
  167. /*
  168. * DAVINCI_MCASP_ACLKRCTL_REG Receive Clock Control Register Bits
  169. */
  170. #define ACLKRDIV(val) (val)
  171. #define ACLKRE BIT(5)
  172. #define RX_ASYNC BIT(6)
  173. #define ACLKRPOL BIT(7)
  174. /*
  175. * DAVINCI_MCASP_AHCLKXCTL_REG - High Frequency Transmit Clock Control
  176. * Register Bits
  177. */
  178. #define AHCLKXDIV(val) (val)
  179. #define AHCLKXPOL BIT(14)
  180. #define AHCLKXE BIT(15)
  181. /*
  182. * DAVINCI_MCASP_AHCLKRCTL_REG - High Frequency Receive Clock Control
  183. * Register Bits
  184. */
  185. #define AHCLKRDIV(val) (val)
  186. #define AHCLKRPOL BIT(14)
  187. #define AHCLKRE BIT(15)
  188. /*
  189. * DAVINCI_MCASP_XRSRCTL_BASE_REG - Serializer Control Register Bits
  190. */
  191. #define MODE(val) (val)
  192. #define DISMOD (val)(val<<2)
  193. #define TXSTATE BIT(4)
  194. #define RXSTATE BIT(5)
  195. /*
  196. * DAVINCI_MCASP_LBCTL_REG - Loop Back Control Register Bits
  197. */
  198. #define LBEN BIT(0)
  199. #define LBORD BIT(1)
  200. #define LBGENMODE(val) (val<<2)
  201. /*
  202. * DAVINCI_MCASP_TXTDMSLOT_REG - Transmit TDM Slot Register configuration
  203. */
  204. #define TXTDMS(n) (1<<n)
  205. /*
  206. * DAVINCI_MCASP_RXTDMSLOT_REG - Receive TDM Slot Register configuration
  207. */
  208. #define RXTDMS(n) (1<<n)
  209. /*
  210. * DAVINCI_MCASP_GBLCTL_REG - Global Control Register Bits
  211. */
  212. #define RXCLKRST BIT(0) /* Receiver Clock Divider Reset */
  213. #define RXHCLKRST BIT(1) /* Receiver High Frequency Clock Divider */
  214. #define RXSERCLR BIT(2) /* Receiver Serializer Clear */
  215. #define RXSMRST BIT(3) /* Receiver State Machine Reset */
  216. #define RXFSRST BIT(4) /* Frame Sync Generator Reset */
  217. #define TXCLKRST BIT(8) /* Transmitter Clock Divider Reset */
  218. #define TXHCLKRST BIT(9) /* Transmitter High Frequency Clock Divider*/
  219. #define TXSERCLR BIT(10) /* Transmit Serializer Clear */
  220. #define TXSMRST BIT(11) /* Transmitter State Machine Reset */
  221. #define TXFSRST BIT(12) /* Frame Sync Generator Reset */
  222. /*
  223. * DAVINCI_MCASP_AMUTE_REG - Mute Control Register Bits
  224. */
  225. #define MUTENA(val) (val)
  226. #define MUTEINPOL BIT(2)
  227. #define MUTEINENA BIT(3)
  228. #define MUTEIN BIT(4)
  229. #define MUTER BIT(5)
  230. #define MUTEX BIT(6)
  231. #define MUTEFSR BIT(7)
  232. #define MUTEFSX BIT(8)
  233. #define MUTEBADCLKR BIT(9)
  234. #define MUTEBADCLKX BIT(10)
  235. #define MUTERXDMAERR BIT(11)
  236. #define MUTETXDMAERR BIT(12)
  237. /*
  238. * DAVINCI_MCASP_REVTCTL_REG - Receiver DMA Event Control Register bits
  239. */
  240. #define RXDATADMADIS BIT(0)
  241. /*
  242. * DAVINCI_MCASP_XEVTCTL_REG - Transmitter DMA Event Control Register bits
  243. */
  244. #define TXDATADMADIS BIT(0)
  245. /*
  246. * DAVINCI_MCASP_W[R]FIFOCTL - Write/Read FIFO Control Register bits
  247. */
  248. #define FIFO_ENABLE BIT(16)
  249. #define NUMEVT_MASK (0xFF << 8)
  250. #define NUMDMA_MASK (0xFF)
  251. #define DAVINCI_MCASP_NUM_SERIALIZER 16
  252. static inline void mcasp_set_bits(void __iomem *reg, u32 val)
  253. {
  254. __raw_writel(__raw_readl(reg) | val, reg);
  255. }
  256. static inline void mcasp_clr_bits(void __iomem *reg, u32 val)
  257. {
  258. __raw_writel((__raw_readl(reg) & ~(val)), reg);
  259. }
  260. static inline void mcasp_mod_bits(void __iomem *reg, u32 val, u32 mask)
  261. {
  262. __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
  263. }
  264. static inline void mcasp_set_reg(void __iomem *reg, u32 val)
  265. {
  266. __raw_writel(val, reg);
  267. }
  268. static inline u32 mcasp_get_reg(void __iomem *reg)
  269. {
  270. return (unsigned int)__raw_readl(reg);
  271. }
  272. static inline void mcasp_set_ctl_reg(void __iomem *regs, u32 val)
  273. {
  274. int i = 0;
  275. mcasp_set_bits(regs, val);
  276. /* programming GBLCTL needs to read back from GBLCTL and verfiy */
  277. /* loop count is to avoid the lock-up */
  278. for (i = 0; i < 1000; i++) {
  279. if ((mcasp_get_reg(regs) & val) == val)
  280. break;
  281. }
  282. if (i == 1000 && ((mcasp_get_reg(regs) & val) != val))
  283. printk(KERN_ERR "GBLCTL write error\n");
  284. }
  285. static void mcasp_start_rx(struct davinci_audio_dev *dev)
  286. {
  287. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
  288. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
  289. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
  290. mcasp_set_reg(dev->base + DAVINCI_MCASP_RXBUF_REG, 0);
  291. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
  292. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
  293. mcasp_set_reg(dev->base + DAVINCI_MCASP_RXBUF_REG, 0);
  294. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
  295. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
  296. }
  297. static void mcasp_start_tx(struct davinci_audio_dev *dev)
  298. {
  299. u8 offset = 0, i;
  300. u32 cnt;
  301. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
  302. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
  303. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
  304. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXBUF_REG, 0);
  305. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
  306. mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
  307. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXBUF_REG, 0);
  308. for (i = 0; i < dev->num_serializer; i++) {
  309. if (dev->serial_dir[i] == TX_MODE) {
  310. offset = i;
  311. break;
  312. }
  313. }
  314. /* wait for TX ready */
  315. cnt = 0;
  316. while (!(mcasp_get_reg(dev->base + DAVINCI_MCASP_XRSRCTL_REG(offset)) &
  317. TXSTATE) && (cnt < 100000))
  318. cnt++;
  319. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXBUF_REG, 0);
  320. }
  321. static void davinci_mcasp_start(struct davinci_audio_dev *dev, int stream)
  322. {
  323. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  324. if (dev->txnumevt) /* enable FIFO */
  325. mcasp_set_bits(dev->base + DAVINCI_MCASP_WFIFOCTL,
  326. FIFO_ENABLE);
  327. mcasp_start_tx(dev);
  328. } else {
  329. if (dev->rxnumevt) /* enable FIFO */
  330. mcasp_set_bits(dev->base + DAVINCI_MCASP_RFIFOCTL,
  331. FIFO_ENABLE);
  332. mcasp_start_rx(dev);
  333. }
  334. }
  335. static void mcasp_stop_rx(struct davinci_audio_dev *dev)
  336. {
  337. mcasp_set_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, 0);
  338. mcasp_set_reg(dev->base + DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
  339. }
  340. static void mcasp_stop_tx(struct davinci_audio_dev *dev)
  341. {
  342. mcasp_set_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, 0);
  343. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
  344. }
  345. static void davinci_mcasp_stop(struct davinci_audio_dev *dev, int stream)
  346. {
  347. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  348. if (dev->txnumevt) /* disable FIFO */
  349. mcasp_clr_bits(dev->base + DAVINCI_MCASP_WFIFOCTL,
  350. FIFO_ENABLE);
  351. mcasp_stop_tx(dev);
  352. } else {
  353. if (dev->rxnumevt) /* disable FIFO */
  354. mcasp_clr_bits(dev->base + DAVINCI_MCASP_RFIFOCTL,
  355. FIFO_ENABLE);
  356. mcasp_stop_rx(dev);
  357. }
  358. }
  359. static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  360. unsigned int fmt)
  361. {
  362. struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  363. void __iomem *base = dev->base;
  364. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  365. case SND_SOC_DAIFMT_CBS_CFS:
  366. /* codec is clock and frame slave */
  367. mcasp_set_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
  368. mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
  369. mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
  370. mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
  371. mcasp_set_bits(base + DAVINCI_MCASP_PDIR_REG,
  372. ACLKX | AHCLKX | AFSX);
  373. break;
  374. case SND_SOC_DAIFMT_CBM_CFS:
  375. /* codec is clock master and frame slave */
  376. mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
  377. mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
  378. mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
  379. mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
  380. mcasp_clr_bits(base + DAVINCI_MCASP_PDIR_REG,
  381. ACLKX | ACLKR);
  382. mcasp_set_bits(base + DAVINCI_MCASP_PDIR_REG,
  383. AFSX | AFSR);
  384. break;
  385. case SND_SOC_DAIFMT_CBM_CFM:
  386. /* codec is clock and frame master */
  387. mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
  388. mcasp_clr_bits(base + DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
  389. mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
  390. mcasp_clr_bits(base + DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
  391. mcasp_clr_bits(base + DAVINCI_MCASP_PDIR_REG,
  392. ACLKX | AHCLKX | AFSX | ACLKR | AHCLKR | AFSR);
  393. break;
  394. default:
  395. return -EINVAL;
  396. }
  397. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  398. case SND_SOC_DAIFMT_IB_NF:
  399. mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
  400. mcasp_clr_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
  401. mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
  402. mcasp_clr_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
  403. break;
  404. case SND_SOC_DAIFMT_NB_IF:
  405. mcasp_set_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
  406. mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
  407. mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
  408. mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
  409. break;
  410. case SND_SOC_DAIFMT_IB_IF:
  411. mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
  412. mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
  413. mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
  414. mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
  415. break;
  416. case SND_SOC_DAIFMT_NB_NF:
  417. mcasp_set_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
  418. mcasp_clr_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
  419. mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
  420. mcasp_clr_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
  421. break;
  422. default:
  423. return -EINVAL;
  424. }
  425. return 0;
  426. }
  427. static int davinci_config_channel_size(struct davinci_audio_dev *dev,
  428. int channel_size)
  429. {
  430. u32 fmt = 0;
  431. u32 mask, rotate;
  432. switch (channel_size) {
  433. case DAVINCI_AUDIO_WORD_8:
  434. fmt = 0x03;
  435. rotate = 6;
  436. mask = 0x000000ff;
  437. break;
  438. case DAVINCI_AUDIO_WORD_12:
  439. fmt = 0x05;
  440. rotate = 5;
  441. mask = 0x00000fff;
  442. break;
  443. case DAVINCI_AUDIO_WORD_16:
  444. fmt = 0x07;
  445. rotate = 4;
  446. mask = 0x0000ffff;
  447. break;
  448. case DAVINCI_AUDIO_WORD_20:
  449. fmt = 0x09;
  450. rotate = 3;
  451. mask = 0x000fffff;
  452. break;
  453. case DAVINCI_AUDIO_WORD_24:
  454. fmt = 0x0B;
  455. rotate = 2;
  456. mask = 0x00ffffff;
  457. break;
  458. case DAVINCI_AUDIO_WORD_28:
  459. fmt = 0x0D;
  460. rotate = 1;
  461. mask = 0x0fffffff;
  462. break;
  463. case DAVINCI_AUDIO_WORD_32:
  464. fmt = 0x0F;
  465. rotate = 0;
  466. mask = 0xffffffff;
  467. break;
  468. default:
  469. return -EINVAL;
  470. }
  471. mcasp_mod_bits(dev->base + DAVINCI_MCASP_RXFMT_REG,
  472. RXSSZ(fmt), RXSSZ(0x0F));
  473. mcasp_mod_bits(dev->base + DAVINCI_MCASP_TXFMT_REG,
  474. TXSSZ(fmt), TXSSZ(0x0F));
  475. mcasp_mod_bits(dev->base + DAVINCI_MCASP_TXFMT_REG, TXROT(rotate),
  476. TXROT(7));
  477. mcasp_mod_bits(dev->base + DAVINCI_MCASP_RXFMT_REG, RXROT(rotate),
  478. RXROT(7));
  479. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXMASK_REG, mask);
  480. mcasp_set_reg(dev->base + DAVINCI_MCASP_RXMASK_REG, mask);
  481. return 0;
  482. }
  483. static void davinci_hw_common_param(struct davinci_audio_dev *dev, int stream)
  484. {
  485. int i;
  486. u8 tx_ser = 0;
  487. u8 rx_ser = 0;
  488. /* Default configuration */
  489. mcasp_set_bits(dev->base + DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
  490. /* All PINS as McASP */
  491. mcasp_set_reg(dev->base + DAVINCI_MCASP_PFUNC_REG, 0x00000000);
  492. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  493. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
  494. mcasp_clr_bits(dev->base + DAVINCI_MCASP_XEVTCTL_REG,
  495. TXDATADMADIS);
  496. } else {
  497. mcasp_set_reg(dev->base + DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
  498. mcasp_clr_bits(dev->base + DAVINCI_MCASP_REVTCTL_REG,
  499. RXDATADMADIS);
  500. }
  501. for (i = 0; i < dev->num_serializer; i++) {
  502. mcasp_set_bits(dev->base + DAVINCI_MCASP_XRSRCTL_REG(i),
  503. dev->serial_dir[i]);
  504. if (dev->serial_dir[i] == TX_MODE) {
  505. mcasp_set_bits(dev->base + DAVINCI_MCASP_PDIR_REG,
  506. AXR(i));
  507. tx_ser++;
  508. } else if (dev->serial_dir[i] == RX_MODE) {
  509. mcasp_clr_bits(dev->base + DAVINCI_MCASP_PDIR_REG,
  510. AXR(i));
  511. rx_ser++;
  512. }
  513. }
  514. if (dev->txnumevt && stream == SNDRV_PCM_STREAM_PLAYBACK) {
  515. if (dev->txnumevt * tx_ser > 64)
  516. dev->txnumevt = 1;
  517. mcasp_mod_bits(dev->base + DAVINCI_MCASP_WFIFOCTL, tx_ser,
  518. NUMDMA_MASK);
  519. mcasp_mod_bits(dev->base + DAVINCI_MCASP_WFIFOCTL,
  520. ((dev->txnumevt * tx_ser) << 8), NUMEVT_MASK);
  521. }
  522. if (dev->rxnumevt && stream == SNDRV_PCM_STREAM_CAPTURE) {
  523. if (dev->rxnumevt * rx_ser > 64)
  524. dev->rxnumevt = 1;
  525. mcasp_mod_bits(dev->base + DAVINCI_MCASP_RFIFOCTL, rx_ser,
  526. NUMDMA_MASK);
  527. mcasp_mod_bits(dev->base + DAVINCI_MCASP_RFIFOCTL,
  528. ((dev->rxnumevt * rx_ser) << 8), NUMEVT_MASK);
  529. }
  530. }
  531. static void davinci_hw_param(struct davinci_audio_dev *dev, int stream)
  532. {
  533. int i, active_slots;
  534. u32 mask = 0;
  535. active_slots = (dev->tdm_slots > 31) ? 32 : dev->tdm_slots;
  536. for (i = 0; i < active_slots; i++)
  537. mask |= (1 << i);
  538. mcasp_clr_bits(dev->base + DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
  539. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  540. /* bit stream is MSB first with no delay */
  541. /* DSP_B mode */
  542. mcasp_set_bits(dev->base + DAVINCI_MCASP_AHCLKXCTL_REG,
  543. AHCLKXE);
  544. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXTDM_REG, mask);
  545. mcasp_set_bits(dev->base + DAVINCI_MCASP_TXFMT_REG, TXORD);
  546. if ((dev->tdm_slots >= 2) && (dev->tdm_slots <= 32))
  547. mcasp_mod_bits(dev->base + DAVINCI_MCASP_TXFMCTL_REG,
  548. FSXMOD(dev->tdm_slots), FSXMOD(0x1FF));
  549. else
  550. printk(KERN_ERR "playback tdm slot %d not supported\n",
  551. dev->tdm_slots);
  552. mcasp_clr_bits(dev->base + DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
  553. } else {
  554. /* bit stream is MSB first with no delay */
  555. /* DSP_B mode */
  556. mcasp_set_bits(dev->base + DAVINCI_MCASP_RXFMT_REG, RXORD);
  557. mcasp_set_bits(dev->base + DAVINCI_MCASP_AHCLKRCTL_REG,
  558. AHCLKRE);
  559. mcasp_set_reg(dev->base + DAVINCI_MCASP_RXTDM_REG, mask);
  560. if ((dev->tdm_slots >= 2) && (dev->tdm_slots <= 32))
  561. mcasp_mod_bits(dev->base + DAVINCI_MCASP_RXFMCTL_REG,
  562. FSRMOD(dev->tdm_slots), FSRMOD(0x1FF));
  563. else
  564. printk(KERN_ERR "capture tdm slot %d not supported\n",
  565. dev->tdm_slots);
  566. mcasp_clr_bits(dev->base + DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
  567. }
  568. }
  569. /* S/PDIF */
  570. static void davinci_hw_dit_param(struct davinci_audio_dev *dev)
  571. {
  572. /* Set the PDIR for Serialiser as output */
  573. mcasp_set_bits(dev->base + DAVINCI_MCASP_PDIR_REG, AFSX);
  574. /* TXMASK for 24 bits */
  575. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXMASK_REG, 0x00FFFFFF);
  576. /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
  577. and LSB first */
  578. mcasp_set_bits(dev->base + DAVINCI_MCASP_TXFMT_REG,
  579. TXROT(6) | TXSSZ(15));
  580. /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
  581. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXFMCTL_REG,
  582. AFSXE | FSXMOD(0x180));
  583. /* Set the TX tdm : for all the slots */
  584. mcasp_set_reg(dev->base + DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
  585. /* Set the TX clock controls : div = 1 and internal */
  586. mcasp_set_bits(dev->base + DAVINCI_MCASP_ACLKXCTL_REG,
  587. ACLKXE | TX_ASYNC);
  588. mcasp_clr_bits(dev->base + DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
  589. /* Only 44100 and 48000 are valid, both have the same setting */
  590. mcasp_set_bits(dev->base + DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
  591. /* Enable the DIT */
  592. mcasp_set_bits(dev->base + DAVINCI_MCASP_TXDITCTL_REG, DITEN);
  593. }
  594. static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
  595. struct snd_pcm_hw_params *params,
  596. struct snd_soc_dai *cpu_dai)
  597. {
  598. struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  599. struct davinci_pcm_dma_params *dma_params =
  600. &dev->dma_params[substream->stream];
  601. int word_length;
  602. u8 fifo_level;
  603. davinci_hw_common_param(dev, substream->stream);
  604. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  605. fifo_level = dev->txnumevt;
  606. else
  607. fifo_level = dev->rxnumevt;
  608. if (dev->op_mode == DAVINCI_MCASP_DIT_MODE)
  609. davinci_hw_dit_param(dev);
  610. else
  611. davinci_hw_param(dev, substream->stream);
  612. switch (params_format(params)) {
  613. case SNDRV_PCM_FORMAT_U8:
  614. case SNDRV_PCM_FORMAT_S8:
  615. dma_params->data_type = 1;
  616. word_length = DAVINCI_AUDIO_WORD_8;
  617. break;
  618. case SNDRV_PCM_FORMAT_U16_LE:
  619. case SNDRV_PCM_FORMAT_S16_LE:
  620. dma_params->data_type = 2;
  621. word_length = DAVINCI_AUDIO_WORD_16;
  622. break;
  623. case SNDRV_PCM_FORMAT_U32_LE:
  624. case SNDRV_PCM_FORMAT_S32_LE:
  625. dma_params->data_type = 4;
  626. word_length = DAVINCI_AUDIO_WORD_32;
  627. break;
  628. default:
  629. printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
  630. return -EINVAL;
  631. }
  632. if (dev->version == MCASP_VERSION_2 && !fifo_level)
  633. dma_params->acnt = 4;
  634. else
  635. dma_params->acnt = dma_params->data_type;
  636. dma_params->fifo_level = fifo_level;
  637. davinci_config_channel_size(dev, word_length);
  638. return 0;
  639. }
  640. static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
  641. int cmd, struct snd_soc_dai *cpu_dai)
  642. {
  643. struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
  644. int ret = 0;
  645. switch (cmd) {
  646. case SNDRV_PCM_TRIGGER_RESUME:
  647. case SNDRV_PCM_TRIGGER_START:
  648. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  649. if (!dev->clk_active) {
  650. clk_enable(dev->clk);
  651. dev->clk_active = 1;
  652. }
  653. davinci_mcasp_start(dev, substream->stream);
  654. break;
  655. case SNDRV_PCM_TRIGGER_SUSPEND:
  656. davinci_mcasp_stop(dev, substream->stream);
  657. if (dev->clk_active) {
  658. clk_disable(dev->clk);
  659. dev->clk_active = 0;
  660. }
  661. break;
  662. case SNDRV_PCM_TRIGGER_STOP:
  663. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  664. davinci_mcasp_stop(dev, substream->stream);
  665. break;
  666. default:
  667. ret = -EINVAL;
  668. }
  669. return ret;
  670. }
  671. static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
  672. struct snd_soc_dai *dai)
  673. {
  674. struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(dai);
  675. snd_soc_dai_set_dma_data(dai, substream, dev->dma_params);
  676. return 0;
  677. }
  678. static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
  679. .startup = davinci_mcasp_startup,
  680. .trigger = davinci_mcasp_trigger,
  681. .hw_params = davinci_mcasp_hw_params,
  682. .set_fmt = davinci_mcasp_set_dai_fmt,
  683. };
  684. #define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
  685. SNDRV_PCM_FMTBIT_U8 | \
  686. SNDRV_PCM_FMTBIT_S16_LE | \
  687. SNDRV_PCM_FMTBIT_U16_LE | \
  688. SNDRV_PCM_FMTBIT_S32_LE | \
  689. SNDRV_PCM_FMTBIT_U32_LE)
  690. static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
  691. {
  692. .name = "davinci-mcasp.0",
  693. .playback = {
  694. .channels_min = 2,
  695. .channels_max = 2,
  696. .rates = DAVINCI_MCASP_RATES,
  697. .formats = DAVINCI_MCASP_PCM_FMTS,
  698. },
  699. .capture = {
  700. .channels_min = 2,
  701. .channels_max = 2,
  702. .rates = DAVINCI_MCASP_RATES,
  703. .formats = DAVINCI_MCASP_PCM_FMTS,
  704. },
  705. .ops = &davinci_mcasp_dai_ops,
  706. },
  707. {
  708. "davinci-mcasp.1",
  709. .playback = {
  710. .channels_min = 1,
  711. .channels_max = 384,
  712. .rates = DAVINCI_MCASP_RATES,
  713. .formats = DAVINCI_MCASP_PCM_FMTS,
  714. },
  715. .ops = &davinci_mcasp_dai_ops,
  716. },
  717. };
  718. static int davinci_mcasp_probe(struct platform_device *pdev)
  719. {
  720. struct davinci_pcm_dma_params *dma_data;
  721. struct resource *mem, *ioarea, *res;
  722. struct snd_platform_data *pdata;
  723. struct davinci_audio_dev *dev;
  724. int ret;
  725. dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_audio_dev),
  726. GFP_KERNEL);
  727. if (!dev)
  728. return -ENOMEM;
  729. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  730. if (!mem) {
  731. dev_err(&pdev->dev, "no mem resource?\n");
  732. return -ENODEV;
  733. }
  734. ioarea = devm_request_mem_region(&pdev->dev, mem->start,
  735. resource_size(mem), pdev->name);
  736. if (!ioarea) {
  737. dev_err(&pdev->dev, "Audio region already claimed\n");
  738. return -EBUSY;
  739. }
  740. pdata = pdev->dev.platform_data;
  741. dev->clk = clk_get(&pdev->dev, NULL);
  742. if (IS_ERR(dev->clk))
  743. return -ENODEV;
  744. clk_enable(dev->clk);
  745. dev->clk_active = 1;
  746. dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
  747. if (!dev->base) {
  748. dev_err(&pdev->dev, "ioremap failed\n");
  749. ret = -ENOMEM;
  750. goto err_release_clk;
  751. }
  752. dev->op_mode = pdata->op_mode;
  753. dev->tdm_slots = pdata->tdm_slots;
  754. dev->num_serializer = pdata->num_serializer;
  755. dev->serial_dir = pdata->serial_dir;
  756. dev->codec_fmt = pdata->codec_fmt;
  757. dev->version = pdata->version;
  758. dev->txnumevt = pdata->txnumevt;
  759. dev->rxnumevt = pdata->rxnumevt;
  760. dma_data = &dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK];
  761. dma_data->asp_chan_q = pdata->asp_chan_q;
  762. dma_data->ram_chan_q = pdata->ram_chan_q;
  763. dma_data->sram_size = pdata->sram_size_playback;
  764. dma_data->dma_addr = (dma_addr_t) (pdata->tx_dma_offset +
  765. mem->start);
  766. /* first TX, then RX */
  767. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  768. if (!res) {
  769. dev_err(&pdev->dev, "no DMA resource\n");
  770. ret = -ENODEV;
  771. goto err_release_clk;
  772. }
  773. dma_data->channel = res->start;
  774. dma_data = &dev->dma_params[SNDRV_PCM_STREAM_CAPTURE];
  775. dma_data->asp_chan_q = pdata->asp_chan_q;
  776. dma_data->ram_chan_q = pdata->ram_chan_q;
  777. dma_data->sram_size = pdata->sram_size_capture;
  778. dma_data->dma_addr = (dma_addr_t)(pdata->rx_dma_offset +
  779. mem->start);
  780. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  781. if (!res) {
  782. dev_err(&pdev->dev, "no DMA resource\n");
  783. ret = -ENODEV;
  784. goto err_release_clk;
  785. }
  786. dma_data->channel = res->start;
  787. dev_set_drvdata(&pdev->dev, dev);
  788. ret = snd_soc_register_dai(&pdev->dev, &davinci_mcasp_dai[pdata->op_mode]);
  789. if (ret != 0)
  790. goto err_release_clk;
  791. return 0;
  792. err_release_clk:
  793. clk_disable(dev->clk);
  794. clk_put(dev->clk);
  795. return ret;
  796. }
  797. static int davinci_mcasp_remove(struct platform_device *pdev)
  798. {
  799. struct davinci_audio_dev *dev = dev_get_drvdata(&pdev->dev);
  800. snd_soc_unregister_dai(&pdev->dev);
  801. clk_disable(dev->clk);
  802. clk_put(dev->clk);
  803. dev->clk = NULL;
  804. return 0;
  805. }
  806. static struct platform_driver davinci_mcasp_driver = {
  807. .probe = davinci_mcasp_probe,
  808. .remove = davinci_mcasp_remove,
  809. .driver = {
  810. .name = "davinci-mcasp",
  811. .owner = THIS_MODULE,
  812. },
  813. };
  814. module_platform_driver(davinci_mcasp_driver);
  815. MODULE_AUTHOR("Steve Chen");
  816. MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
  817. MODULE_LICENSE("GPL");