davinci-mcasp.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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 <linux/pm_runtime.h>
  25. #include <linux/of.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_data/davinci_asp.h>
  29. #include <linux/math64.h>
  30. #include <sound/asoundef.h>
  31. #include <sound/core.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/initval.h>
  35. #include <sound/soc.h>
  36. #include <sound/dmaengine_pcm.h>
  37. #include <sound/omap-pcm.h>
  38. #include "edma-pcm.h"
  39. #include "davinci-mcasp.h"
  40. #define MCASP_MAX_AFIFO_DEPTH 64
  41. static u32 context_regs[] = {
  42. DAVINCI_MCASP_TXFMCTL_REG,
  43. DAVINCI_MCASP_RXFMCTL_REG,
  44. DAVINCI_MCASP_TXFMT_REG,
  45. DAVINCI_MCASP_RXFMT_REG,
  46. DAVINCI_MCASP_ACLKXCTL_REG,
  47. DAVINCI_MCASP_ACLKRCTL_REG,
  48. DAVINCI_MCASP_AHCLKXCTL_REG,
  49. DAVINCI_MCASP_AHCLKRCTL_REG,
  50. DAVINCI_MCASP_PDIR_REG,
  51. DAVINCI_MCASP_RXMASK_REG,
  52. DAVINCI_MCASP_TXMASK_REG,
  53. DAVINCI_MCASP_RXTDM_REG,
  54. DAVINCI_MCASP_TXTDM_REG,
  55. };
  56. struct davinci_mcasp_context {
  57. u32 config_regs[ARRAY_SIZE(context_regs)];
  58. u32 afifo_regs[2]; /* for read/write fifo control registers */
  59. u32 *xrsr_regs; /* for serializer configuration */
  60. bool pm_state;
  61. };
  62. struct davinci_mcasp_ruledata {
  63. struct davinci_mcasp *mcasp;
  64. int serializers;
  65. };
  66. struct davinci_mcasp {
  67. struct snd_dmaengine_dai_dma_data dma_data[2];
  68. void __iomem *base;
  69. u32 fifo_base;
  70. struct device *dev;
  71. struct snd_pcm_substream *substreams[2];
  72. /* McASP specific data */
  73. int tdm_slots;
  74. u32 tdm_mask[2];
  75. int slot_width;
  76. u8 op_mode;
  77. u8 num_serializer;
  78. u8 *serial_dir;
  79. u8 version;
  80. u8 bclk_div;
  81. int streams;
  82. u32 irq_request[2];
  83. int dma_request[2];
  84. int sysclk_freq;
  85. bool bclk_master;
  86. /* McASP FIFO related */
  87. u8 txnumevt;
  88. u8 rxnumevt;
  89. bool dat_port;
  90. /* Used for comstraint setting on the second stream */
  91. u32 channels;
  92. #ifdef CONFIG_PM_SLEEP
  93. struct davinci_mcasp_context context;
  94. #endif
  95. struct davinci_mcasp_ruledata ruledata[2];
  96. struct snd_pcm_hw_constraint_list chconstr[2];
  97. };
  98. static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
  99. u32 val)
  100. {
  101. void __iomem *reg = mcasp->base + offset;
  102. __raw_writel(__raw_readl(reg) | val, reg);
  103. }
  104. static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
  105. u32 val)
  106. {
  107. void __iomem *reg = mcasp->base + offset;
  108. __raw_writel((__raw_readl(reg) & ~(val)), reg);
  109. }
  110. static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
  111. u32 val, u32 mask)
  112. {
  113. void __iomem *reg = mcasp->base + offset;
  114. __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
  115. }
  116. static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
  117. u32 val)
  118. {
  119. __raw_writel(val, mcasp->base + offset);
  120. }
  121. static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
  122. {
  123. return (u32)__raw_readl(mcasp->base + offset);
  124. }
  125. static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
  126. {
  127. int i = 0;
  128. mcasp_set_bits(mcasp, ctl_reg, val);
  129. /* programming GBLCTL needs to read back from GBLCTL and verfiy */
  130. /* loop count is to avoid the lock-up */
  131. for (i = 0; i < 1000; i++) {
  132. if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
  133. break;
  134. }
  135. if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
  136. printk(KERN_ERR "GBLCTL write error\n");
  137. }
  138. static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
  139. {
  140. u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
  141. u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
  142. return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
  143. }
  144. static void mcasp_start_rx(struct davinci_mcasp *mcasp)
  145. {
  146. if (mcasp->rxnumevt) { /* enable FIFO */
  147. u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
  148. mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
  149. mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
  150. }
  151. /* Start clocks */
  152. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
  153. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
  154. /*
  155. * When ASYNC == 0 the transmit and receive sections operate
  156. * synchronously from the transmit clock and frame sync. We need to make
  157. * sure that the TX signlas are enabled when starting reception.
  158. */
  159. if (mcasp_is_synchronous(mcasp)) {
  160. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
  161. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
  162. }
  163. /* Activate serializer(s) */
  164. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
  165. /* Release RX state machine */
  166. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
  167. /* Release Frame Sync generator */
  168. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
  169. if (mcasp_is_synchronous(mcasp))
  170. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
  171. /* enable receive IRQs */
  172. mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
  173. mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
  174. }
  175. static void mcasp_start_tx(struct davinci_mcasp *mcasp)
  176. {
  177. u32 cnt;
  178. if (mcasp->txnumevt) { /* enable FIFO */
  179. u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
  180. mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
  181. mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
  182. }
  183. /* Start clocks */
  184. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
  185. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
  186. /* Activate serializer(s) */
  187. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
  188. /* wait for XDATA to be cleared */
  189. cnt = 0;
  190. while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) &&
  191. (cnt < 100000))
  192. cnt++;
  193. /* Release TX state machine */
  194. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
  195. /* Release Frame Sync generator */
  196. mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
  197. /* enable transmit IRQs */
  198. mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
  199. mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
  200. }
  201. static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
  202. {
  203. mcasp->streams++;
  204. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  205. mcasp_start_tx(mcasp);
  206. else
  207. mcasp_start_rx(mcasp);
  208. }
  209. static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
  210. {
  211. /* disable IRQ sources */
  212. mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
  213. mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
  214. /*
  215. * In synchronous mode stop the TX clocks if no other stream is
  216. * running
  217. */
  218. if (mcasp_is_synchronous(mcasp) && !mcasp->streams)
  219. mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
  220. mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
  221. mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
  222. if (mcasp->rxnumevt) { /* disable FIFO */
  223. u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
  224. mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
  225. }
  226. }
  227. static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
  228. {
  229. u32 val = 0;
  230. /* disable IRQ sources */
  231. mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
  232. mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
  233. /*
  234. * In synchronous mode keep TX clocks running if the capture stream is
  235. * still running.
  236. */
  237. if (mcasp_is_synchronous(mcasp) && mcasp->streams)
  238. val = TXHCLKRST | TXCLKRST | TXFSRST;
  239. mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
  240. mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
  241. if (mcasp->txnumevt) { /* disable FIFO */
  242. u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
  243. mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
  244. }
  245. }
  246. static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
  247. {
  248. mcasp->streams--;
  249. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  250. mcasp_stop_tx(mcasp);
  251. else
  252. mcasp_stop_rx(mcasp);
  253. }
  254. static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
  255. {
  256. struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
  257. struct snd_pcm_substream *substream;
  258. u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
  259. u32 handled_mask = 0;
  260. u32 stat;
  261. stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
  262. if (stat & XUNDRN & irq_mask) {
  263. dev_warn(mcasp->dev, "Transmit buffer underflow\n");
  264. handled_mask |= XUNDRN;
  265. substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
  266. if (substream) {
  267. snd_pcm_stream_lock_irq(substream);
  268. if (snd_pcm_running(substream))
  269. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  270. snd_pcm_stream_unlock_irq(substream);
  271. }
  272. }
  273. if (!handled_mask)
  274. dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
  275. stat);
  276. if (stat & XRERR)
  277. handled_mask |= XRERR;
  278. /* Ack the handled event only */
  279. mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
  280. return IRQ_RETVAL(handled_mask);
  281. }
  282. static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
  283. {
  284. struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
  285. struct snd_pcm_substream *substream;
  286. u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
  287. u32 handled_mask = 0;
  288. u32 stat;
  289. stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
  290. if (stat & ROVRN & irq_mask) {
  291. dev_warn(mcasp->dev, "Receive buffer overflow\n");
  292. handled_mask |= ROVRN;
  293. substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
  294. if (substream) {
  295. snd_pcm_stream_lock_irq(substream);
  296. if (snd_pcm_running(substream))
  297. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  298. snd_pcm_stream_unlock_irq(substream);
  299. }
  300. }
  301. if (!handled_mask)
  302. dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
  303. stat);
  304. if (stat & XRERR)
  305. handled_mask |= XRERR;
  306. /* Ack the handled event only */
  307. mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
  308. return IRQ_RETVAL(handled_mask);
  309. }
  310. static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
  311. {
  312. struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
  313. irqreturn_t ret = IRQ_NONE;
  314. if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
  315. ret = davinci_mcasp_tx_irq_handler(irq, data);
  316. if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
  317. ret |= davinci_mcasp_rx_irq_handler(irq, data);
  318. return ret;
  319. }
  320. static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  321. unsigned int fmt)
  322. {
  323. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
  324. int ret = 0;
  325. u32 data_delay;
  326. bool fs_pol_rising;
  327. bool inv_fs = false;
  328. pm_runtime_get_sync(mcasp->dev);
  329. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  330. case SND_SOC_DAIFMT_DSP_A:
  331. mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
  332. mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
  333. /* 1st data bit occur one ACLK cycle after the frame sync */
  334. data_delay = 1;
  335. break;
  336. case SND_SOC_DAIFMT_DSP_B:
  337. case SND_SOC_DAIFMT_AC97:
  338. mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
  339. mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
  340. /* No delay after FS */
  341. data_delay = 0;
  342. break;
  343. case SND_SOC_DAIFMT_I2S:
  344. /* configure a full-word SYNC pulse (LRCLK) */
  345. mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
  346. mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
  347. /* 1st data bit occur one ACLK cycle after the frame sync */
  348. data_delay = 1;
  349. /* FS need to be inverted */
  350. inv_fs = true;
  351. break;
  352. case SND_SOC_DAIFMT_LEFT_J:
  353. /* configure a full-word SYNC pulse (LRCLK) */
  354. mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
  355. mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
  356. /* No delay after FS */
  357. data_delay = 0;
  358. break;
  359. default:
  360. ret = -EINVAL;
  361. goto out;
  362. }
  363. mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
  364. FSXDLY(3));
  365. mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
  366. FSRDLY(3));
  367. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  368. case SND_SOC_DAIFMT_CBS_CFS:
  369. /* codec is clock and frame slave */
  370. mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
  371. mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
  372. mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
  373. mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
  374. mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
  375. mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
  376. mcasp->bclk_master = 1;
  377. break;
  378. case SND_SOC_DAIFMT_CBS_CFM:
  379. /* codec is clock slave and frame master */
  380. mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
  381. mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
  382. mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
  383. mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
  384. mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
  385. mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
  386. mcasp->bclk_master = 1;
  387. break;
  388. case SND_SOC_DAIFMT_CBM_CFS:
  389. /* codec is clock master and frame slave */
  390. mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
  391. mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
  392. mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
  393. mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
  394. mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
  395. mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
  396. mcasp->bclk_master = 0;
  397. break;
  398. case SND_SOC_DAIFMT_CBM_CFM:
  399. /* codec is clock and frame master */
  400. mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
  401. mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
  402. mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
  403. mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
  404. mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG,
  405. ACLKX | AHCLKX | AFSX | ACLKR | AHCLKR | AFSR);
  406. mcasp->bclk_master = 0;
  407. break;
  408. default:
  409. ret = -EINVAL;
  410. goto out;
  411. }
  412. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  413. case SND_SOC_DAIFMT_IB_NF:
  414. mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
  415. mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
  416. fs_pol_rising = true;
  417. break;
  418. case SND_SOC_DAIFMT_NB_IF:
  419. mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
  420. mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
  421. fs_pol_rising = false;
  422. break;
  423. case SND_SOC_DAIFMT_IB_IF:
  424. mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
  425. mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
  426. fs_pol_rising = false;
  427. break;
  428. case SND_SOC_DAIFMT_NB_NF:
  429. mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
  430. mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
  431. fs_pol_rising = true;
  432. break;
  433. default:
  434. ret = -EINVAL;
  435. goto out;
  436. }
  437. if (inv_fs)
  438. fs_pol_rising = !fs_pol_rising;
  439. if (fs_pol_rising) {
  440. mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
  441. mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
  442. } else {
  443. mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
  444. mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
  445. }
  446. out:
  447. pm_runtime_put(mcasp->dev);
  448. return ret;
  449. }
  450. static int __davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
  451. int div, bool explicit)
  452. {
  453. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
  454. pm_runtime_get_sync(mcasp->dev);
  455. switch (div_id) {
  456. case 0: /* MCLK divider */
  457. mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
  458. AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
  459. mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
  460. AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
  461. break;
  462. case 1: /* BCLK divider */
  463. mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
  464. ACLKXDIV(div - 1), ACLKXDIV_MASK);
  465. mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
  466. ACLKRDIV(div - 1), ACLKRDIV_MASK);
  467. if (explicit)
  468. mcasp->bclk_div = div;
  469. break;
  470. case 2: /*
  471. * BCLK/LRCLK ratio descries how many bit-clock cycles
  472. * fit into one frame. The clock ratio is given for a
  473. * full period of data (for I2S format both left and
  474. * right channels), so it has to be divided by number
  475. * of tdm-slots (for I2S - divided by 2).
  476. * Instead of storing this ratio, we calculate a new
  477. * tdm_slot width by dividing the the ratio by the
  478. * number of configured tdm slots.
  479. */
  480. mcasp->slot_width = div / mcasp->tdm_slots;
  481. if (div % mcasp->tdm_slots)
  482. dev_warn(mcasp->dev,
  483. "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots",
  484. __func__, div, mcasp->tdm_slots);
  485. break;
  486. default:
  487. return -EINVAL;
  488. }
  489. pm_runtime_put(mcasp->dev);
  490. return 0;
  491. }
  492. static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
  493. int div)
  494. {
  495. return __davinci_mcasp_set_clkdiv(dai, div_id, div, 1);
  496. }
  497. static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  498. unsigned int freq, int dir)
  499. {
  500. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
  501. pm_runtime_get_sync(mcasp->dev);
  502. if (dir == SND_SOC_CLOCK_OUT) {
  503. mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
  504. mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
  505. mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
  506. } else {
  507. mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
  508. mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
  509. mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
  510. }
  511. mcasp->sysclk_freq = freq;
  512. pm_runtime_put(mcasp->dev);
  513. return 0;
  514. }
  515. /* All serializers must have equal number of channels */
  516. static int davinci_mcasp_ch_constraint(struct davinci_mcasp *mcasp, int stream,
  517. int serializers)
  518. {
  519. struct snd_pcm_hw_constraint_list *cl = &mcasp->chconstr[stream];
  520. unsigned int *list = (unsigned int *) cl->list;
  521. int slots = mcasp->tdm_slots;
  522. int i, count = 0;
  523. if (mcasp->tdm_mask[stream])
  524. slots = hweight32(mcasp->tdm_mask[stream]);
  525. for (i = 2; i <= slots; i++)
  526. list[count++] = i;
  527. for (i = 2; i <= serializers; i++)
  528. list[count++] = i*slots;
  529. cl->count = count;
  530. return 0;
  531. }
  532. static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp *mcasp)
  533. {
  534. int rx_serializers = 0, tx_serializers = 0, ret, i;
  535. for (i = 0; i < mcasp->num_serializer; i++)
  536. if (mcasp->serial_dir[i] == TX_MODE)
  537. tx_serializers++;
  538. else if (mcasp->serial_dir[i] == RX_MODE)
  539. rx_serializers++;
  540. ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_PLAYBACK,
  541. tx_serializers);
  542. if (ret)
  543. return ret;
  544. ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_CAPTURE,
  545. rx_serializers);
  546. return ret;
  547. }
  548. static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai *dai,
  549. unsigned int tx_mask,
  550. unsigned int rx_mask,
  551. int slots, int slot_width)
  552. {
  553. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
  554. dev_dbg(mcasp->dev,
  555. "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
  556. __func__, tx_mask, rx_mask, slots, slot_width);
  557. if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) {
  558. dev_err(mcasp->dev,
  559. "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
  560. tx_mask, rx_mask, slots);
  561. return -EINVAL;
  562. }
  563. if (slot_width &&
  564. (slot_width < 8 || slot_width > 32 || slot_width % 4 != 0)) {
  565. dev_err(mcasp->dev, "%s: Unsupported slot_width %d\n",
  566. __func__, slot_width);
  567. return -EINVAL;
  568. }
  569. mcasp->tdm_slots = slots;
  570. mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
  571. mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
  572. mcasp->slot_width = slot_width;
  573. return davinci_mcasp_set_ch_constraints(mcasp);
  574. }
  575. static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
  576. int sample_width)
  577. {
  578. u32 fmt;
  579. u32 tx_rotate = (sample_width / 4) & 0x7;
  580. u32 mask = (1ULL << sample_width) - 1;
  581. u32 slot_width = sample_width;
  582. /*
  583. * For captured data we should not rotate, inversion and masking is
  584. * enoguh to get the data to the right position:
  585. * Format data from bus after reverse (XRBUF)
  586. * S16_LE: |LSB|MSB|xxx|xxx| |xxx|xxx|MSB|LSB|
  587. * S24_3LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
  588. * S24_LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
  589. * S32_LE: |LSB|DAT|DAT|MSB| |MSB|DAT|DAT|LSB|
  590. */
  591. u32 rx_rotate = 0;
  592. /*
  593. * Setting the tdm slot width either with set_clkdiv() or
  594. * set_tdm_slot() allows us to for example send 32 bits per
  595. * channel to the codec, while only 16 of them carry audio
  596. * payload.
  597. */
  598. if (mcasp->slot_width) {
  599. /*
  600. * When we have more bclk then it is needed for the
  601. * data, we need to use the rotation to move the
  602. * received samples to have correct alignment.
  603. */
  604. slot_width = mcasp->slot_width;
  605. rx_rotate = (slot_width - sample_width) / 4;
  606. }
  607. /* mapping of the XSSZ bit-field as described in the datasheet */
  608. fmt = (slot_width >> 1) - 1;
  609. if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
  610. mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
  611. RXSSZ(0x0F));
  612. mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
  613. TXSSZ(0x0F));
  614. mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
  615. TXROT(7));
  616. mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
  617. RXROT(7));
  618. mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
  619. }
  620. mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
  621. return 0;
  622. }
  623. static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
  624. int period_words, int channels)
  625. {
  626. struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
  627. int i;
  628. u8 tx_ser = 0;
  629. u8 rx_ser = 0;
  630. u8 slots = mcasp->tdm_slots;
  631. u8 max_active_serializers = (channels + slots - 1) / slots;
  632. int active_serializers, numevt;
  633. u32 reg;
  634. /* Default configuration */
  635. if (mcasp->version < MCASP_VERSION_3)
  636. mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
  637. /* All PINS as McASP */
  638. mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
  639. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  640. mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
  641. mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
  642. } else {
  643. mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
  644. mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
  645. }
  646. for (i = 0; i < mcasp->num_serializer; i++) {
  647. mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
  648. mcasp->serial_dir[i]);
  649. if (mcasp->serial_dir[i] == TX_MODE &&
  650. tx_ser < max_active_serializers) {
  651. mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
  652. mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
  653. DISMOD_LOW, DISMOD_MASK);
  654. tx_ser++;
  655. } else if (mcasp->serial_dir[i] == RX_MODE &&
  656. rx_ser < max_active_serializers) {
  657. mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
  658. rx_ser++;
  659. } else {
  660. mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
  661. SRMOD_INACTIVE, SRMOD_MASK);
  662. }
  663. }
  664. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  665. active_serializers = tx_ser;
  666. numevt = mcasp->txnumevt;
  667. reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
  668. } else {
  669. active_serializers = rx_ser;
  670. numevt = mcasp->rxnumevt;
  671. reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
  672. }
  673. if (active_serializers < max_active_serializers) {
  674. dev_warn(mcasp->dev, "stream has more channels (%d) than are "
  675. "enabled in mcasp (%d)\n", channels,
  676. active_serializers * slots);
  677. return -EINVAL;
  678. }
  679. /* AFIFO is not in use */
  680. if (!numevt) {
  681. /* Configure the burst size for platform drivers */
  682. if (active_serializers > 1) {
  683. /*
  684. * If more than one serializers are in use we have one
  685. * DMA request to provide data for all serializers.
  686. * For example if three serializers are enabled the DMA
  687. * need to transfer three words per DMA request.
  688. */
  689. dma_data->maxburst = active_serializers;
  690. } else {
  691. dma_data->maxburst = 0;
  692. }
  693. return 0;
  694. }
  695. if (period_words % active_serializers) {
  696. dev_err(mcasp->dev, "Invalid combination of period words and "
  697. "active serializers: %d, %d\n", period_words,
  698. active_serializers);
  699. return -EINVAL;
  700. }
  701. /*
  702. * Calculate the optimal AFIFO depth for platform side:
  703. * The number of words for numevt need to be in steps of active
  704. * serializers.
  705. */
  706. numevt = (numevt / active_serializers) * active_serializers;
  707. while (period_words % numevt && numevt > 0)
  708. numevt -= active_serializers;
  709. if (numevt <= 0)
  710. numevt = active_serializers;
  711. mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
  712. mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
  713. /* Configure the burst size for platform drivers */
  714. if (numevt == 1)
  715. numevt = 0;
  716. dma_data->maxburst = numevt;
  717. return 0;
  718. }
  719. static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
  720. int channels)
  721. {
  722. int i, active_slots;
  723. int total_slots;
  724. int active_serializers;
  725. u32 mask = 0;
  726. u32 busel = 0;
  727. total_slots = mcasp->tdm_slots;
  728. /*
  729. * If more than one serializer is needed, then use them with
  730. * all the specified tdm_slots. Otherwise, one serializer can
  731. * cope with the transaction using just as many slots as there
  732. * are channels in the stream.
  733. */
  734. if (mcasp->tdm_mask[stream]) {
  735. active_slots = hweight32(mcasp->tdm_mask[stream]);
  736. active_serializers = (channels + active_slots - 1) /
  737. active_slots;
  738. if (active_serializers == 1) {
  739. active_slots = channels;
  740. for (i = 0; i < total_slots; i++) {
  741. if ((1 << i) & mcasp->tdm_mask[stream]) {
  742. mask |= (1 << i);
  743. if (--active_slots <= 0)
  744. break;
  745. }
  746. }
  747. }
  748. } else {
  749. active_serializers = (channels + total_slots - 1) / total_slots;
  750. if (active_serializers == 1)
  751. active_slots = channels;
  752. else
  753. active_slots = total_slots;
  754. for (i = 0; i < active_slots; i++)
  755. mask |= (1 << i);
  756. }
  757. mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
  758. if (!mcasp->dat_port)
  759. busel = TXSEL;
  760. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  761. mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
  762. mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
  763. mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
  764. FSXMOD(total_slots), FSXMOD(0x1FF));
  765. } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  766. mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
  767. mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
  768. mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
  769. FSRMOD(total_slots), FSRMOD(0x1FF));
  770. /*
  771. * If McASP is set to be TX/RX synchronous and the playback is
  772. * not running already we need to configure the TX slots in
  773. * order to have correct FSX on the bus
  774. */
  775. if (mcasp_is_synchronous(mcasp) && !mcasp->channels)
  776. mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
  777. FSXMOD(total_slots), FSXMOD(0x1FF));
  778. }
  779. return 0;
  780. }
  781. /* S/PDIF */
  782. static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
  783. unsigned int rate)
  784. {
  785. u32 cs_value = 0;
  786. u8 *cs_bytes = (u8*) &cs_value;
  787. /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
  788. and LSB first */
  789. mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
  790. /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
  791. mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
  792. /* Set the TX tdm : for all the slots */
  793. mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
  794. /* Set the TX clock controls : div = 1 and internal */
  795. mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
  796. mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
  797. /* Only 44100 and 48000 are valid, both have the same setting */
  798. mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
  799. /* Enable the DIT */
  800. mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
  801. /* Set S/PDIF channel status bits */
  802. cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
  803. cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
  804. switch (rate) {
  805. case 22050:
  806. cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
  807. break;
  808. case 24000:
  809. cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
  810. break;
  811. case 32000:
  812. cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
  813. break;
  814. case 44100:
  815. cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
  816. break;
  817. case 48000:
  818. cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
  819. break;
  820. case 88200:
  821. cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
  822. break;
  823. case 96000:
  824. cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
  825. break;
  826. case 176400:
  827. cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
  828. break;
  829. case 192000:
  830. cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
  831. break;
  832. default:
  833. printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
  834. return -EINVAL;
  835. }
  836. mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
  837. mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
  838. return 0;
  839. }
  840. static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
  841. unsigned int bclk_freq,
  842. int *error_ppm)
  843. {
  844. int div = mcasp->sysclk_freq / bclk_freq;
  845. int rem = mcasp->sysclk_freq % bclk_freq;
  846. if (rem != 0) {
  847. if (div == 0 ||
  848. ((mcasp->sysclk_freq / div) - bclk_freq) >
  849. (bclk_freq - (mcasp->sysclk_freq / (div+1)))) {
  850. div++;
  851. rem = rem - bclk_freq;
  852. }
  853. }
  854. if (error_ppm)
  855. *error_ppm =
  856. (div*1000000 + (int)div64_long(1000000LL*rem,
  857. (int)bclk_freq))
  858. /div - 1000000;
  859. return div;
  860. }
  861. static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
  862. struct snd_pcm_hw_params *params,
  863. struct snd_soc_dai *cpu_dai)
  864. {
  865. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
  866. int word_length;
  867. int channels = params_channels(params);
  868. int period_size = params_period_size(params);
  869. int ret;
  870. /*
  871. * If mcasp is BCLK master, and a BCLK divider was not provided by
  872. * the machine driver, we need to calculate the ratio.
  873. */
  874. if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
  875. int slots = mcasp->tdm_slots;
  876. int rate = params_rate(params);
  877. int sbits = params_width(params);
  878. int ppm, div;
  879. if (mcasp->slot_width)
  880. sbits = mcasp->slot_width;
  881. div = davinci_mcasp_calc_clk_div(mcasp, rate*sbits*slots,
  882. &ppm);
  883. if (ppm)
  884. dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n",
  885. ppm);
  886. __davinci_mcasp_set_clkdiv(cpu_dai, 1, div, 0);
  887. }
  888. ret = mcasp_common_hw_param(mcasp, substream->stream,
  889. period_size * channels, channels);
  890. if (ret)
  891. return ret;
  892. if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
  893. ret = mcasp_dit_hw_param(mcasp, params_rate(params));
  894. else
  895. ret = mcasp_i2s_hw_param(mcasp, substream->stream,
  896. channels);
  897. if (ret)
  898. return ret;
  899. switch (params_format(params)) {
  900. case SNDRV_PCM_FORMAT_U8:
  901. case SNDRV_PCM_FORMAT_S8:
  902. word_length = 8;
  903. break;
  904. case SNDRV_PCM_FORMAT_U16_LE:
  905. case SNDRV_PCM_FORMAT_S16_LE:
  906. word_length = 16;
  907. break;
  908. case SNDRV_PCM_FORMAT_U24_3LE:
  909. case SNDRV_PCM_FORMAT_S24_3LE:
  910. word_length = 24;
  911. break;
  912. case SNDRV_PCM_FORMAT_U24_LE:
  913. case SNDRV_PCM_FORMAT_S24_LE:
  914. word_length = 24;
  915. break;
  916. case SNDRV_PCM_FORMAT_U32_LE:
  917. case SNDRV_PCM_FORMAT_S32_LE:
  918. word_length = 32;
  919. break;
  920. default:
  921. printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
  922. return -EINVAL;
  923. }
  924. davinci_config_channel_size(mcasp, word_length);
  925. if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE)
  926. mcasp->channels = channels;
  927. return 0;
  928. }
  929. static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
  930. int cmd, struct snd_soc_dai *cpu_dai)
  931. {
  932. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
  933. int ret = 0;
  934. switch (cmd) {
  935. case SNDRV_PCM_TRIGGER_RESUME:
  936. case SNDRV_PCM_TRIGGER_START:
  937. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  938. davinci_mcasp_start(mcasp, substream->stream);
  939. break;
  940. case SNDRV_PCM_TRIGGER_SUSPEND:
  941. case SNDRV_PCM_TRIGGER_STOP:
  942. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  943. davinci_mcasp_stop(mcasp, substream->stream);
  944. break;
  945. default:
  946. ret = -EINVAL;
  947. }
  948. return ret;
  949. }
  950. static const unsigned int davinci_mcasp_dai_rates[] = {
  951. 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
  952. 88200, 96000, 176400, 192000,
  953. };
  954. #define DAVINCI_MAX_RATE_ERROR_PPM 1000
  955. static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params,
  956. struct snd_pcm_hw_rule *rule)
  957. {
  958. struct davinci_mcasp_ruledata *rd = rule->private;
  959. struct snd_interval *ri =
  960. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  961. int sbits = params_width(params);
  962. int slots = rd->mcasp->tdm_slots;
  963. struct snd_interval range;
  964. int i;
  965. if (rd->mcasp->slot_width)
  966. sbits = rd->mcasp->slot_width;
  967. snd_interval_any(&range);
  968. range.empty = 1;
  969. for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) {
  970. if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) {
  971. uint bclk_freq = sbits*slots*
  972. davinci_mcasp_dai_rates[i];
  973. int ppm;
  974. davinci_mcasp_calc_clk_div(rd->mcasp, bclk_freq, &ppm);
  975. if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
  976. if (range.empty) {
  977. range.min = davinci_mcasp_dai_rates[i];
  978. range.empty = 0;
  979. }
  980. range.max = davinci_mcasp_dai_rates[i];
  981. }
  982. }
  983. }
  984. dev_dbg(rd->mcasp->dev,
  985. "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
  986. ri->min, ri->max, range.min, range.max, sbits, slots);
  987. return snd_interval_refine(hw_param_interval(params, rule->var),
  988. &range);
  989. }
  990. static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params,
  991. struct snd_pcm_hw_rule *rule)
  992. {
  993. struct davinci_mcasp_ruledata *rd = rule->private;
  994. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  995. struct snd_mask nfmt;
  996. int rate = params_rate(params);
  997. int slots = rd->mcasp->tdm_slots;
  998. int i, count = 0;
  999. snd_mask_none(&nfmt);
  1000. for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
  1001. if (snd_mask_test(fmt, i)) {
  1002. uint sbits = snd_pcm_format_width(i);
  1003. int ppm;
  1004. if (rd->mcasp->slot_width)
  1005. sbits = rd->mcasp->slot_width;
  1006. davinci_mcasp_calc_clk_div(rd->mcasp, sbits*slots*rate,
  1007. &ppm);
  1008. if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
  1009. snd_mask_set(&nfmt, i);
  1010. count++;
  1011. }
  1012. }
  1013. }
  1014. dev_dbg(rd->mcasp->dev,
  1015. "%d possible sample format for %d Hz and %d tdm slots\n",
  1016. count, rate, slots);
  1017. return snd_mask_refine(fmt, &nfmt);
  1018. }
  1019. static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
  1020. struct snd_soc_dai *cpu_dai)
  1021. {
  1022. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
  1023. struct davinci_mcasp_ruledata *ruledata =
  1024. &mcasp->ruledata[substream->stream];
  1025. u32 max_channels = 0;
  1026. int i, dir;
  1027. int tdm_slots = mcasp->tdm_slots;
  1028. if (mcasp->tdm_mask[substream->stream])
  1029. tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]);
  1030. mcasp->substreams[substream->stream] = substream;
  1031. if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
  1032. return 0;
  1033. /*
  1034. * Limit the maximum allowed channels for the first stream:
  1035. * number of serializers for the direction * tdm slots per serializer
  1036. */
  1037. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1038. dir = TX_MODE;
  1039. else
  1040. dir = RX_MODE;
  1041. for (i = 0; i < mcasp->num_serializer; i++) {
  1042. if (mcasp->serial_dir[i] == dir)
  1043. max_channels++;
  1044. }
  1045. ruledata->serializers = max_channels;
  1046. max_channels *= tdm_slots;
  1047. /*
  1048. * If the already active stream has less channels than the calculated
  1049. * limnit based on the seirializers * tdm_slots, we need to use that as
  1050. * a constraint for the second stream.
  1051. * Otherwise (first stream or less allowed channels) we use the
  1052. * calculated constraint.
  1053. */
  1054. if (mcasp->channels && mcasp->channels < max_channels)
  1055. max_channels = mcasp->channels;
  1056. /*
  1057. * But we can always allow channels upto the amount of
  1058. * the available tdm_slots.
  1059. */
  1060. if (max_channels < tdm_slots)
  1061. max_channels = tdm_slots;
  1062. snd_pcm_hw_constraint_minmax(substream->runtime,
  1063. SNDRV_PCM_HW_PARAM_CHANNELS,
  1064. 2, max_channels);
  1065. snd_pcm_hw_constraint_list(substream->runtime,
  1066. 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1067. &mcasp->chconstr[substream->stream]);
  1068. if (mcasp->slot_width)
  1069. snd_pcm_hw_constraint_minmax(substream->runtime,
  1070. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1071. 8, mcasp->slot_width);
  1072. /*
  1073. * If we rely on implicit BCLK divider setting we should
  1074. * set constraints based on what we can provide.
  1075. */
  1076. if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
  1077. int ret;
  1078. ruledata->mcasp = mcasp;
  1079. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  1080. SNDRV_PCM_HW_PARAM_RATE,
  1081. davinci_mcasp_hw_rule_rate,
  1082. ruledata,
  1083. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  1084. if (ret)
  1085. return ret;
  1086. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  1087. SNDRV_PCM_HW_PARAM_FORMAT,
  1088. davinci_mcasp_hw_rule_format,
  1089. ruledata,
  1090. SNDRV_PCM_HW_PARAM_RATE, -1);
  1091. if (ret)
  1092. return ret;
  1093. }
  1094. return 0;
  1095. }
  1096. static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
  1097. struct snd_soc_dai *cpu_dai)
  1098. {
  1099. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
  1100. mcasp->substreams[substream->stream] = NULL;
  1101. if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
  1102. return;
  1103. if (!cpu_dai->active)
  1104. mcasp->channels = 0;
  1105. }
  1106. static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
  1107. .startup = davinci_mcasp_startup,
  1108. .shutdown = davinci_mcasp_shutdown,
  1109. .trigger = davinci_mcasp_trigger,
  1110. .hw_params = davinci_mcasp_hw_params,
  1111. .set_fmt = davinci_mcasp_set_dai_fmt,
  1112. .set_clkdiv = davinci_mcasp_set_clkdiv,
  1113. .set_sysclk = davinci_mcasp_set_sysclk,
  1114. .set_tdm_slot = davinci_mcasp_set_tdm_slot,
  1115. };
  1116. static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
  1117. {
  1118. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
  1119. dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  1120. dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
  1121. return 0;
  1122. }
  1123. #ifdef CONFIG_PM_SLEEP
  1124. static int davinci_mcasp_suspend(struct snd_soc_dai *dai)
  1125. {
  1126. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
  1127. struct davinci_mcasp_context *context = &mcasp->context;
  1128. u32 reg;
  1129. int i;
  1130. context->pm_state = pm_runtime_active(mcasp->dev);
  1131. if (!context->pm_state)
  1132. pm_runtime_get_sync(mcasp->dev);
  1133. for (i = 0; i < ARRAY_SIZE(context_regs); i++)
  1134. context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
  1135. if (mcasp->txnumevt) {
  1136. reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
  1137. context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
  1138. }
  1139. if (mcasp->rxnumevt) {
  1140. reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
  1141. context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
  1142. }
  1143. for (i = 0; i < mcasp->num_serializer; i++)
  1144. context->xrsr_regs[i] = mcasp_get_reg(mcasp,
  1145. DAVINCI_MCASP_XRSRCTL_REG(i));
  1146. pm_runtime_put_sync(mcasp->dev);
  1147. return 0;
  1148. }
  1149. static int davinci_mcasp_resume(struct snd_soc_dai *dai)
  1150. {
  1151. struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
  1152. struct davinci_mcasp_context *context = &mcasp->context;
  1153. u32 reg;
  1154. int i;
  1155. pm_runtime_get_sync(mcasp->dev);
  1156. for (i = 0; i < ARRAY_SIZE(context_regs); i++)
  1157. mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
  1158. if (mcasp->txnumevt) {
  1159. reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
  1160. mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
  1161. }
  1162. if (mcasp->rxnumevt) {
  1163. reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
  1164. mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
  1165. }
  1166. for (i = 0; i < mcasp->num_serializer; i++)
  1167. mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
  1168. context->xrsr_regs[i]);
  1169. if (!context->pm_state)
  1170. pm_runtime_put_sync(mcasp->dev);
  1171. return 0;
  1172. }
  1173. #else
  1174. #define davinci_mcasp_suspend NULL
  1175. #define davinci_mcasp_resume NULL
  1176. #endif
  1177. #define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
  1178. #define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
  1179. SNDRV_PCM_FMTBIT_U8 | \
  1180. SNDRV_PCM_FMTBIT_S16_LE | \
  1181. SNDRV_PCM_FMTBIT_U16_LE | \
  1182. SNDRV_PCM_FMTBIT_S24_LE | \
  1183. SNDRV_PCM_FMTBIT_U24_LE | \
  1184. SNDRV_PCM_FMTBIT_S24_3LE | \
  1185. SNDRV_PCM_FMTBIT_U24_3LE | \
  1186. SNDRV_PCM_FMTBIT_S32_LE | \
  1187. SNDRV_PCM_FMTBIT_U32_LE)
  1188. static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
  1189. {
  1190. .name = "davinci-mcasp.0",
  1191. .probe = davinci_mcasp_dai_probe,
  1192. .suspend = davinci_mcasp_suspend,
  1193. .resume = davinci_mcasp_resume,
  1194. .playback = {
  1195. .channels_min = 2,
  1196. .channels_max = 32 * 16,
  1197. .rates = DAVINCI_MCASP_RATES,
  1198. .formats = DAVINCI_MCASP_PCM_FMTS,
  1199. },
  1200. .capture = {
  1201. .channels_min = 2,
  1202. .channels_max = 32 * 16,
  1203. .rates = DAVINCI_MCASP_RATES,
  1204. .formats = DAVINCI_MCASP_PCM_FMTS,
  1205. },
  1206. .ops = &davinci_mcasp_dai_ops,
  1207. .symmetric_samplebits = 1,
  1208. .symmetric_rates = 1,
  1209. },
  1210. {
  1211. .name = "davinci-mcasp.1",
  1212. .probe = davinci_mcasp_dai_probe,
  1213. .playback = {
  1214. .channels_min = 1,
  1215. .channels_max = 384,
  1216. .rates = DAVINCI_MCASP_RATES,
  1217. .formats = DAVINCI_MCASP_PCM_FMTS,
  1218. },
  1219. .ops = &davinci_mcasp_dai_ops,
  1220. },
  1221. };
  1222. static const struct snd_soc_component_driver davinci_mcasp_component = {
  1223. .name = "davinci-mcasp",
  1224. };
  1225. /* Some HW specific values and defaults. The rest is filled in from DT. */
  1226. static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
  1227. .tx_dma_offset = 0x400,
  1228. .rx_dma_offset = 0x400,
  1229. .version = MCASP_VERSION_1,
  1230. };
  1231. static struct davinci_mcasp_pdata da830_mcasp_pdata = {
  1232. .tx_dma_offset = 0x2000,
  1233. .rx_dma_offset = 0x2000,
  1234. .version = MCASP_VERSION_2,
  1235. };
  1236. static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
  1237. .tx_dma_offset = 0,
  1238. .rx_dma_offset = 0,
  1239. .version = MCASP_VERSION_3,
  1240. };
  1241. static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
  1242. .tx_dma_offset = 0x200,
  1243. .rx_dma_offset = 0x284,
  1244. .version = MCASP_VERSION_4,
  1245. };
  1246. static const struct of_device_id mcasp_dt_ids[] = {
  1247. {
  1248. .compatible = "ti,dm646x-mcasp-audio",
  1249. .data = &dm646x_mcasp_pdata,
  1250. },
  1251. {
  1252. .compatible = "ti,da830-mcasp-audio",
  1253. .data = &da830_mcasp_pdata,
  1254. },
  1255. {
  1256. .compatible = "ti,am33xx-mcasp-audio",
  1257. .data = &am33xx_mcasp_pdata,
  1258. },
  1259. {
  1260. .compatible = "ti,dra7-mcasp-audio",
  1261. .data = &dra7_mcasp_pdata,
  1262. },
  1263. { /* sentinel */ }
  1264. };
  1265. MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
  1266. static int mcasp_reparent_fck(struct platform_device *pdev)
  1267. {
  1268. struct device_node *node = pdev->dev.of_node;
  1269. struct clk *gfclk, *parent_clk;
  1270. const char *parent_name;
  1271. int ret;
  1272. if (!node)
  1273. return 0;
  1274. parent_name = of_get_property(node, "fck_parent", NULL);
  1275. if (!parent_name)
  1276. return 0;
  1277. gfclk = clk_get(&pdev->dev, "fck");
  1278. if (IS_ERR(gfclk)) {
  1279. dev_err(&pdev->dev, "failed to get fck\n");
  1280. return PTR_ERR(gfclk);
  1281. }
  1282. parent_clk = clk_get(NULL, parent_name);
  1283. if (IS_ERR(parent_clk)) {
  1284. dev_err(&pdev->dev, "failed to get parent clock\n");
  1285. ret = PTR_ERR(parent_clk);
  1286. goto err1;
  1287. }
  1288. ret = clk_set_parent(gfclk, parent_clk);
  1289. if (ret) {
  1290. dev_err(&pdev->dev, "failed to reparent fck\n");
  1291. goto err2;
  1292. }
  1293. err2:
  1294. clk_put(parent_clk);
  1295. err1:
  1296. clk_put(gfclk);
  1297. return ret;
  1298. }
  1299. static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of(
  1300. struct platform_device *pdev)
  1301. {
  1302. struct device_node *np = pdev->dev.of_node;
  1303. struct davinci_mcasp_pdata *pdata = NULL;
  1304. const struct of_device_id *match =
  1305. of_match_device(mcasp_dt_ids, &pdev->dev);
  1306. struct of_phandle_args dma_spec;
  1307. const u32 *of_serial_dir32;
  1308. u32 val;
  1309. int i, ret = 0;
  1310. if (pdev->dev.platform_data) {
  1311. pdata = pdev->dev.platform_data;
  1312. return pdata;
  1313. } else if (match) {
  1314. pdata = (struct davinci_mcasp_pdata*) match->data;
  1315. } else {
  1316. /* control shouldn't reach here. something is wrong */
  1317. ret = -EINVAL;
  1318. goto nodata;
  1319. }
  1320. ret = of_property_read_u32(np, "op-mode", &val);
  1321. if (ret >= 0)
  1322. pdata->op_mode = val;
  1323. ret = of_property_read_u32(np, "tdm-slots", &val);
  1324. if (ret >= 0) {
  1325. if (val < 2 || val > 32) {
  1326. dev_err(&pdev->dev,
  1327. "tdm-slots must be in rage [2-32]\n");
  1328. ret = -EINVAL;
  1329. goto nodata;
  1330. }
  1331. pdata->tdm_slots = val;
  1332. }
  1333. of_serial_dir32 = of_get_property(np, "serial-dir", &val);
  1334. val /= sizeof(u32);
  1335. if (of_serial_dir32) {
  1336. u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
  1337. (sizeof(*of_serial_dir) * val),
  1338. GFP_KERNEL);
  1339. if (!of_serial_dir) {
  1340. ret = -ENOMEM;
  1341. goto nodata;
  1342. }
  1343. for (i = 0; i < val; i++)
  1344. of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
  1345. pdata->num_serializer = val;
  1346. pdata->serial_dir = of_serial_dir;
  1347. }
  1348. ret = of_property_match_string(np, "dma-names", "tx");
  1349. if (ret < 0)
  1350. goto nodata;
  1351. ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
  1352. &dma_spec);
  1353. if (ret < 0)
  1354. goto nodata;
  1355. pdata->tx_dma_channel = dma_spec.args[0];
  1356. /* RX is not valid in DIT mode */
  1357. if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) {
  1358. ret = of_property_match_string(np, "dma-names", "rx");
  1359. if (ret < 0)
  1360. goto nodata;
  1361. ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
  1362. &dma_spec);
  1363. if (ret < 0)
  1364. goto nodata;
  1365. pdata->rx_dma_channel = dma_spec.args[0];
  1366. }
  1367. ret = of_property_read_u32(np, "tx-num-evt", &val);
  1368. if (ret >= 0)
  1369. pdata->txnumevt = val;
  1370. ret = of_property_read_u32(np, "rx-num-evt", &val);
  1371. if (ret >= 0)
  1372. pdata->rxnumevt = val;
  1373. ret = of_property_read_u32(np, "sram-size-playback", &val);
  1374. if (ret >= 0)
  1375. pdata->sram_size_playback = val;
  1376. ret = of_property_read_u32(np, "sram-size-capture", &val);
  1377. if (ret >= 0)
  1378. pdata->sram_size_capture = val;
  1379. return pdata;
  1380. nodata:
  1381. if (ret < 0) {
  1382. dev_err(&pdev->dev, "Error populating platform data, err %d\n",
  1383. ret);
  1384. pdata = NULL;
  1385. }
  1386. return pdata;
  1387. }
  1388. enum {
  1389. PCM_EDMA,
  1390. PCM_SDMA,
  1391. };
  1392. static const char *sdma_prefix = "ti,omap";
  1393. static int davinci_mcasp_get_dma_type(struct davinci_mcasp *mcasp)
  1394. {
  1395. struct dma_chan *chan;
  1396. const char *tmp;
  1397. int ret = PCM_EDMA;
  1398. if (!mcasp->dev->of_node)
  1399. return PCM_EDMA;
  1400. tmp = mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data;
  1401. chan = dma_request_slave_channel_reason(mcasp->dev, tmp);
  1402. if (IS_ERR(chan)) {
  1403. if (PTR_ERR(chan) != -EPROBE_DEFER)
  1404. dev_err(mcasp->dev,
  1405. "Can't verify DMA configuration (%ld)\n",
  1406. PTR_ERR(chan));
  1407. return PTR_ERR(chan);
  1408. }
  1409. BUG_ON(!chan->device || !chan->device->dev);
  1410. if (chan->device->dev->of_node)
  1411. ret = of_property_read_string(chan->device->dev->of_node,
  1412. "compatible", &tmp);
  1413. else
  1414. dev_dbg(mcasp->dev, "DMA controller has no of-node\n");
  1415. dma_release_channel(chan);
  1416. if (ret)
  1417. return ret;
  1418. dev_dbg(mcasp->dev, "DMA controller compatible = \"%s\"\n", tmp);
  1419. if (!strncmp(tmp, sdma_prefix, strlen(sdma_prefix)))
  1420. return PCM_SDMA;
  1421. return PCM_EDMA;
  1422. }
  1423. static int davinci_mcasp_probe(struct platform_device *pdev)
  1424. {
  1425. struct snd_dmaengine_dai_dma_data *dma_data;
  1426. struct resource *mem, *res, *dat;
  1427. struct davinci_mcasp_pdata *pdata;
  1428. struct davinci_mcasp *mcasp;
  1429. char *irq_name;
  1430. int *dma;
  1431. int irq;
  1432. int ret;
  1433. if (!pdev->dev.platform_data && !pdev->dev.of_node) {
  1434. dev_err(&pdev->dev, "No platform data supplied\n");
  1435. return -EINVAL;
  1436. }
  1437. mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
  1438. GFP_KERNEL);
  1439. if (!mcasp)
  1440. return -ENOMEM;
  1441. pdata = davinci_mcasp_set_pdata_from_of(pdev);
  1442. if (!pdata) {
  1443. dev_err(&pdev->dev, "no platform data\n");
  1444. return -EINVAL;
  1445. }
  1446. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
  1447. if (!mem) {
  1448. dev_warn(mcasp->dev,
  1449. "\"mpu\" mem resource not found, using index 0\n");
  1450. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1451. if (!mem) {
  1452. dev_err(&pdev->dev, "no mem resource?\n");
  1453. return -ENODEV;
  1454. }
  1455. }
  1456. mcasp->base = devm_ioremap_resource(&pdev->dev, mem);
  1457. if (IS_ERR(mcasp->base))
  1458. return PTR_ERR(mcasp->base);
  1459. pm_runtime_enable(&pdev->dev);
  1460. mcasp->op_mode = pdata->op_mode;
  1461. /* sanity check for tdm slots parameter */
  1462. if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
  1463. if (pdata->tdm_slots < 2) {
  1464. dev_err(&pdev->dev, "invalid tdm slots: %d\n",
  1465. pdata->tdm_slots);
  1466. mcasp->tdm_slots = 2;
  1467. } else if (pdata->tdm_slots > 32) {
  1468. dev_err(&pdev->dev, "invalid tdm slots: %d\n",
  1469. pdata->tdm_slots);
  1470. mcasp->tdm_slots = 32;
  1471. } else {
  1472. mcasp->tdm_slots = pdata->tdm_slots;
  1473. }
  1474. }
  1475. mcasp->num_serializer = pdata->num_serializer;
  1476. #ifdef CONFIG_PM_SLEEP
  1477. mcasp->context.xrsr_regs = devm_kzalloc(&pdev->dev,
  1478. sizeof(u32) * mcasp->num_serializer,
  1479. GFP_KERNEL);
  1480. #endif
  1481. mcasp->serial_dir = pdata->serial_dir;
  1482. mcasp->version = pdata->version;
  1483. mcasp->txnumevt = pdata->txnumevt;
  1484. mcasp->rxnumevt = pdata->rxnumevt;
  1485. mcasp->dev = &pdev->dev;
  1486. irq = platform_get_irq_byname(pdev, "common");
  1487. if (irq >= 0) {
  1488. irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common",
  1489. dev_name(&pdev->dev));
  1490. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  1491. davinci_mcasp_common_irq_handler,
  1492. IRQF_ONESHOT | IRQF_SHARED,
  1493. irq_name, mcasp);
  1494. if (ret) {
  1495. dev_err(&pdev->dev, "common IRQ request failed\n");
  1496. goto err;
  1497. }
  1498. mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
  1499. mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
  1500. }
  1501. irq = platform_get_irq_byname(pdev, "rx");
  1502. if (irq >= 0) {
  1503. irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx",
  1504. dev_name(&pdev->dev));
  1505. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  1506. davinci_mcasp_rx_irq_handler,
  1507. IRQF_ONESHOT, irq_name, mcasp);
  1508. if (ret) {
  1509. dev_err(&pdev->dev, "RX IRQ request failed\n");
  1510. goto err;
  1511. }
  1512. mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
  1513. }
  1514. irq = platform_get_irq_byname(pdev, "tx");
  1515. if (irq >= 0) {
  1516. irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx",
  1517. dev_name(&pdev->dev));
  1518. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  1519. davinci_mcasp_tx_irq_handler,
  1520. IRQF_ONESHOT, irq_name, mcasp);
  1521. if (ret) {
  1522. dev_err(&pdev->dev, "TX IRQ request failed\n");
  1523. goto err;
  1524. }
  1525. mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
  1526. }
  1527. dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
  1528. if (dat)
  1529. mcasp->dat_port = true;
  1530. dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  1531. if (dat)
  1532. dma_data->addr = dat->start;
  1533. else
  1534. dma_data->addr = mem->start + pdata->tx_dma_offset;
  1535. dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
  1536. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1537. if (res)
  1538. *dma = res->start;
  1539. else
  1540. *dma = pdata->tx_dma_channel;
  1541. /* dmaengine filter data for DT and non-DT boot */
  1542. if (pdev->dev.of_node)
  1543. dma_data->filter_data = "tx";
  1544. else
  1545. dma_data->filter_data = dma;
  1546. /* RX is not valid in DIT mode */
  1547. if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
  1548. dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
  1549. if (dat)
  1550. dma_data->addr = dat->start;
  1551. else
  1552. dma_data->addr = mem->start + pdata->rx_dma_offset;
  1553. dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE];
  1554. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  1555. if (res)
  1556. *dma = res->start;
  1557. else
  1558. *dma = pdata->rx_dma_channel;
  1559. /* dmaengine filter data for DT and non-DT boot */
  1560. if (pdev->dev.of_node)
  1561. dma_data->filter_data = "rx";
  1562. else
  1563. dma_data->filter_data = dma;
  1564. }
  1565. if (mcasp->version < MCASP_VERSION_3) {
  1566. mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
  1567. /* dma_params->dma_addr is pointing to the data port address */
  1568. mcasp->dat_port = true;
  1569. } else {
  1570. mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
  1571. }
  1572. /* Allocate memory for long enough list for all possible
  1573. * scenarios. Maximum number tdm slots is 32 and there cannot
  1574. * be more serializers than given in the configuration. The
  1575. * serializer directions could be taken into account, but it
  1576. * would make code much more complex and save only couple of
  1577. * bytes.
  1578. */
  1579. mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list =
  1580. devm_kzalloc(mcasp->dev, sizeof(unsigned int) *
  1581. (32 + mcasp->num_serializer - 2),
  1582. GFP_KERNEL);
  1583. mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list =
  1584. devm_kzalloc(mcasp->dev, sizeof(unsigned int) *
  1585. (32 + mcasp->num_serializer - 2),
  1586. GFP_KERNEL);
  1587. if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list ||
  1588. !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list)
  1589. return -ENOMEM;
  1590. ret = davinci_mcasp_set_ch_constraints(mcasp);
  1591. if (ret)
  1592. goto err;
  1593. dev_set_drvdata(&pdev->dev, mcasp);
  1594. mcasp_reparent_fck(pdev);
  1595. ret = devm_snd_soc_register_component(&pdev->dev,
  1596. &davinci_mcasp_component,
  1597. &davinci_mcasp_dai[pdata->op_mode], 1);
  1598. if (ret != 0)
  1599. goto err;
  1600. ret = davinci_mcasp_get_dma_type(mcasp);
  1601. switch (ret) {
  1602. case PCM_EDMA:
  1603. #if IS_BUILTIN(CONFIG_SND_EDMA_SOC) || \
  1604. (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
  1605. IS_MODULE(CONFIG_SND_EDMA_SOC))
  1606. ret = edma_pcm_platform_register(&pdev->dev);
  1607. #else
  1608. dev_err(&pdev->dev, "Missing SND_EDMA_SOC\n");
  1609. ret = -EINVAL;
  1610. goto err;
  1611. #endif
  1612. break;
  1613. case PCM_SDMA:
  1614. #if IS_BUILTIN(CONFIG_SND_OMAP_SOC) || \
  1615. (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
  1616. IS_MODULE(CONFIG_SND_OMAP_SOC))
  1617. ret = omap_pcm_platform_register(&pdev->dev);
  1618. #else
  1619. dev_err(&pdev->dev, "Missing SND_SDMA_SOC\n");
  1620. ret = -EINVAL;
  1621. goto err;
  1622. #endif
  1623. break;
  1624. default:
  1625. dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret);
  1626. case -EPROBE_DEFER:
  1627. goto err;
  1628. break;
  1629. }
  1630. if (ret) {
  1631. dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
  1632. goto err;
  1633. }
  1634. return 0;
  1635. err:
  1636. pm_runtime_disable(&pdev->dev);
  1637. return ret;
  1638. }
  1639. static int davinci_mcasp_remove(struct platform_device *pdev)
  1640. {
  1641. pm_runtime_disable(&pdev->dev);
  1642. return 0;
  1643. }
  1644. static struct platform_driver davinci_mcasp_driver = {
  1645. .probe = davinci_mcasp_probe,
  1646. .remove = davinci_mcasp_remove,
  1647. .driver = {
  1648. .name = "davinci-mcasp",
  1649. .of_match_table = mcasp_dt_ids,
  1650. },
  1651. };
  1652. module_platform_driver(davinci_mcasp_driver);
  1653. MODULE_AUTHOR("Steve Chen");
  1654. MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
  1655. MODULE_LICENSE("GPL");