fsl_spdif.c 35 KB

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