fsl_spdif.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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. /*
  340. * The S/PDIF block needs a clock of 64 * fs * txclk_df.
  341. * So request 64 * fs * (txclk_df + 1) to get rounded.
  342. */
  343. ret = clk_set_rate(spdif_priv->txclk[rate], 64 * sample_rate * (txclk_df + 1));
  344. if (ret) {
  345. dev_err(&pdev->dev, "failed to set tx clock rate\n");
  346. return ret;
  347. }
  348. clk_set_bypass:
  349. dev_dbg(&pdev->dev, "expected clock rate = %d\n",
  350. (64 * sample_rate * txclk_df * sysclk_df));
  351. dev_dbg(&pdev->dev, "actual clock rate = %ld\n",
  352. clk_get_rate(spdif_priv->txclk[rate]));
  353. /* set fs field in consumer channel status */
  354. spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs);
  355. /* select clock source and divisor */
  356. stc = STC_TXCLK_ALL_EN | STC_TXCLK_SRC_SET(clk) |
  357. STC_TXCLK_DF(txclk_df) | STC_SYSCLK_DF(sysclk_df);
  358. mask = STC_TXCLK_ALL_EN_MASK | STC_TXCLK_SRC_MASK |
  359. STC_TXCLK_DF_MASK | STC_SYSCLK_DF_MASK;
  360. regmap_update_bits(regmap, REG_SPDIF_STC, mask, stc);
  361. dev_dbg(&pdev->dev, "set sample rate to %dHz for %dHz playback\n",
  362. spdif_priv->txrate[rate], sample_rate);
  363. return 0;
  364. }
  365. static int fsl_spdif_startup(struct snd_pcm_substream *substream,
  366. struct snd_soc_dai *cpu_dai)
  367. {
  368. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  369. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  370. struct platform_device *pdev = spdif_priv->pdev;
  371. struct regmap *regmap = spdif_priv->regmap;
  372. u32 scr, mask, i;
  373. int ret;
  374. /* Reset module and interrupts only for first initialization */
  375. if (!cpu_dai->active) {
  376. ret = clk_prepare_enable(spdif_priv->coreclk);
  377. if (ret) {
  378. dev_err(&pdev->dev, "failed to enable core clock\n");
  379. return ret;
  380. }
  381. ret = spdif_softreset(spdif_priv);
  382. if (ret) {
  383. dev_err(&pdev->dev, "failed to soft reset\n");
  384. goto err;
  385. }
  386. /* Disable all the interrupts */
  387. regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
  388. }
  389. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  390. scr = SCR_TXFIFO_AUTOSYNC | SCR_TXFIFO_CTRL_NORMAL |
  391. SCR_TXSEL_NORMAL | SCR_USRC_SEL_CHIP |
  392. SCR_TXFIFO_FSEL_IF8;
  393. mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
  394. SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
  395. SCR_TXFIFO_FSEL_MASK;
  396. for (i = 0; i < SPDIF_TXRATE_MAX; i++)
  397. clk_prepare_enable(spdif_priv->txclk[i]);
  398. } else {
  399. scr = SCR_RXFIFO_FSEL_IF8 | SCR_RXFIFO_AUTOSYNC;
  400. mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
  401. SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
  402. clk_prepare_enable(spdif_priv->rxclk);
  403. }
  404. regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
  405. /* Power up SPDIF module */
  406. regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0);
  407. return 0;
  408. err:
  409. clk_disable_unprepare(spdif_priv->coreclk);
  410. return ret;
  411. }
  412. static void fsl_spdif_shutdown(struct snd_pcm_substream *substream,
  413. struct snd_soc_dai *cpu_dai)
  414. {
  415. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  416. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  417. struct regmap *regmap = spdif_priv->regmap;
  418. u32 scr, mask, i;
  419. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  420. scr = 0;
  421. mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
  422. SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
  423. SCR_TXFIFO_FSEL_MASK;
  424. for (i = 0; i < SPDIF_TXRATE_MAX; i++)
  425. clk_disable_unprepare(spdif_priv->txclk[i]);
  426. } else {
  427. scr = SCR_RXFIFO_OFF | SCR_RXFIFO_CTL_ZERO;
  428. mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
  429. SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
  430. clk_disable_unprepare(spdif_priv->rxclk);
  431. }
  432. regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
  433. /* Power down SPDIF module only if tx&rx are both inactive */
  434. if (!cpu_dai->active) {
  435. spdif_intr_status_clear(spdif_priv);
  436. regmap_update_bits(regmap, REG_SPDIF_SCR,
  437. SCR_LOW_POWER, SCR_LOW_POWER);
  438. clk_disable_unprepare(spdif_priv->coreclk);
  439. }
  440. }
  441. static int fsl_spdif_hw_params(struct snd_pcm_substream *substream,
  442. struct snd_pcm_hw_params *params,
  443. struct snd_soc_dai *dai)
  444. {
  445. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  446. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  447. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  448. struct platform_device *pdev = spdif_priv->pdev;
  449. u32 sample_rate = params_rate(params);
  450. int ret = 0;
  451. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  452. ret = spdif_set_sample_rate(substream, sample_rate);
  453. if (ret) {
  454. dev_err(&pdev->dev, "%s: set sample rate failed: %d\n",
  455. __func__, sample_rate);
  456. return ret;
  457. }
  458. spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK,
  459. IEC958_AES3_CON_CLOCK_1000PPM);
  460. spdif_write_channel_status(spdif_priv);
  461. } else {
  462. /* Setup rx clock source */
  463. ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1);
  464. }
  465. return ret;
  466. }
  467. static int fsl_spdif_trigger(struct snd_pcm_substream *substream,
  468. int cmd, struct snd_soc_dai *dai)
  469. {
  470. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  471. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  472. struct regmap *regmap = spdif_priv->regmap;
  473. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  474. u32 intr = SIE_INTR_FOR(tx);
  475. u32 dmaen = SCR_DMA_xX_EN(tx);
  476. switch (cmd) {
  477. case SNDRV_PCM_TRIGGER_START:
  478. case SNDRV_PCM_TRIGGER_RESUME:
  479. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  480. regmap_update_bits(regmap, REG_SPDIF_SIE, intr, intr);
  481. regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, dmaen);
  482. break;
  483. case SNDRV_PCM_TRIGGER_STOP:
  484. case SNDRV_PCM_TRIGGER_SUSPEND:
  485. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  486. regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, 0);
  487. regmap_update_bits(regmap, REG_SPDIF_SIE, intr, 0);
  488. break;
  489. default:
  490. return -EINVAL;
  491. }
  492. return 0;
  493. }
  494. static struct snd_soc_dai_ops fsl_spdif_dai_ops = {
  495. .startup = fsl_spdif_startup,
  496. .hw_params = fsl_spdif_hw_params,
  497. .trigger = fsl_spdif_trigger,
  498. .shutdown = fsl_spdif_shutdown,
  499. };
  500. /*
  501. * FSL SPDIF IEC958 controller(mixer) functions
  502. *
  503. * Channel status get/put control
  504. * User bit value get/put control
  505. * Valid bit value get control
  506. * DPLL lock status get control
  507. * User bit sync mode selection control
  508. */
  509. static int fsl_spdif_info(struct snd_kcontrol *kcontrol,
  510. struct snd_ctl_elem_info *uinfo)
  511. {
  512. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  513. uinfo->count = 1;
  514. return 0;
  515. }
  516. static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol,
  517. struct snd_ctl_elem_value *uvalue)
  518. {
  519. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  520. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  521. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  522. uvalue->value.iec958.status[0] = ctrl->ch_status[0];
  523. uvalue->value.iec958.status[1] = ctrl->ch_status[1];
  524. uvalue->value.iec958.status[2] = ctrl->ch_status[2];
  525. uvalue->value.iec958.status[3] = ctrl->ch_status[3];
  526. return 0;
  527. }
  528. static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol,
  529. struct snd_ctl_elem_value *uvalue)
  530. {
  531. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  532. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  533. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  534. ctrl->ch_status[0] = uvalue->value.iec958.status[0];
  535. ctrl->ch_status[1] = uvalue->value.iec958.status[1];
  536. ctrl->ch_status[2] = uvalue->value.iec958.status[2];
  537. ctrl->ch_status[3] = uvalue->value.iec958.status[3];
  538. spdif_write_channel_status(spdif_priv);
  539. return 0;
  540. }
  541. /* Get channel status from SPDIF_RX_CCHAN register */
  542. static int fsl_spdif_capture_get(struct snd_kcontrol *kcontrol,
  543. struct snd_ctl_elem_value *ucontrol)
  544. {
  545. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  546. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  547. struct regmap *regmap = spdif_priv->regmap;
  548. u32 cstatus, val;
  549. regmap_read(regmap, REG_SPDIF_SIS, &val);
  550. if (!(val & INT_CNEW))
  551. return -EAGAIN;
  552. regmap_read(regmap, REG_SPDIF_SRCSH, &cstatus);
  553. ucontrol->value.iec958.status[0] = (cstatus >> 16) & 0xFF;
  554. ucontrol->value.iec958.status[1] = (cstatus >> 8) & 0xFF;
  555. ucontrol->value.iec958.status[2] = cstatus & 0xFF;
  556. regmap_read(regmap, REG_SPDIF_SRCSL, &cstatus);
  557. ucontrol->value.iec958.status[3] = (cstatus >> 16) & 0xFF;
  558. ucontrol->value.iec958.status[4] = (cstatus >> 8) & 0xFF;
  559. ucontrol->value.iec958.status[5] = cstatus & 0xFF;
  560. /* Clear intr */
  561. regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW);
  562. return 0;
  563. }
  564. /*
  565. * Get User bits (subcode) from chip value which readed out
  566. * in UChannel register.
  567. */
  568. static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol,
  569. struct snd_ctl_elem_value *ucontrol)
  570. {
  571. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  572. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  573. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  574. unsigned long flags;
  575. int ret = -EAGAIN;
  576. spin_lock_irqsave(&ctrl->ctl_lock, flags);
  577. if (ctrl->ready_buf) {
  578. int idx = (ctrl->ready_buf - 1) * SPDIF_UBITS_SIZE;
  579. memcpy(&ucontrol->value.iec958.subcode[0],
  580. &ctrl->subcode[idx], SPDIF_UBITS_SIZE);
  581. ret = 0;
  582. }
  583. spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
  584. return ret;
  585. }
  586. /* Q-subcode infomation. The byte size is SPDIF_UBITS_SIZE/8 */
  587. static int fsl_spdif_qinfo(struct snd_kcontrol *kcontrol,
  588. struct snd_ctl_elem_info *uinfo)
  589. {
  590. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  591. uinfo->count = SPDIF_QSUB_SIZE;
  592. return 0;
  593. }
  594. /* Get Q subcode from chip value which readed out in QChannel register */
  595. static int fsl_spdif_qget(struct snd_kcontrol *kcontrol,
  596. struct snd_ctl_elem_value *ucontrol)
  597. {
  598. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  599. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  600. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  601. unsigned long flags;
  602. int ret = -EAGAIN;
  603. spin_lock_irqsave(&ctrl->ctl_lock, flags);
  604. if (ctrl->ready_buf) {
  605. int idx = (ctrl->ready_buf - 1) * SPDIF_QSUB_SIZE;
  606. memcpy(&ucontrol->value.bytes.data[0],
  607. &ctrl->qsub[idx], SPDIF_QSUB_SIZE);
  608. ret = 0;
  609. }
  610. spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
  611. return ret;
  612. }
  613. /* Valid bit infomation */
  614. static int fsl_spdif_vbit_info(struct snd_kcontrol *kcontrol,
  615. struct snd_ctl_elem_info *uinfo)
  616. {
  617. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  618. uinfo->count = 1;
  619. uinfo->value.integer.min = 0;
  620. uinfo->value.integer.max = 1;
  621. return 0;
  622. }
  623. /* Get valid good bit from interrupt status register */
  624. static int fsl_spdif_vbit_get(struct snd_kcontrol *kcontrol,
  625. struct snd_ctl_elem_value *ucontrol)
  626. {
  627. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  628. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  629. struct regmap *regmap = spdif_priv->regmap;
  630. u32 val;
  631. regmap_read(regmap, REG_SPDIF_SIS, &val);
  632. ucontrol->value.integer.value[0] = (val & INT_VAL_NOGOOD) != 0;
  633. regmap_write(regmap, REG_SPDIF_SIC, INT_VAL_NOGOOD);
  634. return 0;
  635. }
  636. /* DPLL lock infomation */
  637. static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol,
  638. struct snd_ctl_elem_info *uinfo)
  639. {
  640. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  641. uinfo->count = 1;
  642. uinfo->value.integer.min = 16000;
  643. uinfo->value.integer.max = 96000;
  644. return 0;
  645. }
  646. static u32 gainsel_multi[GAINSEL_MULTI_MAX] = {
  647. 24, 16, 12, 8, 6, 4, 3,
  648. };
  649. /* Get RX data clock rate given the SPDIF bus_clk */
  650. static int spdif_get_rxclk_rate(struct fsl_spdif_priv *spdif_priv,
  651. enum spdif_gainsel gainsel)
  652. {
  653. struct regmap *regmap = spdif_priv->regmap;
  654. struct platform_device *pdev = spdif_priv->pdev;
  655. u64 tmpval64, busclk_freq = 0;
  656. u32 freqmeas, phaseconf;
  657. u8 clksrc;
  658. regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas);
  659. regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf);
  660. clksrc = (phaseconf >> SRPC_CLKSRC_SEL_OFFSET) & 0xf;
  661. /* Get bus clock from system */
  662. if (srpc_dpll_locked[clksrc] && (phaseconf & SRPC_DPLL_LOCKED))
  663. busclk_freq = clk_get_rate(spdif_priv->sysclk);
  664. /* FreqMeas_CLK = (BUS_CLK * FreqMeas) / 2 ^ 10 / GAINSEL / 128 */
  665. tmpval64 = (u64) busclk_freq * freqmeas;
  666. do_div(tmpval64, gainsel_multi[gainsel] * 1024);
  667. do_div(tmpval64, 128 * 1024);
  668. dev_dbg(&pdev->dev, "FreqMeas: %d\n", freqmeas);
  669. dev_dbg(&pdev->dev, "BusclkFreq: %lld\n", busclk_freq);
  670. dev_dbg(&pdev->dev, "RxRate: %lld\n", tmpval64);
  671. return (int)tmpval64;
  672. }
  673. /*
  674. * Get DPLL lock or not info from stable interrupt status register.
  675. * User application must use this control to get locked,
  676. * then can do next PCM operation
  677. */
  678. static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol,
  679. struct snd_ctl_elem_value *ucontrol)
  680. {
  681. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  682. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  683. int rate = 0;
  684. if (spdif_priv->dpll_locked)
  685. rate = spdif_get_rxclk_rate(spdif_priv, SPDIF_DEFAULT_GAINSEL);
  686. ucontrol->value.integer.value[0] = rate;
  687. return 0;
  688. }
  689. /* User bit sync mode info */
  690. static int fsl_spdif_usync_info(struct snd_kcontrol *kcontrol,
  691. struct snd_ctl_elem_info *uinfo)
  692. {
  693. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  694. uinfo->count = 1;
  695. uinfo->value.integer.min = 0;
  696. uinfo->value.integer.max = 1;
  697. return 0;
  698. }
  699. /*
  700. * User bit sync mode:
  701. * 1 CD User channel subcode
  702. * 0 Non-CD data
  703. */
  704. static int fsl_spdif_usync_get(struct snd_kcontrol *kcontrol,
  705. struct snd_ctl_elem_value *ucontrol)
  706. {
  707. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  708. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  709. struct regmap *regmap = spdif_priv->regmap;
  710. u32 val;
  711. regmap_read(regmap, REG_SPDIF_SRCD, &val);
  712. ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0;
  713. return 0;
  714. }
  715. /*
  716. * User bit sync mode:
  717. * 1 CD User channel subcode
  718. * 0 Non-CD data
  719. */
  720. static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol,
  721. struct snd_ctl_elem_value *ucontrol)
  722. {
  723. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  724. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  725. struct regmap *regmap = spdif_priv->regmap;
  726. u32 val = ucontrol->value.integer.value[0] << SRCD_CD_USER_OFFSET;
  727. regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val);
  728. return 0;
  729. }
  730. /* FSL SPDIF IEC958 controller defines */
  731. static struct snd_kcontrol_new fsl_spdif_ctrls[] = {
  732. /* Status cchanel controller */
  733. {
  734. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  735. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  736. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  737. SNDRV_CTL_ELEM_ACCESS_WRITE |
  738. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  739. .info = fsl_spdif_info,
  740. .get = fsl_spdif_pb_get,
  741. .put = fsl_spdif_pb_put,
  742. },
  743. {
  744. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  745. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  746. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  747. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  748. .info = fsl_spdif_info,
  749. .get = fsl_spdif_capture_get,
  750. },
  751. /* User bits controller */
  752. {
  753. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  754. .name = "IEC958 Subcode Capture Default",
  755. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  756. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  757. .info = fsl_spdif_info,
  758. .get = fsl_spdif_subcode_get,
  759. },
  760. {
  761. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  762. .name = "IEC958 Q-subcode Capture Default",
  763. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  764. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  765. .info = fsl_spdif_qinfo,
  766. .get = fsl_spdif_qget,
  767. },
  768. /* Valid bit error controller */
  769. {
  770. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  771. .name = "IEC958 V-Bit Errors",
  772. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  773. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  774. .info = fsl_spdif_vbit_info,
  775. .get = fsl_spdif_vbit_get,
  776. },
  777. /* DPLL lock info get controller */
  778. {
  779. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  780. .name = "RX Sample Rate",
  781. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  782. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  783. .info = fsl_spdif_rxrate_info,
  784. .get = fsl_spdif_rxrate_get,
  785. },
  786. /* User bit sync mode set/get controller */
  787. {
  788. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  789. .name = "IEC958 USyncMode CDText",
  790. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  791. SNDRV_CTL_ELEM_ACCESS_WRITE |
  792. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  793. .info = fsl_spdif_usync_info,
  794. .get = fsl_spdif_usync_get,
  795. .put = fsl_spdif_usync_put,
  796. },
  797. };
  798. static int fsl_spdif_dai_probe(struct snd_soc_dai *dai)
  799. {
  800. struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai);
  801. snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx,
  802. &spdif_private->dma_params_rx);
  803. snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls));
  804. return 0;
  805. }
  806. static struct snd_soc_dai_driver fsl_spdif_dai = {
  807. .probe = &fsl_spdif_dai_probe,
  808. .playback = {
  809. .stream_name = "CPU-Playback",
  810. .channels_min = 2,
  811. .channels_max = 2,
  812. .rates = FSL_SPDIF_RATES_PLAYBACK,
  813. .formats = FSL_SPDIF_FORMATS_PLAYBACK,
  814. },
  815. .capture = {
  816. .stream_name = "CPU-Capture",
  817. .channels_min = 2,
  818. .channels_max = 2,
  819. .rates = FSL_SPDIF_RATES_CAPTURE,
  820. .formats = FSL_SPDIF_FORMATS_CAPTURE,
  821. },
  822. .ops = &fsl_spdif_dai_ops,
  823. };
  824. static const struct snd_soc_component_driver fsl_spdif_component = {
  825. .name = "fsl-spdif",
  826. };
  827. /* FSL SPDIF REGMAP */
  828. static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg)
  829. {
  830. switch (reg) {
  831. case REG_SPDIF_SCR:
  832. case REG_SPDIF_SRCD:
  833. case REG_SPDIF_SRPC:
  834. case REG_SPDIF_SIE:
  835. case REG_SPDIF_SIS:
  836. case REG_SPDIF_SRL:
  837. case REG_SPDIF_SRR:
  838. case REG_SPDIF_SRCSH:
  839. case REG_SPDIF_SRCSL:
  840. case REG_SPDIF_SRU:
  841. case REG_SPDIF_SRQ:
  842. case REG_SPDIF_STCSCH:
  843. case REG_SPDIF_STCSCL:
  844. case REG_SPDIF_SRFM:
  845. case REG_SPDIF_STC:
  846. return true;
  847. default:
  848. return false;
  849. }
  850. }
  851. static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg)
  852. {
  853. switch (reg) {
  854. case REG_SPDIF_SCR:
  855. case REG_SPDIF_SRCD:
  856. case REG_SPDIF_SRPC:
  857. case REG_SPDIF_SIE:
  858. case REG_SPDIF_SIC:
  859. case REG_SPDIF_STL:
  860. case REG_SPDIF_STR:
  861. case REG_SPDIF_STCSCH:
  862. case REG_SPDIF_STCSCL:
  863. case REG_SPDIF_STC:
  864. return true;
  865. default:
  866. return false;
  867. }
  868. }
  869. static const struct regmap_config fsl_spdif_regmap_config = {
  870. .reg_bits = 32,
  871. .reg_stride = 4,
  872. .val_bits = 32,
  873. .max_register = REG_SPDIF_STC,
  874. .readable_reg = fsl_spdif_readable_reg,
  875. .writeable_reg = fsl_spdif_writeable_reg,
  876. };
  877. static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
  878. struct clk *clk, u64 savesub,
  879. enum spdif_txrate index, bool round)
  880. {
  881. const u32 rate[] = { 32000, 44100, 48000, 96000, 192000 };
  882. bool is_sysclk = clk_is_match(clk, spdif_priv->sysclk);
  883. u64 rate_ideal, rate_actual, sub;
  884. u32 sysclk_dfmin, sysclk_dfmax;
  885. u32 txclk_df, sysclk_df, arate;
  886. /* The sysclk has an extra divisor [2, 512] */
  887. sysclk_dfmin = is_sysclk ? 2 : 1;
  888. sysclk_dfmax = is_sysclk ? 512 : 1;
  889. for (sysclk_df = sysclk_dfmin; sysclk_df <= sysclk_dfmax; sysclk_df++) {
  890. for (txclk_df = 1; txclk_df <= 128; txclk_df++) {
  891. rate_ideal = rate[index] * (txclk_df + 1) * 64;
  892. if (round)
  893. rate_actual = clk_round_rate(clk, rate_ideal);
  894. else
  895. rate_actual = clk_get_rate(clk);
  896. arate = rate_actual / 64;
  897. arate /= txclk_df * sysclk_df;
  898. if (arate == rate[index]) {
  899. /* We are lucky */
  900. savesub = 0;
  901. spdif_priv->txclk_df[index] = txclk_df;
  902. spdif_priv->sysclk_df[index] = sysclk_df;
  903. spdif_priv->txrate[index] = arate;
  904. goto out;
  905. } else if (arate / rate[index] == 1) {
  906. /* A little bigger than expect */
  907. sub = (u64)(arate - rate[index]) * 100000;
  908. do_div(sub, rate[index]);
  909. if (sub >= savesub)
  910. continue;
  911. savesub = sub;
  912. spdif_priv->txclk_df[index] = txclk_df;
  913. spdif_priv->sysclk_df[index] = sysclk_df;
  914. spdif_priv->txrate[index] = arate;
  915. } else if (rate[index] / arate == 1) {
  916. /* A little smaller than expect */
  917. sub = (u64)(rate[index] - arate) * 100000;
  918. do_div(sub, rate[index]);
  919. if (sub >= savesub)
  920. continue;
  921. savesub = sub;
  922. spdif_priv->txclk_df[index] = txclk_df;
  923. spdif_priv->sysclk_df[index] = sysclk_df;
  924. spdif_priv->txrate[index] = arate;
  925. }
  926. }
  927. }
  928. out:
  929. return savesub;
  930. }
  931. static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv,
  932. enum spdif_txrate index)
  933. {
  934. const u32 rate[] = { 32000, 44100, 48000, 96000, 192000 };
  935. struct platform_device *pdev = spdif_priv->pdev;
  936. struct device *dev = &pdev->dev;
  937. u64 savesub = 100000, ret;
  938. struct clk *clk;
  939. char tmp[16];
  940. int i;
  941. for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
  942. sprintf(tmp, "rxtx%d", i);
  943. clk = devm_clk_get(&pdev->dev, tmp);
  944. if (IS_ERR(clk)) {
  945. dev_err(dev, "no rxtx%d clock in devicetree\n", i);
  946. return PTR_ERR(clk);
  947. }
  948. if (!clk_get_rate(clk))
  949. continue;
  950. ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index,
  951. i == STC_TXCLK_SPDIF_ROOT);
  952. if (savesub == ret)
  953. continue;
  954. savesub = ret;
  955. spdif_priv->txclk[index] = clk;
  956. spdif_priv->txclk_src[index] = i;
  957. /* To quick catch a divisor, we allow a 0.1% deviation */
  958. if (savesub < 100)
  959. break;
  960. }
  961. dev_dbg(&pdev->dev, "use rxtx%d as tx clock source for %dHz sample rate\n",
  962. spdif_priv->txclk_src[index], rate[index]);
  963. dev_dbg(&pdev->dev, "use txclk df %d for %dHz sample rate\n",
  964. spdif_priv->txclk_df[index], rate[index]);
  965. if (clk_is_match(spdif_priv->txclk[index], spdif_priv->sysclk))
  966. dev_dbg(&pdev->dev, "use sysclk df %d for %dHz sample rate\n",
  967. spdif_priv->sysclk_df[index], rate[index]);
  968. dev_dbg(&pdev->dev, "the best rate for %dHz sample rate is %dHz\n",
  969. rate[index], spdif_priv->txrate[index]);
  970. return 0;
  971. }
  972. static int fsl_spdif_probe(struct platform_device *pdev)
  973. {
  974. struct device_node *np = pdev->dev.of_node;
  975. struct fsl_spdif_priv *spdif_priv;
  976. struct spdif_mixer_control *ctrl;
  977. struct resource *res;
  978. void __iomem *regs;
  979. int irq, ret, i;
  980. if (!np)
  981. return -ENODEV;
  982. spdif_priv = devm_kzalloc(&pdev->dev, sizeof(*spdif_priv), GFP_KERNEL);
  983. if (!spdif_priv)
  984. return -ENOMEM;
  985. spdif_priv->pdev = pdev;
  986. /* Initialize this copy of the CPU DAI driver structure */
  987. memcpy(&spdif_priv->cpu_dai_drv, &fsl_spdif_dai, sizeof(fsl_spdif_dai));
  988. spdif_priv->cpu_dai_drv.name = dev_name(&pdev->dev);
  989. /* Get the addresses and IRQ */
  990. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  991. regs = devm_ioremap_resource(&pdev->dev, res);
  992. if (IS_ERR(regs))
  993. return PTR_ERR(regs);
  994. spdif_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
  995. "core", regs, &fsl_spdif_regmap_config);
  996. if (IS_ERR(spdif_priv->regmap)) {
  997. dev_err(&pdev->dev, "regmap init failed\n");
  998. return PTR_ERR(spdif_priv->regmap);
  999. }
  1000. irq = platform_get_irq(pdev, 0);
  1001. if (irq < 0) {
  1002. dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
  1003. return irq;
  1004. }
  1005. ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0,
  1006. dev_name(&pdev->dev), spdif_priv);
  1007. if (ret) {
  1008. dev_err(&pdev->dev, "could not claim irq %u\n", irq);
  1009. return ret;
  1010. }
  1011. /* Get system clock for rx clock rate calculation */
  1012. spdif_priv->sysclk = devm_clk_get(&pdev->dev, "rxtx5");
  1013. if (IS_ERR(spdif_priv->sysclk)) {
  1014. dev_err(&pdev->dev, "no sys clock (rxtx5) in devicetree\n");
  1015. return PTR_ERR(spdif_priv->sysclk);
  1016. }
  1017. /* Get core clock for data register access via DMA */
  1018. spdif_priv->coreclk = devm_clk_get(&pdev->dev, "core");
  1019. if (IS_ERR(spdif_priv->coreclk)) {
  1020. dev_err(&pdev->dev, "no core clock in devicetree\n");
  1021. return PTR_ERR(spdif_priv->coreclk);
  1022. }
  1023. /* Select clock source for rx/tx clock */
  1024. spdif_priv->rxclk = devm_clk_get(&pdev->dev, "rxtx1");
  1025. if (IS_ERR(spdif_priv->rxclk)) {
  1026. dev_err(&pdev->dev, "no rxtx1 clock in devicetree\n");
  1027. return PTR_ERR(spdif_priv->rxclk);
  1028. }
  1029. spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC;
  1030. for (i = 0; i < SPDIF_TXRATE_MAX; i++) {
  1031. ret = fsl_spdif_probe_txclk(spdif_priv, i);
  1032. if (ret)
  1033. return ret;
  1034. }
  1035. /* Initial spinlock for control data */
  1036. ctrl = &spdif_priv->fsl_spdif_control;
  1037. spin_lock_init(&ctrl->ctl_lock);
  1038. /* Init tx channel status default value */
  1039. ctrl->ch_status[0] = IEC958_AES0_CON_NOT_COPYRIGHT |
  1040. IEC958_AES0_CON_EMPHASIS_5015;
  1041. ctrl->ch_status[1] = IEC958_AES1_CON_DIGDIGCONV_ID;
  1042. ctrl->ch_status[2] = 0x00;
  1043. ctrl->ch_status[3] = IEC958_AES3_CON_FS_44100 |
  1044. IEC958_AES3_CON_CLOCK_1000PPM;
  1045. spdif_priv->dpll_locked = false;
  1046. spdif_priv->dma_params_tx.maxburst = FSL_SPDIF_TXFIFO_WML;
  1047. spdif_priv->dma_params_rx.maxburst = FSL_SPDIF_RXFIFO_WML;
  1048. spdif_priv->dma_params_tx.addr = res->start + REG_SPDIF_STL;
  1049. spdif_priv->dma_params_rx.addr = res->start + REG_SPDIF_SRL;
  1050. /* Register with ASoC */
  1051. dev_set_drvdata(&pdev->dev, spdif_priv);
  1052. ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component,
  1053. &spdif_priv->cpu_dai_drv, 1);
  1054. if (ret) {
  1055. dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
  1056. return ret;
  1057. }
  1058. ret = imx_pcm_dma_init(pdev);
  1059. if (ret)
  1060. dev_err(&pdev->dev, "imx_pcm_dma_init failed: %d\n", ret);
  1061. return ret;
  1062. }
  1063. static const struct of_device_id fsl_spdif_dt_ids[] = {
  1064. { .compatible = "fsl,imx35-spdif", },
  1065. { .compatible = "fsl,vf610-spdif", },
  1066. {}
  1067. };
  1068. MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids);
  1069. static struct platform_driver fsl_spdif_driver = {
  1070. .driver = {
  1071. .name = "fsl-spdif-dai",
  1072. .of_match_table = fsl_spdif_dt_ids,
  1073. },
  1074. .probe = fsl_spdif_probe,
  1075. };
  1076. module_platform_driver(fsl_spdif_driver);
  1077. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1078. MODULE_DESCRIPTION("Freescale S/PDIF CPU DAI Driver");
  1079. MODULE_LICENSE("GPL v2");
  1080. MODULE_ALIAS("platform:fsl-spdif-dai");