fsl_sai.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Freescale ALSA SoC Digital Audio Interface (SAI) driver.
  4. //
  5. // Copyright 2012-2015 Freescale Semiconductor, Inc.
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/dmaengine.h>
  9. #include <linux/module.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_device.h>
  12. #include <linux/pinctrl/consumer.h>
  13. #include <linux/pm_qos.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regmap.h>
  16. #include <linux/slab.h>
  17. #include <linux/time.h>
  18. #include <sound/core.h>
  19. #include <sound/dmaengine_pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <linux/mfd/syscon.h>
  22. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  23. #include "fsl_sai.h"
  24. #include "fsl_utils.h"
  25. #include "imx-pcm.h"
  26. #define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\
  27. FSL_SAI_CSR_FEIE)
  28. static const unsigned int fsl_sai_rates[] = {
  29. 8000, 11025, 12000, 16000, 22050,
  30. 24000, 32000, 44100, 48000, 64000,
  31. 88200, 96000, 176400, 192000, 352800,
  32. 384000, 705600, 768000, 1411200, 2822400,
  33. };
  34. static const struct snd_pcm_hw_constraint_list fsl_sai_rate_constraints = {
  35. .count = ARRAY_SIZE(fsl_sai_rates),
  36. .list = fsl_sai_rates,
  37. };
  38. /**
  39. * fsl_sai_dir_is_synced - Check if stream is synced by the opposite stream
  40. *
  41. * SAI supports synchronous mode using bit/frame clocks of either Transmitter's
  42. * or Receiver's for both streams. This function is used to check if clocks of
  43. * the stream's are synced by the opposite stream.
  44. *
  45. * @sai: SAI context
  46. * @dir: stream direction
  47. */
  48. static inline bool fsl_sai_dir_is_synced(struct fsl_sai *sai, int dir)
  49. {
  50. int adir = (dir == TX) ? RX : TX;
  51. /* current dir in async mode while opposite dir in sync mode */
  52. return !sai->synchronous[dir] && sai->synchronous[adir];
  53. }
  54. static struct pinctrl_state *fsl_sai_get_pins_state(struct fsl_sai *sai, u32 bclk)
  55. {
  56. struct pinctrl_state *state = NULL;
  57. if (sai->is_pdm_mode) {
  58. /* [email protected], DSD512@48kHz */
  59. if (bclk >= 22579200)
  60. state = pinctrl_lookup_state(sai->pinctrl, "dsd512");
  61. /* Get default DSD state */
  62. if (IS_ERR_OR_NULL(state))
  63. state = pinctrl_lookup_state(sai->pinctrl, "dsd");
  64. } else {
  65. /* 706k32b2c, 768k32b2c, etc */
  66. if (bclk >= 45158400)
  67. state = pinctrl_lookup_state(sai->pinctrl, "pcm_b2m");
  68. }
  69. /* Get default state */
  70. if (IS_ERR_OR_NULL(state))
  71. state = pinctrl_lookup_state(sai->pinctrl, "default");
  72. return state;
  73. }
  74. static irqreturn_t fsl_sai_isr(int irq, void *devid)
  75. {
  76. struct fsl_sai *sai = (struct fsl_sai *)devid;
  77. unsigned int ofs = sai->soc_data->reg_offset;
  78. struct device *dev = &sai->pdev->dev;
  79. u32 flags, xcsr, mask;
  80. irqreturn_t iret = IRQ_NONE;
  81. /*
  82. * Both IRQ status bits and IRQ mask bits are in the xCSR but
  83. * different shifts. And we here create a mask only for those
  84. * IRQs that we activated.
  85. */
  86. mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT;
  87. /* Tx IRQ */
  88. regmap_read(sai->regmap, FSL_SAI_TCSR(ofs), &xcsr);
  89. flags = xcsr & mask;
  90. if (flags)
  91. iret = IRQ_HANDLED;
  92. else
  93. goto irq_rx;
  94. if (flags & FSL_SAI_CSR_WSF)
  95. dev_dbg(dev, "isr: Start of Tx word detected\n");
  96. if (flags & FSL_SAI_CSR_SEF)
  97. dev_dbg(dev, "isr: Tx Frame sync error detected\n");
  98. if (flags & FSL_SAI_CSR_FEF)
  99. dev_dbg(dev, "isr: Transmit underrun detected\n");
  100. if (flags & FSL_SAI_CSR_FWF)
  101. dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n");
  102. if (flags & FSL_SAI_CSR_FRF)
  103. dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n");
  104. flags &= FSL_SAI_CSR_xF_W_MASK;
  105. xcsr &= ~FSL_SAI_CSR_xF_MASK;
  106. if (flags)
  107. regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), flags | xcsr);
  108. irq_rx:
  109. /* Rx IRQ */
  110. regmap_read(sai->regmap, FSL_SAI_RCSR(ofs), &xcsr);
  111. flags = xcsr & mask;
  112. if (flags)
  113. iret = IRQ_HANDLED;
  114. else
  115. goto out;
  116. if (flags & FSL_SAI_CSR_WSF)
  117. dev_dbg(dev, "isr: Start of Rx word detected\n");
  118. if (flags & FSL_SAI_CSR_SEF)
  119. dev_dbg(dev, "isr: Rx Frame sync error detected\n");
  120. if (flags & FSL_SAI_CSR_FEF)
  121. dev_dbg(dev, "isr: Receive overflow detected\n");
  122. if (flags & FSL_SAI_CSR_FWF)
  123. dev_dbg(dev, "isr: Enabled receive FIFO is full\n");
  124. if (flags & FSL_SAI_CSR_FRF)
  125. dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n");
  126. flags &= FSL_SAI_CSR_xF_W_MASK;
  127. xcsr &= ~FSL_SAI_CSR_xF_MASK;
  128. if (flags)
  129. regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), flags | xcsr);
  130. out:
  131. return iret;
  132. }
  133. static int fsl_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
  134. u32 rx_mask, int slots, int slot_width)
  135. {
  136. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  137. sai->slots = slots;
  138. sai->slot_width = slot_width;
  139. return 0;
  140. }
  141. static int fsl_sai_set_dai_bclk_ratio(struct snd_soc_dai *dai,
  142. unsigned int ratio)
  143. {
  144. struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
  145. sai->bclk_ratio = ratio;
  146. return 0;
  147. }
  148. static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai,
  149. int clk_id, unsigned int freq, bool tx)
  150. {
  151. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  152. unsigned int ofs = sai->soc_data->reg_offset;
  153. u32 val_cr2 = 0;
  154. switch (clk_id) {
  155. case FSL_SAI_CLK_BUS:
  156. val_cr2 |= FSL_SAI_CR2_MSEL_BUS;
  157. break;
  158. case FSL_SAI_CLK_MAST1:
  159. val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1;
  160. break;
  161. case FSL_SAI_CLK_MAST2:
  162. val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2;
  163. break;
  164. case FSL_SAI_CLK_MAST3:
  165. val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3;
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
  171. FSL_SAI_CR2_MSEL_MASK, val_cr2);
  172. return 0;
  173. }
  174. static int fsl_sai_set_mclk_rate(struct snd_soc_dai *dai, int clk_id, unsigned int freq)
  175. {
  176. struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
  177. int ret;
  178. fsl_asoc_reparent_pll_clocks(dai->dev, sai->mclk_clk[clk_id],
  179. sai->pll8k_clk, sai->pll11k_clk, freq);
  180. ret = clk_set_rate(sai->mclk_clk[clk_id], freq);
  181. if (ret < 0)
  182. dev_err(dai->dev, "failed to set clock rate (%u): %d\n", freq, ret);
  183. return ret;
  184. }
  185. static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  186. int clk_id, unsigned int freq, int dir)
  187. {
  188. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  189. int ret;
  190. if (dir == SND_SOC_CLOCK_IN)
  191. return 0;
  192. if (freq > 0 && clk_id != FSL_SAI_CLK_BUS) {
  193. if (clk_id < 0 || clk_id >= FSL_SAI_MCLK_MAX) {
  194. dev_err(cpu_dai->dev, "Unknown clock id: %d\n", clk_id);
  195. return -EINVAL;
  196. }
  197. if (IS_ERR_OR_NULL(sai->mclk_clk[clk_id])) {
  198. dev_err(cpu_dai->dev, "Unassigned clock: %d\n", clk_id);
  199. return -EINVAL;
  200. }
  201. if (sai->mclk_streams == 0) {
  202. ret = fsl_sai_set_mclk_rate(cpu_dai, clk_id, freq);
  203. if (ret < 0)
  204. return ret;
  205. }
  206. }
  207. ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, true);
  208. if (ret) {
  209. dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret);
  210. return ret;
  211. }
  212. ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, false);
  213. if (ret)
  214. dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret);
  215. return ret;
  216. }
  217. static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai,
  218. unsigned int fmt, bool tx)
  219. {
  220. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  221. unsigned int ofs = sai->soc_data->reg_offset;
  222. u32 val_cr2 = 0, val_cr4 = 0;
  223. if (!sai->is_lsb_first)
  224. val_cr4 |= FSL_SAI_CR4_MF;
  225. sai->is_pdm_mode = false;
  226. sai->is_dsp_mode = false;
  227. /* DAI mode */
  228. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  229. case SND_SOC_DAIFMT_I2S:
  230. /*
  231. * Frame low, 1clk before data, one word length for frame sync,
  232. * frame sync starts one serial clock cycle earlier,
  233. * that is, together with the last bit of the previous
  234. * data word.
  235. */
  236. val_cr2 |= FSL_SAI_CR2_BCP;
  237. val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP;
  238. break;
  239. case SND_SOC_DAIFMT_LEFT_J:
  240. /*
  241. * Frame high, one word length for frame sync,
  242. * frame sync asserts with the first bit of the frame.
  243. */
  244. val_cr2 |= FSL_SAI_CR2_BCP;
  245. break;
  246. case SND_SOC_DAIFMT_DSP_A:
  247. /*
  248. * Frame high, 1clk before data, one bit for frame sync,
  249. * frame sync starts one serial clock cycle earlier,
  250. * that is, together with the last bit of the previous
  251. * data word.
  252. */
  253. val_cr2 |= FSL_SAI_CR2_BCP;
  254. val_cr4 |= FSL_SAI_CR4_FSE;
  255. sai->is_dsp_mode = true;
  256. break;
  257. case SND_SOC_DAIFMT_DSP_B:
  258. /*
  259. * Frame high, one bit for frame sync,
  260. * frame sync asserts with the first bit of the frame.
  261. */
  262. val_cr2 |= FSL_SAI_CR2_BCP;
  263. sai->is_dsp_mode = true;
  264. break;
  265. case SND_SOC_DAIFMT_PDM:
  266. val_cr2 |= FSL_SAI_CR2_BCP;
  267. val_cr4 &= ~FSL_SAI_CR4_MF;
  268. sai->is_pdm_mode = true;
  269. break;
  270. case SND_SOC_DAIFMT_RIGHT_J:
  271. /* To be done */
  272. default:
  273. return -EINVAL;
  274. }
  275. /* DAI clock inversion */
  276. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  277. case SND_SOC_DAIFMT_IB_IF:
  278. /* Invert both clocks */
  279. val_cr2 ^= FSL_SAI_CR2_BCP;
  280. val_cr4 ^= FSL_SAI_CR4_FSP;
  281. break;
  282. case SND_SOC_DAIFMT_IB_NF:
  283. /* Invert bit clock */
  284. val_cr2 ^= FSL_SAI_CR2_BCP;
  285. break;
  286. case SND_SOC_DAIFMT_NB_IF:
  287. /* Invert frame clock */
  288. val_cr4 ^= FSL_SAI_CR4_FSP;
  289. break;
  290. case SND_SOC_DAIFMT_NB_NF:
  291. /* Nothing to do for both normal cases */
  292. break;
  293. default:
  294. return -EINVAL;
  295. }
  296. /* DAI clock provider masks */
  297. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  298. case SND_SOC_DAIFMT_BP_FP:
  299. val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
  300. val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
  301. sai->is_consumer_mode = false;
  302. break;
  303. case SND_SOC_DAIFMT_BC_FC:
  304. sai->is_consumer_mode = true;
  305. break;
  306. case SND_SOC_DAIFMT_BP_FC:
  307. val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
  308. sai->is_consumer_mode = false;
  309. break;
  310. case SND_SOC_DAIFMT_BC_FP:
  311. val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
  312. sai->is_consumer_mode = true;
  313. break;
  314. default:
  315. return -EINVAL;
  316. }
  317. regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
  318. FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2);
  319. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
  320. FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE |
  321. FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4);
  322. return 0;
  323. }
  324. static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  325. {
  326. int ret;
  327. ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, true);
  328. if (ret) {
  329. dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
  330. return ret;
  331. }
  332. ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, false);
  333. if (ret)
  334. dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
  335. return ret;
  336. }
  337. static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
  338. {
  339. struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
  340. unsigned int reg, ofs = sai->soc_data->reg_offset;
  341. unsigned long clk_rate;
  342. u32 savediv = 0, ratio, bestdiff = freq;
  343. int adir = tx ? RX : TX;
  344. int dir = tx ? TX : RX;
  345. u32 id;
  346. bool support_1_1_ratio = sai->verid.version >= 0x0301;
  347. /* Don't apply to consumer mode */
  348. if (sai->is_consumer_mode)
  349. return 0;
  350. /*
  351. * There is no point in polling MCLK0 if it is identical to MCLK1.
  352. * And given that MQS use case has to use MCLK1 though two clocks
  353. * are the same, we simply skip MCLK0 and start to find from MCLK1.
  354. */
  355. id = sai->soc_data->mclk0_is_mclk1 ? 1 : 0;
  356. for (; id < FSL_SAI_MCLK_MAX; id++) {
  357. int diff;
  358. clk_rate = clk_get_rate(sai->mclk_clk[id]);
  359. if (!clk_rate)
  360. continue;
  361. ratio = DIV_ROUND_CLOSEST(clk_rate, freq);
  362. if (!ratio || ratio > 512)
  363. continue;
  364. if (ratio == 1 && !support_1_1_ratio)
  365. continue;
  366. if ((ratio & 1) && ratio > 1)
  367. continue;
  368. diff = abs((long)clk_rate - ratio * freq);
  369. /*
  370. * Drop the source that can not be
  371. * divided into the required rate.
  372. */
  373. if (diff != 0 && clk_rate / diff < 1000)
  374. continue;
  375. dev_dbg(dai->dev,
  376. "ratio %d for freq %dHz based on clock %ldHz\n",
  377. ratio, freq, clk_rate);
  378. if (diff < bestdiff) {
  379. savediv = ratio;
  380. sai->mclk_id[tx] = id;
  381. bestdiff = diff;
  382. }
  383. if (diff == 0)
  384. break;
  385. }
  386. if (savediv == 0) {
  387. dev_err(dai->dev, "failed to derive required %cx rate: %d\n",
  388. tx ? 'T' : 'R', freq);
  389. return -EINVAL;
  390. }
  391. dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n",
  392. sai->mclk_id[tx], savediv, bestdiff);
  393. /*
  394. * 1) For Asynchronous mode, we must set RCR2 register for capture, and
  395. * set TCR2 register for playback.
  396. * 2) For Tx sync with Rx clock, we must set RCR2 register for playback
  397. * and capture.
  398. * 3) For Rx sync with Tx clock, we must set TCR2 register for playback
  399. * and capture.
  400. * 4) For Tx and Rx are both Synchronous with another SAI, we just
  401. * ignore it.
  402. */
  403. if (fsl_sai_dir_is_synced(sai, adir))
  404. reg = FSL_SAI_xCR2(!tx, ofs);
  405. else if (!sai->synchronous[dir])
  406. reg = FSL_SAI_xCR2(tx, ofs);
  407. else
  408. return 0;
  409. regmap_update_bits(sai->regmap, reg, FSL_SAI_CR2_MSEL_MASK,
  410. FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
  411. if (savediv == 1) {
  412. regmap_update_bits(sai->regmap, reg,
  413. FSL_SAI_CR2_DIV_MASK | FSL_SAI_CR2_BYP,
  414. FSL_SAI_CR2_BYP);
  415. if (fsl_sai_dir_is_synced(sai, adir))
  416. regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
  417. FSL_SAI_CR2_BCI, FSL_SAI_CR2_BCI);
  418. else
  419. regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
  420. FSL_SAI_CR2_BCI, 0);
  421. } else {
  422. regmap_update_bits(sai->regmap, reg,
  423. FSL_SAI_CR2_DIV_MASK | FSL_SAI_CR2_BYP,
  424. savediv / 2 - 1);
  425. }
  426. return 0;
  427. }
  428. static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
  429. struct snd_pcm_hw_params *params,
  430. struct snd_soc_dai *cpu_dai)
  431. {
  432. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  433. unsigned int ofs = sai->soc_data->reg_offset;
  434. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  435. unsigned int channels = params_channels(params);
  436. struct snd_dmaengine_dai_dma_data *dma_params;
  437. struct fsl_sai_dl_cfg *dl_cfg = sai->dl_cfg;
  438. u32 word_width = params_width(params);
  439. int trce_mask = 0, dl_cfg_idx = 0;
  440. int dl_cfg_cnt = sai->dl_cfg_cnt;
  441. u32 dl_type = FSL_SAI_DL_I2S;
  442. u32 val_cr4 = 0, val_cr5 = 0;
  443. u32 slots = (channels == 1) ? 2 : channels;
  444. u32 slot_width = word_width;
  445. int adir = tx ? RX : TX;
  446. u32 pins, bclk;
  447. u32 watermark;
  448. int ret, i;
  449. if (sai->slot_width)
  450. slot_width = sai->slot_width;
  451. if (sai->slots)
  452. slots = sai->slots;
  453. else if (sai->bclk_ratio)
  454. slots = sai->bclk_ratio / slot_width;
  455. pins = DIV_ROUND_UP(channels, slots);
  456. /*
  457. * PDM mode, channels are independent
  458. * each channels are on one dataline/FIFO.
  459. */
  460. if (sai->is_pdm_mode) {
  461. pins = channels;
  462. dl_type = FSL_SAI_DL_PDM;
  463. }
  464. for (i = 0; i < dl_cfg_cnt; i++) {
  465. if (dl_cfg[i].type == dl_type && dl_cfg[i].pins[tx] == pins) {
  466. dl_cfg_idx = i;
  467. break;
  468. }
  469. }
  470. if (hweight8(dl_cfg[dl_cfg_idx].mask[tx]) < pins) {
  471. dev_err(cpu_dai->dev, "channel not supported\n");
  472. return -EINVAL;
  473. }
  474. bclk = params_rate(params) * (sai->bclk_ratio ? sai->bclk_ratio : slots * slot_width);
  475. if (!IS_ERR_OR_NULL(sai->pinctrl)) {
  476. sai->pins_state = fsl_sai_get_pins_state(sai, bclk);
  477. if (!IS_ERR_OR_NULL(sai->pins_state)) {
  478. ret = pinctrl_select_state(sai->pinctrl, sai->pins_state);
  479. if (ret) {
  480. dev_err(cpu_dai->dev, "failed to set proper pins state: %d\n", ret);
  481. return ret;
  482. }
  483. }
  484. }
  485. if (!sai->is_consumer_mode) {
  486. ret = fsl_sai_set_bclk(cpu_dai, tx, bclk);
  487. if (ret)
  488. return ret;
  489. /* Do not enable the clock if it is already enabled */
  490. if (!(sai->mclk_streams & BIT(substream->stream))) {
  491. ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]);
  492. if (ret)
  493. return ret;
  494. sai->mclk_streams |= BIT(substream->stream);
  495. }
  496. }
  497. if (!sai->is_dsp_mode && !sai->is_pdm_mode)
  498. val_cr4 |= FSL_SAI_CR4_SYWD(slot_width);
  499. val_cr5 |= FSL_SAI_CR5_WNW(slot_width);
  500. val_cr5 |= FSL_SAI_CR5_W0W(slot_width);
  501. if (sai->is_lsb_first || sai->is_pdm_mode)
  502. val_cr5 |= FSL_SAI_CR5_FBT(0);
  503. else
  504. val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
  505. val_cr4 |= FSL_SAI_CR4_FRSZ(slots);
  506. /* Set to output mode to avoid tri-stated data pins */
  507. if (tx)
  508. val_cr4 |= FSL_SAI_CR4_CHMOD;
  509. /*
  510. * For SAI provider mode, when Tx(Rx) sync with Rx(Tx) clock, Rx(Tx) will
  511. * generate bclk and frame clock for Tx(Rx), we should set RCR4(TCR4),
  512. * RCR5(TCR5) for playback(capture), or there will be sync error.
  513. */
  514. if (!sai->is_consumer_mode && fsl_sai_dir_is_synced(sai, adir)) {
  515. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(!tx, ofs),
  516. FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
  517. FSL_SAI_CR4_CHMOD_MASK,
  518. val_cr4);
  519. regmap_update_bits(sai->regmap, FSL_SAI_xCR5(!tx, ofs),
  520. FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
  521. FSL_SAI_CR5_FBT_MASK, val_cr5);
  522. }
  523. /*
  524. * Combine mode has limation:
  525. * - Can't used for singel dataline/FIFO case except the FIFO0
  526. * - Can't used for multi dataline/FIFO case except the enabled FIFOs
  527. * are successive and start from FIFO0
  528. *
  529. * So for common usage, all multi fifo case disable the combine mode.
  530. */
  531. if (hweight8(dl_cfg[dl_cfg_idx].mask[tx]) <= 1 || sai->is_multi_fifo_dma)
  532. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
  533. FSL_SAI_CR4_FCOMB_MASK, 0);
  534. else
  535. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
  536. FSL_SAI_CR4_FCOMB_MASK, FSL_SAI_CR4_FCOMB_SOFT);
  537. dma_params = tx ? &sai->dma_params_tx : &sai->dma_params_rx;
  538. dma_params->addr = sai->res->start + FSL_SAI_xDR0(tx) +
  539. dl_cfg[dl_cfg_idx].start_off[tx] * 0x4;
  540. if (sai->is_multi_fifo_dma) {
  541. sai->audio_config[tx].words_per_fifo = min(slots, channels);
  542. if (tx) {
  543. sai->audio_config[tx].n_fifos_dst = pins;
  544. sai->audio_config[tx].stride_fifos_dst = dl_cfg[dl_cfg_idx].next_off[tx];
  545. } else {
  546. sai->audio_config[tx].n_fifos_src = pins;
  547. sai->audio_config[tx].stride_fifos_src = dl_cfg[dl_cfg_idx].next_off[tx];
  548. }
  549. dma_params->maxburst = sai->audio_config[tx].words_per_fifo * pins;
  550. dma_params->peripheral_config = &sai->audio_config[tx];
  551. dma_params->peripheral_size = sizeof(sai->audio_config[tx]);
  552. watermark = tx ? (sai->soc_data->fifo_depth - dma_params->maxburst) :
  553. (dma_params->maxburst - 1);
  554. regmap_update_bits(sai->regmap, FSL_SAI_xCR1(tx, ofs),
  555. FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
  556. watermark);
  557. }
  558. /* Find a proper tcre setting */
  559. for (i = 0; i < sai->soc_data->pins; i++) {
  560. trce_mask = (1 << (i + 1)) - 1;
  561. if (hweight8(dl_cfg[dl_cfg_idx].mask[tx] & trce_mask) == pins)
  562. break;
  563. }
  564. regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
  565. FSL_SAI_CR3_TRCE_MASK,
  566. FSL_SAI_CR3_TRCE((dl_cfg[dl_cfg_idx].mask[tx] & trce_mask)));
  567. /*
  568. * When the TERE and FSD_MSTR enabled before configuring the word width
  569. * There will be no frame sync clock issue, because word width impact
  570. * the generation of frame sync clock.
  571. *
  572. * TERE enabled earlier only for i.MX8MP case for the hardware limitation,
  573. * We need to disable FSD_MSTR before configuring word width, then enable
  574. * FSD_MSTR bit for this specific case.
  575. */
  576. if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output &&
  577. !sai->is_consumer_mode)
  578. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
  579. FSL_SAI_CR4_FSD_MSTR, 0);
  580. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
  581. FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
  582. FSL_SAI_CR4_CHMOD_MASK,
  583. val_cr4);
  584. regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx, ofs),
  585. FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
  586. FSL_SAI_CR5_FBT_MASK, val_cr5);
  587. /* Enable FSD_MSTR after configuring word width */
  588. if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output &&
  589. !sai->is_consumer_mode)
  590. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
  591. FSL_SAI_CR4_FSD_MSTR, FSL_SAI_CR4_FSD_MSTR);
  592. regmap_write(sai->regmap, FSL_SAI_xMR(tx),
  593. ~0UL - ((1 << min(channels, slots)) - 1));
  594. return 0;
  595. }
  596. static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
  597. struct snd_soc_dai *cpu_dai)
  598. {
  599. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  600. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  601. unsigned int ofs = sai->soc_data->reg_offset;
  602. regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
  603. FSL_SAI_CR3_TRCE_MASK, 0);
  604. if (!sai->is_consumer_mode &&
  605. sai->mclk_streams & BIT(substream->stream)) {
  606. clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]);
  607. sai->mclk_streams &= ~BIT(substream->stream);
  608. }
  609. return 0;
  610. }
  611. static void fsl_sai_config_disable(struct fsl_sai *sai, int dir)
  612. {
  613. unsigned int ofs = sai->soc_data->reg_offset;
  614. bool tx = dir == TX;
  615. u32 xcsr, count = 100, mask;
  616. if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output)
  617. mask = FSL_SAI_CSR_TERE;
  618. else
  619. mask = FSL_SAI_CSR_TERE | FSL_SAI_CSR_BCE;
  620. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
  621. mask, 0);
  622. /* TERE will remain set till the end of current frame */
  623. do {
  624. udelay(10);
  625. regmap_read(sai->regmap, FSL_SAI_xCSR(tx, ofs), &xcsr);
  626. } while (--count && xcsr & FSL_SAI_CSR_TERE);
  627. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
  628. FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
  629. /*
  630. * For sai master mode, after several open/close sai,
  631. * there will be no frame clock, and can't recover
  632. * anymore. Add software reset to fix this issue.
  633. * This is a hardware bug, and will be fix in the
  634. * next sai version.
  635. */
  636. if (!sai->is_consumer_mode) {
  637. /* Software Reset */
  638. regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), FSL_SAI_CSR_SR);
  639. /* Clear SR bit to finish the reset */
  640. regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), 0);
  641. }
  642. }
  643. static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
  644. struct snd_soc_dai *cpu_dai)
  645. {
  646. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  647. unsigned int ofs = sai->soc_data->reg_offset;
  648. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  649. int adir = tx ? RX : TX;
  650. int dir = tx ? TX : RX;
  651. u32 xcsr;
  652. /*
  653. * Asynchronous mode: Clear SYNC for both Tx and Rx.
  654. * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
  655. * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx.
  656. */
  657. regmap_update_bits(sai->regmap, FSL_SAI_TCR2(ofs), FSL_SAI_CR2_SYNC,
  658. sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0);
  659. regmap_update_bits(sai->regmap, FSL_SAI_RCR2(ofs), FSL_SAI_CR2_SYNC,
  660. sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0);
  661. /*
  662. * It is recommended that the transmitter is the last enabled
  663. * and the first disabled.
  664. */
  665. switch (cmd) {
  666. case SNDRV_PCM_TRIGGER_START:
  667. case SNDRV_PCM_TRIGGER_RESUME:
  668. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  669. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
  670. FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
  671. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
  672. FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
  673. /*
  674. * Enable the opposite direction for synchronous mode
  675. * 1. Tx sync with Rx: only set RE for Rx; set TE & RE for Tx
  676. * 2. Rx sync with Tx: only set TE for Tx; set RE & TE for Rx
  677. *
  678. * RM recommends to enable RE after TE for case 1 and to enable
  679. * TE after RE for case 2, but we here may not always guarantee
  680. * that happens: "arecord 1.wav; aplay 2.wav" in case 1 enables
  681. * TE after RE, which is against what RM recommends but should
  682. * be safe to do, judging by years of testing results.
  683. */
  684. if (fsl_sai_dir_is_synced(sai, adir))
  685. regmap_update_bits(sai->regmap, FSL_SAI_xCSR((!tx), ofs),
  686. FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
  687. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
  688. FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
  689. break;
  690. case SNDRV_PCM_TRIGGER_STOP:
  691. case SNDRV_PCM_TRIGGER_SUSPEND:
  692. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  693. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
  694. FSL_SAI_CSR_FRDE, 0);
  695. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
  696. FSL_SAI_CSR_xIE_MASK, 0);
  697. /* Check if the opposite FRDE is also disabled */
  698. regmap_read(sai->regmap, FSL_SAI_xCSR(!tx, ofs), &xcsr);
  699. /*
  700. * If opposite stream provides clocks for synchronous mode and
  701. * it is inactive, disable it before disabling the current one
  702. */
  703. if (fsl_sai_dir_is_synced(sai, adir) && !(xcsr & FSL_SAI_CSR_FRDE))
  704. fsl_sai_config_disable(sai, adir);
  705. /*
  706. * Disable current stream if either of:
  707. * 1. current stream doesn't provide clocks for synchronous mode
  708. * 2. current stream provides clocks for synchronous mode but no
  709. * more stream is active.
  710. */
  711. if (!fsl_sai_dir_is_synced(sai, dir) || !(xcsr & FSL_SAI_CSR_FRDE))
  712. fsl_sai_config_disable(sai, dir);
  713. break;
  714. default:
  715. return -EINVAL;
  716. }
  717. return 0;
  718. }
  719. static int fsl_sai_startup(struct snd_pcm_substream *substream,
  720. struct snd_soc_dai *cpu_dai)
  721. {
  722. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  723. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  724. int ret;
  725. /*
  726. * EDMA controller needs period size to be a multiple of
  727. * tx/rx maxburst
  728. */
  729. if (sai->soc_data->use_edma)
  730. snd_pcm_hw_constraint_step(substream->runtime, 0,
  731. SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  732. tx ? sai->dma_params_tx.maxburst :
  733. sai->dma_params_rx.maxburst);
  734. ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
  735. SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints);
  736. return ret;
  737. }
  738. static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
  739. .set_bclk_ratio = fsl_sai_set_dai_bclk_ratio,
  740. .set_sysclk = fsl_sai_set_dai_sysclk,
  741. .set_fmt = fsl_sai_set_dai_fmt,
  742. .set_tdm_slot = fsl_sai_set_dai_tdm_slot,
  743. .hw_params = fsl_sai_hw_params,
  744. .hw_free = fsl_sai_hw_free,
  745. .trigger = fsl_sai_trigger,
  746. .startup = fsl_sai_startup,
  747. };
  748. static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
  749. {
  750. struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
  751. unsigned int ofs = sai->soc_data->reg_offset;
  752. /* Software Reset for both Tx and Rx */
  753. regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR);
  754. regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR);
  755. /* Clear SR bit to finish the reset */
  756. regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0);
  757. regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0);
  758. regmap_update_bits(sai->regmap, FSL_SAI_TCR1(ofs),
  759. FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
  760. sai->soc_data->fifo_depth - FSL_SAI_MAXBURST_TX);
  761. regmap_update_bits(sai->regmap, FSL_SAI_RCR1(ofs),
  762. FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
  763. FSL_SAI_MAXBURST_RX - 1);
  764. snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
  765. &sai->dma_params_rx);
  766. return 0;
  767. }
  768. static int fsl_sai_dai_resume(struct snd_soc_component *component)
  769. {
  770. struct fsl_sai *sai = snd_soc_component_get_drvdata(component);
  771. struct device *dev = &sai->pdev->dev;
  772. int ret;
  773. if (!IS_ERR_OR_NULL(sai->pinctrl) && !IS_ERR_OR_NULL(sai->pins_state)) {
  774. ret = pinctrl_select_state(sai->pinctrl, sai->pins_state);
  775. if (ret) {
  776. dev_err(dev, "failed to set proper pins state: %d\n", ret);
  777. return ret;
  778. }
  779. }
  780. return 0;
  781. }
  782. static struct snd_soc_dai_driver fsl_sai_dai_template = {
  783. .probe = fsl_sai_dai_probe,
  784. .playback = {
  785. .stream_name = "CPU-Playback",
  786. .channels_min = 1,
  787. .channels_max = 32,
  788. .rate_min = 8000,
  789. .rate_max = 2822400,
  790. .rates = SNDRV_PCM_RATE_KNOT,
  791. .formats = FSL_SAI_FORMATS,
  792. },
  793. .capture = {
  794. .stream_name = "CPU-Capture",
  795. .channels_min = 1,
  796. .channels_max = 32,
  797. .rate_min = 8000,
  798. .rate_max = 2822400,
  799. .rates = SNDRV_PCM_RATE_KNOT,
  800. .formats = FSL_SAI_FORMATS,
  801. },
  802. .ops = &fsl_sai_pcm_dai_ops,
  803. };
  804. static const struct snd_soc_component_driver fsl_component = {
  805. .name = "fsl-sai",
  806. .resume = fsl_sai_dai_resume,
  807. .legacy_dai_naming = 1,
  808. };
  809. static struct reg_default fsl_sai_reg_defaults_ofs0[] = {
  810. {FSL_SAI_TCR1(0), 0},
  811. {FSL_SAI_TCR2(0), 0},
  812. {FSL_SAI_TCR3(0), 0},
  813. {FSL_SAI_TCR4(0), 0},
  814. {FSL_SAI_TCR5(0), 0},
  815. {FSL_SAI_TDR0, 0},
  816. {FSL_SAI_TDR1, 0},
  817. {FSL_SAI_TDR2, 0},
  818. {FSL_SAI_TDR3, 0},
  819. {FSL_SAI_TDR4, 0},
  820. {FSL_SAI_TDR5, 0},
  821. {FSL_SAI_TDR6, 0},
  822. {FSL_SAI_TDR7, 0},
  823. {FSL_SAI_TMR, 0},
  824. {FSL_SAI_RCR1(0), 0},
  825. {FSL_SAI_RCR2(0), 0},
  826. {FSL_SAI_RCR3(0), 0},
  827. {FSL_SAI_RCR4(0), 0},
  828. {FSL_SAI_RCR5(0), 0},
  829. {FSL_SAI_RMR, 0},
  830. };
  831. static struct reg_default fsl_sai_reg_defaults_ofs8[] = {
  832. {FSL_SAI_TCR1(8), 0},
  833. {FSL_SAI_TCR2(8), 0},
  834. {FSL_SAI_TCR3(8), 0},
  835. {FSL_SAI_TCR4(8), 0},
  836. {FSL_SAI_TCR5(8), 0},
  837. {FSL_SAI_TDR0, 0},
  838. {FSL_SAI_TDR1, 0},
  839. {FSL_SAI_TDR2, 0},
  840. {FSL_SAI_TDR3, 0},
  841. {FSL_SAI_TDR4, 0},
  842. {FSL_SAI_TDR5, 0},
  843. {FSL_SAI_TDR6, 0},
  844. {FSL_SAI_TDR7, 0},
  845. {FSL_SAI_TMR, 0},
  846. {FSL_SAI_RCR1(8), 0},
  847. {FSL_SAI_RCR2(8), 0},
  848. {FSL_SAI_RCR3(8), 0},
  849. {FSL_SAI_RCR4(8), 0},
  850. {FSL_SAI_RCR5(8), 0},
  851. {FSL_SAI_RMR, 0},
  852. {FSL_SAI_MCTL, 0},
  853. {FSL_SAI_MDIV, 0},
  854. };
  855. static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
  856. {
  857. struct fsl_sai *sai = dev_get_drvdata(dev);
  858. unsigned int ofs = sai->soc_data->reg_offset;
  859. if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
  860. return true;
  861. if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
  862. return true;
  863. switch (reg) {
  864. case FSL_SAI_TFR0:
  865. case FSL_SAI_TFR1:
  866. case FSL_SAI_TFR2:
  867. case FSL_SAI_TFR3:
  868. case FSL_SAI_TFR4:
  869. case FSL_SAI_TFR5:
  870. case FSL_SAI_TFR6:
  871. case FSL_SAI_TFR7:
  872. case FSL_SAI_TMR:
  873. case FSL_SAI_RDR0:
  874. case FSL_SAI_RDR1:
  875. case FSL_SAI_RDR2:
  876. case FSL_SAI_RDR3:
  877. case FSL_SAI_RDR4:
  878. case FSL_SAI_RDR5:
  879. case FSL_SAI_RDR6:
  880. case FSL_SAI_RDR7:
  881. case FSL_SAI_RFR0:
  882. case FSL_SAI_RFR1:
  883. case FSL_SAI_RFR2:
  884. case FSL_SAI_RFR3:
  885. case FSL_SAI_RFR4:
  886. case FSL_SAI_RFR5:
  887. case FSL_SAI_RFR6:
  888. case FSL_SAI_RFR7:
  889. case FSL_SAI_RMR:
  890. case FSL_SAI_MCTL:
  891. case FSL_SAI_MDIV:
  892. case FSL_SAI_VERID:
  893. case FSL_SAI_PARAM:
  894. case FSL_SAI_TTCTN:
  895. case FSL_SAI_RTCTN:
  896. case FSL_SAI_TTCTL:
  897. case FSL_SAI_TBCTN:
  898. case FSL_SAI_TTCAP:
  899. case FSL_SAI_RTCTL:
  900. case FSL_SAI_RBCTN:
  901. case FSL_SAI_RTCAP:
  902. return true;
  903. default:
  904. return false;
  905. }
  906. }
  907. static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
  908. {
  909. struct fsl_sai *sai = dev_get_drvdata(dev);
  910. unsigned int ofs = sai->soc_data->reg_offset;
  911. if (reg == FSL_SAI_TCSR(ofs) || reg == FSL_SAI_RCSR(ofs))
  912. return true;
  913. /* Set VERID and PARAM be volatile for reading value in probe */
  914. if (ofs == 8 && (reg == FSL_SAI_VERID || reg == FSL_SAI_PARAM))
  915. return true;
  916. switch (reg) {
  917. case FSL_SAI_TFR0:
  918. case FSL_SAI_TFR1:
  919. case FSL_SAI_TFR2:
  920. case FSL_SAI_TFR3:
  921. case FSL_SAI_TFR4:
  922. case FSL_SAI_TFR5:
  923. case FSL_SAI_TFR6:
  924. case FSL_SAI_TFR7:
  925. case FSL_SAI_RFR0:
  926. case FSL_SAI_RFR1:
  927. case FSL_SAI_RFR2:
  928. case FSL_SAI_RFR3:
  929. case FSL_SAI_RFR4:
  930. case FSL_SAI_RFR5:
  931. case FSL_SAI_RFR6:
  932. case FSL_SAI_RFR7:
  933. case FSL_SAI_RDR0:
  934. case FSL_SAI_RDR1:
  935. case FSL_SAI_RDR2:
  936. case FSL_SAI_RDR3:
  937. case FSL_SAI_RDR4:
  938. case FSL_SAI_RDR5:
  939. case FSL_SAI_RDR6:
  940. case FSL_SAI_RDR7:
  941. return true;
  942. default:
  943. return false;
  944. }
  945. }
  946. static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
  947. {
  948. struct fsl_sai *sai = dev_get_drvdata(dev);
  949. unsigned int ofs = sai->soc_data->reg_offset;
  950. if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
  951. return true;
  952. if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
  953. return true;
  954. switch (reg) {
  955. case FSL_SAI_TDR0:
  956. case FSL_SAI_TDR1:
  957. case FSL_SAI_TDR2:
  958. case FSL_SAI_TDR3:
  959. case FSL_SAI_TDR4:
  960. case FSL_SAI_TDR5:
  961. case FSL_SAI_TDR6:
  962. case FSL_SAI_TDR7:
  963. case FSL_SAI_TMR:
  964. case FSL_SAI_RMR:
  965. case FSL_SAI_MCTL:
  966. case FSL_SAI_MDIV:
  967. case FSL_SAI_TTCTL:
  968. case FSL_SAI_RTCTL:
  969. return true;
  970. default:
  971. return false;
  972. }
  973. }
  974. static struct regmap_config fsl_sai_regmap_config = {
  975. .reg_bits = 32,
  976. .reg_stride = 4,
  977. .val_bits = 32,
  978. .fast_io = true,
  979. .max_register = FSL_SAI_RMR,
  980. .reg_defaults = fsl_sai_reg_defaults_ofs0,
  981. .num_reg_defaults = ARRAY_SIZE(fsl_sai_reg_defaults_ofs0),
  982. .readable_reg = fsl_sai_readable_reg,
  983. .volatile_reg = fsl_sai_volatile_reg,
  984. .writeable_reg = fsl_sai_writeable_reg,
  985. .cache_type = REGCACHE_FLAT,
  986. };
  987. static int fsl_sai_check_version(struct device *dev)
  988. {
  989. struct fsl_sai *sai = dev_get_drvdata(dev);
  990. unsigned char ofs = sai->soc_data->reg_offset;
  991. unsigned int val;
  992. int ret;
  993. if (FSL_SAI_TCSR(ofs) == FSL_SAI_VERID)
  994. return 0;
  995. ret = regmap_read(sai->regmap, FSL_SAI_VERID, &val);
  996. if (ret < 0)
  997. return ret;
  998. dev_dbg(dev, "VERID: 0x%016X\n", val);
  999. sai->verid.version = val &
  1000. (FSL_SAI_VERID_MAJOR_MASK | FSL_SAI_VERID_MINOR_MASK);
  1001. sai->verid.version >>= FSL_SAI_VERID_MINOR_SHIFT;
  1002. sai->verid.feature = val & FSL_SAI_VERID_FEATURE_MASK;
  1003. ret = regmap_read(sai->regmap, FSL_SAI_PARAM, &val);
  1004. if (ret < 0)
  1005. return ret;
  1006. dev_dbg(dev, "PARAM: 0x%016X\n", val);
  1007. /* Max slots per frame, power of 2 */
  1008. sai->param.slot_num = 1 <<
  1009. ((val & FSL_SAI_PARAM_SPF_MASK) >> FSL_SAI_PARAM_SPF_SHIFT);
  1010. /* Words per fifo, power of 2 */
  1011. sai->param.fifo_depth = 1 <<
  1012. ((val & FSL_SAI_PARAM_WPF_MASK) >> FSL_SAI_PARAM_WPF_SHIFT);
  1013. /* Number of datalines implemented */
  1014. sai->param.dataline = val & FSL_SAI_PARAM_DLN_MASK;
  1015. return 0;
  1016. }
  1017. /*
  1018. * Calculate the offset between first two datalines, don't
  1019. * different offset in one case.
  1020. */
  1021. static unsigned int fsl_sai_calc_dl_off(unsigned long dl_mask)
  1022. {
  1023. int fbidx, nbidx, offset;
  1024. fbidx = find_first_bit(&dl_mask, FSL_SAI_DL_NUM);
  1025. nbidx = find_next_bit(&dl_mask, FSL_SAI_DL_NUM, fbidx + 1);
  1026. offset = nbidx - fbidx - 1;
  1027. return (offset < 0 || offset >= (FSL_SAI_DL_NUM - 1) ? 0 : offset);
  1028. }
  1029. /*
  1030. * read the fsl,dataline property from dts file.
  1031. * It has 3 value for each configuration, first one means the type:
  1032. * I2S(1) or PDM(2), second one is dataline mask for 'rx', third one is
  1033. * dataline mask for 'tx'. for example
  1034. *
  1035. * fsl,dataline = <1 0xff 0xff 2 0xff 0x11>,
  1036. *
  1037. * It means I2S type rx mask is 0xff, tx mask is 0xff, PDM type
  1038. * rx mask is 0xff, tx mask is 0x11 (dataline 1 and 4 enabled).
  1039. *
  1040. */
  1041. static int fsl_sai_read_dlcfg(struct fsl_sai *sai)
  1042. {
  1043. struct platform_device *pdev = sai->pdev;
  1044. struct device_node *np = pdev->dev.of_node;
  1045. struct device *dev = &pdev->dev;
  1046. int ret, elems, i, index, num_cfg;
  1047. char *propname = "fsl,dataline";
  1048. struct fsl_sai_dl_cfg *cfg;
  1049. unsigned long dl_mask;
  1050. unsigned int soc_dl;
  1051. u32 rx, tx, type;
  1052. elems = of_property_count_u32_elems(np, propname);
  1053. if (elems <= 0) {
  1054. elems = 0;
  1055. } else if (elems % 3) {
  1056. dev_err(dev, "Number of elements must be divisible to 3.\n");
  1057. return -EINVAL;
  1058. }
  1059. num_cfg = elems / 3;
  1060. /* Add one more for default value */
  1061. cfg = devm_kzalloc(&pdev->dev, (num_cfg + 1) * sizeof(*cfg), GFP_KERNEL);
  1062. if (!cfg)
  1063. return -ENOMEM;
  1064. /* Consider default value "0 0xFF 0xFF" if property is missing */
  1065. soc_dl = BIT(sai->soc_data->pins) - 1;
  1066. cfg[0].type = FSL_SAI_DL_DEFAULT;
  1067. cfg[0].pins[0] = sai->soc_data->pins;
  1068. cfg[0].mask[0] = soc_dl;
  1069. cfg[0].start_off[0] = 0;
  1070. cfg[0].next_off[0] = 0;
  1071. cfg[0].pins[1] = sai->soc_data->pins;
  1072. cfg[0].mask[1] = soc_dl;
  1073. cfg[0].start_off[1] = 0;
  1074. cfg[0].next_off[1] = 0;
  1075. for (i = 1, index = 0; i < num_cfg + 1; i++) {
  1076. /*
  1077. * type of dataline
  1078. * 0 means default mode
  1079. * 1 means I2S mode
  1080. * 2 means PDM mode
  1081. */
  1082. ret = of_property_read_u32_index(np, propname, index++, &type);
  1083. if (ret)
  1084. return -EINVAL;
  1085. ret = of_property_read_u32_index(np, propname, index++, &rx);
  1086. if (ret)
  1087. return -EINVAL;
  1088. ret = of_property_read_u32_index(np, propname, index++, &tx);
  1089. if (ret)
  1090. return -EINVAL;
  1091. if ((rx & ~soc_dl) || (tx & ~soc_dl)) {
  1092. dev_err(dev, "dataline cfg[%d] setting error, mask is 0x%x\n", i, soc_dl);
  1093. return -EINVAL;
  1094. }
  1095. rx = rx & soc_dl;
  1096. tx = tx & soc_dl;
  1097. cfg[i].type = type;
  1098. cfg[i].pins[0] = hweight8(rx);
  1099. cfg[i].mask[0] = rx;
  1100. dl_mask = rx;
  1101. cfg[i].start_off[0] = find_first_bit(&dl_mask, FSL_SAI_DL_NUM);
  1102. cfg[i].next_off[0] = fsl_sai_calc_dl_off(rx);
  1103. cfg[i].pins[1] = hweight8(tx);
  1104. cfg[i].mask[1] = tx;
  1105. dl_mask = tx;
  1106. cfg[i].start_off[1] = find_first_bit(&dl_mask, FSL_SAI_DL_NUM);
  1107. cfg[i].next_off[1] = fsl_sai_calc_dl_off(tx);
  1108. }
  1109. sai->dl_cfg = cfg;
  1110. sai->dl_cfg_cnt = num_cfg + 1;
  1111. return 0;
  1112. }
  1113. static int fsl_sai_runtime_suspend(struct device *dev);
  1114. static int fsl_sai_runtime_resume(struct device *dev);
  1115. static int fsl_sai_probe(struct platform_device *pdev)
  1116. {
  1117. struct device_node *np = pdev->dev.of_node;
  1118. struct device *dev = &pdev->dev;
  1119. struct fsl_sai *sai;
  1120. struct regmap *gpr;
  1121. void __iomem *base;
  1122. char tmp[8];
  1123. int irq, ret, i;
  1124. int index;
  1125. u32 dmas[4];
  1126. sai = devm_kzalloc(dev, sizeof(*sai), GFP_KERNEL);
  1127. if (!sai)
  1128. return -ENOMEM;
  1129. sai->pdev = pdev;
  1130. sai->soc_data = of_device_get_match_data(dev);
  1131. sai->is_lsb_first = of_property_read_bool(np, "lsb-first");
  1132. base = devm_platform_get_and_ioremap_resource(pdev, 0, &sai->res);
  1133. if (IS_ERR(base))
  1134. return PTR_ERR(base);
  1135. if (sai->soc_data->reg_offset == 8) {
  1136. fsl_sai_regmap_config.reg_defaults = fsl_sai_reg_defaults_ofs8;
  1137. fsl_sai_regmap_config.max_register = FSL_SAI_MDIV;
  1138. fsl_sai_regmap_config.num_reg_defaults =
  1139. ARRAY_SIZE(fsl_sai_reg_defaults_ofs8);
  1140. }
  1141. sai->regmap = devm_regmap_init_mmio(dev, base, &fsl_sai_regmap_config);
  1142. if (IS_ERR(sai->regmap)) {
  1143. dev_err(dev, "regmap init failed\n");
  1144. return PTR_ERR(sai->regmap);
  1145. }
  1146. sai->bus_clk = devm_clk_get(dev, "bus");
  1147. /* Compatible with old DTB cases */
  1148. if (IS_ERR(sai->bus_clk) && PTR_ERR(sai->bus_clk) != -EPROBE_DEFER)
  1149. sai->bus_clk = devm_clk_get(dev, "sai");
  1150. if (IS_ERR(sai->bus_clk)) {
  1151. dev_err(dev, "failed to get bus clock: %ld\n",
  1152. PTR_ERR(sai->bus_clk));
  1153. /* -EPROBE_DEFER */
  1154. return PTR_ERR(sai->bus_clk);
  1155. }
  1156. for (i = 1; i < FSL_SAI_MCLK_MAX; i++) {
  1157. sprintf(tmp, "mclk%d", i);
  1158. sai->mclk_clk[i] = devm_clk_get(dev, tmp);
  1159. if (IS_ERR(sai->mclk_clk[i])) {
  1160. dev_err(dev, "failed to get mclk%d clock: %ld\n",
  1161. i, PTR_ERR(sai->mclk_clk[i]));
  1162. sai->mclk_clk[i] = NULL;
  1163. }
  1164. }
  1165. if (sai->soc_data->mclk0_is_mclk1)
  1166. sai->mclk_clk[0] = sai->mclk_clk[1];
  1167. else
  1168. sai->mclk_clk[0] = sai->bus_clk;
  1169. fsl_asoc_get_pll_clocks(&pdev->dev, &sai->pll8k_clk,
  1170. &sai->pll11k_clk);
  1171. /* Use Multi FIFO mode depending on the support from SDMA script */
  1172. ret = of_property_read_u32_array(np, "dmas", dmas, 4);
  1173. if (!sai->soc_data->use_edma && !ret && dmas[2] == IMX_DMATYPE_MULTI_SAI)
  1174. sai->is_multi_fifo_dma = true;
  1175. /* read dataline mask for rx and tx*/
  1176. ret = fsl_sai_read_dlcfg(sai);
  1177. if (ret < 0) {
  1178. dev_err(dev, "failed to read dlcfg %d\n", ret);
  1179. return ret;
  1180. }
  1181. irq = platform_get_irq(pdev, 0);
  1182. if (irq < 0)
  1183. return irq;
  1184. ret = devm_request_irq(dev, irq, fsl_sai_isr, IRQF_SHARED,
  1185. np->name, sai);
  1186. if (ret) {
  1187. dev_err(dev, "failed to claim irq %u\n", irq);
  1188. return ret;
  1189. }
  1190. memcpy(&sai->cpu_dai_drv, &fsl_sai_dai_template,
  1191. sizeof(fsl_sai_dai_template));
  1192. /* Sync Tx with Rx as default by following old DT binding */
  1193. sai->synchronous[RX] = true;
  1194. sai->synchronous[TX] = false;
  1195. sai->cpu_dai_drv.symmetric_rate = 1;
  1196. sai->cpu_dai_drv.symmetric_channels = 1;
  1197. sai->cpu_dai_drv.symmetric_sample_bits = 1;
  1198. if (of_property_read_bool(np, "fsl,sai-synchronous-rx") &&
  1199. of_property_read_bool(np, "fsl,sai-asynchronous")) {
  1200. /* error out if both synchronous and asynchronous are present */
  1201. dev_err(dev, "invalid binding for synchronous mode\n");
  1202. return -EINVAL;
  1203. }
  1204. if (of_property_read_bool(np, "fsl,sai-synchronous-rx")) {
  1205. /* Sync Rx with Tx */
  1206. sai->synchronous[RX] = false;
  1207. sai->synchronous[TX] = true;
  1208. } else if (of_property_read_bool(np, "fsl,sai-asynchronous")) {
  1209. /* Discard all settings for asynchronous mode */
  1210. sai->synchronous[RX] = false;
  1211. sai->synchronous[TX] = false;
  1212. sai->cpu_dai_drv.symmetric_rate = 0;
  1213. sai->cpu_dai_drv.symmetric_channels = 0;
  1214. sai->cpu_dai_drv.symmetric_sample_bits = 0;
  1215. }
  1216. sai->mclk_direction_output = of_property_read_bool(np, "fsl,sai-mclk-direction-output");
  1217. if (sai->mclk_direction_output &&
  1218. of_device_is_compatible(np, "fsl,imx6ul-sai")) {
  1219. gpr = syscon_regmap_lookup_by_compatible("fsl,imx6ul-iomuxc-gpr");
  1220. if (IS_ERR(gpr)) {
  1221. dev_err(dev, "cannot find iomuxc registers\n");
  1222. return PTR_ERR(gpr);
  1223. }
  1224. index = of_alias_get_id(np, "sai");
  1225. if (index < 0)
  1226. return index;
  1227. regmap_update_bits(gpr, IOMUXC_GPR1, MCLK_DIR(index),
  1228. MCLK_DIR(index));
  1229. }
  1230. sai->dma_params_rx.addr = sai->res->start + FSL_SAI_RDR0;
  1231. sai->dma_params_tx.addr = sai->res->start + FSL_SAI_TDR0;
  1232. sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX;
  1233. sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX;
  1234. sai->pinctrl = devm_pinctrl_get(&pdev->dev);
  1235. platform_set_drvdata(pdev, sai);
  1236. pm_runtime_enable(dev);
  1237. if (!pm_runtime_enabled(dev)) {
  1238. ret = fsl_sai_runtime_resume(dev);
  1239. if (ret)
  1240. goto err_pm_disable;
  1241. }
  1242. ret = pm_runtime_resume_and_get(dev);
  1243. if (ret < 0)
  1244. goto err_pm_get_sync;
  1245. /* Get sai version */
  1246. ret = fsl_sai_check_version(dev);
  1247. if (ret < 0)
  1248. dev_warn(dev, "Error reading SAI version: %d\n", ret);
  1249. /* Select MCLK direction */
  1250. if (sai->mclk_direction_output &&
  1251. sai->soc_data->max_register >= FSL_SAI_MCTL) {
  1252. regmap_update_bits(sai->regmap, FSL_SAI_MCTL,
  1253. FSL_SAI_MCTL_MCLK_EN, FSL_SAI_MCTL_MCLK_EN);
  1254. }
  1255. ret = pm_runtime_put_sync(dev);
  1256. if (ret < 0 && ret != -ENOSYS)
  1257. goto err_pm_get_sync;
  1258. /*
  1259. * Register platform component before registering cpu dai for there
  1260. * is not defer probe for platform component in snd_soc_add_pcm_runtime().
  1261. */
  1262. if (sai->soc_data->use_imx_pcm) {
  1263. ret = imx_pcm_dma_init(pdev);
  1264. if (ret) {
  1265. if (!IS_ENABLED(CONFIG_SND_SOC_IMX_PCM_DMA))
  1266. dev_err(dev, "Error: You must enable the imx-pcm-dma support!\n");
  1267. goto err_pm_get_sync;
  1268. }
  1269. } else {
  1270. ret = devm_snd_dmaengine_pcm_register(dev, NULL, 0);
  1271. if (ret)
  1272. goto err_pm_get_sync;
  1273. }
  1274. ret = devm_snd_soc_register_component(dev, &fsl_component,
  1275. &sai->cpu_dai_drv, 1);
  1276. if (ret)
  1277. goto err_pm_get_sync;
  1278. return ret;
  1279. err_pm_get_sync:
  1280. if (!pm_runtime_status_suspended(dev))
  1281. fsl_sai_runtime_suspend(dev);
  1282. err_pm_disable:
  1283. pm_runtime_disable(dev);
  1284. return ret;
  1285. }
  1286. static int fsl_sai_remove(struct platform_device *pdev)
  1287. {
  1288. pm_runtime_disable(&pdev->dev);
  1289. if (!pm_runtime_status_suspended(&pdev->dev))
  1290. fsl_sai_runtime_suspend(&pdev->dev);
  1291. return 0;
  1292. }
  1293. static const struct fsl_sai_soc_data fsl_sai_vf610_data = {
  1294. .use_imx_pcm = false,
  1295. .use_edma = false,
  1296. .fifo_depth = 32,
  1297. .pins = 1,
  1298. .reg_offset = 0,
  1299. .mclk0_is_mclk1 = false,
  1300. .flags = 0,
  1301. .max_register = FSL_SAI_RMR,
  1302. };
  1303. static const struct fsl_sai_soc_data fsl_sai_imx6sx_data = {
  1304. .use_imx_pcm = true,
  1305. .use_edma = false,
  1306. .fifo_depth = 32,
  1307. .pins = 1,
  1308. .reg_offset = 0,
  1309. .mclk0_is_mclk1 = true,
  1310. .flags = 0,
  1311. .max_register = FSL_SAI_RMR,
  1312. };
  1313. static const struct fsl_sai_soc_data fsl_sai_imx7ulp_data = {
  1314. .use_imx_pcm = true,
  1315. .use_edma = false,
  1316. .fifo_depth = 16,
  1317. .pins = 2,
  1318. .reg_offset = 8,
  1319. .mclk0_is_mclk1 = false,
  1320. .flags = PMQOS_CPU_LATENCY,
  1321. .max_register = FSL_SAI_RMR,
  1322. };
  1323. static const struct fsl_sai_soc_data fsl_sai_imx8mq_data = {
  1324. .use_imx_pcm = true,
  1325. .use_edma = false,
  1326. .fifo_depth = 128,
  1327. .pins = 8,
  1328. .reg_offset = 8,
  1329. .mclk0_is_mclk1 = false,
  1330. .flags = 0,
  1331. .max_register = FSL_SAI_RMR,
  1332. };
  1333. static const struct fsl_sai_soc_data fsl_sai_imx8qm_data = {
  1334. .use_imx_pcm = true,
  1335. .use_edma = true,
  1336. .fifo_depth = 64,
  1337. .pins = 4,
  1338. .reg_offset = 0,
  1339. .mclk0_is_mclk1 = false,
  1340. .flags = 0,
  1341. .max_register = FSL_SAI_RMR,
  1342. };
  1343. static const struct fsl_sai_soc_data fsl_sai_imx8mm_data = {
  1344. .use_imx_pcm = true,
  1345. .use_edma = false,
  1346. .fifo_depth = 128,
  1347. .reg_offset = 8,
  1348. .mclk0_is_mclk1 = false,
  1349. .pins = 8,
  1350. .flags = 0,
  1351. .max_register = FSL_SAI_MCTL,
  1352. };
  1353. static const struct fsl_sai_soc_data fsl_sai_imx8mn_data = {
  1354. .use_imx_pcm = true,
  1355. .use_edma = false,
  1356. .fifo_depth = 128,
  1357. .reg_offset = 8,
  1358. .mclk0_is_mclk1 = false,
  1359. .pins = 8,
  1360. .flags = 0,
  1361. .max_register = FSL_SAI_MDIV,
  1362. };
  1363. static const struct fsl_sai_soc_data fsl_sai_imx8mp_data = {
  1364. .use_imx_pcm = true,
  1365. .use_edma = false,
  1366. .fifo_depth = 128,
  1367. .reg_offset = 8,
  1368. .mclk0_is_mclk1 = false,
  1369. .pins = 8,
  1370. .flags = 0,
  1371. .max_register = FSL_SAI_MDIV,
  1372. .mclk_with_tere = true,
  1373. };
  1374. static const struct fsl_sai_soc_data fsl_sai_imx8ulp_data = {
  1375. .use_imx_pcm = true,
  1376. .use_edma = true,
  1377. .fifo_depth = 16,
  1378. .reg_offset = 8,
  1379. .mclk0_is_mclk1 = false,
  1380. .pins = 4,
  1381. .flags = PMQOS_CPU_LATENCY,
  1382. .max_register = FSL_SAI_RTCAP,
  1383. };
  1384. static const struct of_device_id fsl_sai_ids[] = {
  1385. { .compatible = "fsl,vf610-sai", .data = &fsl_sai_vf610_data },
  1386. { .compatible = "fsl,imx6sx-sai", .data = &fsl_sai_imx6sx_data },
  1387. { .compatible = "fsl,imx6ul-sai", .data = &fsl_sai_imx6sx_data },
  1388. { .compatible = "fsl,imx7ulp-sai", .data = &fsl_sai_imx7ulp_data },
  1389. { .compatible = "fsl,imx8mq-sai", .data = &fsl_sai_imx8mq_data },
  1390. { .compatible = "fsl,imx8qm-sai", .data = &fsl_sai_imx8qm_data },
  1391. { .compatible = "fsl,imx8mm-sai", .data = &fsl_sai_imx8mm_data },
  1392. { .compatible = "fsl,imx8mp-sai", .data = &fsl_sai_imx8mp_data },
  1393. { .compatible = "fsl,imx8ulp-sai", .data = &fsl_sai_imx8ulp_data },
  1394. { .compatible = "fsl,imx8mn-sai", .data = &fsl_sai_imx8mn_data },
  1395. { /* sentinel */ }
  1396. };
  1397. MODULE_DEVICE_TABLE(of, fsl_sai_ids);
  1398. static int fsl_sai_runtime_suspend(struct device *dev)
  1399. {
  1400. struct fsl_sai *sai = dev_get_drvdata(dev);
  1401. if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
  1402. clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
  1403. if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
  1404. clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
  1405. clk_disable_unprepare(sai->bus_clk);
  1406. if (sai->soc_data->flags & PMQOS_CPU_LATENCY)
  1407. cpu_latency_qos_remove_request(&sai->pm_qos_req);
  1408. regcache_cache_only(sai->regmap, true);
  1409. return 0;
  1410. }
  1411. static int fsl_sai_runtime_resume(struct device *dev)
  1412. {
  1413. struct fsl_sai *sai = dev_get_drvdata(dev);
  1414. unsigned int ofs = sai->soc_data->reg_offset;
  1415. int ret;
  1416. ret = clk_prepare_enable(sai->bus_clk);
  1417. if (ret) {
  1418. dev_err(dev, "failed to enable bus clock: %d\n", ret);
  1419. return ret;
  1420. }
  1421. if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK)) {
  1422. ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[1]]);
  1423. if (ret)
  1424. goto disable_bus_clk;
  1425. }
  1426. if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE)) {
  1427. ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[0]]);
  1428. if (ret)
  1429. goto disable_tx_clk;
  1430. }
  1431. if (sai->soc_data->flags & PMQOS_CPU_LATENCY)
  1432. cpu_latency_qos_add_request(&sai->pm_qos_req, 0);
  1433. regcache_cache_only(sai->regmap, false);
  1434. regcache_mark_dirty(sai->regmap);
  1435. regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR);
  1436. regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR);
  1437. usleep_range(1000, 2000);
  1438. regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0);
  1439. regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0);
  1440. ret = regcache_sync(sai->regmap);
  1441. if (ret)
  1442. goto disable_rx_clk;
  1443. if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output)
  1444. regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs),
  1445. FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
  1446. return 0;
  1447. disable_rx_clk:
  1448. if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
  1449. clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
  1450. disable_tx_clk:
  1451. if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
  1452. clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
  1453. disable_bus_clk:
  1454. clk_disable_unprepare(sai->bus_clk);
  1455. return ret;
  1456. }
  1457. static const struct dev_pm_ops fsl_sai_pm_ops = {
  1458. SET_RUNTIME_PM_OPS(fsl_sai_runtime_suspend,
  1459. fsl_sai_runtime_resume, NULL)
  1460. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1461. pm_runtime_force_resume)
  1462. };
  1463. static struct platform_driver fsl_sai_driver = {
  1464. .probe = fsl_sai_probe,
  1465. .remove = fsl_sai_remove,
  1466. .driver = {
  1467. .name = "fsl-sai",
  1468. .pm = &fsl_sai_pm_ops,
  1469. .of_match_table = fsl_sai_ids,
  1470. },
  1471. };
  1472. module_platform_driver(fsl_sai_driver);
  1473. MODULE_DESCRIPTION("Freescale Soc SAI Interface");
  1474. MODULE_AUTHOR("Xiubo Li, <[email protected]>");
  1475. MODULE_ALIAS("platform:fsl-sai");
  1476. MODULE_LICENSE("GPL");