fsl_spdif.c 36 KB

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