fsl_spdif.c 36 KB

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