fsl_sai.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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/regmap.h>
  12. #include <linux/slab.h>
  13. #include <linux/time.h>
  14. #include <sound/core.h>
  15. #include <sound/dmaengine_pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  19. #include "fsl_sai.h"
  20. #include "imx-pcm.h"
  21. #define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\
  22. FSL_SAI_CSR_FEIE)
  23. static const unsigned int fsl_sai_rates[] = {
  24. 8000, 11025, 12000, 16000, 22050,
  25. 24000, 32000, 44100, 48000, 64000,
  26. 88200, 96000, 176400, 192000
  27. };
  28. static const struct snd_pcm_hw_constraint_list fsl_sai_rate_constraints = {
  29. .count = ARRAY_SIZE(fsl_sai_rates),
  30. .list = fsl_sai_rates,
  31. };
  32. static irqreturn_t fsl_sai_isr(int irq, void *devid)
  33. {
  34. struct fsl_sai *sai = (struct fsl_sai *)devid;
  35. struct device *dev = &sai->pdev->dev;
  36. u32 flags, xcsr, mask;
  37. bool irq_none = true;
  38. /*
  39. * Both IRQ status bits and IRQ mask bits are in the xCSR but
  40. * different shifts. And we here create a mask only for those
  41. * IRQs that we activated.
  42. */
  43. mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT;
  44. /* Tx IRQ */
  45. regmap_read(sai->regmap, FSL_SAI_TCSR, &xcsr);
  46. flags = xcsr & mask;
  47. if (flags)
  48. irq_none = false;
  49. else
  50. goto irq_rx;
  51. if (flags & FSL_SAI_CSR_WSF)
  52. dev_dbg(dev, "isr: Start of Tx word detected\n");
  53. if (flags & FSL_SAI_CSR_SEF)
  54. dev_warn(dev, "isr: Tx Frame sync error detected\n");
  55. if (flags & FSL_SAI_CSR_FEF) {
  56. dev_warn(dev, "isr: Transmit underrun detected\n");
  57. /* FIFO reset for safety */
  58. xcsr |= FSL_SAI_CSR_FR;
  59. }
  60. if (flags & FSL_SAI_CSR_FWF)
  61. dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n");
  62. if (flags & FSL_SAI_CSR_FRF)
  63. dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n");
  64. flags &= FSL_SAI_CSR_xF_W_MASK;
  65. xcsr &= ~FSL_SAI_CSR_xF_MASK;
  66. if (flags)
  67. regmap_write(sai->regmap, FSL_SAI_TCSR, flags | xcsr);
  68. irq_rx:
  69. /* Rx IRQ */
  70. regmap_read(sai->regmap, FSL_SAI_RCSR, &xcsr);
  71. flags = xcsr & mask;
  72. if (flags)
  73. irq_none = false;
  74. else
  75. goto out;
  76. if (flags & FSL_SAI_CSR_WSF)
  77. dev_dbg(dev, "isr: Start of Rx word detected\n");
  78. if (flags & FSL_SAI_CSR_SEF)
  79. dev_warn(dev, "isr: Rx Frame sync error detected\n");
  80. if (flags & FSL_SAI_CSR_FEF) {
  81. dev_warn(dev, "isr: Receive overflow detected\n");
  82. /* FIFO reset for safety */
  83. xcsr |= FSL_SAI_CSR_FR;
  84. }
  85. if (flags & FSL_SAI_CSR_FWF)
  86. dev_dbg(dev, "isr: Enabled receive FIFO is full\n");
  87. if (flags & FSL_SAI_CSR_FRF)
  88. dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n");
  89. flags &= FSL_SAI_CSR_xF_W_MASK;
  90. xcsr &= ~FSL_SAI_CSR_xF_MASK;
  91. if (flags)
  92. regmap_write(sai->regmap, FSL_SAI_RCSR, flags | xcsr);
  93. out:
  94. if (irq_none)
  95. return IRQ_NONE;
  96. else
  97. return IRQ_HANDLED;
  98. }
  99. static int fsl_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
  100. u32 rx_mask, int slots, int slot_width)
  101. {
  102. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  103. sai->slots = slots;
  104. sai->slot_width = slot_width;
  105. return 0;
  106. }
  107. static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai,
  108. int clk_id, unsigned int freq, int fsl_dir)
  109. {
  110. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  111. bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
  112. u32 val_cr2 = 0;
  113. switch (clk_id) {
  114. case FSL_SAI_CLK_BUS:
  115. val_cr2 |= FSL_SAI_CR2_MSEL_BUS;
  116. break;
  117. case FSL_SAI_CLK_MAST1:
  118. val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1;
  119. break;
  120. case FSL_SAI_CLK_MAST2:
  121. val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2;
  122. break;
  123. case FSL_SAI_CLK_MAST3:
  124. val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3;
  125. break;
  126. default:
  127. return -EINVAL;
  128. }
  129. regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx),
  130. FSL_SAI_CR2_MSEL_MASK, val_cr2);
  131. return 0;
  132. }
  133. static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  134. int clk_id, unsigned int freq, int dir)
  135. {
  136. int ret;
  137. if (dir == SND_SOC_CLOCK_IN)
  138. return 0;
  139. ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
  140. FSL_FMT_TRANSMITTER);
  141. if (ret) {
  142. dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret);
  143. return ret;
  144. }
  145. ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
  146. FSL_FMT_RECEIVER);
  147. if (ret)
  148. dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret);
  149. return ret;
  150. }
  151. static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai,
  152. unsigned int fmt, int fsl_dir)
  153. {
  154. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  155. bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
  156. u32 val_cr2 = 0, val_cr4 = 0;
  157. if (!sai->is_lsb_first)
  158. val_cr4 |= FSL_SAI_CR4_MF;
  159. /* DAI mode */
  160. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  161. case SND_SOC_DAIFMT_I2S:
  162. /*
  163. * Frame low, 1clk before data, one word length for frame sync,
  164. * frame sync starts one serial clock cycle earlier,
  165. * that is, together with the last bit of the previous
  166. * data word.
  167. */
  168. val_cr2 |= FSL_SAI_CR2_BCP;
  169. val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP;
  170. break;
  171. case SND_SOC_DAIFMT_LEFT_J:
  172. /*
  173. * Frame high, one word length for frame sync,
  174. * frame sync asserts with the first bit of the frame.
  175. */
  176. val_cr2 |= FSL_SAI_CR2_BCP;
  177. break;
  178. case SND_SOC_DAIFMT_DSP_A:
  179. /*
  180. * Frame high, 1clk before data, one bit for frame sync,
  181. * frame sync starts one serial clock cycle earlier,
  182. * that is, together with the last bit of the previous
  183. * data word.
  184. */
  185. val_cr2 |= FSL_SAI_CR2_BCP;
  186. val_cr4 |= FSL_SAI_CR4_FSE;
  187. sai->is_dsp_mode = true;
  188. break;
  189. case SND_SOC_DAIFMT_DSP_B:
  190. /*
  191. * Frame high, one bit for frame sync,
  192. * frame sync asserts with the first bit of the frame.
  193. */
  194. val_cr2 |= FSL_SAI_CR2_BCP;
  195. sai->is_dsp_mode = true;
  196. break;
  197. case SND_SOC_DAIFMT_RIGHT_J:
  198. /* To be done */
  199. default:
  200. return -EINVAL;
  201. }
  202. /* DAI clock inversion */
  203. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  204. case SND_SOC_DAIFMT_IB_IF:
  205. /* Invert both clocks */
  206. val_cr2 ^= FSL_SAI_CR2_BCP;
  207. val_cr4 ^= FSL_SAI_CR4_FSP;
  208. break;
  209. case SND_SOC_DAIFMT_IB_NF:
  210. /* Invert bit clock */
  211. val_cr2 ^= FSL_SAI_CR2_BCP;
  212. break;
  213. case SND_SOC_DAIFMT_NB_IF:
  214. /* Invert frame clock */
  215. val_cr4 ^= FSL_SAI_CR4_FSP;
  216. break;
  217. case SND_SOC_DAIFMT_NB_NF:
  218. /* Nothing to do for both normal cases */
  219. break;
  220. default:
  221. return -EINVAL;
  222. }
  223. /* DAI clock master masks */
  224. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  225. case SND_SOC_DAIFMT_CBS_CFS:
  226. val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
  227. val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
  228. break;
  229. case SND_SOC_DAIFMT_CBM_CFM:
  230. sai->is_slave_mode = true;
  231. break;
  232. case SND_SOC_DAIFMT_CBS_CFM:
  233. val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
  234. break;
  235. case SND_SOC_DAIFMT_CBM_CFS:
  236. val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
  237. sai->is_slave_mode = true;
  238. break;
  239. default:
  240. return -EINVAL;
  241. }
  242. regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx),
  243. FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2);
  244. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx),
  245. FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE |
  246. FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4);
  247. return 0;
  248. }
  249. static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  250. {
  251. int ret;
  252. ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_TRANSMITTER);
  253. if (ret) {
  254. dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
  255. return ret;
  256. }
  257. ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_RECEIVER);
  258. if (ret)
  259. dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
  260. return ret;
  261. }
  262. static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
  263. {
  264. struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
  265. unsigned long clk_rate;
  266. u32 savediv = 0, ratio, savesub = freq;
  267. u32 id;
  268. int ret = 0;
  269. /* Don't apply to slave mode */
  270. if (sai->is_slave_mode)
  271. return 0;
  272. for (id = 0; id < FSL_SAI_MCLK_MAX; id++) {
  273. clk_rate = clk_get_rate(sai->mclk_clk[id]);
  274. if (!clk_rate)
  275. continue;
  276. ratio = clk_rate / freq;
  277. ret = clk_rate - ratio * freq;
  278. /*
  279. * Drop the source that can not be
  280. * divided into the required rate.
  281. */
  282. if (ret != 0 && clk_rate / ret < 1000)
  283. continue;
  284. dev_dbg(dai->dev,
  285. "ratio %d for freq %dHz based on clock %ldHz\n",
  286. ratio, freq, clk_rate);
  287. if (ratio % 2 == 0 && ratio >= 2 && ratio <= 512)
  288. ratio /= 2;
  289. else
  290. continue;
  291. if (ret < savesub) {
  292. savediv = ratio;
  293. sai->mclk_id[tx] = id;
  294. savesub = ret;
  295. }
  296. if (ret == 0)
  297. break;
  298. }
  299. if (savediv == 0) {
  300. dev_err(dai->dev, "failed to derive required %cx rate: %d\n",
  301. tx ? 'T' : 'R', freq);
  302. return -EINVAL;
  303. }
  304. /*
  305. * 1) For Asynchronous mode, we must set RCR2 register for capture, and
  306. * set TCR2 register for playback.
  307. * 2) For Tx sync with Rx clock, we must set RCR2 register for playback
  308. * and capture.
  309. * 3) For Rx sync with Tx clock, we must set TCR2 register for playback
  310. * and capture.
  311. * 4) For Tx and Rx are both Synchronous with another SAI, we just
  312. * ignore it.
  313. */
  314. if ((sai->synchronous[TX] && !sai->synchronous[RX]) ||
  315. (!tx && !sai->synchronous[RX])) {
  316. regmap_update_bits(sai->regmap, FSL_SAI_RCR2,
  317. FSL_SAI_CR2_MSEL_MASK,
  318. FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
  319. regmap_update_bits(sai->regmap, FSL_SAI_RCR2,
  320. FSL_SAI_CR2_DIV_MASK, savediv - 1);
  321. } else if ((sai->synchronous[RX] && !sai->synchronous[TX]) ||
  322. (tx && !sai->synchronous[TX])) {
  323. regmap_update_bits(sai->regmap, FSL_SAI_TCR2,
  324. FSL_SAI_CR2_MSEL_MASK,
  325. FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
  326. regmap_update_bits(sai->regmap, FSL_SAI_TCR2,
  327. FSL_SAI_CR2_DIV_MASK, savediv - 1);
  328. }
  329. dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n",
  330. sai->mclk_id[tx], savediv, savesub);
  331. return 0;
  332. }
  333. static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
  334. struct snd_pcm_hw_params *params,
  335. struct snd_soc_dai *cpu_dai)
  336. {
  337. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  338. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  339. unsigned int channels = params_channels(params);
  340. u32 word_width = params_width(params);
  341. u32 val_cr4 = 0, val_cr5 = 0;
  342. u32 slots = (channels == 1) ? 2 : channels;
  343. u32 slot_width = word_width;
  344. int ret;
  345. if (sai->slots)
  346. slots = sai->slots;
  347. if (sai->slot_width)
  348. slot_width = sai->slot_width;
  349. if (!sai->is_slave_mode) {
  350. ret = fsl_sai_set_bclk(cpu_dai, tx,
  351. slots * slot_width * params_rate(params));
  352. if (ret)
  353. return ret;
  354. /* Do not enable the clock if it is already enabled */
  355. if (!(sai->mclk_streams & BIT(substream->stream))) {
  356. ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]);
  357. if (ret)
  358. return ret;
  359. sai->mclk_streams |= BIT(substream->stream);
  360. }
  361. }
  362. if (!sai->is_dsp_mode)
  363. val_cr4 |= FSL_SAI_CR4_SYWD(slot_width);
  364. val_cr5 |= FSL_SAI_CR5_WNW(slot_width);
  365. val_cr5 |= FSL_SAI_CR5_W0W(slot_width);
  366. if (sai->is_lsb_first)
  367. val_cr5 |= FSL_SAI_CR5_FBT(0);
  368. else
  369. val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
  370. val_cr4 |= FSL_SAI_CR4_FRSZ(slots);
  371. /*
  372. * For SAI master mode, when Tx(Rx) sync with Rx(Tx) clock, Rx(Tx) will
  373. * generate bclk and frame clock for Tx(Rx), we should set RCR4(TCR4),
  374. * RCR5(TCR5) and RMR(TMR) for playback(capture), or there will be sync
  375. * error.
  376. */
  377. if (!sai->is_slave_mode) {
  378. if (!sai->synchronous[TX] && sai->synchronous[RX] && !tx) {
  379. regmap_update_bits(sai->regmap, FSL_SAI_TCR4,
  380. FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
  381. val_cr4);
  382. regmap_update_bits(sai->regmap, FSL_SAI_TCR5,
  383. FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
  384. FSL_SAI_CR5_FBT_MASK, val_cr5);
  385. regmap_write(sai->regmap, FSL_SAI_TMR,
  386. ~0UL - ((1 << channels) - 1));
  387. } else if (!sai->synchronous[RX] && sai->synchronous[TX] && tx) {
  388. regmap_update_bits(sai->regmap, FSL_SAI_RCR4,
  389. FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
  390. val_cr4);
  391. regmap_update_bits(sai->regmap, FSL_SAI_RCR5,
  392. FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
  393. FSL_SAI_CR5_FBT_MASK, val_cr5);
  394. regmap_write(sai->regmap, FSL_SAI_RMR,
  395. ~0UL - ((1 << channels) - 1));
  396. }
  397. }
  398. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx),
  399. FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
  400. val_cr4);
  401. regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx),
  402. FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
  403. FSL_SAI_CR5_FBT_MASK, val_cr5);
  404. regmap_write(sai->regmap, FSL_SAI_xMR(tx), ~0UL - ((1 << channels) - 1));
  405. return 0;
  406. }
  407. static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
  408. struct snd_soc_dai *cpu_dai)
  409. {
  410. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  411. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  412. if (!sai->is_slave_mode &&
  413. sai->mclk_streams & BIT(substream->stream)) {
  414. clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]);
  415. sai->mclk_streams &= ~BIT(substream->stream);
  416. }
  417. return 0;
  418. }
  419. static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
  420. struct snd_soc_dai *cpu_dai)
  421. {
  422. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  423. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  424. u32 xcsr, count = 100;
  425. /*
  426. * Asynchronous mode: Clear SYNC for both Tx and Rx.
  427. * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
  428. * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx.
  429. */
  430. regmap_update_bits(sai->regmap, FSL_SAI_TCR2, FSL_SAI_CR2_SYNC,
  431. sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0);
  432. regmap_update_bits(sai->regmap, FSL_SAI_RCR2, FSL_SAI_CR2_SYNC,
  433. sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0);
  434. /*
  435. * It is recommended that the transmitter is the last enabled
  436. * and the first disabled.
  437. */
  438. switch (cmd) {
  439. case SNDRV_PCM_TRIGGER_START:
  440. case SNDRV_PCM_TRIGGER_RESUME:
  441. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  442. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
  443. FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
  444. regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
  445. FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
  446. regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
  447. FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
  448. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
  449. FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
  450. break;
  451. case SNDRV_PCM_TRIGGER_STOP:
  452. case SNDRV_PCM_TRIGGER_SUSPEND:
  453. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  454. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
  455. FSL_SAI_CSR_FRDE, 0);
  456. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
  457. FSL_SAI_CSR_xIE_MASK, 0);
  458. /* Check if the opposite FRDE is also disabled */
  459. regmap_read(sai->regmap, FSL_SAI_xCSR(!tx), &xcsr);
  460. if (!(xcsr & FSL_SAI_CSR_FRDE)) {
  461. /* Disable both directions and reset their FIFOs */
  462. regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
  463. FSL_SAI_CSR_TERE, 0);
  464. regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
  465. FSL_SAI_CSR_TERE, 0);
  466. /* TERE will remain set till the end of current frame */
  467. do {
  468. udelay(10);
  469. regmap_read(sai->regmap, FSL_SAI_xCSR(tx), &xcsr);
  470. } while (--count && xcsr & FSL_SAI_CSR_TERE);
  471. regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
  472. FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
  473. regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
  474. FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
  475. /*
  476. * For sai master mode, after several open/close sai,
  477. * there will be no frame clock, and can't recover
  478. * anymore. Add software reset to fix this issue.
  479. * This is a hardware bug, and will be fix in the
  480. * next sai version.
  481. */
  482. if (!sai->is_slave_mode) {
  483. /* Software Reset for both Tx and Rx */
  484. regmap_write(sai->regmap,
  485. FSL_SAI_TCSR, FSL_SAI_CSR_SR);
  486. regmap_write(sai->regmap,
  487. FSL_SAI_RCSR, FSL_SAI_CSR_SR);
  488. /* Clear SR bit to finish the reset */
  489. regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
  490. regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
  491. }
  492. }
  493. break;
  494. default:
  495. return -EINVAL;
  496. }
  497. return 0;
  498. }
  499. static int fsl_sai_startup(struct snd_pcm_substream *substream,
  500. struct snd_soc_dai *cpu_dai)
  501. {
  502. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  503. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  504. struct device *dev = &sai->pdev->dev;
  505. int ret;
  506. ret = clk_prepare_enable(sai->bus_clk);
  507. if (ret) {
  508. dev_err(dev, "failed to enable bus clock: %d\n", ret);
  509. return ret;
  510. }
  511. regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE,
  512. FSL_SAI_CR3_TRCE);
  513. ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
  514. SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints);
  515. return ret;
  516. }
  517. static void fsl_sai_shutdown(struct snd_pcm_substream *substream,
  518. struct snd_soc_dai *cpu_dai)
  519. {
  520. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  521. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  522. regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE, 0);
  523. clk_disable_unprepare(sai->bus_clk);
  524. }
  525. static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
  526. .set_sysclk = fsl_sai_set_dai_sysclk,
  527. .set_fmt = fsl_sai_set_dai_fmt,
  528. .set_tdm_slot = fsl_sai_set_dai_tdm_slot,
  529. .hw_params = fsl_sai_hw_params,
  530. .hw_free = fsl_sai_hw_free,
  531. .trigger = fsl_sai_trigger,
  532. .startup = fsl_sai_startup,
  533. .shutdown = fsl_sai_shutdown,
  534. };
  535. static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
  536. {
  537. struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
  538. /* Software Reset for both Tx and Rx */
  539. regmap_write(sai->regmap, FSL_SAI_TCSR, FSL_SAI_CSR_SR);
  540. regmap_write(sai->regmap, FSL_SAI_RCSR, FSL_SAI_CSR_SR);
  541. /* Clear SR bit to finish the reset */
  542. regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
  543. regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
  544. regmap_update_bits(sai->regmap, FSL_SAI_TCR1, FSL_SAI_CR1_RFW_MASK,
  545. FSL_SAI_MAXBURST_TX * 2);
  546. regmap_update_bits(sai->regmap, FSL_SAI_RCR1, FSL_SAI_CR1_RFW_MASK,
  547. FSL_SAI_MAXBURST_RX - 1);
  548. snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
  549. &sai->dma_params_rx);
  550. snd_soc_dai_set_drvdata(cpu_dai, sai);
  551. return 0;
  552. }
  553. static struct snd_soc_dai_driver fsl_sai_dai = {
  554. .probe = fsl_sai_dai_probe,
  555. .playback = {
  556. .stream_name = "CPU-Playback",
  557. .channels_min = 1,
  558. .channels_max = 32,
  559. .rate_min = 8000,
  560. .rate_max = 192000,
  561. .rates = SNDRV_PCM_RATE_KNOT,
  562. .formats = FSL_SAI_FORMATS,
  563. },
  564. .capture = {
  565. .stream_name = "CPU-Capture",
  566. .channels_min = 1,
  567. .channels_max = 32,
  568. .rate_min = 8000,
  569. .rate_max = 192000,
  570. .rates = SNDRV_PCM_RATE_KNOT,
  571. .formats = FSL_SAI_FORMATS,
  572. },
  573. .ops = &fsl_sai_pcm_dai_ops,
  574. };
  575. static const struct snd_soc_component_driver fsl_component = {
  576. .name = "fsl-sai",
  577. };
  578. static struct reg_default fsl_sai_reg_defaults[] = {
  579. {FSL_SAI_TCR1, 0},
  580. {FSL_SAI_TCR2, 0},
  581. {FSL_SAI_TCR3, 0},
  582. {FSL_SAI_TCR4, 0},
  583. {FSL_SAI_TCR5, 0},
  584. {FSL_SAI_TDR, 0},
  585. {FSL_SAI_TMR, 0},
  586. {FSL_SAI_RCR1, 0},
  587. {FSL_SAI_RCR2, 0},
  588. {FSL_SAI_RCR3, 0},
  589. {FSL_SAI_RCR4, 0},
  590. {FSL_SAI_RCR5, 0},
  591. {FSL_SAI_RMR, 0},
  592. };
  593. static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
  594. {
  595. switch (reg) {
  596. case FSL_SAI_TCSR:
  597. case FSL_SAI_TCR1:
  598. case FSL_SAI_TCR2:
  599. case FSL_SAI_TCR3:
  600. case FSL_SAI_TCR4:
  601. case FSL_SAI_TCR5:
  602. case FSL_SAI_TFR:
  603. case FSL_SAI_TMR:
  604. case FSL_SAI_RCSR:
  605. case FSL_SAI_RCR1:
  606. case FSL_SAI_RCR2:
  607. case FSL_SAI_RCR3:
  608. case FSL_SAI_RCR4:
  609. case FSL_SAI_RCR5:
  610. case FSL_SAI_RDR:
  611. case FSL_SAI_RFR:
  612. case FSL_SAI_RMR:
  613. return true;
  614. default:
  615. return false;
  616. }
  617. }
  618. static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
  619. {
  620. switch (reg) {
  621. case FSL_SAI_TCSR:
  622. case FSL_SAI_RCSR:
  623. case FSL_SAI_TFR:
  624. case FSL_SAI_RFR:
  625. case FSL_SAI_RDR:
  626. return true;
  627. default:
  628. return false;
  629. }
  630. }
  631. static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
  632. {
  633. switch (reg) {
  634. case FSL_SAI_TCSR:
  635. case FSL_SAI_TCR1:
  636. case FSL_SAI_TCR2:
  637. case FSL_SAI_TCR3:
  638. case FSL_SAI_TCR4:
  639. case FSL_SAI_TCR5:
  640. case FSL_SAI_TDR:
  641. case FSL_SAI_TMR:
  642. case FSL_SAI_RCSR:
  643. case FSL_SAI_RCR1:
  644. case FSL_SAI_RCR2:
  645. case FSL_SAI_RCR3:
  646. case FSL_SAI_RCR4:
  647. case FSL_SAI_RCR5:
  648. case FSL_SAI_RMR:
  649. return true;
  650. default:
  651. return false;
  652. }
  653. }
  654. static const struct regmap_config fsl_sai_regmap_config = {
  655. .reg_bits = 32,
  656. .reg_stride = 4,
  657. .val_bits = 32,
  658. .max_register = FSL_SAI_RMR,
  659. .reg_defaults = fsl_sai_reg_defaults,
  660. .num_reg_defaults = ARRAY_SIZE(fsl_sai_reg_defaults),
  661. .readable_reg = fsl_sai_readable_reg,
  662. .volatile_reg = fsl_sai_volatile_reg,
  663. .writeable_reg = fsl_sai_writeable_reg,
  664. .cache_type = REGCACHE_FLAT,
  665. };
  666. static int fsl_sai_probe(struct platform_device *pdev)
  667. {
  668. struct device_node *np = pdev->dev.of_node;
  669. struct fsl_sai *sai;
  670. struct regmap *gpr;
  671. struct resource *res;
  672. void __iomem *base;
  673. char tmp[8];
  674. int irq, ret, i;
  675. int index;
  676. sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
  677. if (!sai)
  678. return -ENOMEM;
  679. sai->pdev = pdev;
  680. if (of_device_is_compatible(np, "fsl,imx6sx-sai") ||
  681. of_device_is_compatible(np, "fsl,imx6ul-sai"))
  682. sai->sai_on_imx = true;
  683. sai->is_lsb_first = of_property_read_bool(np, "lsb-first");
  684. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  685. base = devm_ioremap_resource(&pdev->dev, res);
  686. if (IS_ERR(base))
  687. return PTR_ERR(base);
  688. sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
  689. "bus", base, &fsl_sai_regmap_config);
  690. /* Compatible with old DTB cases */
  691. if (IS_ERR(sai->regmap))
  692. sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
  693. "sai", base, &fsl_sai_regmap_config);
  694. if (IS_ERR(sai->regmap)) {
  695. dev_err(&pdev->dev, "regmap init failed\n");
  696. return PTR_ERR(sai->regmap);
  697. }
  698. /* No error out for old DTB cases but only mark the clock NULL */
  699. sai->bus_clk = devm_clk_get(&pdev->dev, "bus");
  700. if (IS_ERR(sai->bus_clk)) {
  701. dev_err(&pdev->dev, "failed to get bus clock: %ld\n",
  702. PTR_ERR(sai->bus_clk));
  703. sai->bus_clk = NULL;
  704. }
  705. sai->mclk_clk[0] = sai->bus_clk;
  706. for (i = 1; i < FSL_SAI_MCLK_MAX; i++) {
  707. sprintf(tmp, "mclk%d", i);
  708. sai->mclk_clk[i] = devm_clk_get(&pdev->dev, tmp);
  709. if (IS_ERR(sai->mclk_clk[i])) {
  710. dev_err(&pdev->dev, "failed to get mclk%d clock: %ld\n",
  711. i + 1, PTR_ERR(sai->mclk_clk[i]));
  712. sai->mclk_clk[i] = NULL;
  713. }
  714. }
  715. irq = platform_get_irq(pdev, 0);
  716. if (irq < 0) {
  717. dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
  718. return irq;
  719. }
  720. ret = devm_request_irq(&pdev->dev, irq, fsl_sai_isr, 0, np->name, sai);
  721. if (ret) {
  722. dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
  723. return ret;
  724. }
  725. /* Sync Tx with Rx as default by following old DT binding */
  726. sai->synchronous[RX] = true;
  727. sai->synchronous[TX] = false;
  728. fsl_sai_dai.symmetric_rates = 1;
  729. fsl_sai_dai.symmetric_channels = 1;
  730. fsl_sai_dai.symmetric_samplebits = 1;
  731. if (of_find_property(np, "fsl,sai-synchronous-rx", NULL) &&
  732. of_find_property(np, "fsl,sai-asynchronous", NULL)) {
  733. /* error out if both synchronous and asynchronous are present */
  734. dev_err(&pdev->dev, "invalid binding for synchronous mode\n");
  735. return -EINVAL;
  736. }
  737. if (of_find_property(np, "fsl,sai-synchronous-rx", NULL)) {
  738. /* Sync Rx with Tx */
  739. sai->synchronous[RX] = false;
  740. sai->synchronous[TX] = true;
  741. } else if (of_find_property(np, "fsl,sai-asynchronous", NULL)) {
  742. /* Discard all settings for asynchronous mode */
  743. sai->synchronous[RX] = false;
  744. sai->synchronous[TX] = false;
  745. fsl_sai_dai.symmetric_rates = 0;
  746. fsl_sai_dai.symmetric_channels = 0;
  747. fsl_sai_dai.symmetric_samplebits = 0;
  748. }
  749. if (of_find_property(np, "fsl,sai-mclk-direction-output", NULL) &&
  750. of_device_is_compatible(np, "fsl,imx6ul-sai")) {
  751. gpr = syscon_regmap_lookup_by_compatible("fsl,imx6ul-iomuxc-gpr");
  752. if (IS_ERR(gpr)) {
  753. dev_err(&pdev->dev, "cannot find iomuxc registers\n");
  754. return PTR_ERR(gpr);
  755. }
  756. index = of_alias_get_id(np, "sai");
  757. if (index < 0)
  758. return index;
  759. regmap_update_bits(gpr, IOMUXC_GPR1, MCLK_DIR(index),
  760. MCLK_DIR(index));
  761. }
  762. sai->dma_params_rx.addr = res->start + FSL_SAI_RDR;
  763. sai->dma_params_tx.addr = res->start + FSL_SAI_TDR;
  764. sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX;
  765. sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX;
  766. platform_set_drvdata(pdev, sai);
  767. ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component,
  768. &fsl_sai_dai, 1);
  769. if (ret)
  770. return ret;
  771. if (sai->sai_on_imx)
  772. return imx_pcm_dma_init(pdev, IMX_SAI_DMABUF_SIZE);
  773. else
  774. return devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  775. }
  776. static const struct of_device_id fsl_sai_ids[] = {
  777. { .compatible = "fsl,vf610-sai", },
  778. { .compatible = "fsl,imx6sx-sai", },
  779. { .compatible = "fsl,imx6ul-sai", },
  780. { /* sentinel */ }
  781. };
  782. MODULE_DEVICE_TABLE(of, fsl_sai_ids);
  783. #ifdef CONFIG_PM_SLEEP
  784. static int fsl_sai_suspend(struct device *dev)
  785. {
  786. struct fsl_sai *sai = dev_get_drvdata(dev);
  787. regcache_cache_only(sai->regmap, true);
  788. regcache_mark_dirty(sai->regmap);
  789. return 0;
  790. }
  791. static int fsl_sai_resume(struct device *dev)
  792. {
  793. struct fsl_sai *sai = dev_get_drvdata(dev);
  794. regcache_cache_only(sai->regmap, false);
  795. regmap_write(sai->regmap, FSL_SAI_TCSR, FSL_SAI_CSR_SR);
  796. regmap_write(sai->regmap, FSL_SAI_RCSR, FSL_SAI_CSR_SR);
  797. usleep_range(1000, 2000);
  798. regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
  799. regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
  800. return regcache_sync(sai->regmap);
  801. }
  802. #endif /* CONFIG_PM_SLEEP */
  803. static const struct dev_pm_ops fsl_sai_pm_ops = {
  804. SET_SYSTEM_SLEEP_PM_OPS(fsl_sai_suspend, fsl_sai_resume)
  805. };
  806. static struct platform_driver fsl_sai_driver = {
  807. .probe = fsl_sai_probe,
  808. .driver = {
  809. .name = "fsl-sai",
  810. .pm = &fsl_sai_pm_ops,
  811. .of_match_table = fsl_sai_ids,
  812. },
  813. };
  814. module_platform_driver(fsl_sai_driver);
  815. MODULE_DESCRIPTION("Freescale Soc SAI Interface");
  816. MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>");
  817. MODULE_ALIAS("platform:fsl-sai");
  818. MODULE_LICENSE("GPL");