fsl_spdif.c 37 KB

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