fsl_sai.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. sai->is_slave_mode = false;
  229. break;
  230. case SND_SOC_DAIFMT_CBM_CFM:
  231. sai->is_slave_mode = true;
  232. break;
  233. case SND_SOC_DAIFMT_CBS_CFM:
  234. val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
  235. sai->is_slave_mode = false;
  236. break;
  237. case SND_SOC_DAIFMT_CBM_CFS:
  238. val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
  239. sai->is_slave_mode = true;
  240. break;
  241. default:
  242. return -EINVAL;
  243. }
  244. regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx),
  245. FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2);
  246. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx),
  247. FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE |
  248. FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4);
  249. return 0;
  250. }
  251. static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  252. {
  253. int ret;
  254. ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_TRANSMITTER);
  255. if (ret) {
  256. dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
  257. return ret;
  258. }
  259. ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_RECEIVER);
  260. if (ret)
  261. dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
  262. return ret;
  263. }
  264. static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
  265. {
  266. struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
  267. unsigned long clk_rate;
  268. u32 savediv = 0, ratio, savesub = freq;
  269. u32 id;
  270. int ret = 0;
  271. /* Don't apply to slave mode */
  272. if (sai->is_slave_mode)
  273. return 0;
  274. for (id = 0; id < FSL_SAI_MCLK_MAX; id++) {
  275. clk_rate = clk_get_rate(sai->mclk_clk[id]);
  276. if (!clk_rate)
  277. continue;
  278. ratio = clk_rate / freq;
  279. ret = clk_rate - ratio * freq;
  280. /*
  281. * Drop the source that can not be
  282. * divided into the required rate.
  283. */
  284. if (ret != 0 && clk_rate / ret < 1000)
  285. continue;
  286. dev_dbg(dai->dev,
  287. "ratio %d for freq %dHz based on clock %ldHz\n",
  288. ratio, freq, clk_rate);
  289. if (ratio % 2 == 0 && ratio >= 2 && ratio <= 512)
  290. ratio /= 2;
  291. else
  292. continue;
  293. if (ret < savesub) {
  294. savediv = ratio;
  295. sai->mclk_id[tx] = id;
  296. savesub = ret;
  297. }
  298. if (ret == 0)
  299. break;
  300. }
  301. if (savediv == 0) {
  302. dev_err(dai->dev, "failed to derive required %cx rate: %d\n",
  303. tx ? 'T' : 'R', freq);
  304. return -EINVAL;
  305. }
  306. /*
  307. * 1) For Asynchronous mode, we must set RCR2 register for capture, and
  308. * set TCR2 register for playback.
  309. * 2) For Tx sync with Rx clock, we must set RCR2 register for playback
  310. * and capture.
  311. * 3) For Rx sync with Tx clock, we must set TCR2 register for playback
  312. * and capture.
  313. * 4) For Tx and Rx are both Synchronous with another SAI, we just
  314. * ignore it.
  315. */
  316. if ((sai->synchronous[TX] && !sai->synchronous[RX]) ||
  317. (!tx && !sai->synchronous[RX])) {
  318. regmap_update_bits(sai->regmap, FSL_SAI_RCR2,
  319. FSL_SAI_CR2_MSEL_MASK,
  320. FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
  321. regmap_update_bits(sai->regmap, FSL_SAI_RCR2,
  322. FSL_SAI_CR2_DIV_MASK, savediv - 1);
  323. } else if ((sai->synchronous[RX] && !sai->synchronous[TX]) ||
  324. (tx && !sai->synchronous[TX])) {
  325. regmap_update_bits(sai->regmap, FSL_SAI_TCR2,
  326. FSL_SAI_CR2_MSEL_MASK,
  327. FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
  328. regmap_update_bits(sai->regmap, FSL_SAI_TCR2,
  329. FSL_SAI_CR2_DIV_MASK, savediv - 1);
  330. }
  331. dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n",
  332. sai->mclk_id[tx], savediv, savesub);
  333. return 0;
  334. }
  335. static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
  336. struct snd_pcm_hw_params *params,
  337. struct snd_soc_dai *cpu_dai)
  338. {
  339. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  340. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  341. unsigned int channels = params_channels(params);
  342. u32 word_width = params_width(params);
  343. u32 val_cr4 = 0, val_cr5 = 0;
  344. u32 slots = (channels == 1) ? 2 : channels;
  345. u32 slot_width = word_width;
  346. int ret;
  347. if (sai->slots)
  348. slots = sai->slots;
  349. if (sai->slot_width)
  350. slot_width = sai->slot_width;
  351. if (!sai->is_slave_mode) {
  352. ret = fsl_sai_set_bclk(cpu_dai, tx,
  353. slots * slot_width * params_rate(params));
  354. if (ret)
  355. return ret;
  356. /* Do not enable the clock if it is already enabled */
  357. if (!(sai->mclk_streams & BIT(substream->stream))) {
  358. ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]);
  359. if (ret)
  360. return ret;
  361. sai->mclk_streams |= BIT(substream->stream);
  362. }
  363. }
  364. if (!sai->is_dsp_mode)
  365. val_cr4 |= FSL_SAI_CR4_SYWD(slot_width);
  366. val_cr5 |= FSL_SAI_CR5_WNW(slot_width);
  367. val_cr5 |= FSL_SAI_CR5_W0W(slot_width);
  368. if (sai->is_lsb_first)
  369. val_cr5 |= FSL_SAI_CR5_FBT(0);
  370. else
  371. val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
  372. val_cr4 |= FSL_SAI_CR4_FRSZ(slots);
  373. /*
  374. * For SAI master mode, when Tx(Rx) sync with Rx(Tx) clock, Rx(Tx) will
  375. * generate bclk and frame clock for Tx(Rx), we should set RCR4(TCR4),
  376. * RCR5(TCR5) and RMR(TMR) for playback(capture), or there will be sync
  377. * error.
  378. */
  379. if (!sai->is_slave_mode) {
  380. if (!sai->synchronous[TX] && sai->synchronous[RX] && !tx) {
  381. regmap_update_bits(sai->regmap, FSL_SAI_TCR4,
  382. FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
  383. val_cr4);
  384. regmap_update_bits(sai->regmap, FSL_SAI_TCR5,
  385. FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
  386. FSL_SAI_CR5_FBT_MASK, val_cr5);
  387. regmap_write(sai->regmap, FSL_SAI_TMR,
  388. ~0UL - ((1 << channels) - 1));
  389. } else if (!sai->synchronous[RX] && sai->synchronous[TX] && tx) {
  390. regmap_update_bits(sai->regmap, FSL_SAI_RCR4,
  391. FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
  392. val_cr4);
  393. regmap_update_bits(sai->regmap, FSL_SAI_RCR5,
  394. FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
  395. FSL_SAI_CR5_FBT_MASK, val_cr5);
  396. regmap_write(sai->regmap, FSL_SAI_RMR,
  397. ~0UL - ((1 << channels) - 1));
  398. }
  399. }
  400. regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx),
  401. FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
  402. val_cr4);
  403. regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx),
  404. FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
  405. FSL_SAI_CR5_FBT_MASK, val_cr5);
  406. regmap_write(sai->regmap, FSL_SAI_xMR(tx), ~0UL - ((1 << channels) - 1));
  407. return 0;
  408. }
  409. static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
  410. struct snd_soc_dai *cpu_dai)
  411. {
  412. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  413. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  414. if (!sai->is_slave_mode &&
  415. sai->mclk_streams & BIT(substream->stream)) {
  416. clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]);
  417. sai->mclk_streams &= ~BIT(substream->stream);
  418. }
  419. return 0;
  420. }
  421. static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
  422. struct snd_soc_dai *cpu_dai)
  423. {
  424. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  425. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  426. u32 xcsr, count = 100;
  427. /*
  428. * Asynchronous mode: Clear SYNC for both Tx and Rx.
  429. * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
  430. * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx.
  431. */
  432. regmap_update_bits(sai->regmap, FSL_SAI_TCR2, FSL_SAI_CR2_SYNC,
  433. sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0);
  434. regmap_update_bits(sai->regmap, FSL_SAI_RCR2, FSL_SAI_CR2_SYNC,
  435. sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0);
  436. /*
  437. * It is recommended that the transmitter is the last enabled
  438. * and the first disabled.
  439. */
  440. switch (cmd) {
  441. case SNDRV_PCM_TRIGGER_START:
  442. case SNDRV_PCM_TRIGGER_RESUME:
  443. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  444. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
  445. FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
  446. regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
  447. FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
  448. regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
  449. FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
  450. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
  451. FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
  452. break;
  453. case SNDRV_PCM_TRIGGER_STOP:
  454. case SNDRV_PCM_TRIGGER_SUSPEND:
  455. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  456. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
  457. FSL_SAI_CSR_FRDE, 0);
  458. regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
  459. FSL_SAI_CSR_xIE_MASK, 0);
  460. /* Check if the opposite FRDE is also disabled */
  461. regmap_read(sai->regmap, FSL_SAI_xCSR(!tx), &xcsr);
  462. if (!(xcsr & FSL_SAI_CSR_FRDE)) {
  463. /* Disable both directions and reset their FIFOs */
  464. regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
  465. FSL_SAI_CSR_TERE, 0);
  466. regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
  467. FSL_SAI_CSR_TERE, 0);
  468. /* TERE will remain set till the end of current frame */
  469. do {
  470. udelay(10);
  471. regmap_read(sai->regmap, FSL_SAI_xCSR(tx), &xcsr);
  472. } while (--count && xcsr & FSL_SAI_CSR_TERE);
  473. regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
  474. FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
  475. regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
  476. FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
  477. /*
  478. * For sai master mode, after several open/close sai,
  479. * there will be no frame clock, and can't recover
  480. * anymore. Add software reset to fix this issue.
  481. * This is a hardware bug, and will be fix in the
  482. * next sai version.
  483. */
  484. if (!sai->is_slave_mode) {
  485. /* Software Reset for both Tx and Rx */
  486. regmap_write(sai->regmap,
  487. FSL_SAI_TCSR, FSL_SAI_CSR_SR);
  488. regmap_write(sai->regmap,
  489. FSL_SAI_RCSR, FSL_SAI_CSR_SR);
  490. /* Clear SR bit to finish the reset */
  491. regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
  492. regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
  493. }
  494. }
  495. break;
  496. default:
  497. return -EINVAL;
  498. }
  499. return 0;
  500. }
  501. static int fsl_sai_startup(struct snd_pcm_substream *substream,
  502. struct snd_soc_dai *cpu_dai)
  503. {
  504. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  505. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  506. struct device *dev = &sai->pdev->dev;
  507. int ret;
  508. ret = clk_prepare_enable(sai->bus_clk);
  509. if (ret) {
  510. dev_err(dev, "failed to enable bus clock: %d\n", ret);
  511. return ret;
  512. }
  513. regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE,
  514. FSL_SAI_CR3_TRCE);
  515. ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
  516. SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints);
  517. return ret;
  518. }
  519. static void fsl_sai_shutdown(struct snd_pcm_substream *substream,
  520. struct snd_soc_dai *cpu_dai)
  521. {
  522. struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
  523. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  524. regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE, 0);
  525. clk_disable_unprepare(sai->bus_clk);
  526. }
  527. static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
  528. .set_sysclk = fsl_sai_set_dai_sysclk,
  529. .set_fmt = fsl_sai_set_dai_fmt,
  530. .set_tdm_slot = fsl_sai_set_dai_tdm_slot,
  531. .hw_params = fsl_sai_hw_params,
  532. .hw_free = fsl_sai_hw_free,
  533. .trigger = fsl_sai_trigger,
  534. .startup = fsl_sai_startup,
  535. .shutdown = fsl_sai_shutdown,
  536. };
  537. static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
  538. {
  539. struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
  540. /* Software Reset for both Tx and Rx */
  541. regmap_write(sai->regmap, FSL_SAI_TCSR, FSL_SAI_CSR_SR);
  542. regmap_write(sai->regmap, FSL_SAI_RCSR, FSL_SAI_CSR_SR);
  543. /* Clear SR bit to finish the reset */
  544. regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
  545. regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
  546. regmap_update_bits(sai->regmap, FSL_SAI_TCR1, FSL_SAI_CR1_RFW_MASK,
  547. FSL_SAI_MAXBURST_TX * 2);
  548. regmap_update_bits(sai->regmap, FSL_SAI_RCR1, FSL_SAI_CR1_RFW_MASK,
  549. FSL_SAI_MAXBURST_RX - 1);
  550. snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
  551. &sai->dma_params_rx);
  552. snd_soc_dai_set_drvdata(cpu_dai, sai);
  553. return 0;
  554. }
  555. static struct snd_soc_dai_driver fsl_sai_dai = {
  556. .probe = fsl_sai_dai_probe,
  557. .playback = {
  558. .stream_name = "CPU-Playback",
  559. .channels_min = 1,
  560. .channels_max = 32,
  561. .rate_min = 8000,
  562. .rate_max = 192000,
  563. .rates = SNDRV_PCM_RATE_KNOT,
  564. .formats = FSL_SAI_FORMATS,
  565. },
  566. .capture = {
  567. .stream_name = "CPU-Capture",
  568. .channels_min = 1,
  569. .channels_max = 32,
  570. .rate_min = 8000,
  571. .rate_max = 192000,
  572. .rates = SNDRV_PCM_RATE_KNOT,
  573. .formats = FSL_SAI_FORMATS,
  574. },
  575. .ops = &fsl_sai_pcm_dai_ops,
  576. };
  577. static const struct snd_soc_component_driver fsl_component = {
  578. .name = "fsl-sai",
  579. };
  580. static struct reg_default fsl_sai_reg_defaults[] = {
  581. {FSL_SAI_TCR1, 0},
  582. {FSL_SAI_TCR2, 0},
  583. {FSL_SAI_TCR3, 0},
  584. {FSL_SAI_TCR4, 0},
  585. {FSL_SAI_TCR5, 0},
  586. {FSL_SAI_TDR, 0},
  587. {FSL_SAI_TMR, 0},
  588. {FSL_SAI_RCR1, 0},
  589. {FSL_SAI_RCR2, 0},
  590. {FSL_SAI_RCR3, 0},
  591. {FSL_SAI_RCR4, 0},
  592. {FSL_SAI_RCR5, 0},
  593. {FSL_SAI_RMR, 0},
  594. };
  595. static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
  596. {
  597. switch (reg) {
  598. case FSL_SAI_TCSR:
  599. case FSL_SAI_TCR1:
  600. case FSL_SAI_TCR2:
  601. case FSL_SAI_TCR3:
  602. case FSL_SAI_TCR4:
  603. case FSL_SAI_TCR5:
  604. case FSL_SAI_TFR:
  605. case FSL_SAI_TMR:
  606. case FSL_SAI_RCSR:
  607. case FSL_SAI_RCR1:
  608. case FSL_SAI_RCR2:
  609. case FSL_SAI_RCR3:
  610. case FSL_SAI_RCR4:
  611. case FSL_SAI_RCR5:
  612. case FSL_SAI_RDR:
  613. case FSL_SAI_RFR:
  614. case FSL_SAI_RMR:
  615. return true;
  616. default:
  617. return false;
  618. }
  619. }
  620. static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
  621. {
  622. switch (reg) {
  623. case FSL_SAI_TCSR:
  624. case FSL_SAI_RCSR:
  625. case FSL_SAI_TFR:
  626. case FSL_SAI_RFR:
  627. case FSL_SAI_RDR:
  628. return true;
  629. default:
  630. return false;
  631. }
  632. }
  633. static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
  634. {
  635. switch (reg) {
  636. case FSL_SAI_TCSR:
  637. case FSL_SAI_TCR1:
  638. case FSL_SAI_TCR2:
  639. case FSL_SAI_TCR3:
  640. case FSL_SAI_TCR4:
  641. case FSL_SAI_TCR5:
  642. case FSL_SAI_TDR:
  643. case FSL_SAI_TMR:
  644. case FSL_SAI_RCSR:
  645. case FSL_SAI_RCR1:
  646. case FSL_SAI_RCR2:
  647. case FSL_SAI_RCR3:
  648. case FSL_SAI_RCR4:
  649. case FSL_SAI_RCR5:
  650. case FSL_SAI_RMR:
  651. return true;
  652. default:
  653. return false;
  654. }
  655. }
  656. static const struct regmap_config fsl_sai_regmap_config = {
  657. .reg_bits = 32,
  658. .reg_stride = 4,
  659. .val_bits = 32,
  660. .max_register = FSL_SAI_RMR,
  661. .reg_defaults = fsl_sai_reg_defaults,
  662. .num_reg_defaults = ARRAY_SIZE(fsl_sai_reg_defaults),
  663. .readable_reg = fsl_sai_readable_reg,
  664. .volatile_reg = fsl_sai_volatile_reg,
  665. .writeable_reg = fsl_sai_writeable_reg,
  666. .cache_type = REGCACHE_FLAT,
  667. };
  668. static int fsl_sai_probe(struct platform_device *pdev)
  669. {
  670. struct device_node *np = pdev->dev.of_node;
  671. struct fsl_sai *sai;
  672. struct regmap *gpr;
  673. struct resource *res;
  674. void __iomem *base;
  675. char tmp[8];
  676. int irq, ret, i;
  677. int index;
  678. sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
  679. if (!sai)
  680. return -ENOMEM;
  681. sai->pdev = pdev;
  682. if (of_device_is_compatible(np, "fsl,imx6sx-sai") ||
  683. of_device_is_compatible(np, "fsl,imx6ul-sai"))
  684. sai->sai_on_imx = true;
  685. sai->is_lsb_first = of_property_read_bool(np, "lsb-first");
  686. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  687. base = devm_ioremap_resource(&pdev->dev, res);
  688. if (IS_ERR(base))
  689. return PTR_ERR(base);
  690. sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
  691. "bus", base, &fsl_sai_regmap_config);
  692. /* Compatible with old DTB cases */
  693. if (IS_ERR(sai->regmap))
  694. sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
  695. "sai", base, &fsl_sai_regmap_config);
  696. if (IS_ERR(sai->regmap)) {
  697. dev_err(&pdev->dev, "regmap init failed\n");
  698. return PTR_ERR(sai->regmap);
  699. }
  700. /* No error out for old DTB cases but only mark the clock NULL */
  701. sai->bus_clk = devm_clk_get(&pdev->dev, "bus");
  702. if (IS_ERR(sai->bus_clk)) {
  703. dev_err(&pdev->dev, "failed to get bus clock: %ld\n",
  704. PTR_ERR(sai->bus_clk));
  705. sai->bus_clk = NULL;
  706. }
  707. sai->mclk_clk[0] = sai->bus_clk;
  708. for (i = 1; i < FSL_SAI_MCLK_MAX; i++) {
  709. sprintf(tmp, "mclk%d", i);
  710. sai->mclk_clk[i] = devm_clk_get(&pdev->dev, tmp);
  711. if (IS_ERR(sai->mclk_clk[i])) {
  712. dev_err(&pdev->dev, "failed to get mclk%d clock: %ld\n",
  713. i + 1, PTR_ERR(sai->mclk_clk[i]));
  714. sai->mclk_clk[i] = NULL;
  715. }
  716. }
  717. irq = platform_get_irq(pdev, 0);
  718. if (irq < 0) {
  719. dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
  720. return irq;
  721. }
  722. ret = devm_request_irq(&pdev->dev, irq, fsl_sai_isr, 0, np->name, sai);
  723. if (ret) {
  724. dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
  725. return ret;
  726. }
  727. /* Sync Tx with Rx as default by following old DT binding */
  728. sai->synchronous[RX] = true;
  729. sai->synchronous[TX] = false;
  730. fsl_sai_dai.symmetric_rates = 1;
  731. fsl_sai_dai.symmetric_channels = 1;
  732. fsl_sai_dai.symmetric_samplebits = 1;
  733. if (of_find_property(np, "fsl,sai-synchronous-rx", NULL) &&
  734. of_find_property(np, "fsl,sai-asynchronous", NULL)) {
  735. /* error out if both synchronous and asynchronous are present */
  736. dev_err(&pdev->dev, "invalid binding for synchronous mode\n");
  737. return -EINVAL;
  738. }
  739. if (of_find_property(np, "fsl,sai-synchronous-rx", NULL)) {
  740. /* Sync Rx with Tx */
  741. sai->synchronous[RX] = false;
  742. sai->synchronous[TX] = true;
  743. } else if (of_find_property(np, "fsl,sai-asynchronous", NULL)) {
  744. /* Discard all settings for asynchronous mode */
  745. sai->synchronous[RX] = false;
  746. sai->synchronous[TX] = false;
  747. fsl_sai_dai.symmetric_rates = 0;
  748. fsl_sai_dai.symmetric_channels = 0;
  749. fsl_sai_dai.symmetric_samplebits = 0;
  750. }
  751. if (of_find_property(np, "fsl,sai-mclk-direction-output", NULL) &&
  752. of_device_is_compatible(np, "fsl,imx6ul-sai")) {
  753. gpr = syscon_regmap_lookup_by_compatible("fsl,imx6ul-iomuxc-gpr");
  754. if (IS_ERR(gpr)) {
  755. dev_err(&pdev->dev, "cannot find iomuxc registers\n");
  756. return PTR_ERR(gpr);
  757. }
  758. index = of_alias_get_id(np, "sai");
  759. if (index < 0)
  760. return index;
  761. regmap_update_bits(gpr, IOMUXC_GPR1, MCLK_DIR(index),
  762. MCLK_DIR(index));
  763. }
  764. sai->dma_params_rx.addr = res->start + FSL_SAI_RDR;
  765. sai->dma_params_tx.addr = res->start + FSL_SAI_TDR;
  766. sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX;
  767. sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX;
  768. platform_set_drvdata(pdev, sai);
  769. ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component,
  770. &fsl_sai_dai, 1);
  771. if (ret)
  772. return ret;
  773. if (sai->sai_on_imx)
  774. return imx_pcm_dma_init(pdev, IMX_SAI_DMABUF_SIZE);
  775. else
  776. return devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  777. }
  778. static const struct of_device_id fsl_sai_ids[] = {
  779. { .compatible = "fsl,vf610-sai", },
  780. { .compatible = "fsl,imx6sx-sai", },
  781. { .compatible = "fsl,imx6ul-sai", },
  782. { /* sentinel */ }
  783. };
  784. MODULE_DEVICE_TABLE(of, fsl_sai_ids);
  785. #ifdef CONFIG_PM_SLEEP
  786. static int fsl_sai_suspend(struct device *dev)
  787. {
  788. struct fsl_sai *sai = dev_get_drvdata(dev);
  789. regcache_cache_only(sai->regmap, true);
  790. regcache_mark_dirty(sai->regmap);
  791. return 0;
  792. }
  793. static int fsl_sai_resume(struct device *dev)
  794. {
  795. struct fsl_sai *sai = dev_get_drvdata(dev);
  796. regcache_cache_only(sai->regmap, false);
  797. regmap_write(sai->regmap, FSL_SAI_TCSR, FSL_SAI_CSR_SR);
  798. regmap_write(sai->regmap, FSL_SAI_RCSR, FSL_SAI_CSR_SR);
  799. usleep_range(1000, 2000);
  800. regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
  801. regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
  802. return regcache_sync(sai->regmap);
  803. }
  804. #endif /* CONFIG_PM_SLEEP */
  805. static const struct dev_pm_ops fsl_sai_pm_ops = {
  806. SET_SYSTEM_SLEEP_PM_OPS(fsl_sai_suspend, fsl_sai_resume)
  807. };
  808. static struct platform_driver fsl_sai_driver = {
  809. .probe = fsl_sai_probe,
  810. .driver = {
  811. .name = "fsl-sai",
  812. .pm = &fsl_sai_pm_ops,
  813. .of_match_table = fsl_sai_ids,
  814. },
  815. };
  816. module_platform_driver(fsl_sai_driver);
  817. MODULE_DESCRIPTION("Freescale Soc SAI Interface");
  818. MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>");
  819. MODULE_ALIAS("platform:fsl-sai");
  820. MODULE_LICENSE("GPL");