fsl_spdif.c 33 KB

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