fsl_spdif.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /*
  2. * Freescale S/PDIF ALSA SoC Digital Audio Interface (DAI) driver
  3. *
  4. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  5. *
  6. * Based on stmp3xxx_spdif_dai.c
  7. * Vladimir Barinov <vbarinov@embeddedalley.com>
  8. * Copyright 2008 SigmaTel, Inc
  9. * Copyright 2008 Embedded Alley Solutions, Inc
  10. *
  11. * This file is licensed under the terms of the GNU General Public License
  12. * version 2. This program is licensed "as is" without any warranty of any
  13. * kind, whether express or implied.
  14. */
  15. #include <linux/bitrev.h>
  16. #include <linux/clk.h>
  17. #include <linux/module.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/regmap.h>
  22. #include <sound/asoundef.h>
  23. #include <sound/dmaengine_pcm.h>
  24. #include <sound/soc.h>
  25. #include "fsl_spdif.h"
  26. #include "imx-pcm.h"
  27. #define FSL_SPDIF_TXFIFO_WML 0x8
  28. #define FSL_SPDIF_RXFIFO_WML 0x8
  29. #define INTR_FOR_PLAYBACK (INT_TXFIFO_RESYNC)
  30. #define INTR_FOR_CAPTURE (INT_SYM_ERR | INT_BIT_ERR | INT_URX_FUL |\
  31. INT_URX_OV | INT_QRX_FUL | INT_QRX_OV |\
  32. INT_UQ_SYNC | INT_UQ_ERR | INT_RXFIFO_RESYNC |\
  33. INT_LOSS_LOCK | INT_DPLL_LOCKED)
  34. #define SIE_INTR_FOR(tx) (tx ? INTR_FOR_PLAYBACK : INTR_FOR_CAPTURE)
  35. /* Index list for the values that has if (DPLL Locked) condition */
  36. static u8 srpc_dpll_locked[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0xa, 0xb };
  37. #define SRPC_NODPLL_START1 0x5
  38. #define SRPC_NODPLL_START2 0xc
  39. #define DEFAULT_RXCLK_SRC 1
  40. /*
  41. * SPDIF control structure
  42. * Defines channel status, subcode and Q sub
  43. */
  44. struct spdif_mixer_control {
  45. /* spinlock to access control data */
  46. spinlock_t ctl_lock;
  47. /* IEC958 channel tx status bit */
  48. unsigned char ch_status[4];
  49. /* User bits */
  50. unsigned char subcode[2 * SPDIF_UBITS_SIZE];
  51. /* Q subcode part of user bits */
  52. unsigned char qsub[2 * SPDIF_QSUB_SIZE];
  53. /* Buffer offset for U/Q */
  54. u32 upos;
  55. u32 qpos;
  56. /* Ready buffer index of the two buffers */
  57. u32 ready_buf;
  58. };
  59. /**
  60. * fsl_spdif_priv: Freescale SPDIF private data
  61. *
  62. * @fsl_spdif_control: SPDIF control data
  63. * @cpu_dai_drv: cpu dai driver
  64. * @pdev: platform device pointer
  65. * @regmap: regmap handler
  66. * @dpll_locked: dpll lock flag
  67. * @txrate: the best rates for playback
  68. * @txclk_df: STC_TXCLK_DF dividers value for playback
  69. * @sysclk_df: STC_SYSCLK_DF dividers value for playback
  70. * @txclk_src: STC_TXCLK_SRC values for playback
  71. * @rxclk_src: SRPC_CLKSRC_SEL values for capture
  72. * @txclk: tx clock sources for playback
  73. * @rxclk: rx clock sources for capture
  74. * @coreclk: core clock for register access via DMA
  75. * @sysclk: system clock for rx clock rate measurement
  76. * @spbaclk: SPBA clock (optional, depending on SoC design)
  77. * @dma_params_tx: DMA parameters for transmit channel
  78. * @dma_params_rx: DMA parameters for receive channel
  79. */
  80. struct fsl_spdif_priv {
  81. struct spdif_mixer_control fsl_spdif_control;
  82. struct snd_soc_dai_driver cpu_dai_drv;
  83. struct platform_device *pdev;
  84. struct regmap *regmap;
  85. bool dpll_locked;
  86. u32 txrate[SPDIF_TXRATE_MAX];
  87. u8 txclk_df[SPDIF_TXRATE_MAX];
  88. u8 sysclk_df[SPDIF_TXRATE_MAX];
  89. u8 txclk_src[SPDIF_TXRATE_MAX];
  90. u8 rxclk_src;
  91. struct clk *txclk[SPDIF_TXRATE_MAX];
  92. struct clk *rxclk;
  93. struct clk *coreclk;
  94. struct clk *sysclk;
  95. struct clk *spbaclk;
  96. struct snd_dmaengine_dai_dma_data dma_params_tx;
  97. struct snd_dmaengine_dai_dma_data dma_params_rx;
  98. /* regcache for SRPC */
  99. u32 regcache_srpc;
  100. };
  101. /* DPLL locked and lock loss interrupt handler */
  102. static void spdif_irq_dpll_lock(struct fsl_spdif_priv *spdif_priv)
  103. {
  104. struct regmap *regmap = spdif_priv->regmap;
  105. struct platform_device *pdev = spdif_priv->pdev;
  106. u32 locked;
  107. regmap_read(regmap, REG_SPDIF_SRPC, &locked);
  108. locked &= SRPC_DPLL_LOCKED;
  109. dev_dbg(&pdev->dev, "isr: Rx dpll %s \n",
  110. locked ? "locked" : "loss lock");
  111. spdif_priv->dpll_locked = locked ? true : false;
  112. }
  113. /* Receiver found illegal symbol interrupt handler */
  114. static void spdif_irq_sym_error(struct fsl_spdif_priv *spdif_priv)
  115. {
  116. struct regmap *regmap = spdif_priv->regmap;
  117. struct platform_device *pdev = spdif_priv->pdev;
  118. dev_dbg(&pdev->dev, "isr: receiver found illegal symbol\n");
  119. /* Clear illegal symbol if DPLL unlocked since no audio stream */
  120. if (!spdif_priv->dpll_locked)
  121. regmap_update_bits(regmap, REG_SPDIF_SIE, INT_SYM_ERR, 0);
  122. }
  123. /* U/Q Channel receive register full */
  124. static void spdif_irq_uqrx_full(struct fsl_spdif_priv *spdif_priv, char name)
  125. {
  126. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  127. struct regmap *regmap = spdif_priv->regmap;
  128. struct platform_device *pdev = spdif_priv->pdev;
  129. u32 *pos, size, val, reg;
  130. switch (name) {
  131. case 'U':
  132. pos = &ctrl->upos;
  133. size = SPDIF_UBITS_SIZE;
  134. reg = REG_SPDIF_SRU;
  135. break;
  136. case 'Q':
  137. pos = &ctrl->qpos;
  138. size = SPDIF_QSUB_SIZE;
  139. reg = REG_SPDIF_SRQ;
  140. break;
  141. default:
  142. dev_err(&pdev->dev, "unsupported channel name\n");
  143. return;
  144. }
  145. dev_dbg(&pdev->dev, "isr: %c Channel receive register full\n", name);
  146. if (*pos >= size * 2) {
  147. *pos = 0;
  148. } else if (unlikely((*pos % size) + 3 > size)) {
  149. dev_err(&pdev->dev, "User bit receive buffer overflow\n");
  150. return;
  151. }
  152. regmap_read(regmap, reg, &val);
  153. ctrl->subcode[*pos++] = val >> 16;
  154. ctrl->subcode[*pos++] = val >> 8;
  155. ctrl->subcode[*pos++] = val;
  156. }
  157. /* U/Q Channel sync found */
  158. static void spdif_irq_uq_sync(struct fsl_spdif_priv *spdif_priv)
  159. {
  160. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  161. struct platform_device *pdev = spdif_priv->pdev;
  162. dev_dbg(&pdev->dev, "isr: U/Q Channel sync found\n");
  163. /* U/Q buffer reset */
  164. if (ctrl->qpos == 0)
  165. return;
  166. /* Set ready to this buffer */
  167. ctrl->ready_buf = (ctrl->qpos - 1) / SPDIF_QSUB_SIZE + 1;
  168. }
  169. /* U/Q Channel framing error */
  170. static void spdif_irq_uq_err(struct fsl_spdif_priv *spdif_priv)
  171. {
  172. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  173. struct regmap *regmap = spdif_priv->regmap;
  174. struct platform_device *pdev = spdif_priv->pdev;
  175. u32 val;
  176. dev_dbg(&pdev->dev, "isr: U/Q Channel framing error\n");
  177. /* Read U/Q data to clear the irq and do buffer reset */
  178. regmap_read(regmap, REG_SPDIF_SRU, &val);
  179. regmap_read(regmap, REG_SPDIF_SRQ, &val);
  180. /* Drop this U/Q buffer */
  181. ctrl->ready_buf = 0;
  182. ctrl->upos = 0;
  183. ctrl->qpos = 0;
  184. }
  185. /* Get spdif interrupt status and clear the interrupt */
  186. static u32 spdif_intr_status_clear(struct fsl_spdif_priv *spdif_priv)
  187. {
  188. struct regmap *regmap = spdif_priv->regmap;
  189. u32 val, val2;
  190. regmap_read(regmap, REG_SPDIF_SIS, &val);
  191. regmap_read(regmap, REG_SPDIF_SIE, &val2);
  192. regmap_write(regmap, REG_SPDIF_SIC, val & val2);
  193. return val;
  194. }
  195. static irqreturn_t spdif_isr(int irq, void *devid)
  196. {
  197. struct fsl_spdif_priv *spdif_priv = (struct fsl_spdif_priv *)devid;
  198. struct platform_device *pdev = spdif_priv->pdev;
  199. u32 sis;
  200. sis = spdif_intr_status_clear(spdif_priv);
  201. if (sis & INT_DPLL_LOCKED)
  202. spdif_irq_dpll_lock(spdif_priv);
  203. if (sis & INT_TXFIFO_UNOV)
  204. dev_dbg(&pdev->dev, "isr: Tx FIFO under/overrun\n");
  205. if (sis & INT_TXFIFO_RESYNC)
  206. dev_dbg(&pdev->dev, "isr: Tx FIFO resync\n");
  207. if (sis & INT_CNEW)
  208. dev_dbg(&pdev->dev, "isr: cstatus new\n");
  209. if (sis & INT_VAL_NOGOOD)
  210. dev_dbg(&pdev->dev, "isr: validity flag no good\n");
  211. if (sis & INT_SYM_ERR)
  212. spdif_irq_sym_error(spdif_priv);
  213. if (sis & INT_BIT_ERR)
  214. dev_dbg(&pdev->dev, "isr: receiver found parity bit error\n");
  215. if (sis & INT_URX_FUL)
  216. spdif_irq_uqrx_full(spdif_priv, 'U');
  217. if (sis & INT_URX_OV)
  218. dev_dbg(&pdev->dev, "isr: U Channel receive register overrun\n");
  219. if (sis & INT_QRX_FUL)
  220. spdif_irq_uqrx_full(spdif_priv, 'Q');
  221. if (sis & INT_QRX_OV)
  222. dev_dbg(&pdev->dev, "isr: Q Channel receive register overrun\n");
  223. if (sis & INT_UQ_SYNC)
  224. spdif_irq_uq_sync(spdif_priv);
  225. if (sis & INT_UQ_ERR)
  226. spdif_irq_uq_err(spdif_priv);
  227. if (sis & INT_RXFIFO_UNOV)
  228. dev_dbg(&pdev->dev, "isr: Rx FIFO under/overrun\n");
  229. if (sis & INT_RXFIFO_RESYNC)
  230. dev_dbg(&pdev->dev, "isr: Rx FIFO resync\n");
  231. if (sis & INT_LOSS_LOCK)
  232. spdif_irq_dpll_lock(spdif_priv);
  233. /* FIXME: Write Tx FIFO to clear TxEm */
  234. if (sis & INT_TX_EM)
  235. dev_dbg(&pdev->dev, "isr: Tx FIFO empty\n");
  236. /* FIXME: Read Rx FIFO to clear RxFIFOFul */
  237. if (sis & INT_RXFIFO_FUL)
  238. dev_dbg(&pdev->dev, "isr: Rx FIFO full\n");
  239. return IRQ_HANDLED;
  240. }
  241. static int spdif_softreset(struct fsl_spdif_priv *spdif_priv)
  242. {
  243. struct regmap *regmap = spdif_priv->regmap;
  244. u32 val, cycle = 1000;
  245. regcache_cache_bypass(regmap, true);
  246. regmap_write(regmap, REG_SPDIF_SCR, SCR_SOFT_RESET);
  247. /*
  248. * RESET bit would be cleared after finishing its reset procedure,
  249. * which typically lasts 8 cycles. 1000 cycles will keep it safe.
  250. */
  251. do {
  252. regmap_read(regmap, REG_SPDIF_SCR, &val);
  253. } while ((val & SCR_SOFT_RESET) && cycle--);
  254. regcache_cache_bypass(regmap, false);
  255. regcache_mark_dirty(regmap);
  256. regcache_sync(regmap);
  257. if (cycle)
  258. return 0;
  259. else
  260. return -EBUSY;
  261. }
  262. static void spdif_set_cstatus(struct spdif_mixer_control *ctrl,
  263. u8 mask, u8 cstatus)
  264. {
  265. ctrl->ch_status[3] &= ~mask;
  266. ctrl->ch_status[3] |= cstatus & mask;
  267. }
  268. static void spdif_write_channel_status(struct fsl_spdif_priv *spdif_priv)
  269. {
  270. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  271. struct regmap *regmap = spdif_priv->regmap;
  272. struct platform_device *pdev = spdif_priv->pdev;
  273. u32 ch_status;
  274. ch_status = (bitrev8(ctrl->ch_status[0]) << 16) |
  275. (bitrev8(ctrl->ch_status[1]) << 8) |
  276. bitrev8(ctrl->ch_status[2]);
  277. regmap_write(regmap, REG_SPDIF_STCSCH, ch_status);
  278. dev_dbg(&pdev->dev, "STCSCH: 0x%06x\n", ch_status);
  279. ch_status = bitrev8(ctrl->ch_status[3]) << 16;
  280. regmap_write(regmap, REG_SPDIF_STCSCL, ch_status);
  281. dev_dbg(&pdev->dev, "STCSCL: 0x%06x\n", ch_status);
  282. }
  283. /* Set SPDIF PhaseConfig register for rx clock */
  284. static int spdif_set_rx_clksrc(struct fsl_spdif_priv *spdif_priv,
  285. enum spdif_gainsel gainsel, int dpll_locked)
  286. {
  287. struct regmap *regmap = spdif_priv->regmap;
  288. u8 clksrc = spdif_priv->rxclk_src;
  289. if (clksrc >= SRPC_CLKSRC_MAX || gainsel >= GAINSEL_MULTI_MAX)
  290. return -EINVAL;
  291. regmap_update_bits(regmap, REG_SPDIF_SRPC,
  292. SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK,
  293. SRPC_CLKSRC_SEL_SET(clksrc) | SRPC_GAINSEL_SET(gainsel));
  294. return 0;
  295. }
  296. static int spdif_set_sample_rate(struct snd_pcm_substream *substream,
  297. int sample_rate)
  298. {
  299. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  300. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  301. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  302. struct regmap *regmap = spdif_priv->regmap;
  303. struct platform_device *pdev = spdif_priv->pdev;
  304. unsigned long csfs = 0;
  305. u32 stc, mask, rate;
  306. u8 clk, txclk_df, sysclk_df;
  307. int ret;
  308. switch (sample_rate) {
  309. case 32000:
  310. rate = SPDIF_TXRATE_32000;
  311. csfs = IEC958_AES3_CON_FS_32000;
  312. break;
  313. case 44100:
  314. rate = SPDIF_TXRATE_44100;
  315. csfs = IEC958_AES3_CON_FS_44100;
  316. break;
  317. case 48000:
  318. rate = SPDIF_TXRATE_48000;
  319. csfs = IEC958_AES3_CON_FS_48000;
  320. break;
  321. case 96000:
  322. rate = SPDIF_TXRATE_96000;
  323. csfs = IEC958_AES3_CON_FS_96000;
  324. break;
  325. case 192000:
  326. rate = SPDIF_TXRATE_192000;
  327. csfs = IEC958_AES3_CON_FS_192000;
  328. break;
  329. default:
  330. dev_err(&pdev->dev, "unsupported sample rate %d\n", sample_rate);
  331. return -EINVAL;
  332. }
  333. clk = spdif_priv->txclk_src[rate];
  334. if (clk >= STC_TXCLK_SRC_MAX) {
  335. dev_err(&pdev->dev, "tx clock source is out of range\n");
  336. return -EINVAL;
  337. }
  338. txclk_df = spdif_priv->txclk_df[rate];
  339. if (txclk_df == 0) {
  340. dev_err(&pdev->dev, "the txclk_df can't be zero\n");
  341. return -EINVAL;
  342. }
  343. sysclk_df = spdif_priv->sysclk_df[rate];
  344. /* Don't mess up the clocks from other modules */
  345. if (clk != STC_TXCLK_SPDIF_ROOT)
  346. goto clk_set_bypass;
  347. /* The S/PDIF block needs a clock of 64 * fs * txclk_df */
  348. ret = clk_set_rate(spdif_priv->txclk[rate],
  349. 64 * sample_rate * txclk_df);
  350. if (ret) {
  351. dev_err(&pdev->dev, "failed to set tx clock rate\n");
  352. return ret;
  353. }
  354. clk_set_bypass:
  355. dev_dbg(&pdev->dev, "expected clock rate = %d\n",
  356. (64 * sample_rate * txclk_df * sysclk_df));
  357. dev_dbg(&pdev->dev, "actual clock rate = %ld\n",
  358. clk_get_rate(spdif_priv->txclk[rate]));
  359. /* set fs field in consumer channel status */
  360. spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs);
  361. /* select clock source and divisor */
  362. stc = STC_TXCLK_ALL_EN | STC_TXCLK_SRC_SET(clk) |
  363. STC_TXCLK_DF(txclk_df) | STC_SYSCLK_DF(sysclk_df);
  364. mask = STC_TXCLK_ALL_EN_MASK | STC_TXCLK_SRC_MASK |
  365. STC_TXCLK_DF_MASK | STC_SYSCLK_DF_MASK;
  366. regmap_update_bits(regmap, REG_SPDIF_STC, mask, stc);
  367. dev_dbg(&pdev->dev, "set sample rate to %dHz for %dHz playback\n",
  368. spdif_priv->txrate[rate], sample_rate);
  369. return 0;
  370. }
  371. static int fsl_spdif_startup(struct snd_pcm_substream *substream,
  372. struct snd_soc_dai *cpu_dai)
  373. {
  374. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  375. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  376. struct platform_device *pdev = spdif_priv->pdev;
  377. struct regmap *regmap = spdif_priv->regmap;
  378. u32 scr, mask;
  379. int i;
  380. int ret;
  381. /* Reset module and interrupts only for first initialization */
  382. if (!cpu_dai->active) {
  383. ret = clk_prepare_enable(spdif_priv->coreclk);
  384. if (ret) {
  385. dev_err(&pdev->dev, "failed to enable core clock\n");
  386. return ret;
  387. }
  388. if (!IS_ERR(spdif_priv->spbaclk)) {
  389. ret = clk_prepare_enable(spdif_priv->spbaclk);
  390. if (ret) {
  391. dev_err(&pdev->dev, "failed to enable spba clock\n");
  392. goto err_spbaclk;
  393. }
  394. }
  395. ret = spdif_softreset(spdif_priv);
  396. if (ret) {
  397. dev_err(&pdev->dev, "failed to soft reset\n");
  398. goto err;
  399. }
  400. /* Disable all the interrupts */
  401. regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
  402. }
  403. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  404. scr = SCR_TXFIFO_AUTOSYNC | SCR_TXFIFO_CTRL_NORMAL |
  405. SCR_TXSEL_NORMAL | SCR_USRC_SEL_CHIP |
  406. SCR_TXFIFO_FSEL_IF8;
  407. mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
  408. SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
  409. SCR_TXFIFO_FSEL_MASK;
  410. for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
  411. ret = clk_prepare_enable(spdif_priv->txclk[i]);
  412. if (ret)
  413. goto disable_txclk;
  414. }
  415. } else {
  416. scr = SCR_RXFIFO_FSEL_IF8 | SCR_RXFIFO_AUTOSYNC;
  417. mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
  418. SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
  419. ret = clk_prepare_enable(spdif_priv->rxclk);
  420. if (ret)
  421. goto err;
  422. }
  423. regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
  424. /* Power up SPDIF module */
  425. regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0);
  426. return 0;
  427. disable_txclk:
  428. for (i--; i >= 0; i--)
  429. clk_disable_unprepare(spdif_priv->txclk[i]);
  430. err:
  431. if (!IS_ERR(spdif_priv->spbaclk))
  432. clk_disable_unprepare(spdif_priv->spbaclk);
  433. err_spbaclk:
  434. clk_disable_unprepare(spdif_priv->coreclk);
  435. return ret;
  436. }
  437. static void fsl_spdif_shutdown(struct snd_pcm_substream *substream,
  438. struct snd_soc_dai *cpu_dai)
  439. {
  440. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  441. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  442. struct regmap *regmap = spdif_priv->regmap;
  443. u32 scr, mask, i;
  444. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  445. scr = 0;
  446. mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
  447. SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
  448. SCR_TXFIFO_FSEL_MASK;
  449. for (i = 0; i < SPDIF_TXRATE_MAX; i++)
  450. clk_disable_unprepare(spdif_priv->txclk[i]);
  451. } else {
  452. scr = SCR_RXFIFO_OFF | SCR_RXFIFO_CTL_ZERO;
  453. mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
  454. SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
  455. clk_disable_unprepare(spdif_priv->rxclk);
  456. }
  457. regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
  458. /* Power down SPDIF module only if tx&rx are both inactive */
  459. if (!cpu_dai->active) {
  460. spdif_intr_status_clear(spdif_priv);
  461. regmap_update_bits(regmap, REG_SPDIF_SCR,
  462. SCR_LOW_POWER, SCR_LOW_POWER);
  463. if (!IS_ERR(spdif_priv->spbaclk))
  464. clk_disable_unprepare(spdif_priv->spbaclk);
  465. clk_disable_unprepare(spdif_priv->coreclk);
  466. }
  467. }
  468. static int fsl_spdif_hw_params(struct snd_pcm_substream *substream,
  469. struct snd_pcm_hw_params *params,
  470. struct snd_soc_dai *dai)
  471. {
  472. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  473. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  474. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  475. struct platform_device *pdev = spdif_priv->pdev;
  476. u32 sample_rate = params_rate(params);
  477. int ret = 0;
  478. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  479. ret = spdif_set_sample_rate(substream, sample_rate);
  480. if (ret) {
  481. dev_err(&pdev->dev, "%s: set sample rate failed: %d\n",
  482. __func__, sample_rate);
  483. return ret;
  484. }
  485. spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK,
  486. IEC958_AES3_CON_CLOCK_1000PPM);
  487. spdif_write_channel_status(spdif_priv);
  488. } else {
  489. /* Setup rx clock source */
  490. ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1);
  491. }
  492. return ret;
  493. }
  494. static int fsl_spdif_trigger(struct snd_pcm_substream *substream,
  495. int cmd, struct snd_soc_dai *dai)
  496. {
  497. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  498. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  499. struct regmap *regmap = spdif_priv->regmap;
  500. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  501. u32 intr = SIE_INTR_FOR(tx);
  502. u32 dmaen = SCR_DMA_xX_EN(tx);
  503. switch (cmd) {
  504. case SNDRV_PCM_TRIGGER_START:
  505. case SNDRV_PCM_TRIGGER_RESUME:
  506. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  507. regmap_update_bits(regmap, REG_SPDIF_SIE, intr, intr);
  508. regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, dmaen);
  509. break;
  510. case SNDRV_PCM_TRIGGER_STOP:
  511. case SNDRV_PCM_TRIGGER_SUSPEND:
  512. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  513. regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, 0);
  514. regmap_update_bits(regmap, REG_SPDIF_SIE, intr, 0);
  515. break;
  516. default:
  517. return -EINVAL;
  518. }
  519. return 0;
  520. }
  521. static struct snd_soc_dai_ops fsl_spdif_dai_ops = {
  522. .startup = fsl_spdif_startup,
  523. .hw_params = fsl_spdif_hw_params,
  524. .trigger = fsl_spdif_trigger,
  525. .shutdown = fsl_spdif_shutdown,
  526. };
  527. /*
  528. * FSL SPDIF IEC958 controller(mixer) functions
  529. *
  530. * Channel status get/put control
  531. * User bit value get/put control
  532. * Valid bit value get control
  533. * DPLL lock status get control
  534. * User bit sync mode selection control
  535. */
  536. static int fsl_spdif_info(struct snd_kcontrol *kcontrol,
  537. struct snd_ctl_elem_info *uinfo)
  538. {
  539. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  540. uinfo->count = 1;
  541. return 0;
  542. }
  543. static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol,
  544. struct snd_ctl_elem_value *uvalue)
  545. {
  546. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  547. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  548. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  549. uvalue->value.iec958.status[0] = ctrl->ch_status[0];
  550. uvalue->value.iec958.status[1] = ctrl->ch_status[1];
  551. uvalue->value.iec958.status[2] = ctrl->ch_status[2];
  552. uvalue->value.iec958.status[3] = ctrl->ch_status[3];
  553. return 0;
  554. }
  555. static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol,
  556. struct snd_ctl_elem_value *uvalue)
  557. {
  558. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  559. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  560. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  561. ctrl->ch_status[0] = uvalue->value.iec958.status[0];
  562. ctrl->ch_status[1] = uvalue->value.iec958.status[1];
  563. ctrl->ch_status[2] = uvalue->value.iec958.status[2];
  564. ctrl->ch_status[3] = uvalue->value.iec958.status[3];
  565. spdif_write_channel_status(spdif_priv);
  566. return 0;
  567. }
  568. /* Get channel status from SPDIF_RX_CCHAN register */
  569. static int fsl_spdif_capture_get(struct snd_kcontrol *kcontrol,
  570. struct snd_ctl_elem_value *ucontrol)
  571. {
  572. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  573. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  574. struct regmap *regmap = spdif_priv->regmap;
  575. u32 cstatus, val;
  576. regmap_read(regmap, REG_SPDIF_SIS, &val);
  577. if (!(val & INT_CNEW))
  578. return -EAGAIN;
  579. regmap_read(regmap, REG_SPDIF_SRCSH, &cstatus);
  580. ucontrol->value.iec958.status[0] = (cstatus >> 16) & 0xFF;
  581. ucontrol->value.iec958.status[1] = (cstatus >> 8) & 0xFF;
  582. ucontrol->value.iec958.status[2] = cstatus & 0xFF;
  583. regmap_read(regmap, REG_SPDIF_SRCSL, &cstatus);
  584. ucontrol->value.iec958.status[3] = (cstatus >> 16) & 0xFF;
  585. ucontrol->value.iec958.status[4] = (cstatus >> 8) & 0xFF;
  586. ucontrol->value.iec958.status[5] = cstatus & 0xFF;
  587. /* Clear intr */
  588. regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW);
  589. return 0;
  590. }
  591. /*
  592. * Get User bits (subcode) from chip value which readed out
  593. * in UChannel register.
  594. */
  595. static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol,
  596. struct snd_ctl_elem_value *ucontrol)
  597. {
  598. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  599. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  600. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  601. unsigned long flags;
  602. int ret = -EAGAIN;
  603. spin_lock_irqsave(&ctrl->ctl_lock, flags);
  604. if (ctrl->ready_buf) {
  605. int idx = (ctrl->ready_buf - 1) * SPDIF_UBITS_SIZE;
  606. memcpy(&ucontrol->value.iec958.subcode[0],
  607. &ctrl->subcode[idx], SPDIF_UBITS_SIZE);
  608. ret = 0;
  609. }
  610. spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
  611. return ret;
  612. }
  613. /* Q-subcode information. The byte size is SPDIF_UBITS_SIZE/8 */
  614. static int fsl_spdif_qinfo(struct snd_kcontrol *kcontrol,
  615. struct snd_ctl_elem_info *uinfo)
  616. {
  617. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  618. uinfo->count = SPDIF_QSUB_SIZE;
  619. return 0;
  620. }
  621. /* Get Q subcode from chip value which readed out in QChannel register */
  622. static int fsl_spdif_qget(struct snd_kcontrol *kcontrol,
  623. struct snd_ctl_elem_value *ucontrol)
  624. {
  625. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  626. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  627. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  628. unsigned long flags;
  629. int ret = -EAGAIN;
  630. spin_lock_irqsave(&ctrl->ctl_lock, flags);
  631. if (ctrl->ready_buf) {
  632. int idx = (ctrl->ready_buf - 1) * SPDIF_QSUB_SIZE;
  633. memcpy(&ucontrol->value.bytes.data[0],
  634. &ctrl->qsub[idx], SPDIF_QSUB_SIZE);
  635. ret = 0;
  636. }
  637. spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
  638. return ret;
  639. }
  640. /* Valid bit information */
  641. static int fsl_spdif_vbit_info(struct snd_kcontrol *kcontrol,
  642. struct snd_ctl_elem_info *uinfo)
  643. {
  644. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  645. uinfo->count = 1;
  646. uinfo->value.integer.min = 0;
  647. uinfo->value.integer.max = 1;
  648. return 0;
  649. }
  650. /* Get valid good bit from interrupt status register */
  651. static int fsl_spdif_vbit_get(struct snd_kcontrol *kcontrol,
  652. struct snd_ctl_elem_value *ucontrol)
  653. {
  654. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  655. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  656. struct regmap *regmap = spdif_priv->regmap;
  657. u32 val;
  658. regmap_read(regmap, REG_SPDIF_SIS, &val);
  659. ucontrol->value.integer.value[0] = (val & INT_VAL_NOGOOD) != 0;
  660. regmap_write(regmap, REG_SPDIF_SIC, INT_VAL_NOGOOD);
  661. return 0;
  662. }
  663. /* DPLL lock information */
  664. static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol,
  665. struct snd_ctl_elem_info *uinfo)
  666. {
  667. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  668. uinfo->count = 1;
  669. uinfo->value.integer.min = 16000;
  670. uinfo->value.integer.max = 96000;
  671. return 0;
  672. }
  673. static u32 gainsel_multi[GAINSEL_MULTI_MAX] = {
  674. 24, 16, 12, 8, 6, 4, 3,
  675. };
  676. /* Get RX data clock rate given the SPDIF bus_clk */
  677. static int spdif_get_rxclk_rate(struct fsl_spdif_priv *spdif_priv,
  678. enum spdif_gainsel gainsel)
  679. {
  680. struct regmap *regmap = spdif_priv->regmap;
  681. struct platform_device *pdev = spdif_priv->pdev;
  682. u64 tmpval64, busclk_freq = 0;
  683. u32 freqmeas, phaseconf;
  684. u8 clksrc;
  685. regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas);
  686. regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf);
  687. clksrc = (phaseconf >> SRPC_CLKSRC_SEL_OFFSET) & 0xf;
  688. /* Get bus clock from system */
  689. if (srpc_dpll_locked[clksrc] && (phaseconf & SRPC_DPLL_LOCKED))
  690. busclk_freq = clk_get_rate(spdif_priv->sysclk);
  691. /* FreqMeas_CLK = (BUS_CLK * FreqMeas) / 2 ^ 10 / GAINSEL / 128 */
  692. tmpval64 = (u64) busclk_freq * freqmeas;
  693. do_div(tmpval64, gainsel_multi[gainsel] * 1024);
  694. do_div(tmpval64, 128 * 1024);
  695. dev_dbg(&pdev->dev, "FreqMeas: %d\n", freqmeas);
  696. dev_dbg(&pdev->dev, "BusclkFreq: %lld\n", busclk_freq);
  697. dev_dbg(&pdev->dev, "RxRate: %lld\n", tmpval64);
  698. return (int)tmpval64;
  699. }
  700. /*
  701. * Get DPLL lock or not info from stable interrupt status register.
  702. * User application must use this control to get locked,
  703. * then can do next PCM operation
  704. */
  705. static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol,
  706. struct snd_ctl_elem_value *ucontrol)
  707. {
  708. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  709. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  710. int rate = 0;
  711. if (spdif_priv->dpll_locked)
  712. rate = spdif_get_rxclk_rate(spdif_priv, SPDIF_DEFAULT_GAINSEL);
  713. ucontrol->value.integer.value[0] = rate;
  714. return 0;
  715. }
  716. /* User bit sync mode info */
  717. static int fsl_spdif_usync_info(struct snd_kcontrol *kcontrol,
  718. struct snd_ctl_elem_info *uinfo)
  719. {
  720. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  721. uinfo->count = 1;
  722. uinfo->value.integer.min = 0;
  723. uinfo->value.integer.max = 1;
  724. return 0;
  725. }
  726. /*
  727. * User bit sync mode:
  728. * 1 CD User channel subcode
  729. * 0 Non-CD data
  730. */
  731. static int fsl_spdif_usync_get(struct snd_kcontrol *kcontrol,
  732. struct snd_ctl_elem_value *ucontrol)
  733. {
  734. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  735. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  736. struct regmap *regmap = spdif_priv->regmap;
  737. u32 val;
  738. regmap_read(regmap, REG_SPDIF_SRCD, &val);
  739. ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0;
  740. return 0;
  741. }
  742. /*
  743. * User bit sync mode:
  744. * 1 CD User channel subcode
  745. * 0 Non-CD data
  746. */
  747. static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol,
  748. struct snd_ctl_elem_value *ucontrol)
  749. {
  750. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  751. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  752. struct regmap *regmap = spdif_priv->regmap;
  753. u32 val = ucontrol->value.integer.value[0] << SRCD_CD_USER_OFFSET;
  754. regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val);
  755. return 0;
  756. }
  757. /* FSL SPDIF IEC958 controller defines */
  758. static struct snd_kcontrol_new fsl_spdif_ctrls[] = {
  759. /* Status cchanel controller */
  760. {
  761. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  762. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  763. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  764. SNDRV_CTL_ELEM_ACCESS_WRITE |
  765. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  766. .info = fsl_spdif_info,
  767. .get = fsl_spdif_pb_get,
  768. .put = fsl_spdif_pb_put,
  769. },
  770. {
  771. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  772. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  773. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  774. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  775. .info = fsl_spdif_info,
  776. .get = fsl_spdif_capture_get,
  777. },
  778. /* User bits controller */
  779. {
  780. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  781. .name = "IEC958 Subcode Capture Default",
  782. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  783. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  784. .info = fsl_spdif_info,
  785. .get = fsl_spdif_subcode_get,
  786. },
  787. {
  788. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  789. .name = "IEC958 Q-subcode Capture Default",
  790. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  791. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  792. .info = fsl_spdif_qinfo,
  793. .get = fsl_spdif_qget,
  794. },
  795. /* Valid bit error controller */
  796. {
  797. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  798. .name = "IEC958 V-Bit Errors",
  799. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  800. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  801. .info = fsl_spdif_vbit_info,
  802. .get = fsl_spdif_vbit_get,
  803. },
  804. /* DPLL lock info get controller */
  805. {
  806. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  807. .name = "RX Sample Rate",
  808. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  809. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  810. .info = fsl_spdif_rxrate_info,
  811. .get = fsl_spdif_rxrate_get,
  812. },
  813. /* User bit sync mode set/get controller */
  814. {
  815. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  816. .name = "IEC958 USyncMode CDText",
  817. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  818. SNDRV_CTL_ELEM_ACCESS_WRITE |
  819. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  820. .info = fsl_spdif_usync_info,
  821. .get = fsl_spdif_usync_get,
  822. .put = fsl_spdif_usync_put,
  823. },
  824. };
  825. static int fsl_spdif_dai_probe(struct snd_soc_dai *dai)
  826. {
  827. struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai);
  828. snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx,
  829. &spdif_private->dma_params_rx);
  830. snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls));
  831. return 0;
  832. }
  833. static struct snd_soc_dai_driver fsl_spdif_dai = {
  834. .probe = &fsl_spdif_dai_probe,
  835. .playback = {
  836. .stream_name = "CPU-Playback",
  837. .channels_min = 2,
  838. .channels_max = 2,
  839. .rates = FSL_SPDIF_RATES_PLAYBACK,
  840. .formats = FSL_SPDIF_FORMATS_PLAYBACK,
  841. },
  842. .capture = {
  843. .stream_name = "CPU-Capture",
  844. .channels_min = 2,
  845. .channels_max = 2,
  846. .rates = FSL_SPDIF_RATES_CAPTURE,
  847. .formats = FSL_SPDIF_FORMATS_CAPTURE,
  848. },
  849. .ops = &fsl_spdif_dai_ops,
  850. };
  851. static const struct snd_soc_component_driver fsl_spdif_component = {
  852. .name = "fsl-spdif",
  853. };
  854. /* FSL SPDIF REGMAP */
  855. static const struct reg_default fsl_spdif_reg_defaults[] = {
  856. {REG_SPDIF_SCR, 0x00000400},
  857. {REG_SPDIF_SRCD, 0x00000000},
  858. {REG_SPDIF_SIE, 0x00000000},
  859. {REG_SPDIF_STL, 0x00000000},
  860. {REG_SPDIF_STR, 0x00000000},
  861. {REG_SPDIF_STCSCH, 0x00000000},
  862. {REG_SPDIF_STCSCL, 0x00000000},
  863. {REG_SPDIF_STC, 0x00020f00},
  864. };
  865. static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg)
  866. {
  867. switch (reg) {
  868. case REG_SPDIF_SCR:
  869. case REG_SPDIF_SRCD:
  870. case REG_SPDIF_SRPC:
  871. case REG_SPDIF_SIE:
  872. case REG_SPDIF_SIS:
  873. case REG_SPDIF_SRL:
  874. case REG_SPDIF_SRR:
  875. case REG_SPDIF_SRCSH:
  876. case REG_SPDIF_SRCSL:
  877. case REG_SPDIF_SRU:
  878. case REG_SPDIF_SRQ:
  879. case REG_SPDIF_STCSCH:
  880. case REG_SPDIF_STCSCL:
  881. case REG_SPDIF_SRFM:
  882. case REG_SPDIF_STC:
  883. return true;
  884. default:
  885. return false;
  886. }
  887. }
  888. static bool fsl_spdif_volatile_reg(struct device *dev, unsigned int reg)
  889. {
  890. switch (reg) {
  891. case REG_SPDIF_SRPC:
  892. case REG_SPDIF_SIS:
  893. case REG_SPDIF_SRL:
  894. case REG_SPDIF_SRR:
  895. case REG_SPDIF_SRCSH:
  896. case REG_SPDIF_SRCSL:
  897. case REG_SPDIF_SRU:
  898. case REG_SPDIF_SRQ:
  899. case REG_SPDIF_SRFM:
  900. return true;
  901. default:
  902. return false;
  903. }
  904. }
  905. static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg)
  906. {
  907. switch (reg) {
  908. case REG_SPDIF_SCR:
  909. case REG_SPDIF_SRCD:
  910. case REG_SPDIF_SRPC:
  911. case REG_SPDIF_SIE:
  912. case REG_SPDIF_SIC:
  913. case REG_SPDIF_STL:
  914. case REG_SPDIF_STR:
  915. case REG_SPDIF_STCSCH:
  916. case REG_SPDIF_STCSCL:
  917. case REG_SPDIF_STC:
  918. return true;
  919. default:
  920. return false;
  921. }
  922. }
  923. static const struct regmap_config fsl_spdif_regmap_config = {
  924. .reg_bits = 32,
  925. .reg_stride = 4,
  926. .val_bits = 32,
  927. .max_register = REG_SPDIF_STC,
  928. .reg_defaults = fsl_spdif_reg_defaults,
  929. .num_reg_defaults = ARRAY_SIZE(fsl_spdif_reg_defaults),
  930. .readable_reg = fsl_spdif_readable_reg,
  931. .volatile_reg = fsl_spdif_volatile_reg,
  932. .writeable_reg = fsl_spdif_writeable_reg,
  933. .cache_type = REGCACHE_FLAT,
  934. };
  935. static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
  936. struct clk *clk, u64 savesub,
  937. enum spdif_txrate index, bool round)
  938. {
  939. const u32 rate[] = { 32000, 44100, 48000, 96000, 192000 };
  940. bool is_sysclk = clk_is_match(clk, spdif_priv->sysclk);
  941. u64 rate_ideal, rate_actual, sub;
  942. u32 sysclk_dfmin, sysclk_dfmax;
  943. u32 txclk_df, sysclk_df, arate;
  944. /* The sysclk has an extra divisor [2, 512] */
  945. sysclk_dfmin = is_sysclk ? 2 : 1;
  946. sysclk_dfmax = is_sysclk ? 512 : 1;
  947. for (sysclk_df = sysclk_dfmin; sysclk_df <= sysclk_dfmax; sysclk_df++) {
  948. for (txclk_df = 1; txclk_df <= 128; txclk_df++) {
  949. rate_ideal = rate[index] * txclk_df * 64;
  950. if (round)
  951. rate_actual = clk_round_rate(clk, rate_ideal);
  952. else
  953. rate_actual = clk_get_rate(clk);
  954. arate = rate_actual / 64;
  955. arate /= txclk_df * sysclk_df;
  956. if (arate == rate[index]) {
  957. /* We are lucky */
  958. savesub = 0;
  959. spdif_priv->txclk_df[index] = txclk_df;
  960. spdif_priv->sysclk_df[index] = sysclk_df;
  961. spdif_priv->txrate[index] = arate;
  962. goto out;
  963. } else if (arate / rate[index] == 1) {
  964. /* A little bigger than expect */
  965. sub = (u64)(arate - rate[index]) * 100000;
  966. do_div(sub, rate[index]);
  967. if (sub >= savesub)
  968. continue;
  969. savesub = sub;
  970. spdif_priv->txclk_df[index] = txclk_df;
  971. spdif_priv->sysclk_df[index] = sysclk_df;
  972. spdif_priv->txrate[index] = arate;
  973. } else if (rate[index] / arate == 1) {
  974. /* A little smaller than expect */
  975. sub = (u64)(rate[index] - arate) * 100000;
  976. do_div(sub, rate[index]);
  977. if (sub >= savesub)
  978. continue;
  979. savesub = sub;
  980. spdif_priv->txclk_df[index] = txclk_df;
  981. spdif_priv->sysclk_df[index] = sysclk_df;
  982. spdif_priv->txrate[index] = arate;
  983. }
  984. }
  985. }
  986. out:
  987. return savesub;
  988. }
  989. static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv,
  990. enum spdif_txrate index)
  991. {
  992. const u32 rate[] = { 32000, 44100, 48000, 96000, 192000 };
  993. struct platform_device *pdev = spdif_priv->pdev;
  994. struct device *dev = &pdev->dev;
  995. u64 savesub = 100000, ret;
  996. struct clk *clk;
  997. char tmp[16];
  998. int i;
  999. for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
  1000. sprintf(tmp, "rxtx%d", i);
  1001. clk = devm_clk_get(&pdev->dev, tmp);
  1002. if (IS_ERR(clk)) {
  1003. dev_err(dev, "no rxtx%d clock in devicetree\n", i);
  1004. return PTR_ERR(clk);
  1005. }
  1006. if (!clk_get_rate(clk))
  1007. continue;
  1008. ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index,
  1009. i == STC_TXCLK_SPDIF_ROOT);
  1010. if (savesub == ret)
  1011. continue;
  1012. savesub = ret;
  1013. spdif_priv->txclk[index] = clk;
  1014. spdif_priv->txclk_src[index] = i;
  1015. /* To quick catch a divisor, we allow a 0.1% deviation */
  1016. if (savesub < 100)
  1017. break;
  1018. }
  1019. dev_dbg(&pdev->dev, "use rxtx%d as tx clock source for %dHz sample rate\n",
  1020. spdif_priv->txclk_src[index], rate[index]);
  1021. dev_dbg(&pdev->dev, "use txclk df %d for %dHz sample rate\n",
  1022. spdif_priv->txclk_df[index], rate[index]);
  1023. if (clk_is_match(spdif_priv->txclk[index], spdif_priv->sysclk))
  1024. dev_dbg(&pdev->dev, "use sysclk df %d for %dHz sample rate\n",
  1025. spdif_priv->sysclk_df[index], rate[index]);
  1026. dev_dbg(&pdev->dev, "the best rate for %dHz sample rate is %dHz\n",
  1027. rate[index], spdif_priv->txrate[index]);
  1028. return 0;
  1029. }
  1030. static int fsl_spdif_probe(struct platform_device *pdev)
  1031. {
  1032. struct device_node *np = pdev->dev.of_node;
  1033. struct fsl_spdif_priv *spdif_priv;
  1034. struct spdif_mixer_control *ctrl;
  1035. struct resource *res;
  1036. void __iomem *regs;
  1037. int irq, ret, i;
  1038. if (!np)
  1039. return -ENODEV;
  1040. spdif_priv = devm_kzalloc(&pdev->dev, sizeof(*spdif_priv), GFP_KERNEL);
  1041. if (!spdif_priv)
  1042. return -ENOMEM;
  1043. spdif_priv->pdev = pdev;
  1044. /* Initialize this copy of the CPU DAI driver structure */
  1045. memcpy(&spdif_priv->cpu_dai_drv, &fsl_spdif_dai, sizeof(fsl_spdif_dai));
  1046. spdif_priv->cpu_dai_drv.name = dev_name(&pdev->dev);
  1047. /* Get the addresses and IRQ */
  1048. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1049. regs = devm_ioremap_resource(&pdev->dev, res);
  1050. if (IS_ERR(regs))
  1051. return PTR_ERR(regs);
  1052. spdif_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
  1053. "core", regs, &fsl_spdif_regmap_config);
  1054. if (IS_ERR(spdif_priv->regmap)) {
  1055. dev_err(&pdev->dev, "regmap init failed\n");
  1056. return PTR_ERR(spdif_priv->regmap);
  1057. }
  1058. irq = platform_get_irq(pdev, 0);
  1059. if (irq < 0) {
  1060. dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
  1061. return irq;
  1062. }
  1063. ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0,
  1064. dev_name(&pdev->dev), spdif_priv);
  1065. if (ret) {
  1066. dev_err(&pdev->dev, "could not claim irq %u\n", irq);
  1067. return ret;
  1068. }
  1069. /* Get system clock for rx clock rate calculation */
  1070. spdif_priv->sysclk = devm_clk_get(&pdev->dev, "rxtx5");
  1071. if (IS_ERR(spdif_priv->sysclk)) {
  1072. dev_err(&pdev->dev, "no sys clock (rxtx5) in devicetree\n");
  1073. return PTR_ERR(spdif_priv->sysclk);
  1074. }
  1075. /* Get core clock for data register access via DMA */
  1076. spdif_priv->coreclk = devm_clk_get(&pdev->dev, "core");
  1077. if (IS_ERR(spdif_priv->coreclk)) {
  1078. dev_err(&pdev->dev, "no core clock in devicetree\n");
  1079. return PTR_ERR(spdif_priv->coreclk);
  1080. }
  1081. spdif_priv->spbaclk = devm_clk_get(&pdev->dev, "spba");
  1082. if (IS_ERR(spdif_priv->spbaclk))
  1083. dev_warn(&pdev->dev, "no spba clock in devicetree\n");
  1084. /* Select clock source for rx/tx clock */
  1085. spdif_priv->rxclk = devm_clk_get(&pdev->dev, "rxtx1");
  1086. if (IS_ERR(spdif_priv->rxclk)) {
  1087. dev_err(&pdev->dev, "no rxtx1 clock in devicetree\n");
  1088. return PTR_ERR(spdif_priv->rxclk);
  1089. }
  1090. spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC;
  1091. for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
  1092. ret = fsl_spdif_probe_txclk(spdif_priv, i);
  1093. if (ret)
  1094. return ret;
  1095. }
  1096. /* Initial spinlock for control data */
  1097. ctrl = &spdif_priv->fsl_spdif_control;
  1098. spin_lock_init(&ctrl->ctl_lock);
  1099. /* Init tx channel status default value */
  1100. ctrl->ch_status[0] = IEC958_AES0_CON_NOT_COPYRIGHT |
  1101. IEC958_AES0_CON_EMPHASIS_5015;
  1102. ctrl->ch_status[1] = IEC958_AES1_CON_DIGDIGCONV_ID;
  1103. ctrl->ch_status[2] = 0x00;
  1104. ctrl->ch_status[3] = IEC958_AES3_CON_FS_44100 |
  1105. IEC958_AES3_CON_CLOCK_1000PPM;
  1106. spdif_priv->dpll_locked = false;
  1107. spdif_priv->dma_params_tx.maxburst = FSL_SPDIF_TXFIFO_WML;
  1108. spdif_priv->dma_params_rx.maxburst = FSL_SPDIF_RXFIFO_WML;
  1109. spdif_priv->dma_params_tx.addr = res->start + REG_SPDIF_STL;
  1110. spdif_priv->dma_params_rx.addr = res->start + REG_SPDIF_SRL;
  1111. /* Register with ASoC */
  1112. dev_set_drvdata(&pdev->dev, spdif_priv);
  1113. ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component,
  1114. &spdif_priv->cpu_dai_drv, 1);
  1115. if (ret) {
  1116. dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
  1117. return ret;
  1118. }
  1119. ret = imx_pcm_dma_init(pdev, IMX_SPDIF_DMABUF_SIZE);
  1120. if (ret)
  1121. dev_err(&pdev->dev, "imx_pcm_dma_init failed: %d\n", ret);
  1122. return ret;
  1123. }
  1124. #ifdef CONFIG_PM_SLEEP
  1125. static int fsl_spdif_suspend(struct device *dev)
  1126. {
  1127. struct fsl_spdif_priv *spdif_priv = dev_get_drvdata(dev);
  1128. regmap_read(spdif_priv->regmap, REG_SPDIF_SRPC,
  1129. &spdif_priv->regcache_srpc);
  1130. regcache_cache_only(spdif_priv->regmap, true);
  1131. regcache_mark_dirty(spdif_priv->regmap);
  1132. return 0;
  1133. }
  1134. static int fsl_spdif_resume(struct device *dev)
  1135. {
  1136. struct fsl_spdif_priv *spdif_priv = dev_get_drvdata(dev);
  1137. regcache_cache_only(spdif_priv->regmap, false);
  1138. regmap_update_bits(spdif_priv->regmap, REG_SPDIF_SRPC,
  1139. SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK,
  1140. spdif_priv->regcache_srpc);
  1141. return regcache_sync(spdif_priv->regmap);
  1142. }
  1143. #endif /* CONFIG_PM_SLEEP */
  1144. static const struct dev_pm_ops fsl_spdif_pm = {
  1145. SET_SYSTEM_SLEEP_PM_OPS(fsl_spdif_suspend, fsl_spdif_resume)
  1146. };
  1147. static const struct of_device_id fsl_spdif_dt_ids[] = {
  1148. { .compatible = "fsl,imx35-spdif", },
  1149. { .compatible = "fsl,vf610-spdif", },
  1150. {}
  1151. };
  1152. MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids);
  1153. static struct platform_driver fsl_spdif_driver = {
  1154. .driver = {
  1155. .name = "fsl-spdif-dai",
  1156. .of_match_table = fsl_spdif_dt_ids,
  1157. .pm = &fsl_spdif_pm,
  1158. },
  1159. .probe = fsl_spdif_probe,
  1160. };
  1161. module_platform_driver(fsl_spdif_driver);
  1162. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1163. MODULE_DESCRIPTION("Freescale S/PDIF CPU DAI Driver");
  1164. MODULE_LICENSE("GPL v2");
  1165. MODULE_ALIAS("platform:fsl-spdif-dai");