fsl_sai.c 25 KB

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