fsl_spdif.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Freescale S/PDIF ALSA SoC Digital Audio Interface (DAI) driver
  4. //
  5. // Copyright (C) 2013 Freescale Semiconductor, Inc.
  6. //
  7. // Based on stmp3xxx_spdif_dai.c
  8. // Vladimir Barinov <vbarinov@embeddedalley.com>
  9. // Copyright 2008 SigmaTel, Inc
  10. // Copyright 2008 Embedded Alley Solutions, Inc
  11. #include <linux/bitrev.h>
  12. #include <linux/clk.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/regmap.h>
  16. #include <linux/pm_runtime.h>
  17. #include <sound/asoundef.h>
  18. #include <sound/dmaengine_pcm.h>
  19. #include <sound/soc.h>
  20. #include "fsl_spdif.h"
  21. #include "fsl_utils.h"
  22. #include "imx-pcm.h"
  23. #define FSL_SPDIF_TXFIFO_WML 0x8
  24. #define FSL_SPDIF_RXFIFO_WML 0x8
  25. #define INTR_FOR_PLAYBACK (INT_TXFIFO_RESYNC)
  26. #define INTR_FOR_CAPTURE (INT_SYM_ERR | INT_BIT_ERR | INT_URX_FUL |\
  27. INT_URX_OV | INT_QRX_FUL | INT_QRX_OV |\
  28. INT_UQ_SYNC | INT_UQ_ERR | INT_RXFIFO_RESYNC |\
  29. INT_LOSS_LOCK | INT_DPLL_LOCKED)
  30. #define SIE_INTR_FOR(tx) (tx ? INTR_FOR_PLAYBACK : INTR_FOR_CAPTURE)
  31. /* Index list for the values that has if (DPLL Locked) condition */
  32. static u8 srpc_dpll_locked[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0xa, 0xb };
  33. #define SRPC_NODPLL_START1 0x5
  34. #define SRPC_NODPLL_START2 0xc
  35. #define DEFAULT_RXCLK_SRC 1
  36. #define RX_SAMPLE_RATE_KCONTROL "RX Sample Rate"
  37. /**
  38. * struct fsl_spdif_soc_data: soc specific data
  39. *
  40. * @imx: for imx platform
  41. * @shared_root_clock: flag of sharing a clock source with others;
  42. * so the driver shouldn't set root clock rate
  43. * @raw_capture_mode: if raw capture mode support
  44. * @cchannel_192b: if there are registers for 192bits C channel data
  45. * @interrupts: interrupt number
  46. * @tx_burst: tx maxburst size
  47. * @rx_burst: rx maxburst size
  48. * @tx_formats: tx supported data format
  49. */
  50. struct fsl_spdif_soc_data {
  51. bool imx;
  52. bool shared_root_clock;
  53. bool raw_capture_mode;
  54. bool cchannel_192b;
  55. u32 interrupts;
  56. u32 tx_burst;
  57. u32 rx_burst;
  58. u64 tx_formats;
  59. };
  60. /*
  61. * SPDIF control structure
  62. * Defines channel status, subcode and Q sub
  63. */
  64. struct spdif_mixer_control {
  65. /* spinlock to access control data */
  66. spinlock_t ctl_lock;
  67. /* IEC958 channel tx status bit */
  68. unsigned char ch_status[4];
  69. /* User bits */
  70. unsigned char subcode[2 * SPDIF_UBITS_SIZE];
  71. /* Q subcode part of user bits */
  72. unsigned char qsub[2 * SPDIF_QSUB_SIZE];
  73. /* Buffer offset for U/Q */
  74. u32 upos;
  75. u32 qpos;
  76. /* Ready buffer index of the two buffers */
  77. u32 ready_buf;
  78. };
  79. /**
  80. * struct fsl_spdif_priv - Freescale SPDIF private data
  81. * @soc: SPDIF soc data
  82. * @fsl_spdif_control: SPDIF control data
  83. * @cpu_dai_drv: cpu dai driver
  84. * @snd_card: sound card pointer
  85. * @rxrate_kcontrol: kcontrol for RX Sample Rate
  86. * @pdev: platform device pointer
  87. * @regmap: regmap handler
  88. * @dpll_locked: dpll lock flag
  89. * @txrate: the best rates for playback
  90. * @txclk_df: STC_TXCLK_DF dividers value for playback
  91. * @sysclk_df: STC_SYSCLK_DF dividers value for playback
  92. * @txclk_src: STC_TXCLK_SRC values for playback
  93. * @rxclk_src: SRPC_CLKSRC_SEL values for capture
  94. * @txclk: tx clock sources for playback
  95. * @rxclk: rx clock sources for capture
  96. * @coreclk: core clock for register access via DMA
  97. * @sysclk: system clock for rx clock rate measurement
  98. * @spbaclk: SPBA clock (optional, depending on SoC design)
  99. * @dma_params_tx: DMA parameters for transmit channel
  100. * @dma_params_rx: DMA parameters for receive channel
  101. * @regcache_srpc: regcache for SRPC
  102. * @bypass: status of bypass input to output
  103. * @pll8k_clk: PLL clock for the rate of multiply of 8kHz
  104. * @pll11k_clk: PLL clock for the rate of multiply of 11kHz
  105. */
  106. struct fsl_spdif_priv {
  107. const struct fsl_spdif_soc_data *soc;
  108. struct spdif_mixer_control fsl_spdif_control;
  109. struct snd_soc_dai_driver cpu_dai_drv;
  110. struct snd_card *snd_card;
  111. struct snd_kcontrol *rxrate_kcontrol;
  112. struct platform_device *pdev;
  113. struct regmap *regmap;
  114. bool dpll_locked;
  115. u32 txrate[SPDIF_TXRATE_MAX];
  116. u8 txclk_df[SPDIF_TXRATE_MAX];
  117. u16 sysclk_df[SPDIF_TXRATE_MAX];
  118. u8 txclk_src[SPDIF_TXRATE_MAX];
  119. u8 rxclk_src;
  120. struct clk *txclk[STC_TXCLK_SRC_MAX];
  121. struct clk *rxclk;
  122. struct clk *coreclk;
  123. struct clk *sysclk;
  124. struct clk *spbaclk;
  125. struct snd_dmaengine_dai_dma_data dma_params_tx;
  126. struct snd_dmaengine_dai_dma_data dma_params_rx;
  127. /* regcache for SRPC */
  128. u32 regcache_srpc;
  129. bool bypass;
  130. struct clk *pll8k_clk;
  131. struct clk *pll11k_clk;
  132. };
  133. static const struct fsl_spdif_soc_data fsl_spdif_vf610 = {
  134. .imx = false,
  135. .shared_root_clock = false,
  136. .raw_capture_mode = false,
  137. .interrupts = 1,
  138. .tx_burst = FSL_SPDIF_TXFIFO_WML,
  139. .rx_burst = FSL_SPDIF_RXFIFO_WML,
  140. .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
  141. };
  142. static const struct fsl_spdif_soc_data fsl_spdif_imx35 = {
  143. .imx = true,
  144. .shared_root_clock = false,
  145. .raw_capture_mode = false,
  146. .interrupts = 1,
  147. .tx_burst = FSL_SPDIF_TXFIFO_WML,
  148. .rx_burst = FSL_SPDIF_RXFIFO_WML,
  149. .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
  150. };
  151. static const struct fsl_spdif_soc_data fsl_spdif_imx6sx = {
  152. .imx = true,
  153. .shared_root_clock = true,
  154. .raw_capture_mode = false,
  155. .interrupts = 1,
  156. .tx_burst = FSL_SPDIF_TXFIFO_WML,
  157. .rx_burst = FSL_SPDIF_RXFIFO_WML,
  158. .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
  159. };
  160. static const struct fsl_spdif_soc_data fsl_spdif_imx8qm = {
  161. .imx = true,
  162. .shared_root_clock = true,
  163. .raw_capture_mode = false,
  164. .interrupts = 2,
  165. .tx_burst = 2, /* Applied for EDMA */
  166. .rx_burst = 2, /* Applied for EDMA */
  167. .tx_formats = SNDRV_PCM_FMTBIT_S24_LE, /* Applied for EDMA */
  168. };
  169. static const struct fsl_spdif_soc_data fsl_spdif_imx8mm = {
  170. .imx = true,
  171. .shared_root_clock = false,
  172. .raw_capture_mode = true,
  173. .interrupts = 1,
  174. .tx_burst = FSL_SPDIF_TXFIFO_WML,
  175. .rx_burst = FSL_SPDIF_RXFIFO_WML,
  176. .tx_formats = FSL_SPDIF_FORMATS_PLAYBACK,
  177. };
  178. static const struct fsl_spdif_soc_data fsl_spdif_imx8ulp = {
  179. .imx = true,
  180. .shared_root_clock = true,
  181. .raw_capture_mode = false,
  182. .interrupts = 1,
  183. .tx_burst = 2, /* Applied for EDMA */
  184. .rx_burst = 2, /* Applied for EDMA */
  185. .tx_formats = SNDRV_PCM_FMTBIT_S24_LE, /* Applied for EDMA */
  186. .cchannel_192b = true,
  187. };
  188. /* Check if clk is a root clock that does not share clock source with others */
  189. static inline bool fsl_spdif_can_set_clk_rate(struct fsl_spdif_priv *spdif, int clk)
  190. {
  191. return (clk == STC_TXCLK_SPDIF_ROOT) && !spdif->soc->shared_root_clock;
  192. }
  193. /* DPLL locked and lock loss interrupt handler */
  194. static void spdif_irq_dpll_lock(struct fsl_spdif_priv *spdif_priv)
  195. {
  196. struct regmap *regmap = spdif_priv->regmap;
  197. struct platform_device *pdev = spdif_priv->pdev;
  198. u32 locked;
  199. regmap_read(regmap, REG_SPDIF_SRPC, &locked);
  200. locked &= SRPC_DPLL_LOCKED;
  201. dev_dbg(&pdev->dev, "isr: Rx dpll %s \n",
  202. locked ? "locked" : "loss lock");
  203. spdif_priv->dpll_locked = locked ? true : false;
  204. if (spdif_priv->snd_card && spdif_priv->rxrate_kcontrol) {
  205. snd_ctl_notify(spdif_priv->snd_card,
  206. SNDRV_CTL_EVENT_MASK_VALUE,
  207. &spdif_priv->rxrate_kcontrol->id);
  208. }
  209. }
  210. /* Receiver found illegal symbol interrupt handler */
  211. static void spdif_irq_sym_error(struct fsl_spdif_priv *spdif_priv)
  212. {
  213. struct regmap *regmap = spdif_priv->regmap;
  214. struct platform_device *pdev = spdif_priv->pdev;
  215. dev_dbg(&pdev->dev, "isr: receiver found illegal symbol\n");
  216. /* Clear illegal symbol if DPLL unlocked since no audio stream */
  217. if (!spdif_priv->dpll_locked)
  218. regmap_update_bits(regmap, REG_SPDIF_SIE, INT_SYM_ERR, 0);
  219. }
  220. /* U/Q Channel receive register full */
  221. static void spdif_irq_uqrx_full(struct fsl_spdif_priv *spdif_priv, char name)
  222. {
  223. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  224. struct regmap *regmap = spdif_priv->regmap;
  225. struct platform_device *pdev = spdif_priv->pdev;
  226. u32 *pos, size, val, reg;
  227. switch (name) {
  228. case 'U':
  229. pos = &ctrl->upos;
  230. size = SPDIF_UBITS_SIZE;
  231. reg = REG_SPDIF_SRU;
  232. break;
  233. case 'Q':
  234. pos = &ctrl->qpos;
  235. size = SPDIF_QSUB_SIZE;
  236. reg = REG_SPDIF_SRQ;
  237. break;
  238. default:
  239. dev_err(&pdev->dev, "unsupported channel name\n");
  240. return;
  241. }
  242. dev_dbg(&pdev->dev, "isr: %c Channel receive register full\n", name);
  243. if (*pos >= size * 2) {
  244. *pos = 0;
  245. } else if (unlikely((*pos % size) + 3 > size)) {
  246. dev_err(&pdev->dev, "User bit receive buffer overflow\n");
  247. return;
  248. }
  249. regmap_read(regmap, reg, &val);
  250. ctrl->subcode[*pos++] = val >> 16;
  251. ctrl->subcode[*pos++] = val >> 8;
  252. ctrl->subcode[*pos++] = val;
  253. }
  254. /* U/Q Channel sync found */
  255. static void spdif_irq_uq_sync(struct fsl_spdif_priv *spdif_priv)
  256. {
  257. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  258. struct platform_device *pdev = spdif_priv->pdev;
  259. dev_dbg(&pdev->dev, "isr: U/Q Channel sync found\n");
  260. /* U/Q buffer reset */
  261. if (ctrl->qpos == 0)
  262. return;
  263. /* Set ready to this buffer */
  264. ctrl->ready_buf = (ctrl->qpos - 1) / SPDIF_QSUB_SIZE + 1;
  265. }
  266. /* U/Q Channel framing error */
  267. static void spdif_irq_uq_err(struct fsl_spdif_priv *spdif_priv)
  268. {
  269. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  270. struct regmap *regmap = spdif_priv->regmap;
  271. struct platform_device *pdev = spdif_priv->pdev;
  272. u32 val;
  273. dev_dbg(&pdev->dev, "isr: U/Q Channel framing error\n");
  274. /* Read U/Q data to clear the irq and do buffer reset */
  275. regmap_read(regmap, REG_SPDIF_SRU, &val);
  276. regmap_read(regmap, REG_SPDIF_SRQ, &val);
  277. /* Drop this U/Q buffer */
  278. ctrl->ready_buf = 0;
  279. ctrl->upos = 0;
  280. ctrl->qpos = 0;
  281. }
  282. /* Get spdif interrupt status and clear the interrupt */
  283. static u32 spdif_intr_status_clear(struct fsl_spdif_priv *spdif_priv)
  284. {
  285. struct regmap *regmap = spdif_priv->regmap;
  286. u32 val, val2;
  287. regmap_read(regmap, REG_SPDIF_SIS, &val);
  288. regmap_read(regmap, REG_SPDIF_SIE, &val2);
  289. regmap_write(regmap, REG_SPDIF_SIC, val & val2);
  290. return val;
  291. }
  292. static irqreturn_t spdif_isr(int irq, void *devid)
  293. {
  294. struct fsl_spdif_priv *spdif_priv = (struct fsl_spdif_priv *)devid;
  295. struct platform_device *pdev = spdif_priv->pdev;
  296. u32 sis;
  297. sis = spdif_intr_status_clear(spdif_priv);
  298. if (sis & INT_DPLL_LOCKED)
  299. spdif_irq_dpll_lock(spdif_priv);
  300. if (sis & INT_TXFIFO_UNOV)
  301. dev_dbg(&pdev->dev, "isr: Tx FIFO under/overrun\n");
  302. if (sis & INT_TXFIFO_RESYNC)
  303. dev_dbg(&pdev->dev, "isr: Tx FIFO resync\n");
  304. if (sis & INT_CNEW)
  305. dev_dbg(&pdev->dev, "isr: cstatus new\n");
  306. if (sis & INT_VAL_NOGOOD)
  307. dev_dbg(&pdev->dev, "isr: validity flag no good\n");
  308. if (sis & INT_SYM_ERR)
  309. spdif_irq_sym_error(spdif_priv);
  310. if (sis & INT_BIT_ERR)
  311. dev_dbg(&pdev->dev, "isr: receiver found parity bit error\n");
  312. if (sis & INT_URX_FUL)
  313. spdif_irq_uqrx_full(spdif_priv, 'U');
  314. if (sis & INT_URX_OV)
  315. dev_dbg(&pdev->dev, "isr: U Channel receive register overrun\n");
  316. if (sis & INT_QRX_FUL)
  317. spdif_irq_uqrx_full(spdif_priv, 'Q');
  318. if (sis & INT_QRX_OV)
  319. dev_dbg(&pdev->dev, "isr: Q Channel receive register overrun\n");
  320. if (sis & INT_UQ_SYNC)
  321. spdif_irq_uq_sync(spdif_priv);
  322. if (sis & INT_UQ_ERR)
  323. spdif_irq_uq_err(spdif_priv);
  324. if (sis & INT_RXFIFO_UNOV)
  325. dev_dbg(&pdev->dev, "isr: Rx FIFO under/overrun\n");
  326. if (sis & INT_RXFIFO_RESYNC)
  327. dev_dbg(&pdev->dev, "isr: Rx FIFO resync\n");
  328. if (sis & INT_LOSS_LOCK)
  329. spdif_irq_dpll_lock(spdif_priv);
  330. /* FIXME: Write Tx FIFO to clear TxEm */
  331. if (sis & INT_TX_EM)
  332. dev_dbg(&pdev->dev, "isr: Tx FIFO empty\n");
  333. /* FIXME: Read Rx FIFO to clear RxFIFOFul */
  334. if (sis & INT_RXFIFO_FUL)
  335. dev_dbg(&pdev->dev, "isr: Rx FIFO full\n");
  336. return IRQ_HANDLED;
  337. }
  338. static int spdif_softreset(struct fsl_spdif_priv *spdif_priv)
  339. {
  340. struct regmap *regmap = spdif_priv->regmap;
  341. u32 val, cycle = 1000;
  342. regcache_cache_bypass(regmap, true);
  343. regmap_write(regmap, REG_SPDIF_SCR, SCR_SOFT_RESET);
  344. /*
  345. * RESET bit would be cleared after finishing its reset procedure,
  346. * which typically lasts 8 cycles. 1000 cycles will keep it safe.
  347. */
  348. do {
  349. regmap_read(regmap, REG_SPDIF_SCR, &val);
  350. } while ((val & SCR_SOFT_RESET) && cycle--);
  351. regcache_cache_bypass(regmap, false);
  352. regcache_mark_dirty(regmap);
  353. regcache_sync(regmap);
  354. if (cycle)
  355. return 0;
  356. else
  357. return -EBUSY;
  358. }
  359. static void spdif_set_cstatus(struct spdif_mixer_control *ctrl,
  360. u8 mask, u8 cstatus)
  361. {
  362. ctrl->ch_status[3] &= ~mask;
  363. ctrl->ch_status[3] |= cstatus & mask;
  364. }
  365. static void spdif_write_channel_status(struct fsl_spdif_priv *spdif_priv)
  366. {
  367. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  368. struct regmap *regmap = spdif_priv->regmap;
  369. struct platform_device *pdev = spdif_priv->pdev;
  370. u32 ch_status;
  371. ch_status = (bitrev8(ctrl->ch_status[0]) << 16) |
  372. (bitrev8(ctrl->ch_status[1]) << 8) |
  373. bitrev8(ctrl->ch_status[2]);
  374. regmap_write(regmap, REG_SPDIF_STCSCH, ch_status);
  375. dev_dbg(&pdev->dev, "STCSCH: 0x%06x\n", ch_status);
  376. ch_status = bitrev8(ctrl->ch_status[3]) << 16;
  377. regmap_write(regmap, REG_SPDIF_STCSCL, ch_status);
  378. dev_dbg(&pdev->dev, "STCSCL: 0x%06x\n", ch_status);
  379. if (spdif_priv->soc->cchannel_192b) {
  380. ch_status = (bitrev8(ctrl->ch_status[0]) << 24) |
  381. (bitrev8(ctrl->ch_status[1]) << 16) |
  382. (bitrev8(ctrl->ch_status[2]) << 8) |
  383. bitrev8(ctrl->ch_status[3]);
  384. regmap_update_bits(regmap, REG_SPDIF_SCR, 0x1000000, 0x1000000);
  385. /*
  386. * The first 32bit should be in REG_SPDIF_STCCA_31_0 register,
  387. * but here we need to set REG_SPDIF_STCCA_191_160 on 8ULP
  388. * then can get correct result with HDMI analyzer capture.
  389. * There is a hardware bug here.
  390. */
  391. regmap_write(regmap, REG_SPDIF_STCCA_191_160, ch_status);
  392. }
  393. }
  394. /* Set SPDIF PhaseConfig register for rx clock */
  395. static int spdif_set_rx_clksrc(struct fsl_spdif_priv *spdif_priv,
  396. enum spdif_gainsel gainsel, int dpll_locked)
  397. {
  398. struct regmap *regmap = spdif_priv->regmap;
  399. u8 clksrc = spdif_priv->rxclk_src;
  400. if (clksrc >= SRPC_CLKSRC_MAX || gainsel >= GAINSEL_MULTI_MAX)
  401. return -EINVAL;
  402. regmap_update_bits(regmap, REG_SPDIF_SRPC,
  403. SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK,
  404. SRPC_CLKSRC_SEL_SET(clksrc) | SRPC_GAINSEL_SET(gainsel));
  405. return 0;
  406. }
  407. static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv, enum spdif_txrate index);
  408. static int spdif_set_sample_rate(struct snd_pcm_substream *substream,
  409. int sample_rate)
  410. {
  411. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  412. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  413. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  414. struct regmap *regmap = spdif_priv->regmap;
  415. struct platform_device *pdev = spdif_priv->pdev;
  416. unsigned long csfs = 0;
  417. u32 stc, mask, rate;
  418. u16 sysclk_df;
  419. u8 clk, txclk_df;
  420. int ret;
  421. switch (sample_rate) {
  422. case 22050:
  423. rate = SPDIF_TXRATE_22050;
  424. csfs = IEC958_AES3_CON_FS_22050;
  425. break;
  426. case 32000:
  427. rate = SPDIF_TXRATE_32000;
  428. csfs = IEC958_AES3_CON_FS_32000;
  429. break;
  430. case 44100:
  431. rate = SPDIF_TXRATE_44100;
  432. csfs = IEC958_AES3_CON_FS_44100;
  433. break;
  434. case 48000:
  435. rate = SPDIF_TXRATE_48000;
  436. csfs = IEC958_AES3_CON_FS_48000;
  437. break;
  438. case 88200:
  439. rate = SPDIF_TXRATE_88200;
  440. csfs = IEC958_AES3_CON_FS_88200;
  441. break;
  442. case 96000:
  443. rate = SPDIF_TXRATE_96000;
  444. csfs = IEC958_AES3_CON_FS_96000;
  445. break;
  446. case 176400:
  447. rate = SPDIF_TXRATE_176400;
  448. csfs = IEC958_AES3_CON_FS_176400;
  449. break;
  450. case 192000:
  451. rate = SPDIF_TXRATE_192000;
  452. csfs = IEC958_AES3_CON_FS_192000;
  453. break;
  454. default:
  455. dev_err(&pdev->dev, "unsupported sample rate %d\n", sample_rate);
  456. return -EINVAL;
  457. }
  458. ret = fsl_spdif_probe_txclk(spdif_priv, rate);
  459. if (ret)
  460. return ret;
  461. clk = spdif_priv->txclk_src[rate];
  462. if (clk >= STC_TXCLK_SRC_MAX) {
  463. dev_err(&pdev->dev, "tx clock source is out of range\n");
  464. return -EINVAL;
  465. }
  466. txclk_df = spdif_priv->txclk_df[rate];
  467. if (txclk_df == 0) {
  468. dev_err(&pdev->dev, "the txclk_df can't be zero\n");
  469. return -EINVAL;
  470. }
  471. sysclk_df = spdif_priv->sysclk_df[rate];
  472. if (!fsl_spdif_can_set_clk_rate(spdif_priv, clk))
  473. goto clk_set_bypass;
  474. /* The S/PDIF block needs a clock of 64 * fs * txclk_df */
  475. ret = clk_set_rate(spdif_priv->txclk[clk],
  476. 64 * sample_rate * txclk_df);
  477. if (ret) {
  478. dev_err(&pdev->dev, "failed to set tx clock rate\n");
  479. return ret;
  480. }
  481. clk_set_bypass:
  482. dev_dbg(&pdev->dev, "expected clock rate = %d\n",
  483. (64 * sample_rate * txclk_df * sysclk_df));
  484. dev_dbg(&pdev->dev, "actual clock rate = %ld\n",
  485. clk_get_rate(spdif_priv->txclk[clk]));
  486. /* set fs field in consumer channel status */
  487. spdif_set_cstatus(ctrl, IEC958_AES3_CON_FS, csfs);
  488. /* select clock source and divisor */
  489. stc = STC_TXCLK_ALL_EN | STC_TXCLK_SRC_SET(clk) |
  490. STC_TXCLK_DF(txclk_df) | STC_SYSCLK_DF(sysclk_df);
  491. mask = STC_TXCLK_ALL_EN_MASK | STC_TXCLK_SRC_MASK |
  492. STC_TXCLK_DF_MASK | STC_SYSCLK_DF_MASK;
  493. regmap_update_bits(regmap, REG_SPDIF_STC, mask, stc);
  494. dev_dbg(&pdev->dev, "set sample rate to %dHz for %dHz playback\n",
  495. spdif_priv->txrate[rate], sample_rate);
  496. return 0;
  497. }
  498. static int fsl_spdif_startup(struct snd_pcm_substream *substream,
  499. struct snd_soc_dai *cpu_dai)
  500. {
  501. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  502. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  503. struct platform_device *pdev = spdif_priv->pdev;
  504. struct regmap *regmap = spdif_priv->regmap;
  505. u32 scr, mask;
  506. int ret;
  507. /* Reset module and interrupts only for first initialization */
  508. if (!snd_soc_dai_active(cpu_dai)) {
  509. ret = spdif_softreset(spdif_priv);
  510. if (ret) {
  511. dev_err(&pdev->dev, "failed to soft reset\n");
  512. return ret;
  513. }
  514. /* Disable all the interrupts */
  515. regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
  516. }
  517. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  518. scr = SCR_TXFIFO_AUTOSYNC | SCR_TXFIFO_CTRL_NORMAL |
  519. SCR_TXSEL_NORMAL | SCR_USRC_SEL_CHIP |
  520. SCR_TXFIFO_FSEL_IF8;
  521. mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
  522. SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
  523. SCR_TXFIFO_FSEL_MASK;
  524. } else {
  525. scr = SCR_RXFIFO_FSEL_IF8 | SCR_RXFIFO_AUTOSYNC;
  526. mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
  527. SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
  528. }
  529. regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
  530. /* Power up SPDIF module */
  531. regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_LOW_POWER, 0);
  532. return 0;
  533. }
  534. static void fsl_spdif_shutdown(struct snd_pcm_substream *substream,
  535. struct snd_soc_dai *cpu_dai)
  536. {
  537. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  538. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  539. struct regmap *regmap = spdif_priv->regmap;
  540. u32 scr, mask;
  541. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  542. scr = 0;
  543. mask = SCR_TXFIFO_AUTOSYNC_MASK | SCR_TXFIFO_CTRL_MASK |
  544. SCR_TXSEL_MASK | SCR_USRC_SEL_MASK |
  545. SCR_TXFIFO_FSEL_MASK;
  546. /* Disable TX clock */
  547. regmap_update_bits(regmap, REG_SPDIF_STC, STC_TXCLK_ALL_EN_MASK, 0);
  548. } else {
  549. scr = SCR_RXFIFO_OFF | SCR_RXFIFO_CTL_ZERO;
  550. mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK|
  551. SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK;
  552. }
  553. regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
  554. /* Power down SPDIF module only if tx&rx are both inactive */
  555. if (!snd_soc_dai_active(cpu_dai)) {
  556. spdif_intr_status_clear(spdif_priv);
  557. regmap_update_bits(regmap, REG_SPDIF_SCR,
  558. SCR_LOW_POWER, SCR_LOW_POWER);
  559. }
  560. }
  561. static int spdif_reparent_rootclk(struct fsl_spdif_priv *spdif_priv, unsigned int sample_rate)
  562. {
  563. struct platform_device *pdev = spdif_priv->pdev;
  564. struct clk *clk;
  565. int ret;
  566. /* Reparent clock if required condition is true */
  567. if (!fsl_spdif_can_set_clk_rate(spdif_priv, STC_TXCLK_SPDIF_ROOT))
  568. return 0;
  569. /* Get root clock */
  570. clk = spdif_priv->txclk[STC_TXCLK_SPDIF_ROOT];
  571. /* Disable clock first, for it was enabled by pm_runtime */
  572. clk_disable_unprepare(clk);
  573. fsl_asoc_reparent_pll_clocks(&pdev->dev, clk, spdif_priv->pll8k_clk,
  574. spdif_priv->pll11k_clk, sample_rate);
  575. ret = clk_prepare_enable(clk);
  576. if (ret)
  577. return ret;
  578. return 0;
  579. }
  580. static int fsl_spdif_hw_params(struct snd_pcm_substream *substream,
  581. struct snd_pcm_hw_params *params,
  582. struct snd_soc_dai *dai)
  583. {
  584. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  585. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  586. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  587. struct platform_device *pdev = spdif_priv->pdev;
  588. u32 sample_rate = params_rate(params);
  589. int ret = 0;
  590. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  591. ret = spdif_reparent_rootclk(spdif_priv, sample_rate);
  592. if (ret) {
  593. dev_err(&pdev->dev, "%s: reparent root clk failed: %d\n",
  594. __func__, sample_rate);
  595. return ret;
  596. }
  597. ret = spdif_set_sample_rate(substream, sample_rate);
  598. if (ret) {
  599. dev_err(&pdev->dev, "%s: set sample rate failed: %d\n",
  600. __func__, sample_rate);
  601. return ret;
  602. }
  603. spdif_set_cstatus(ctrl, IEC958_AES3_CON_CLOCK,
  604. IEC958_AES3_CON_CLOCK_1000PPM);
  605. spdif_write_channel_status(spdif_priv);
  606. } else {
  607. /* Setup rx clock source */
  608. ret = spdif_set_rx_clksrc(spdif_priv, SPDIF_DEFAULT_GAINSEL, 1);
  609. }
  610. return ret;
  611. }
  612. static int fsl_spdif_trigger(struct snd_pcm_substream *substream,
  613. int cmd, struct snd_soc_dai *dai)
  614. {
  615. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  616. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  617. struct regmap *regmap = spdif_priv->regmap;
  618. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  619. u32 intr = SIE_INTR_FOR(tx);
  620. u32 dmaen = SCR_DMA_xX_EN(tx);
  621. switch (cmd) {
  622. case SNDRV_PCM_TRIGGER_START:
  623. case SNDRV_PCM_TRIGGER_RESUME:
  624. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  625. regmap_update_bits(regmap, REG_SPDIF_SIE, intr, intr);
  626. regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, dmaen);
  627. break;
  628. case SNDRV_PCM_TRIGGER_STOP:
  629. case SNDRV_PCM_TRIGGER_SUSPEND:
  630. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  631. regmap_update_bits(regmap, REG_SPDIF_SCR, dmaen, 0);
  632. regmap_update_bits(regmap, REG_SPDIF_SIE, intr, 0);
  633. regmap_write(regmap, REG_SPDIF_STL, 0x0);
  634. regmap_write(regmap, REG_SPDIF_STR, 0x0);
  635. break;
  636. default:
  637. return -EINVAL;
  638. }
  639. return 0;
  640. }
  641. /*
  642. * FSL SPDIF IEC958 controller(mixer) functions
  643. *
  644. * Channel status get/put control
  645. * User bit value get/put control
  646. * Valid bit value get control
  647. * DPLL lock status get control
  648. * User bit sync mode selection control
  649. */
  650. static int fsl_spdif_info(struct snd_kcontrol *kcontrol,
  651. struct snd_ctl_elem_info *uinfo)
  652. {
  653. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  654. uinfo->count = 1;
  655. return 0;
  656. }
  657. static int fsl_spdif_pb_get(struct snd_kcontrol *kcontrol,
  658. struct snd_ctl_elem_value *uvalue)
  659. {
  660. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  661. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  662. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  663. uvalue->value.iec958.status[0] = ctrl->ch_status[0];
  664. uvalue->value.iec958.status[1] = ctrl->ch_status[1];
  665. uvalue->value.iec958.status[2] = ctrl->ch_status[2];
  666. uvalue->value.iec958.status[3] = ctrl->ch_status[3];
  667. return 0;
  668. }
  669. static int fsl_spdif_pb_put(struct snd_kcontrol *kcontrol,
  670. struct snd_ctl_elem_value *uvalue)
  671. {
  672. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  673. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  674. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  675. ctrl->ch_status[0] = uvalue->value.iec958.status[0];
  676. ctrl->ch_status[1] = uvalue->value.iec958.status[1];
  677. ctrl->ch_status[2] = uvalue->value.iec958.status[2];
  678. ctrl->ch_status[3] = uvalue->value.iec958.status[3];
  679. spdif_write_channel_status(spdif_priv);
  680. return 0;
  681. }
  682. /* Get channel status from SPDIF_RX_CCHAN register */
  683. static int fsl_spdif_capture_get(struct snd_kcontrol *kcontrol,
  684. struct snd_ctl_elem_value *ucontrol)
  685. {
  686. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  687. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  688. struct regmap *regmap = spdif_priv->regmap;
  689. u32 cstatus, val;
  690. regmap_read(regmap, REG_SPDIF_SIS, &val);
  691. if (!(val & INT_CNEW))
  692. return -EAGAIN;
  693. regmap_read(regmap, REG_SPDIF_SRCSH, &cstatus);
  694. ucontrol->value.iec958.status[0] = (cstatus >> 16) & 0xFF;
  695. ucontrol->value.iec958.status[1] = (cstatus >> 8) & 0xFF;
  696. ucontrol->value.iec958.status[2] = cstatus & 0xFF;
  697. regmap_read(regmap, REG_SPDIF_SRCSL, &cstatus);
  698. ucontrol->value.iec958.status[3] = (cstatus >> 16) & 0xFF;
  699. ucontrol->value.iec958.status[4] = (cstatus >> 8) & 0xFF;
  700. ucontrol->value.iec958.status[5] = cstatus & 0xFF;
  701. /* Clear intr */
  702. regmap_write(regmap, REG_SPDIF_SIC, INT_CNEW);
  703. return 0;
  704. }
  705. /*
  706. * Get User bits (subcode) from chip value which readed out
  707. * in UChannel register.
  708. */
  709. static int fsl_spdif_subcode_get(struct snd_kcontrol *kcontrol,
  710. struct snd_ctl_elem_value *ucontrol)
  711. {
  712. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  713. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  714. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  715. unsigned long flags;
  716. int ret = -EAGAIN;
  717. spin_lock_irqsave(&ctrl->ctl_lock, flags);
  718. if (ctrl->ready_buf) {
  719. int idx = (ctrl->ready_buf - 1) * SPDIF_UBITS_SIZE;
  720. memcpy(&ucontrol->value.iec958.subcode[0],
  721. &ctrl->subcode[idx], SPDIF_UBITS_SIZE);
  722. ret = 0;
  723. }
  724. spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
  725. return ret;
  726. }
  727. /* Q-subcode information. The byte size is SPDIF_UBITS_SIZE/8 */
  728. static int fsl_spdif_qinfo(struct snd_kcontrol *kcontrol,
  729. struct snd_ctl_elem_info *uinfo)
  730. {
  731. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  732. uinfo->count = SPDIF_QSUB_SIZE;
  733. return 0;
  734. }
  735. /* Get Q subcode from chip value which readed out in QChannel register */
  736. static int fsl_spdif_qget(struct snd_kcontrol *kcontrol,
  737. struct snd_ctl_elem_value *ucontrol)
  738. {
  739. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  740. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  741. struct spdif_mixer_control *ctrl = &spdif_priv->fsl_spdif_control;
  742. unsigned long flags;
  743. int ret = -EAGAIN;
  744. spin_lock_irqsave(&ctrl->ctl_lock, flags);
  745. if (ctrl->ready_buf) {
  746. int idx = (ctrl->ready_buf - 1) * SPDIF_QSUB_SIZE;
  747. memcpy(&ucontrol->value.bytes.data[0],
  748. &ctrl->qsub[idx], SPDIF_QSUB_SIZE);
  749. ret = 0;
  750. }
  751. spin_unlock_irqrestore(&ctrl->ctl_lock, flags);
  752. return ret;
  753. }
  754. /* Get valid good bit from interrupt status register */
  755. static int fsl_spdif_rx_vbit_get(struct snd_kcontrol *kcontrol,
  756. struct snd_ctl_elem_value *ucontrol)
  757. {
  758. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  759. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  760. struct regmap *regmap = spdif_priv->regmap;
  761. u32 val;
  762. regmap_read(regmap, REG_SPDIF_SIS, &val);
  763. ucontrol->value.integer.value[0] = (val & INT_VAL_NOGOOD) != 0;
  764. regmap_write(regmap, REG_SPDIF_SIC, INT_VAL_NOGOOD);
  765. return 0;
  766. }
  767. static int fsl_spdif_tx_vbit_get(struct snd_kcontrol *kcontrol,
  768. struct snd_ctl_elem_value *ucontrol)
  769. {
  770. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  771. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  772. struct regmap *regmap = spdif_priv->regmap;
  773. u32 val;
  774. regmap_read(regmap, REG_SPDIF_SCR, &val);
  775. val = (val & SCR_VAL_MASK) >> SCR_VAL_OFFSET;
  776. val = 1 - val;
  777. ucontrol->value.integer.value[0] = val;
  778. return 0;
  779. }
  780. static int fsl_spdif_tx_vbit_put(struct snd_kcontrol *kcontrol,
  781. struct snd_ctl_elem_value *ucontrol)
  782. {
  783. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  784. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  785. struct regmap *regmap = spdif_priv->regmap;
  786. u32 val = (1 - ucontrol->value.integer.value[0]) << SCR_VAL_OFFSET;
  787. regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_VAL_MASK, val);
  788. return 0;
  789. }
  790. static int fsl_spdif_rx_rcm_get(struct snd_kcontrol *kcontrol,
  791. struct snd_ctl_elem_value *ucontrol)
  792. {
  793. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  794. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  795. struct regmap *regmap = spdif_priv->regmap;
  796. u32 val;
  797. regmap_read(regmap, REG_SPDIF_SCR, &val);
  798. val = (val & SCR_RAW_CAPTURE_MODE) ? 1 : 0;
  799. ucontrol->value.integer.value[0] = val;
  800. return 0;
  801. }
  802. static int fsl_spdif_rx_rcm_put(struct snd_kcontrol *kcontrol,
  803. struct snd_ctl_elem_value *ucontrol)
  804. {
  805. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  806. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  807. struct regmap *regmap = spdif_priv->regmap;
  808. u32 val = (ucontrol->value.integer.value[0] ? SCR_RAW_CAPTURE_MODE : 0);
  809. if (val)
  810. cpu_dai->driver->capture.formats |= SNDRV_PCM_FMTBIT_S32_LE;
  811. else
  812. cpu_dai->driver->capture.formats &= ~SNDRV_PCM_FMTBIT_S32_LE;
  813. regmap_update_bits(regmap, REG_SPDIF_SCR, SCR_RAW_CAPTURE_MODE, val);
  814. return 0;
  815. }
  816. static int fsl_spdif_bypass_get(struct snd_kcontrol *kcontrol,
  817. struct snd_ctl_elem_value *ucontrol)
  818. {
  819. struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
  820. struct fsl_spdif_priv *priv = snd_soc_dai_get_drvdata(dai);
  821. ucontrol->value.integer.value[0] = priv->bypass ? 1 : 0;
  822. return 0;
  823. }
  824. static int fsl_spdif_bypass_put(struct snd_kcontrol *kcontrol,
  825. struct snd_ctl_elem_value *ucontrol)
  826. {
  827. struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
  828. struct fsl_spdif_priv *priv = snd_soc_dai_get_drvdata(dai);
  829. struct snd_soc_card *card = dai->component->card;
  830. bool set = (ucontrol->value.integer.value[0] != 0);
  831. struct regmap *regmap = priv->regmap;
  832. struct snd_soc_pcm_runtime *rtd;
  833. u32 scr, mask;
  834. int stream;
  835. rtd = snd_soc_get_pcm_runtime(card, card->dai_link);
  836. if (priv->bypass == set)
  837. return 0; /* nothing to do */
  838. if (snd_soc_dai_active(dai)) {
  839. dev_err(dai->dev, "Cannot change BYPASS mode while stream is running.\n");
  840. return -EBUSY;
  841. }
  842. pm_runtime_get_sync(dai->dev);
  843. if (set) {
  844. /* Disable interrupts */
  845. regmap_update_bits(regmap, REG_SPDIF_SIE, 0xffffff, 0);
  846. /* Configure BYPASS mode */
  847. scr = SCR_TXSEL_RX | SCR_RXFIFO_OFF;
  848. mask = SCR_RXFIFO_FSEL_MASK | SCR_RXFIFO_AUTOSYNC_MASK |
  849. SCR_RXFIFO_CTL_MASK | SCR_RXFIFO_OFF_MASK | SCR_TXSEL_MASK;
  850. /* Power up SPDIF module */
  851. mask |= SCR_LOW_POWER;
  852. } else {
  853. /* Power down SPDIF module, disable TX */
  854. scr = SCR_LOW_POWER | SCR_TXSEL_OFF;
  855. mask = SCR_LOW_POWER | SCR_TXSEL_MASK;
  856. }
  857. regmap_update_bits(regmap, REG_SPDIF_SCR, mask, scr);
  858. /* Disable playback & capture if BYPASS mode is enabled, enable otherwise */
  859. for_each_pcm_streams(stream)
  860. rtd->pcm->streams[stream].substream_count = (set ? 0 : 1);
  861. priv->bypass = set;
  862. pm_runtime_put_sync(dai->dev);
  863. return 0;
  864. }
  865. /* DPLL lock information */
  866. static int fsl_spdif_rxrate_info(struct snd_kcontrol *kcontrol,
  867. struct snd_ctl_elem_info *uinfo)
  868. {
  869. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  870. uinfo->count = 1;
  871. uinfo->value.integer.min = 16000;
  872. uinfo->value.integer.max = 192000;
  873. return 0;
  874. }
  875. static u32 gainsel_multi[GAINSEL_MULTI_MAX] = {
  876. 24, 16, 12, 8, 6, 4, 3,
  877. };
  878. /* Get RX data clock rate given the SPDIF bus_clk */
  879. static int spdif_get_rxclk_rate(struct fsl_spdif_priv *spdif_priv,
  880. enum spdif_gainsel gainsel)
  881. {
  882. struct regmap *regmap = spdif_priv->regmap;
  883. struct platform_device *pdev = spdif_priv->pdev;
  884. u64 tmpval64, busclk_freq = 0;
  885. u32 freqmeas, phaseconf;
  886. u8 clksrc;
  887. regmap_read(regmap, REG_SPDIF_SRFM, &freqmeas);
  888. regmap_read(regmap, REG_SPDIF_SRPC, &phaseconf);
  889. clksrc = (phaseconf >> SRPC_CLKSRC_SEL_OFFSET) & 0xf;
  890. /* Get bus clock from system */
  891. if (srpc_dpll_locked[clksrc] && (phaseconf & SRPC_DPLL_LOCKED))
  892. busclk_freq = clk_get_rate(spdif_priv->sysclk);
  893. /* FreqMeas_CLK = (BUS_CLK * FreqMeas) / 2 ^ 10 / GAINSEL / 128 */
  894. tmpval64 = (u64) busclk_freq * freqmeas;
  895. do_div(tmpval64, gainsel_multi[gainsel] * 1024);
  896. do_div(tmpval64, 128 * 1024);
  897. dev_dbg(&pdev->dev, "FreqMeas: %d\n", freqmeas);
  898. dev_dbg(&pdev->dev, "BusclkFreq: %lld\n", busclk_freq);
  899. dev_dbg(&pdev->dev, "RxRate: %lld\n", tmpval64);
  900. return (int)tmpval64;
  901. }
  902. /*
  903. * Get DPLL lock or not info from stable interrupt status register.
  904. * User application must use this control to get locked,
  905. * then can do next PCM operation
  906. */
  907. static int fsl_spdif_rxrate_get(struct snd_kcontrol *kcontrol,
  908. struct snd_ctl_elem_value *ucontrol)
  909. {
  910. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  911. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  912. int rate = 0;
  913. if (spdif_priv->dpll_locked)
  914. rate = spdif_get_rxclk_rate(spdif_priv, SPDIF_DEFAULT_GAINSEL);
  915. ucontrol->value.integer.value[0] = rate;
  916. return 0;
  917. }
  918. /*
  919. * User bit sync mode:
  920. * 1 CD User channel subcode
  921. * 0 Non-CD data
  922. */
  923. static int fsl_spdif_usync_get(struct snd_kcontrol *kcontrol,
  924. struct snd_ctl_elem_value *ucontrol)
  925. {
  926. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  927. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  928. struct regmap *regmap = spdif_priv->regmap;
  929. u32 val;
  930. regmap_read(regmap, REG_SPDIF_SRCD, &val);
  931. ucontrol->value.integer.value[0] = (val & SRCD_CD_USER) != 0;
  932. return 0;
  933. }
  934. /*
  935. * User bit sync mode:
  936. * 1 CD User channel subcode
  937. * 0 Non-CD data
  938. */
  939. static int fsl_spdif_usync_put(struct snd_kcontrol *kcontrol,
  940. struct snd_ctl_elem_value *ucontrol)
  941. {
  942. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  943. struct fsl_spdif_priv *spdif_priv = snd_soc_dai_get_drvdata(cpu_dai);
  944. struct regmap *regmap = spdif_priv->regmap;
  945. u32 val = ucontrol->value.integer.value[0] << SRCD_CD_USER_OFFSET;
  946. regmap_update_bits(regmap, REG_SPDIF_SRCD, SRCD_CD_USER, val);
  947. return 0;
  948. }
  949. /* FSL SPDIF IEC958 controller defines */
  950. static const struct snd_kcontrol_new fsl_spdif_ctrls[] = {
  951. /* Status cchanel controller */
  952. {
  953. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  954. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  955. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  956. SNDRV_CTL_ELEM_ACCESS_WRITE |
  957. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  958. .info = fsl_spdif_info,
  959. .get = fsl_spdif_pb_get,
  960. .put = fsl_spdif_pb_put,
  961. },
  962. {
  963. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  964. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  965. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  966. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  967. .info = fsl_spdif_info,
  968. .get = fsl_spdif_capture_get,
  969. },
  970. /* User bits controller */
  971. {
  972. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  973. .name = "IEC958 Subcode Capture Default",
  974. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  975. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  976. .info = fsl_spdif_info,
  977. .get = fsl_spdif_subcode_get,
  978. },
  979. {
  980. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  981. .name = "IEC958 Q-subcode Capture Default",
  982. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  983. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  984. .info = fsl_spdif_qinfo,
  985. .get = fsl_spdif_qget,
  986. },
  987. /* Valid bit error controller */
  988. {
  989. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  990. .name = "IEC958 RX V-Bit Errors",
  991. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  992. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  993. .info = snd_ctl_boolean_mono_info,
  994. .get = fsl_spdif_rx_vbit_get,
  995. },
  996. {
  997. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  998. .name = "IEC958 TX V-Bit",
  999. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  1000. SNDRV_CTL_ELEM_ACCESS_WRITE |
  1001. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  1002. .info = snd_ctl_boolean_mono_info,
  1003. .get = fsl_spdif_tx_vbit_get,
  1004. .put = fsl_spdif_tx_vbit_put,
  1005. },
  1006. /* DPLL lock info get controller */
  1007. {
  1008. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1009. .name = RX_SAMPLE_RATE_KCONTROL,
  1010. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  1011. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  1012. .info = fsl_spdif_rxrate_info,
  1013. .get = fsl_spdif_rxrate_get,
  1014. },
  1015. /* RX bypass controller */
  1016. {
  1017. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1018. .name = "Bypass Mode",
  1019. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1020. .info = snd_ctl_boolean_mono_info,
  1021. .get = fsl_spdif_bypass_get,
  1022. .put = fsl_spdif_bypass_put,
  1023. },
  1024. /* User bit sync mode set/get controller */
  1025. {
  1026. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1027. .name = "IEC958 USyncMode CDText",
  1028. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  1029. SNDRV_CTL_ELEM_ACCESS_WRITE |
  1030. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  1031. .info = snd_ctl_boolean_mono_info,
  1032. .get = fsl_spdif_usync_get,
  1033. .put = fsl_spdif_usync_put,
  1034. },
  1035. };
  1036. static const struct snd_kcontrol_new fsl_spdif_ctrls_rcm[] = {
  1037. {
  1038. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1039. .name = "IEC958 Raw Capture Mode",
  1040. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  1041. SNDRV_CTL_ELEM_ACCESS_WRITE |
  1042. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  1043. .info = snd_ctl_boolean_mono_info,
  1044. .get = fsl_spdif_rx_rcm_get,
  1045. .put = fsl_spdif_rx_rcm_put,
  1046. },
  1047. };
  1048. static int fsl_spdif_dai_probe(struct snd_soc_dai *dai)
  1049. {
  1050. struct fsl_spdif_priv *spdif_private = snd_soc_dai_get_drvdata(dai);
  1051. snd_soc_dai_init_dma_data(dai, &spdif_private->dma_params_tx,
  1052. &spdif_private->dma_params_rx);
  1053. snd_soc_add_dai_controls(dai, fsl_spdif_ctrls, ARRAY_SIZE(fsl_spdif_ctrls));
  1054. if (spdif_private->soc->raw_capture_mode)
  1055. snd_soc_add_dai_controls(dai, fsl_spdif_ctrls_rcm,
  1056. ARRAY_SIZE(fsl_spdif_ctrls_rcm));
  1057. spdif_private->snd_card = dai->component->card->snd_card;
  1058. spdif_private->rxrate_kcontrol = snd_soc_card_get_kcontrol(dai->component->card,
  1059. RX_SAMPLE_RATE_KCONTROL);
  1060. if (!spdif_private->rxrate_kcontrol)
  1061. dev_err(&spdif_private->pdev->dev, "failed to get %s kcontrol\n",
  1062. RX_SAMPLE_RATE_KCONTROL);
  1063. /*Clear the val bit for Tx*/
  1064. regmap_update_bits(spdif_private->regmap, REG_SPDIF_SCR,
  1065. SCR_VAL_MASK, SCR_VAL_CLEAR);
  1066. return 0;
  1067. }
  1068. static const struct snd_soc_dai_ops fsl_spdif_dai_ops = {
  1069. .probe = fsl_spdif_dai_probe,
  1070. .startup = fsl_spdif_startup,
  1071. .hw_params = fsl_spdif_hw_params,
  1072. .trigger = fsl_spdif_trigger,
  1073. .shutdown = fsl_spdif_shutdown,
  1074. };
  1075. static struct snd_soc_dai_driver fsl_spdif_dai = {
  1076. .playback = {
  1077. .stream_name = "CPU-Playback",
  1078. .channels_min = 2,
  1079. .channels_max = 2,
  1080. .rates = FSL_SPDIF_RATES_PLAYBACK,
  1081. .formats = FSL_SPDIF_FORMATS_PLAYBACK,
  1082. },
  1083. .capture = {
  1084. .stream_name = "CPU-Capture",
  1085. .channels_min = 2,
  1086. .channels_max = 2,
  1087. .rates = FSL_SPDIF_RATES_CAPTURE,
  1088. .formats = FSL_SPDIF_FORMATS_CAPTURE,
  1089. },
  1090. .ops = &fsl_spdif_dai_ops,
  1091. };
  1092. static const struct snd_soc_component_driver fsl_spdif_component = {
  1093. .name = "fsl-spdif",
  1094. .legacy_dai_naming = 1,
  1095. };
  1096. /* FSL SPDIF REGMAP */
  1097. static const struct reg_default fsl_spdif_reg_defaults[] = {
  1098. {REG_SPDIF_SCR, 0x00000400},
  1099. {REG_SPDIF_SRCD, 0x00000000},
  1100. {REG_SPDIF_SIE, 0x00000000},
  1101. {REG_SPDIF_STL, 0x00000000},
  1102. {REG_SPDIF_STR, 0x00000000},
  1103. {REG_SPDIF_STCSCH, 0x00000000},
  1104. {REG_SPDIF_STCSCL, 0x00000000},
  1105. {REG_SPDIF_STCSPH, 0x00000000},
  1106. {REG_SPDIF_STCSPL, 0x00000000},
  1107. {REG_SPDIF_STC, 0x00020f00},
  1108. };
  1109. static bool fsl_spdif_readable_reg(struct device *dev, unsigned int reg)
  1110. {
  1111. switch (reg) {
  1112. case REG_SPDIF_SCR:
  1113. case REG_SPDIF_SRCD:
  1114. case REG_SPDIF_SRPC:
  1115. case REG_SPDIF_SIE:
  1116. case REG_SPDIF_SIS:
  1117. case REG_SPDIF_SRL:
  1118. case REG_SPDIF_SRR:
  1119. case REG_SPDIF_SRCSH:
  1120. case REG_SPDIF_SRCSL:
  1121. case REG_SPDIF_SRU:
  1122. case REG_SPDIF_SRQ:
  1123. case REG_SPDIF_STCSCH:
  1124. case REG_SPDIF_STCSCL:
  1125. case REG_SPDIF_STCSPH:
  1126. case REG_SPDIF_STCSPL:
  1127. case REG_SPDIF_SRFM:
  1128. case REG_SPDIF_STC:
  1129. case REG_SPDIF_SRCCA_31_0:
  1130. case REG_SPDIF_SRCCA_63_32:
  1131. case REG_SPDIF_SRCCA_95_64:
  1132. case REG_SPDIF_SRCCA_127_96:
  1133. case REG_SPDIF_SRCCA_159_128:
  1134. case REG_SPDIF_SRCCA_191_160:
  1135. case REG_SPDIF_STCCA_31_0:
  1136. case REG_SPDIF_STCCA_63_32:
  1137. case REG_SPDIF_STCCA_95_64:
  1138. case REG_SPDIF_STCCA_127_96:
  1139. case REG_SPDIF_STCCA_159_128:
  1140. case REG_SPDIF_STCCA_191_160:
  1141. return true;
  1142. default:
  1143. return false;
  1144. }
  1145. }
  1146. static bool fsl_spdif_volatile_reg(struct device *dev, unsigned int reg)
  1147. {
  1148. switch (reg) {
  1149. case REG_SPDIF_SRPC:
  1150. case REG_SPDIF_SIS:
  1151. case REG_SPDIF_SRL:
  1152. case REG_SPDIF_SRR:
  1153. case REG_SPDIF_SRCSH:
  1154. case REG_SPDIF_SRCSL:
  1155. case REG_SPDIF_SRU:
  1156. case REG_SPDIF_SRQ:
  1157. case REG_SPDIF_SRFM:
  1158. case REG_SPDIF_SRCCA_31_0:
  1159. case REG_SPDIF_SRCCA_63_32:
  1160. case REG_SPDIF_SRCCA_95_64:
  1161. case REG_SPDIF_SRCCA_127_96:
  1162. case REG_SPDIF_SRCCA_159_128:
  1163. case REG_SPDIF_SRCCA_191_160:
  1164. return true;
  1165. default:
  1166. return false;
  1167. }
  1168. }
  1169. static bool fsl_spdif_writeable_reg(struct device *dev, unsigned int reg)
  1170. {
  1171. switch (reg) {
  1172. case REG_SPDIF_SCR:
  1173. case REG_SPDIF_SRCD:
  1174. case REG_SPDIF_SRPC:
  1175. case REG_SPDIF_SIE:
  1176. case REG_SPDIF_SIC:
  1177. case REG_SPDIF_STL:
  1178. case REG_SPDIF_STR:
  1179. case REG_SPDIF_STCSCH:
  1180. case REG_SPDIF_STCSCL:
  1181. case REG_SPDIF_STCSPH:
  1182. case REG_SPDIF_STCSPL:
  1183. case REG_SPDIF_STC:
  1184. case REG_SPDIF_STCCA_31_0:
  1185. case REG_SPDIF_STCCA_63_32:
  1186. case REG_SPDIF_STCCA_95_64:
  1187. case REG_SPDIF_STCCA_127_96:
  1188. case REG_SPDIF_STCCA_159_128:
  1189. case REG_SPDIF_STCCA_191_160:
  1190. return true;
  1191. default:
  1192. return false;
  1193. }
  1194. }
  1195. static const struct regmap_config fsl_spdif_regmap_config = {
  1196. .reg_bits = 32,
  1197. .reg_stride = 4,
  1198. .val_bits = 32,
  1199. .max_register = REG_SPDIF_STCCA_191_160,
  1200. .reg_defaults = fsl_spdif_reg_defaults,
  1201. .num_reg_defaults = ARRAY_SIZE(fsl_spdif_reg_defaults),
  1202. .readable_reg = fsl_spdif_readable_reg,
  1203. .volatile_reg = fsl_spdif_volatile_reg,
  1204. .writeable_reg = fsl_spdif_writeable_reg,
  1205. .cache_type = REGCACHE_FLAT,
  1206. };
  1207. static u32 fsl_spdif_txclk_caldiv(struct fsl_spdif_priv *spdif_priv,
  1208. struct clk *clk, u64 savesub,
  1209. enum spdif_txrate index, bool round)
  1210. {
  1211. static const u32 rate[] = { 22050, 32000, 44100, 48000, 88200, 96000, 176400,
  1212. 192000, };
  1213. bool is_sysclk = clk_is_match(clk, spdif_priv->sysclk);
  1214. u64 rate_ideal, rate_actual, sub;
  1215. u32 arate;
  1216. u16 sysclk_dfmin, sysclk_dfmax, sysclk_df;
  1217. u8 txclk_df;
  1218. /* The sysclk has an extra divisor [2, 512] */
  1219. sysclk_dfmin = is_sysclk ? 2 : 1;
  1220. sysclk_dfmax = is_sysclk ? 512 : 1;
  1221. for (sysclk_df = sysclk_dfmin; sysclk_df <= sysclk_dfmax; sysclk_df++) {
  1222. for (txclk_df = 1; txclk_df <= 128; txclk_df++) {
  1223. rate_ideal = rate[index] * txclk_df * 64ULL;
  1224. if (round)
  1225. rate_actual = clk_round_rate(clk, rate_ideal);
  1226. else
  1227. rate_actual = clk_get_rate(clk);
  1228. arate = rate_actual / 64;
  1229. arate /= txclk_df * sysclk_df;
  1230. if (arate == rate[index]) {
  1231. /* We are lucky */
  1232. savesub = 0;
  1233. spdif_priv->txclk_df[index] = txclk_df;
  1234. spdif_priv->sysclk_df[index] = sysclk_df;
  1235. spdif_priv->txrate[index] = arate;
  1236. goto out;
  1237. } else if (arate / rate[index] == 1) {
  1238. /* A little bigger than expect */
  1239. sub = (u64)(arate - rate[index]) * 100000;
  1240. do_div(sub, rate[index]);
  1241. if (sub >= savesub)
  1242. continue;
  1243. savesub = sub;
  1244. spdif_priv->txclk_df[index] = txclk_df;
  1245. spdif_priv->sysclk_df[index] = sysclk_df;
  1246. spdif_priv->txrate[index] = arate;
  1247. } else if (rate[index] / arate == 1) {
  1248. /* A little smaller than expect */
  1249. sub = (u64)(rate[index] - arate) * 100000;
  1250. do_div(sub, rate[index]);
  1251. if (sub >= savesub)
  1252. continue;
  1253. savesub = sub;
  1254. spdif_priv->txclk_df[index] = txclk_df;
  1255. spdif_priv->sysclk_df[index] = sysclk_df;
  1256. spdif_priv->txrate[index] = arate;
  1257. }
  1258. }
  1259. }
  1260. out:
  1261. return savesub;
  1262. }
  1263. static int fsl_spdif_probe_txclk(struct fsl_spdif_priv *spdif_priv,
  1264. enum spdif_txrate index)
  1265. {
  1266. static const u32 rate[] = { 22050, 32000, 44100, 48000, 88200, 96000, 176400,
  1267. 192000, };
  1268. struct platform_device *pdev = spdif_priv->pdev;
  1269. struct device *dev = &pdev->dev;
  1270. u64 savesub = 100000, ret;
  1271. struct clk *clk;
  1272. int i;
  1273. for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
  1274. clk = spdif_priv->txclk[i];
  1275. if (IS_ERR(clk)) {
  1276. dev_err(dev, "no rxtx%d clock in devicetree\n", i);
  1277. return PTR_ERR(clk);
  1278. }
  1279. if (!clk_get_rate(clk))
  1280. continue;
  1281. ret = fsl_spdif_txclk_caldiv(spdif_priv, clk, savesub, index,
  1282. fsl_spdif_can_set_clk_rate(spdif_priv, i));
  1283. if (savesub == ret)
  1284. continue;
  1285. savesub = ret;
  1286. spdif_priv->txclk_src[index] = i;
  1287. /* To quick catch a divisor, we allow a 0.1% deviation */
  1288. if (savesub < 100)
  1289. break;
  1290. }
  1291. dev_dbg(dev, "use rxtx%d as tx clock source for %dHz sample rate\n",
  1292. spdif_priv->txclk_src[index], rate[index]);
  1293. dev_dbg(dev, "use txclk df %d for %dHz sample rate\n",
  1294. spdif_priv->txclk_df[index], rate[index]);
  1295. if (clk_is_match(spdif_priv->txclk[spdif_priv->txclk_src[index]], spdif_priv->sysclk))
  1296. dev_dbg(dev, "use sysclk df %d for %dHz sample rate\n",
  1297. spdif_priv->sysclk_df[index], rate[index]);
  1298. dev_dbg(dev, "the best rate for %dHz sample rate is %dHz\n",
  1299. rate[index], spdif_priv->txrate[index]);
  1300. return 0;
  1301. }
  1302. static int fsl_spdif_probe(struct platform_device *pdev)
  1303. {
  1304. struct fsl_spdif_priv *spdif_priv;
  1305. struct spdif_mixer_control *ctrl;
  1306. struct resource *res;
  1307. void __iomem *regs;
  1308. int irq, ret, i;
  1309. char tmp[16];
  1310. spdif_priv = devm_kzalloc(&pdev->dev, sizeof(*spdif_priv), GFP_KERNEL);
  1311. if (!spdif_priv)
  1312. return -ENOMEM;
  1313. spdif_priv->pdev = pdev;
  1314. spdif_priv->soc = of_device_get_match_data(&pdev->dev);
  1315. /* Initialize this copy of the CPU DAI driver structure */
  1316. memcpy(&spdif_priv->cpu_dai_drv, &fsl_spdif_dai, sizeof(fsl_spdif_dai));
  1317. spdif_priv->cpu_dai_drv.name = dev_name(&pdev->dev);
  1318. spdif_priv->cpu_dai_drv.playback.formats =
  1319. spdif_priv->soc->tx_formats;
  1320. /* Get the addresses and IRQ */
  1321. regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1322. if (IS_ERR(regs))
  1323. return PTR_ERR(regs);
  1324. spdif_priv->regmap = devm_regmap_init_mmio(&pdev->dev, regs, &fsl_spdif_regmap_config);
  1325. if (IS_ERR(spdif_priv->regmap)) {
  1326. dev_err(&pdev->dev, "regmap init failed\n");
  1327. return PTR_ERR(spdif_priv->regmap);
  1328. }
  1329. for (i = 0; i < spdif_priv->soc->interrupts; i++) {
  1330. irq = platform_get_irq(pdev, i);
  1331. if (irq < 0)
  1332. return irq;
  1333. ret = devm_request_irq(&pdev->dev, irq, spdif_isr, 0,
  1334. dev_name(&pdev->dev), spdif_priv);
  1335. if (ret) {
  1336. dev_err(&pdev->dev, "could not claim irq %u\n", irq);
  1337. return ret;
  1338. }
  1339. }
  1340. for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
  1341. sprintf(tmp, "rxtx%d", i);
  1342. spdif_priv->txclk[i] = devm_clk_get(&pdev->dev, tmp);
  1343. if (IS_ERR(spdif_priv->txclk[i])) {
  1344. dev_err(&pdev->dev, "no rxtx%d clock in devicetree\n", i);
  1345. return PTR_ERR(spdif_priv->txclk[i]);
  1346. }
  1347. }
  1348. /* Get system clock for rx clock rate calculation */
  1349. spdif_priv->sysclk = spdif_priv->txclk[5];
  1350. if (IS_ERR(spdif_priv->sysclk)) {
  1351. dev_err(&pdev->dev, "no sys clock (rxtx5) in devicetree\n");
  1352. return PTR_ERR(spdif_priv->sysclk);
  1353. }
  1354. /* Get core clock for data register access via DMA */
  1355. spdif_priv->coreclk = devm_clk_get(&pdev->dev, "core");
  1356. if (IS_ERR(spdif_priv->coreclk)) {
  1357. dev_err(&pdev->dev, "no core clock in devicetree\n");
  1358. return PTR_ERR(spdif_priv->coreclk);
  1359. }
  1360. spdif_priv->spbaclk = devm_clk_get(&pdev->dev, "spba");
  1361. if (IS_ERR(spdif_priv->spbaclk))
  1362. dev_warn(&pdev->dev, "no spba clock in devicetree\n");
  1363. /* Select clock source for rx/tx clock */
  1364. spdif_priv->rxclk = spdif_priv->txclk[1];
  1365. if (IS_ERR(spdif_priv->rxclk)) {
  1366. dev_err(&pdev->dev, "no rxtx1 clock in devicetree\n");
  1367. return PTR_ERR(spdif_priv->rxclk);
  1368. }
  1369. spdif_priv->rxclk_src = DEFAULT_RXCLK_SRC;
  1370. fsl_asoc_get_pll_clocks(&pdev->dev, &spdif_priv->pll8k_clk,
  1371. &spdif_priv->pll11k_clk);
  1372. /* Initial spinlock for control data */
  1373. ctrl = &spdif_priv->fsl_spdif_control;
  1374. spin_lock_init(&ctrl->ctl_lock);
  1375. /* Init tx channel status default value */
  1376. ctrl->ch_status[0] = IEC958_AES0_CON_NOT_COPYRIGHT |
  1377. IEC958_AES0_CON_EMPHASIS_5015;
  1378. ctrl->ch_status[1] = IEC958_AES1_CON_DIGDIGCONV_ID;
  1379. ctrl->ch_status[2] = 0x00;
  1380. ctrl->ch_status[3] = IEC958_AES3_CON_FS_44100 |
  1381. IEC958_AES3_CON_CLOCK_1000PPM;
  1382. spdif_priv->dpll_locked = false;
  1383. spdif_priv->dma_params_tx.maxburst = spdif_priv->soc->tx_burst;
  1384. spdif_priv->dma_params_rx.maxburst = spdif_priv->soc->rx_burst;
  1385. spdif_priv->dma_params_tx.addr = res->start + REG_SPDIF_STL;
  1386. spdif_priv->dma_params_rx.addr = res->start + REG_SPDIF_SRL;
  1387. /* Register with ASoC */
  1388. dev_set_drvdata(&pdev->dev, spdif_priv);
  1389. pm_runtime_enable(&pdev->dev);
  1390. regcache_cache_only(spdif_priv->regmap, true);
  1391. /*
  1392. * Register platform component before registering cpu dai for there
  1393. * is not defer probe for platform component in snd_soc_add_pcm_runtime().
  1394. */
  1395. ret = imx_pcm_dma_init(pdev);
  1396. if (ret) {
  1397. dev_err_probe(&pdev->dev, ret, "imx_pcm_dma_init failed\n");
  1398. goto err_pm_disable;
  1399. }
  1400. ret = devm_snd_soc_register_component(&pdev->dev, &fsl_spdif_component,
  1401. &spdif_priv->cpu_dai_drv, 1);
  1402. if (ret) {
  1403. dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
  1404. goto err_pm_disable;
  1405. }
  1406. return ret;
  1407. err_pm_disable:
  1408. pm_runtime_disable(&pdev->dev);
  1409. return ret;
  1410. }
  1411. static void fsl_spdif_remove(struct platform_device *pdev)
  1412. {
  1413. pm_runtime_disable(&pdev->dev);
  1414. }
  1415. static int fsl_spdif_runtime_suspend(struct device *dev)
  1416. {
  1417. struct fsl_spdif_priv *spdif_priv = dev_get_drvdata(dev);
  1418. int i;
  1419. /* Disable all the interrupts */
  1420. regmap_update_bits(spdif_priv->regmap, REG_SPDIF_SIE, 0xffffff, 0);
  1421. regmap_read(spdif_priv->regmap, REG_SPDIF_SRPC,
  1422. &spdif_priv->regcache_srpc);
  1423. regcache_cache_only(spdif_priv->regmap, true);
  1424. for (i = 0; i < STC_TXCLK_SRC_MAX; i++)
  1425. clk_disable_unprepare(spdif_priv->txclk[i]);
  1426. if (!IS_ERR(spdif_priv->spbaclk))
  1427. clk_disable_unprepare(spdif_priv->spbaclk);
  1428. clk_disable_unprepare(spdif_priv->coreclk);
  1429. return 0;
  1430. }
  1431. static int fsl_spdif_runtime_resume(struct device *dev)
  1432. {
  1433. struct fsl_spdif_priv *spdif_priv = dev_get_drvdata(dev);
  1434. int ret;
  1435. int i;
  1436. ret = clk_prepare_enable(spdif_priv->coreclk);
  1437. if (ret) {
  1438. dev_err(dev, "failed to enable core clock\n");
  1439. return ret;
  1440. }
  1441. if (!IS_ERR(spdif_priv->spbaclk)) {
  1442. ret = clk_prepare_enable(spdif_priv->spbaclk);
  1443. if (ret) {
  1444. dev_err(dev, "failed to enable spba clock\n");
  1445. goto disable_core_clk;
  1446. }
  1447. }
  1448. for (i = 0; i < STC_TXCLK_SRC_MAX; i++) {
  1449. ret = clk_prepare_enable(spdif_priv->txclk[i]);
  1450. if (ret)
  1451. goto disable_tx_clk;
  1452. }
  1453. regcache_cache_only(spdif_priv->regmap, false);
  1454. regcache_mark_dirty(spdif_priv->regmap);
  1455. regmap_update_bits(spdif_priv->regmap, REG_SPDIF_SRPC,
  1456. SRPC_CLKSRC_SEL_MASK | SRPC_GAINSEL_MASK,
  1457. spdif_priv->regcache_srpc);
  1458. ret = regcache_sync(spdif_priv->regmap);
  1459. if (ret)
  1460. goto disable_tx_clk;
  1461. return 0;
  1462. disable_tx_clk:
  1463. for (i--; i >= 0; i--)
  1464. clk_disable_unprepare(spdif_priv->txclk[i]);
  1465. if (!IS_ERR(spdif_priv->spbaclk))
  1466. clk_disable_unprepare(spdif_priv->spbaclk);
  1467. disable_core_clk:
  1468. clk_disable_unprepare(spdif_priv->coreclk);
  1469. return ret;
  1470. }
  1471. static const struct dev_pm_ops fsl_spdif_pm = {
  1472. SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
  1473. RUNTIME_PM_OPS(fsl_spdif_runtime_suspend, fsl_spdif_runtime_resume,
  1474. NULL)
  1475. };
  1476. static const struct of_device_id fsl_spdif_dt_ids[] = {
  1477. { .compatible = "fsl,imx35-spdif", .data = &fsl_spdif_imx35, },
  1478. { .compatible = "fsl,vf610-spdif", .data = &fsl_spdif_vf610, },
  1479. { .compatible = "fsl,imx6sx-spdif", .data = &fsl_spdif_imx6sx, },
  1480. { .compatible = "fsl,imx8qm-spdif", .data = &fsl_spdif_imx8qm, },
  1481. { .compatible = "fsl,imx8mm-spdif", .data = &fsl_spdif_imx8mm, },
  1482. { .compatible = "fsl,imx8ulp-spdif", .data = &fsl_spdif_imx8ulp, },
  1483. {}
  1484. };
  1485. MODULE_DEVICE_TABLE(of, fsl_spdif_dt_ids);
  1486. static struct platform_driver fsl_spdif_driver = {
  1487. .driver = {
  1488. .name = "fsl-spdif-dai",
  1489. .of_match_table = fsl_spdif_dt_ids,
  1490. .pm = pm_ptr(&fsl_spdif_pm),
  1491. },
  1492. .probe = fsl_spdif_probe,
  1493. .remove = fsl_spdif_remove,
  1494. };
  1495. module_platform_driver(fsl_spdif_driver);
  1496. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1497. MODULE_DESCRIPTION("Freescale S/PDIF CPU DAI Driver");
  1498. MODULE_LICENSE("GPL v2");
  1499. MODULE_ALIAS("platform:fsl-spdif-dai");