fsl_spdif.c 33 KB

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